1. 基础环境介绍与规划
1.1 操作系统
阿里云服务器:云服务器ECS(包年)e实例 2核2G 3M centos7.9
1.2 数据库环境
MySQL 5.7
- 通过 MySQL 官方 Yum Repository 安装
- 启动并设置开机自启
- 配置 root 密码
创建 WordPress 专用数据库和用户,例如:
- 数据库:
wordpress - 用户:
wpuser - 密码:
aliyun@!123
1.3 Web 服务器
你有两个选择:
Nginx(推荐(本次安装使用))
- 轻量、性能好,适合 WordPress
- 配合
php-fpm使用
Apache (httpd)
- WordPress 官方默认支持
- 模块化强,但性能稍逊
两个只需要选一个,一般推荐 Nginx + PHP-FPM。
1.4 PHP 环境
- 推荐版本:PHP 7.4+(兼容 WordPress 最新版本)
- 必备扩展:
php-mysqlnd(MySQL 连接)php-fpm(Nginx 下执行 PHP)php-gd(图片处理)php-xml(XML 解析)php-mbstring(多字节字符串)php-json(JSON 支持)php-curl(远程请求)
1.5 WordPress 应用环境
- WordPress 最新版(推荐中文包)
- 解压放置于
/usr/share/nginx/html(或 Apache 的/var/www/html) - 配置
wp-config.php与数据库连接
1.6 网络与安全
防火墙
- 开放 80 端口:
firewall-cmd --permanent --add-service=http && firewall-cmd --reload
阿里云安全组
- 确认已放行 80 (HTTP) 和 22 (SSH) 端口
1.7 基础环境总览表
| 层级 | 组件 | 说明 |
| 操作系统 | CentOS 7.9 | 稳定版本,适合部署 |
| 数据库 | MySQL 5.7 | WordPress 推荐 |
| Web 服务器 | Nginx 或 Apache | 推荐 Nginx + PHP-FPM |
| PHP | PHP 7.4 + 扩展 | 保证 WordPress 功能完整 |
| 应用 | WordPress 最新版 | 中文版或英文版均可 |
| 网络 | 防火墙 + 安全组 | 必须开放 80 端口 |
2.安装流程
2.1 准备环境
1.登录你的阿里云 ECS
ssh root@<你的ECS公网IP>
当然,你也可以选择vnc、Xshell等方式登录。
2.更新系统软件包
yum update -y
2.2 安装 MySQL 5.7
CentOS 7 默认仓库没有 MySQL 5.7,需要用官方的 YUM 源。
1.下载并安装 MySQL 官方仓库:
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm yum localinstall -y mysql57-community-release-el7-11.noarch.rpm
2.安装 MySQL 5.7:
yum install -y mysql-community-server
如果报以下错误:
是因为安装 MySQL 5.7 的时候,系统找不到正确的 GPG 公钥 来验证 rpm 包,所以报了
解决方法(这里提供a、b三种方法)
a.手动导入 MySQL 的官方 GPG Key
执行:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
然后再试安装:
yum install -y mysql-community-server
确认 GPG key 文件存在
你可以检查一下本地是否有 key 文件:
ls -l /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql*
b.如果还是报错,可以临时关闭 GPG 检查(不推荐长期这样用):
yum install -y mysql-community-server --nogpgcheck
建议优先用方法 a,确保安全。
3.启动并设置开机自启:
systemctl start mysqld systemctl enable mysqld
4.找到 MySQL 初始密码:
grep 'temporary password' /var/log/mysqld.log
这里根据你实际获取的临时密码。
5.登录并修改 root 密码:
mysql -u root -p
6.在 MySQL 里执行(把 NewPassword123! 换成自己的强密码):
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Huawei@!123';
7.创建 WordPress 数据库和用户:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'Huawei@!123'; GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost'; FLUSH PRIVILEGES; EXIT;
2.3 安装 PHP 及扩展
WordPress 推荐 PHP7.4+。CentOS7 默认版本较旧,需要用 Remi 源。
1.安装 Remi 源:
yum install -y epel-release yum-utils yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm yum-config-manager --enable remi-php74
2.安装 PHP7.4 及扩展:
yum install -y php php-mysqlnd php-fpm php-gd php-xml php-mbstring php-json php-curl
3.检查版本:
php -v
2.4 安装 Nginx(或 Apache)
这里用 Nginx 举例。
1.安装 Nginx:
yum install -y nginx
2.启动并开机自启:
systemctl start nginx systemctl enable nginx
3.配置 PHP-FPM:
systemctl start php-fpm systemctl enable php-fpm
2.5 安装 WordPress
1.下载 WordPress 最新版:
cd /usr/share/nginx/html wget https://cn.wordpress.org/latest-zh_CN.tar.gz tar -xvf latest-zh_CN.tar.gz mv wordpress/* .
2.设置权限:
chown -R nginx:nginx /usr/share/nginx/html chmod -R 755 /usr/share/nginx/html
3.配置 Nginx 站点:
编辑 /etc/nginx/conf.d/wordpress.conf
server { listen 80; server_name _; root /usr/share/nginx/html; 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_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
4.重启 Nginx:
systemctl restart nginx
2.6 防火墙与安全组
1.开放 80 端口:
firewall-cmd --permanent --add-service=http firewall-cmd --reload
2.确认阿里云安全组也开放 80端口。
2.7 浏览器访问
1.访问:http://<你的ECS公网IP>
2.进入 WordPress 安装页面,填写数据库信息:
- 数据库名:
wordpress - 用户名:
wpuser - 密码:aliyun@!123
- 数据库主机:
localhost - 表前缀:
wp_
如果遇到无法写入 wp-config.php
进入目录/usr/share/nginx/html
vim /usr/share/nginx/html
复制上面方框中的内容并保存退出。
3.填写信息
4.登录
5.接下来,你就可以发布自己的博客啦
2.8 登录
3.一键安装脚本
3.1 新建脚本文件
vim install_wp.sh
#!/bin/bash # ============================================ # 一键安装 WordPress + Nginx + PHP7.4 + MySQL5.7 # 系统: CentOS 7.9 # 作者: FooBlaze & ChatGPT # ============================================ set -e echo ">>> 更新系统" yum update -y echo ">>> 安装常用工具" yum install -y wget unzip vim epel-release yum-utils # -------------------------- # 安装 MySQL 5.7 # -------------------------- echo ">>> 安装 MySQL 5.7" wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm yum localinstall -y mysql57-community-release-el7-11.noarch.rpm rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 yum install -y mysql-community-server echo ">>> 启动 MySQL" systemctl enable mysqld systemctl start mysqld # 获取初始密码 MYSQL_TEMP_PASS=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}') echo "MySQL 初始密码: $MYSQL_TEMP_PASS" # 设置 root 密码和创建数据库 MYSQL_ROOT_PASS="Huawei@!123" WP_DB="wordpress" WP_USER="wpuser" WP_PASS="Huawei@!123" mysql --connect-expired-password -uroot -p"$MYSQL_TEMP_PASS" <<EOF ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASS}'; CREATE DATABASE ${WP_DB} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER '${WP_USER}'@'localhost' IDENTIFIED BY '${WP_PASS}'; GRANT ALL PRIVILEGES ON ${WP_DB}.* TO '${WP_USER}'@'localhost'; FLUSH PRIVILEGES; EOF echo ">>> MySQL 配置完成,root 密码: ${MYSQL_ROOT_PASS}" # -------------------------- # 安装 PHP 7.4 # -------------------------- echo ">>> 安装 PHP 7.4" yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm yum-config-manager --enable remi-php74 yum install -y php php-mysqlnd php-fpm php-gd php-xml php-mbstring php-json php-curl systemctl enable php-fpm systemctl start php-fpm # -------------------------- # 安装 Nginx # -------------------------- echo ">>> 安装 Nginx" yum install -y nginx systemctl enable nginx systemctl start nginx # -------------------------- # 安装 WordPress # -------------------------- echo ">>> 下载 WordPress" cd /usr/share/nginx/html wget https://cn.wordpress.org/latest-zh_CN.tar.gz tar -xvf latest-zh_CN.tar.gz mv wordpress/* . rm -rf wordpress latest-zh_CN.tar.gz chown -R nginx:nginx /usr/share/nginx/html chmod -R 755 /usr/share/nginx/html # -------------------------- # 配置 Nginx # -------------------------- echo ">>> 配置 Nginx" cat > /etc/nginx/conf.d/wordpress.conf <<EOF server { listen 80; server_name _; root /usr/share/nginx/html; 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_index index.php; fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; } } EOF nginx -t systemctl restart nginx # -------------------------- # 防火墙 # -------------------------- echo ">>> 配置防火墙" firewall-cmd --permanent --add-service=http || true firewall-cmd --reload || true echo "============================================" echo " WordPress 已安装完成!" echo " 浏览器访问: http://<你的ECS公网IP>" echo " 数据库名: ${WP_DB}" echo " 数据库用户: ${WP_USER}" echo " 数据库密码: ${WP_PASS}" echo " MySQL root 密码: ${MYSQL_ROOT_PASS}" echo "============================================"
3.2 赋权并执行
1.赋权
chmod +x install_wp.sh
2.执行
bash install_wp.sh
3.结果
>>> 配置 Nginx nginx: [warn] conflicting server name "_" on 0.0.0.0:80, ignored nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful >>> 配置防火墙 FirewallD is not running FirewallD is not running ============================================ WordPress 已安装完成! 浏览器访问: http://<你的ECS公网IP> 数据库名: wordpress 数据库用户: wpuser 数据库密码: aliyun@!123 MySQL root 密码: aliyun@!123 ============================================
4.接下来就可以到2.7啦