Alibaba Cloud Linux 3服务器如何配置PHP和MySQL运行WordPress?

在 Alibaba Cloud Linux 3(基于 Rocky Linux 9 / AlmaLinux 9)上部署 WordPress,推荐使用 LNMPLAMP 架构。由于阿里云官方推荐且优化较好的方案是 Nginx + PHP-FPM + MySQL (MariaDB),以下以该组合为例提供完整配置步骤。


✅ 前提条件

  • 已创建 ECS 实例,系统为 Alibaba Cloud Linux 3
  • 已开放必要端口:80(HTTP)、443(HTTPS,可选)、22(SSH)
  • 拥有 rootsudo 权限

📦 一、安装基础组件

1. 更新系统并安装 EPEL 及必要工具

sudo dnf update -y
sudo dnf install -y epel-release git wget curl vim net-tools

2. 安装 Nginx

sudo dnf install -y nginx
sudo systemctl enable --now nginx

✅ 验证:访问 http://<服务器公网IP> 应看到 Nginx 默认页面。


3. 安装 MariaDB(MySQL 兼容替代品,推荐用于 ALinux 3)

sudo dnf install -y mariadb-server mariadb
sudo systemctl enable --now mariadb

🔐 初始化数据库安全设置

sudo mysql_secure_installation

按提示操作:

  • 设置 root 密码(强烈建议自定义强密码)
  • 删除匿名用户
  • 禁止 root 远程登录
  • 删除测试数据库
  • 重载权限表

💡 若需命令行直接连接(如后续脚本自动配置),可临时允许本地 root 无密码登录(仅限开发环境):

-- 进入 MySQL 后执行:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_strong_password';
FLUSH PRIVILEGES;

4. 安装 PHP 及扩展(PHP 8.2+ 推荐)

Alibaba Cloud Linux 3 官方源包含 PHP 8.2:

sudo dnf install -y php php-fpm php-mysqlnd php-gd php-json php-curl php-mbstring 
php-intl php-bcmath php-fileinfo php-opcache php-zip php-xml php-dom php-pdo

✅ 验证 PHP 版本:

php -v
# 应显示 PHP 8.2.x

⚙️ 二、配置 PHP-FPM

1. 修改 PHP-FPM 配置(适配 WordPress)

编辑 /etc/php-fpm.d/www.conf(或 /etc/php-fpm.d/wordpress.conf,若存在则新建):

user = apache
group = apache
listen = 127.0.0.1:9000
listen.owner = apache
listen.group = apache
listen.mode = 0660

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

; 其他关键设置
request_terminate_timeout = 60
fastcgi_buffer_size = 128k
fastcgi_buffers = 4 256k
fastcgi_busy_buffers_size = 256k
fastcgi_temp_file_write_size = 256k

📌 注意:user/group 需与 Nginx worker 进程一致(ALinux 3 默认用 nginx 用户)。
更稳妥做法:将 Nginx 和 PHP-FPM 都设为 apache 用户,或统一为 nginx

✅ 推荐调整(避免权限问题):

sudo usermod -aG nginx apache   # 让 apache 加入 nginx 组(可选)
# 或直接改为:
sudo sed -i 's/^user = .*/user = nginx/' /etc/php-fpm.d/www.conf
sudo sed -i 's/^group = .*/group = nginx/' /etc/php-fpm.d/www.conf

重启 PHP-FPM:

sudo systemctl restart php-fpm

🌐 三、配置 Nginx 站点

1. 创建 WordPress 目录

sudo mkdir -p /usr/share/nginx/html/wordpress
sudo chown -R nginx:nginx /usr/share/nginx/html/wordpress
sudo chmod -R 755 /usr/share/nginx/html/wordpress

2. 下载 WordPress

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo cp -r wordpress/* /usr/share/nginx/html/wordpress/
sudo chown -R nginx:nginx /usr/share/nginx/html/wordpress

3. 生成 wp-config.php

sudo cp /usr/share/nginx/html/wordpress/wp-config-sample.php 
       /usr/share/nginx/html/wordpress/wp-config.php
sudo nano /usr/share/nginx/html/wordpress/wp-config.php

修改以下字段(务必替换为实际值):

define( 'DB_NAME', 'wordpress_db' );
define( 'DB_USER', 'wordpress_user' );
define( 'DB_PASSWORD', 'your_strong_password' );
define( 'DB_HOST', 'localhost' );

💡 后续再创建数据库和用户。

4. 创建 Nginx 站点配置

新建 /etc/nginx/conf.d/wordpress.conf

server {
    listen 80;
    server_name _;  # 或替换为域名,如 example.com

    root /usr/share/nginx/html/wordpress;
    index index.php index.html index.htm;

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

    location ~ .php$ {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;

        # 优化性能
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_read_timeout 60;
        fastcgi_send_timeout 60;
    }

    # 安全:禁止访问敏感文件
    location ~* .(env|git|log|sql)$ {
        deny all;
    }

    # 缓存静态资源
    location ~* .(jpg|jpeg|png|gif|ico|css|js|woff|woff2)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }
}

✅ 验证配置:

sudo nginx -t
sudo systemctl reload nginx

🗄️ 四、创建 WordPress 数据库

sudo mysql -u root -p

在 MySQL 提示符下执行:

CREATE DATABASE wordpress_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

🔒 生产环境建议:

  • 使用强密码(≥16位,含大小写+数字+符号)
  • 限制 wordpress_user 仅能访问 wordpress_db
  • 禁用远程 root 登录(已在 mysql_secure_installation 中完成)

🧪 五、访问 WordPress 安装向导

  1. 浏览器访问:http://<服务器公网IP>
  2. 进入 WordPress 安装界面:
    • 选择语言 → 下一步
    • 填写站点标题、用户名、密码、邮箱
    • 点击“安装 WordPress”
  3. 登录后即可开始使用!

🔐 六、安全加固建议(生产环境必做)

项目 操作
防火墙 启用 firewalld,仅开放 80/443/22:
sudo firewall-cmd --permanent --add-service={http,https,ssh}
sudo firewall-cmd --reload
SELinux 保持 Enforcing,但需正确标记:
sudo setsebool -P httpd_can_network_connect on
sudo semanage fcontext -a -t httpd_sys_content_t "/usr/share/nginx/html(/.*)?"
sudo restorecon -Rv /usr/share/nginx/html
WordPress 安全插件 安装 Wordfence / iThemes Security 等
定期备份 使用 mysqldump + rsync 或云备份服务
HTTPS 申请 Let’s Encrypt 证书(见下方补充)

🔒 七、(可选)配置 HTTPS(Let’s Encrypt)

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

替换 your-domain.com 为你的真实域名(需提前解析到服务器 IP)


✅ 验证清单

  • [ ] http://<IP> 能正常访问 WordPress 安装页
  • [ ] wp-admin 可登录
  • [ ] 主题/插件可安装
  • [ ] 数据库连接正常(检查 wp-config.php 日志报错)
  • [ ] 静态资源加载无 403/404
  • [ ] SELinux 未阻断(grep -i denied /var/log/audit/audit.log 应为空)

需要我提供:

  • 一键自动化脚本(Ansible / Shell)?
  • Docker 部署方案?
  • 高可用/负载均衡架构设计?
    欢迎继续提问!