# 检查更新 Ubuntu 系统

sudo apt-get update
sudo apt-get upgrade -y

# 安装 Nginx

sudo apt-get install nginx -y

# 查看 Nginx 运行状态

sudo systemctl status nginx

# 启动 Nginx

sudo systemctl start nginx

# 配置 Nginx 开机自启动

sudo systemctl enable nginx

# 安装数据库 MariaDB

sudo apt-get install mariadb-server -y  
sudo systemctl start mariadb  
sudo systemctl enable mariadb

# 运行以下命令提高数据库安全性

sudo mysql_secure_installation

# 安装 PHP 7.4

sudo apt-get install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-json php7.4-opcache php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl -y

# 查看 PHP 版本

php -verson

# 创建 WordPress 数据库

# 进入 MariaDB。

mysql

# 创建 MariaDB 数据库。例如 “wordpress”。

CREATE DATABASE wordpress;

# 创建一个新用户。例如 “wordpress”,登录密码为 123456 。

CREATE USER 'wordpress'@'localhost' IDENTIFIED BY '123456';

# 赋予用户对 “wordpress” 数据库的全部权限。

GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost';

# 设置 root 账户密码。

ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD('输入您的密码');

# 使所有配置生效。

FLUSH PRIVILEGES;.

# 退出 MariaDB。

\q

# 编辑 Nginx 相关文件

# 创建 WordPress 根目录

sudo mkdir /var/www/wordpress

# 为 WordPress 创建 Nginx 文件

sudo vim /etc/nginx/sites-available/wordpress.conf
server {             
            listen 80;             
            root /var/www/wordpress;             
            index index.php index.html;             
            server_name SUBDOMAIN.DOMAIN.TLD;             
            access_log /var/log/nginx/www.access.log;             
            error_log /var/log/nginx/www.error.log;              
            location / {                            
                 try_files $uri $uri/ /index.php?$args;
            }
            location ~ \.php$ {
            include snippets/fastcgi-php.conf;                            
                 fastcgi_pass unix:/run/php/php7.4-fpm.sock;             
            }              
            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;             
            }  
}

* 替换 SUBDOMAIN.DOMAIN.TLD 为你的域名,同时检查 PHP-FPM 版本与上文中是否匹配,否则会导致 502 bad gateway

# 检查上述配置文件的正确性

nginx -t

# 创建链接

cd /etc/nginx/sites-enabled  
sudo ln -s ../sites-available/wordpress.conf

# 重新加载 Nginx 以应用更改的设置

sudo systemctl reload nginx

# 下载和配置 WordPress

cd /var/www/wordpress  
sudo wget https://wordpress.org/latest.zip  
sudo unzip latest.zip  
sudo mv wordpress/* .  
sudo rm -rf wordpress latest.zip

# 如果未安装 unzip,执行以下命令安装:

apt install unzip -y

# 更改文件所有权并且应用权限给 WordPress 所有文件

cd /var/www/wordpress  
sudo chown -R www-data:www-data *  
sudo chmod -R 755 *

# 配置 wp-config.php 文件

cd /var/www/wordpress  
sudo cp wp-config-sample.php wp-config.php  
sudo vim wp-config.php
/** 在 wp-config.php 文件中找出并更改以下内容 */ 
...  
...  
define('DB_NAME', 'wordpress_db');  
define('DB_USER', 'wpuser');  
define('DB_PASSWORD', 'Passw0rd!');  
...  
... 
define( 'AUTH_KEY', 'put your unique phrase here' ); 
define( 'SECURE_AUTH_KEY', 'put your unique phrase here' ); 
define( 'LOGGED_IN_KEY', 'put your unique phrase here' ); 
define( 'NONCE_KEY', 'put your unique phrase here' ); 
define( 'AUTH_SALT', 'put your unique phrase here' ); 
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' ); 
define( 'LOGGED_IN_SALT', 'put your unique phrase here' ); 
define( 'NONCE_SALT', 'put your unique phrase here' ); 
... 
...

为了保证 WordPress 网站的安全,在上面的 WordPress 配置文件中,在数据库配置选项之后,通过 https://api.wordpress.org/secret-key/1.1/salt/ 生成安全密钥,粘贴在配置中。

# 安装 WordPress

在浏览器中访问你的域名,根据 WordPress 设置指引完成最后的配置。

WordPress 安装完成。