Ubuntu 22.04 安装 WordPress 并配置 SSL(Let’s Encrypt)+ Cloudflare

1. 安装必要组件

bash
复制编辑
sudo apt update && sudo apt upgrade -y
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql unzip -y

确保 Apache 和 MySQL 运行:

bash
复制编辑
sudo systemctl enable apache2
sudo systemctl enable mysql
sudo systemctl start apache2
sudo systemctl start mysql


2. 下载并配置 WordPress

bash
复制编辑
cd /var/www/html
sudo wget <https://wordpress.org/latest.tar.gz>
sudo tar -xvzf latest.tar.gz
sudo mv wordpress/* .
sudo rmdir wordpress
sudo rm latest.tar.gz

设置目录权限

bash
复制编辑
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html


3. 创建 MySQL 数据库

sql
复制编辑
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'your-strong-password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;


4. 配置 WordPress

bash
复制编辑
sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sudo nano /var/www/html/wp-config.php

修改数据库信息:

php
复制编辑
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpressuser');
define('DB_PASSWORD', 'your-strong-password');
define('DB_HOST', 'localhost');

保存退出 (CTRL + X, Y, Enter)


5. 配置 Apache 虚拟主机

创建 WordPress 配置文件:

bash
复制编辑
sudo nano /etc/apache2/sites-available/wordpress.conf

添加以下内容:

apache
复制编辑
<VirtualHost *:80>
    ServerAdmin admin@itandtrading.blog
    ServerName itandtrading.blog
    ServerAlias www.itandtrading.blog

    DocumentRoot /var/www/html

    <Directory /var/www/html>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

启用虚拟主机

bash
复制编辑
sudo a2ensite wordpress.conf
sudo a2enmod rewrite
sudo systemctl restart apache2


6. 配置 Let’s Encrypt SSL

(⚠ 重要:先在 Cloudflare 关闭代理,确保 DNS 解析正确!)

bash
复制编辑
sudo certbot --apache -d itandtrading.blog -d www.itandtrading.blog

SSL 证书续期:

bash
复制编辑
sudo certbot renew --dry-run


7. 配置 Cloudflare

  1. 登录 Cloudflare 👉 Cloudflare Dashboard
  2. 进入 DNS 设置
    • 确保 A 记录 指向你的 服务器 IP
    • 关闭代理(点击橙色云朵变成灰色),等待 1-5 分钟
  3. 清除 Cloudflare 缓存
    • Caching -> Purge Everything
  4. 重新开启 Cloudflare 代理(可选)
    • 如果 HTTPS 访问正常,可以开启代理(橙色云朵)

8. 测试访问

  • 本地测试 DNS确保解析到正确的 IP bash 复制编辑 nslookup itandtrading.blog ping itandtrading.blog
  • 访问 WordPress
    • http://itandtrading.blog
    • https://itandtrading.blog
  • 检查 Apache 运行 bash 复制编辑 sudo systemctl status apache2
  • 检查 SSL 证书 bash 复制编辑 sudo certbot certificates

9. 解决可能遇到的问题

(1) 访问不了网站?

  • 先用 http://your-server-ip 直接访问看看
  • 确保 ping itandtrading.blog 返回正确 IP
  • sudo systemctl restart apache2 试试重启 Apache

(2) SSL 证书无法签发?

  • 确保 Cloudflare 代理关闭
  • 运行 certbot域名必须指向你的服务器 IP
  • sudo certbot delete --cert-name itandtrading.blog 删除旧证书后再试

(3) 访问 HTTPS 出现错误?

  • Cloudflare SSL/TLS 设置 选择 Full (Strict)

发表评论