在阿里云 ECS 上安装基于 Ubuntu 的 LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境并部署 WordPress,是一个经典且实用的操作。以下是详细、分步指南,适用于 Ubuntu 20.04/22.04 LTS 系统。
✅ 前提条件
- 已购买阿里云 ECS 实例,操作系统为 Ubuntu 20.04 或 22.04。
- 已通过 SSH 登录服务器(使用 root 或具有 sudo 权限的用户)。
- 安全组中已开放端口:80(HTTP)、443(HTTPS)、22(SSH)。
- (可选)已绑定域名并完成 DNS 解析到 ECS 公网 IP。
🧱 第一步:更新系统
sudo apt update && sudo apt upgrade -y
🌐 第二步:安装 Nginx
sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
验证 Nginx 是否运行:
sudo systemctl status nginx
curl http://localhost
访问 http://<你的ECS公网IP>,应看到 Nginx 默认欢迎页面。
🗄️ 第三步:安装 MariaDB(MySQL 兼容替代)
推荐使用 MariaDB,性能更好且开源友好。
sudo apt install mariadb-server mariadb-client -y
sudo systemctl enable mariadb
sudo systemctl start mariadb
运行安全脚本:
sudo mysql_secure_installation
按提示设置:
- 设置 root 密码
- 移除匿名用户
- 禁止 root 远程登录
- 移除 test 数据库
- 重新加载权限表
🐘 第四步:安装 PHP 及必要模块
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y
检查 PHP-FPM 状态:
sudo systemctl status php*-fpm # 如 php8.1-fpm
注意:Ubuntu 22.04 默认 PHP 版本为 8.1,20.04 为 7.4。后续配置需匹配实际版本。
📁 第五步:创建 WordPress 目录与数据库
1. 创建网站根目录
sudo mkdir -p /var/www/html/wordpress
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
2. 下载 WordPress
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
sudo cp -r wordpress/* /var/www/html/wordpress/
3. 创建 MySQL 数据库和用户
sudo mysql -u root -p
进入 MySQL 后执行:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
⚠️ 替换
StrongPassword123!为你自己的强密码。
⚙️ 第六步:配置 Nginx 虚拟主机
创建配置文件:
sudo nano /etc/nginx/sites-available/wordpress
粘贴以下内容(根据实际 PHP 版本调整 fastcgi_pass):
server {
listen 80;
server_name your_domain.com www.your_domain.com; # 替换为你的域名或 IP
root /var/www/html/wordpress;
index index.php index.html index.htm;
client_max_body_size 64M;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Ubuntu 22.04 用此路径;20.04 可能是 php7.4-fpm.sock
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
# 静态资源缓存优化(可选)
location ~* .(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
启用站点并测试配置:
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
删除默认站点避免冲突:
sudo rm /etc/nginx/sites-enabled/default
🔐 第七步:设置文件权限与 SELinux(如需)
确保 WordPress 可写入上传目录:
sudo find /var/www/html/wordpress -type d -exec chmod 755 {} ;
sudo find /var/www/html/wordpress -type f -exec chmod 644 {} ;
sudo chown -R www-data:www-data /var/www/html/wordpress
Ubuntu 无 SELinux,无需额外配置。
🌍 第八步:通过浏览器安装 WordPress
- 打开浏览器,访问:
http://<你的ECS公网IP> 或 http://your_domain.com - 选择语言 → 点击“开始”
- 填写:
- 站点标题
- 管理员用户名
- 密码
- 邮箱
- 点击“安装 WordPress”
如果之前正确配置了数据库,WordPress 会自动连接。若失败,请检查:
- 数据库名、用户名、密码是否正确
- MySQL 服务是否运行
- Nginx 配置中的
fastcgi_pass路径是否与 PHP-FPM 一致
🛡️ 第九步(推荐):安装 SSL 证书(HTTPS)
使用 Let’s Encrypt 免费证书:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
按提示操作,Certbot 会自动修改 Nginx 配置并重定向 HTTP→HTTPS。
✅ 第十步:验证与优化
- 登录 WordPress 后台:
http://your_domain.com/wp-admin - 安装主题和插件(如 Yoast SEO、WP Super Cache)
- 定期更新系统、Nginx、PHP、WordPress 核心
🧰 常用命令速查
| 操作 | 命令 |
|---|---|
| 重启 Nginx | sudo systemctl restart nginx |
| 重启 PHP-FPM | sudo systemctl restart php*-fpm |
| 重启 MySQL | sudo systemctl restart mariadb |
| 查看错误日志 | sudo tail -f /var/log/nginx/error.log |
| 测试 Nginx 配置 | sudo nginx -t |
❗ 常见问题排查
| 问题 | 解决方案 |
|---|---|
| 白屏或 502 Bad Gateway | 检查 PHP-FPM 是否运行,fastcgi_pass 路径是否正确 |
| 无法上传图片 | 检查 /var/www/html/wordpress/wp-content/uploads 权限是否为 www-data |
| 数据库连接失败 | 检查 MySQL 用户权限、密码、防火墙是否允许本地连接 |
| HTTPS 不生效 | 确认证书已签发,Nginx 配置中 listen 443 ssl 存在 |
🎉 完成!
你现在已在阿里云 ECS Ubuntu 上成功部署了 LNMP + WordPress 环境!
如需自动化部署,可考虑使用:
- Ansible 剧本
- Docker + Docker Compose
- 阿里云市场镜像(一键安装 LNMP)
如有特定需求(如 Redis 缓存、多站点、备份策略等),可进一步扩展配置。
CLOUD云