本文所述配置在阿里云Ubuntu22.04系统部署成功:
Ubuntu22.04 + Nginx1.18 + Mariadb10.6.12 + PHP8.1.2 + WordPress6.4.2
# 更新仓库源数据
sudo apt update
# apt 仓库安装基础程序(nginx+maridb+php)
sudo apt install -y nginx
sudo apt install -y mariadb-server
sudo apt install -y php
# 安装PHP 相关模块(php-cgi服务程序及mariadb本地驱动)
sudo apt install -y php-fpm php-mysqlnd
# 安装PHP 相关模块(wordpress中需要的模块)
sudo apt install -y php-gd
sudo apt install -y php-curl
sudo apt install -y php-dom
sudo apt install -y php-imagick
sudo apt install -y php-mbstring
sudo apt install -y php-intl
sudo apt install -y php-zip
sudo apt install -y php-xml
sudo apt install -y php-json
# 如php默认安装了apache2服务器环境,会占用80端口,导致nginx无法启动
# 可删除相关环境并启动nginx
sudo apt autoremove apache2
sudo rm -f /var/www/html/index.html
sudo systemctl start nginx
# 配置Mariadb:运行以下命令配置Mariadb安全设置
sudo mysql_secure_installation
Enter current password for root (enter for none):(回车)
Switch to unix_socket authentication [Y/n] :(y回车)
Change the root password? [Y/n]:(y回车)
New password: (输入root登录密码)
Re-enter new password: (再次输入root登录密码)
Remove anonymous users? [Y/n]:(y回车)
Disallow root login remotely? [Y/n]:(y回车)
Remove test database and access to it? [Y/n] :(y回车)
Reload privilege tables now? [Y/n]:(y回车)
# 创建数据库:登录到Mariadb控制台,密码为上方设置。
sudo mysql -u root -p
Enter password: (输入上方设置密码)
# wordpress管理数据库信息(带色部分分别为新建wordpress数据库名、添加及授权可操作该库的
# 用户名进入mariadb的mysql.user表、用户登录密码)要与wp-config.php中连接配置文件对应。
create database xxx_wp_db;
create user 'xxx_wp_user'@'localhost' identified by 'MVd#GHo7*wXdwig0A@iYxxXU';
grant all privileges on xxx_wp_db.* to 'xxx_wp_user'@'localhost';
flush privileges;
exit;
# 下载WordPress:在网站根目录(通常在/var/www/html/)下载并解压最新WordPress。
cd /var/www/html
sudo wget -c http://wordpress.org/latest.tar.gz
sudo tar -zxvf latest.tar.gz
sudo rm latest.tar.gz
# 配置WordPress:将样本配置文件改名为正式配置文件名,并设置WordPress配置信息
cd wordpress
sudo mv wp-config-sample.php wp-config.php
sudo vim wp-config.php
# 编辑器中修改信息:将数据库名、用户名和登录密码改为之前创建的数据库名和用户信息。
define('DB_NAME', 'xxx_wp_db');
define('DB_USER', 'xxx_wp_user');
define('DB_PASSWORD', 'MVd#GHo7*wXdwig0A@iYxxXU');
# 设置文件夹权限:运行以下命令设置WordPress目录和文件的操作用户信息和权限。
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
# 配置Nginx虚拟主机:创建一个新的Nginx虚拟主机配置文件,并启用虚拟主机。
cd /etc/nginx/sites-available
sudo mv ./default ./default.bak
sudo vim /etc/nginx/sites-available/default
server {
#监听http协议80端口
listen 80;
#服务器域名及公网IP(空格分隔)
server_name www.xxx.com xxx.com xxx.xxx.xxx.xxx;
#所有访问均跳转到https协议
return 302 https://$server_name;
}
server {
# 监听https协议443端口
listen 443 ssl;
server_name www.xxx.com xxx.com xxx.xxx.xxx.xxx;
# SSL证书配置(提前上传至/etc/nginx/certs目录),可申请阿里云免费证书
ssl_certificate /etc/nginx/certs/www.xxx.com.pem;
ssl_certificate_key /etc/nginx/certs/www.xxx.com.key;
# SSL协议访问过期时间、缓存时间与密钥类型
ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
ssl_ciphers HIGH:!aNULL:!MD5;
# 设置网站位置,需要与前面wordpress释放位置匹配
root /var/www/html/wordpress;
# wordpress上传文件限制设置及管理目录访问方式配置
client_max_body_size 10m;
rewrite /wp-admin$ https://$host$uri/ permanent;
# 根目录文件搜索配置
location / {
index index.php;
# 配置采用文章名形式导航必须
try_files $uri $uri/ /index.php?$args;
}
# php文件解释器配置
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# socket形式访问php-fpm(推荐),查看php版本 php -v
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
include fastcgi_params;
fastcgi_intercept_errors on;
}
# 拒绝访问隐藏文件
location ~ /\.ht {
deny all;
}
}
# 测试配置文件,重启Nginx服务。
sudo nginx -t
systemctl reload nginx
# 更新操作系统、根据需要决定是否升级
sudo apt upgrade -y