Ubuntu 基于 Nginx 部署 WordPress

前置准备

Bash
su  #root登录
apt update #apt检查更新
apt upgrade -y #apt更新
apt install wget unzip -y #安装wget、unzip

下载并配置WordPress文件

Bash
wget https://wordpress.org/latest.zip #下载wordpress文件
mv latest.zip /var/www/ #移动文件至/var/www/
cd /var/www #进入/var/www
unzip latest.zip #解压wordpress文件
chown -R www-data:www-data wordpress #更改wordpress全部文件所有人、所有组
chmod -R 755 wordpress #更改wordpress全部文件权限

安装PHP

Bash
apt install php php-cli php-fpm php-mysql php-json php-opcache php-mbstring php-xml php-gd php-curl -y #安装php
php -v #查看php版本
systemctl stop apache2 #apache2会占用80端口
systemctl disable apache2 

安装及配置Nginx

Bash
apt install nginx -y #安装nginx
systemctl enable nginx #设置nginx开机启动
systemctl start nginx #启动nginx
vi /etc/nginx/sites-available/default #编辑nginx配置文件
Nginx
server {
        listen 80 default_server;
        listen [::]:80 default_server;
        #SUBDOMAIN.DOMAIN.TLD替换为自己的域名
        server_name SUBDOMAIN.DOMAIN.TLD;
        root /var/www/wordpress;

        index index.html index.php index.htm index.nginx-debian.html;

        access_log /var/log/nginx/wp_access.log;
        error_log /var/log/nginx/wp_error.log;

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                #php8.1这里根据自己的php版本来
                fastcgi_pass unix:/run/php/php8.1-fpm.sock;
                client_max_body_size 1024M;
        }

        location ~ /\.ht {
                deny all;
        }

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }

}
Bash
nginx -t #测试配置文件
systemctl reload nginx #重新加载nginx

安装及配置MariaDB

Bash
apt install mariadb-server -y #安装mariadb
systemctl enable mariadb #设置mariadb开机启动
systemctl start mariadb #启动mariadb
mysql_secure_installation #启动mariadb安全配置向导
mysql #进入mariadb
SQL
CREATE DATABASE wp_db; --创建wordpress数据库
CREATE USER 'wp_user'@'%' IDENTIFIED BY 'password'; --创建wordpress用户
GRANT ALL PRIVILEGES ON wp_db.* TO 'wp_user'@'%'; --将wp_db所有的权限赋予wp_user
FLUSH PRIVILEGES; --刷新权限
\q --退出mariadb

WordPress安装

浏览器访问域名,根据wordpress安装指引完成安装。

参考资料

  1. Ubuntu 20.04 基于 Nginx 部署 WordPress – 我的it技术
  2. 基于 Nginx 部署 WordPress