跳至正文

WordPress + Nginx + PHP8.3 快速部署指南:从零到生产环境

昨天帮客户迁移博客到 WordPress,顺便整理了一份完整的部署手册。记录一下关键步骤,免得下次又踩坑。

环境一览

组件 版本 备注
Ubuntu 22.04 LTS 腾讯云服务器
Nginx 1.24 apt 安装
PHP 8.3 需要手动加 PPA
MySQL 8.0 apt 安装
WordPress 6.9.4 最新稳定版

Step 1:安装 PHP 8.3

Ubuntu 默认源的 PHP 版本太旧,WordPress 6.x 需要 PHP 8.0+,建议直接上 8.3:

# 添加 PPA
sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

# 安装 PHP 8.3 及常用扩展
sudo apt install -y php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-imagick php8.3-mbstring php8.3-xml php8.3-xmlrpc php8.3-zip php8.3-bcmath php8.3-intl

Step 2:安装 MySQL 8.0

sudo apt install -y mysql-server
sudo mysql_secure_installation

创建 WordPress 专用数据库和用户:

CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY '你的强密码';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;

Step 3:配置 Nginx + PHP-FPM

server {
    listen 80;
    server_name your-domain.com;
    root /var/www/html/wordpress;
    index index.php index.html;
    client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 4:安装 WordPress

cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo rm latest.tar.gz
sudo chown -R www-data:www-data /var/www/html/wordpress

Step 5:配置 SSL

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com

常见问题

Q: PHP-FPM 连接被拒绝
A: 检查 socket 路径:ls /var/run/php/php8.3-fpm.sock

Q: 上传文件失败
A: 检查目录权限:chown -R www-data:www-data /var/www/html/wordpress/wp-content/uploads

Q: 502 Bad Gateway
A: 通常是 PHP-FPM 没跑起来:systemctl status php8.3-fpm

总结

WordPress 部署其实不难,关键是:

  1. PHP 版本要新(8.3+)
  2. Nginx 配置要匹配 PHP-FPM 的 socket 路径
  3. 目录权限要正确

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注