I. 先决条件

1.1 OS 条件

本教程使用 RedHat 8.x 及其衍生版本,在 RedHat 9.x 及其衍生版本中安装 php 插件时会报错(2024-12-27)。

1.2 其它条件

在继续本教程之前,请确保满足以下先决条件:

  • 将域名指向您的服务器公共 IP 地址。在本教程中,我们将使用 example.com
  • 具有 sudo 权限的用户 身份登录 。
  • 按照这些说明 安装 Nginx 。
  • 您已为您的域安装了 SSL 证书。您可以按照这些说明 生成免费的 Let’s Encrypt SSL 证书 。

II. 安装 Nginx

参考 👉 Nginx 安装 | Mortal

III. 安装 MySQL

3.1 MySQL 安装

参考 👉 MySQL 安装(Rocky 8.x) | Mortal

3.2 创建数据库

1sudo mysql -u root -p
2CREATE DATABASE wordpress;
3CREATE USER 'wordpress'@'%' IDENTIFIED BY '<your-password>';
4GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'%';
5FLUSH PRIVILEGES;
6quit

IV. PHP 安装

4.1 安装 PHP

参考 👉 PHP 安装 | Mortal

4.2 配置 PHP

4.2.1 PHP 扩展安装

ℹ️ WordPress 主机团队维护着一份必需和推荐的模组列表,列于团队手册

1sudo dnf install php php-{json,mysqlnd,gd,curl,dom,exif,fileinfo,hash,igbinary,imagick,intl,mbstring,openssl,pcre,xml,zip} -y

4.2.2 Nginx 配置

Nginx 使用 PHP-FPM 来处理 PHP 文件。默认情况下,PHP-FPM 配置为以 Apache 用户身份运行。所以你需要为 Nginx 配置它。

1sudo vim /etc/php-fpm.d/www.conf

将用户和组从 apache 更改为 nginx,如下所示:

1...
2user = nginx
3...
4group = nginx
5...
6listen = /run/php-fpm/www.sock
7...
8listen.owner = nginx
9listen.group = nginx

使用以下 chown 命令确保 /var/lib/php 目录具有正确的所有权:

1sudo chown -R root:nginx /var/lib/php

进行更改后,启用并启动 PHP FPM 服务:

1sudo systemctl enable php-fpm --now

V. 安装 WordPress

在下载 Wordpress 存档之前,首先创建一个目录,我们将在其中放置 WordPress 文件:

1sudo mkdir -p /var/www/html/wordpress

下一步是使用以下 wget 命令从 WordPress 下载页面下载最新版本的 WordPress:

1sudo wget -P /tmp https://wordpress.org/latest.tar.gz

下载完成后,解压缩 WordPress 存档并将文件移动到域的文档根目录中:

1tar xf latest.tar.gz
2sudo mv /tmp/wordpress/ /var/www/html/

置正确的权限,以便 Web 服务器可以完全访问站点的文件和目录:

1sudo chown -R nginx: /var/www/html/wordpress

5.1 创建 Nginx 虚拟主机

要为我们的 WordPress 实例创建一个新的服务器块,我们将使用来自官方 Nginx 站点的 Nginx 配方

1sudo vim /etc/nginx/conf.d/example.com.conf
  • HTTP 配置参考如下:
 1server {
 2    listen 80;
 3    server_name localhost;
 4
 5    root /var/www/html/wordpress;
 6    index index.php index.html index.htm;
 7
 8    location / {
 9        try_files $uri $uri/ =404;
10   }
11
12    error_page 404 /404.html;
13    error_page 500 502 503 504 /50x.html;
14
15    location = /50x.html {
16        root /usr/share/nginx/html;
17    }
18
19    location ~ \.php$ {
20        try_files $uri =404;
21        fastcgi_split_path_info ^(.+\.php)(/.+)$;
22        fastcgi_pass unix:/run/php-fpm/www.sock;
23        fastcgi_index index.php;
24        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
25        include fastcgi_params;
26    }
27}
  • HTTPS 配置参考如下:
  1server {
  2    listen 80 default_server;
  3    listen [::]:80 default_server;
  4
  5    server_name www.example.com example.com;
  6
  7    location / {
  8        return 301 https://$host$request_uri;
  9    }
 10
 11    if ($host != 'example.com') {
 12        return 444;
 13    }
 14}
 15
 16server {
 17    listen 443 ssl;
 18    listen [::]:443 ssl;
 19    http2 on;
 20
 21    server_name www.example.com;
 22
 23    ssl_certificate /etc/ssl/example-com/fullchain.cer;
 24    ssl_certificate_key /etc/ssl/example-com/example.com.key;
 25
 26    return 301 https://example.com$request_uri;
 27
 28    if ($host != 'example.com') {
 29        return 444;
 30    }
 31}
 32
 33server {
 34    listen 443 ssl;
 35    listen [::]:443 ssl;
 36    http2 on;
 37
 38    server_name example.com;
 39    
 40    if ($host != 'example.com') {
 41        return 444;
 42    }
 43
 44    ssl_certificate /etc/ssl/example-com/fullchain.cer;
 45    ssl_certificate_key /etc/ssl/example-com/example.com.key;
 46    ssl_session_timeout 1d;
 47    ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
 48
 49    # intermediate configuration
 50    ssl_protocols TLSv1.2 TLSv1.3;
 51    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305;
 52    ssl_prefer_server_ciphers on;
 53
 54    # HSTS (ngx_http_headers_module is required) (31536000 seconds)
 55    add_header Strict-Transport-Security "max-age=31536000" always;
 56
 57    # log files
 58    access_log /var/log/nginx/example-com-access.log;
 59    error_log /var/log/nginx/example-com-error.log;
 60
 61    root /var/www/html/wordpress;
 62    index index.php;
 63
 64    location / {
 65        try_files $uri $uri/ /index.php?$args;
 66    }
 67
 68    location = /favicon.ico {
 69        log_not_found off;
 70        access_log off;
 71    }
 72
 73    location = /robots.txt {
 74        allow all;
 75        log_not_found off;
 76        access_log off;
 77    }
 78
 79    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
 80        expires max;
 81        log_not_found off;
 82    }
 83
 84    location ~ \.php$ {
 85        try_files $uri =404;
 86        fastcgi_split_path_info ^(.+\.php)(/.+)$;
 87        fastcgi_pass unix:/run/php-fpm/www.sock;
 88        fastcgi_index index.php;
 89        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 90        include fastcgi_params;
 91    }
 92
 93	location ~ /\. {
 94	    deny all;
 95	}
 96
 97	location = /wp-config.php {
 98		deny all;
 99	}
100}

📢 注意:
不要忘记用您的 WordPress 域替换 example.com 并设置正确的 SSL 证书文件路径。所有 HTTP 请求都将重定向到 HTTPS 。此配置中使用的片段是在本指南 中创建的。

在重新启动 Nginx 服务之前测试配置以确保没有语法错误:

1sudo nginx -t

最后,重新加载 Nginx 服务以应用配置更改:

1sudo systemctl restart nginx

5.2 配置 SELinux 和防火墙

默认情况下,SELinux 在 CentOS 8 服务器上启用。切换到 permissive 模式

1sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config

然后重新启动系统以应用更改。 接下来,您还需要允许 HTTP 和 HTTPS 服务通过防火墙。您可以通过运行以下命令来允许它们:

1firewall-cmd --permanent --add-service=http
2firewall-cmd --permanent --add-service=https

重新加载防火墙守护进程以应用更改:

1firewall-cmd --reload

5.3 完成 WordPress 安装

现在下载了 Wordpress 并完成了服务器配置,您可以通过 Web 界面完成安装。 打开您的浏览器,访问 URL https://example.com 来访问 WordPress 安装向导……

X. 参考文档

参考文档1:How to Install WordPress with Nginx on CentOS 7
参考文档2:How to Install WordPress on CentOS 8 (Step by Step) Tutorial