一、准备环境
操作系统:CentOS 7.x(最少 2 核 CPU + 2GB 内存 + 20GB 磁盘)
网络:能访问公网(用于下载包)
软件版本:
Nginx 1.20+
MySQL 5.7/8.0
PHP 7.4+
WordPress 6.x(商城插件 WooCommerce)
二、安装 Nginx
bash# 1. 安装依赖 yum install -y yum-utils # 2. 添加 Nginx 官方仓库 cat > /etc/yum.repos.d/nginx.repo <<EOF [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/ gpgcheck=0 enabled=1 EOF # 3. 安装 Nginx yum install -y nginx # 4. 设置开机自启 systemctl enable nginx systemctl start nginx
三、安装 MySQL
bash# 1. 下载 MySQL 5.7 官方源 wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm rpm -ivh mysql57-community-release-el7-11.noarch.rpm rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 # 2. 安装 MySQL yum install -y mysql-community-server # 3. 启动 MySQL systemctl enable mysqld systemctl start mysqld # 4. 查看初始密码 grep 'temporary password' /var/log/mysqld.log #如果安装不了 ##就修改 repo 文件 vim /etc/yum.repos.d/mysql-community.repo ###找到 [mysql57-community] 部分,修改 gpgcheck=1 gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 yum clean all yum makecache yum install mysql-community-server
第一次登录 root 用户时,必须先修改密码,否则不能执行任何 SQL。
bashALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassw0rd!'; ##降低密码策略要求(如果不想弄这么复杂,这只是一个实验) SET GLOBAL validate_password_policy=LOW; SET GLOBAL validate_password_length=6; ALTER USER 'root'@'localhost' IDENTIFIED BY '12345';
1、创建数据库
bashmysql -uroot -p ##登陆 CREATE DATABASE wordpress DEFAULT CHARSET utf8mb4; ##建库 CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'Wp@123456'; #新建一个用户wpuser GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost'; ##赋权 FLUSH PRIVILEGES; ##刷新权限 EXIT;
四、安装 PHP
bash# 1. 安装 EPEL 和 Remi 源 yum install -y epel-release yum-utils yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm # 2. 启用 PHP 7.4 yum-config-manager --enable remi-php74 # 3. 安装 PHP 及扩展 yum install -y php php-fpm php-mysqlnd php-gd php-xml php-mbstring php-json php-cli # 4. 启动 PHP-FPM systemctl enable php-fpm systemctl start php-fpm
五、配置 Nginx + PHP
1、编辑nginx配置
bashvim /etc/nginx/conf.d/wordpress.conf server { listen 80; server_name _; # 或改成自己的域名 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_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
重启 Nginx
bashnginx -t ##检查语法是否错误 systemctl restart nginx
六、部署 WordPress
bashcd /usr/share/nginx/html wget https://wordpress.org/latest.tar.gz ##如果没有wget就yum install -y wget tar -zxvf latest.tar.gz cd wordpress cp wp-config-sample.php wp-config.php chown -R nginx:nginx wordpress chmod -R 755 /var/www/html/wordpress
1、修改数据库信息:
bashvim wp-config.php define('DB_NAME', 'wordpress'); define('DB_USER', 'wpuser'); define('DB_PASSWORD', 'Wp@123456'); define('DB_HOST', 'localhost'); ##跟上面数据库的一致就行
七、访问网站
bash浏览器访问: http://你的服务器IP 进入 WordPress 安装页面,设置管理员账号。 安装完成后,进入后台,安装 WooCommerce 插件 → 变成一个商城系统。
####准备阶段二Nginx + Keepalived + MySQL 主从