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 创建数据库

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

IV. PHP 安装

4.1 安装 PHP

参考 👉 PHP 安装 | Mortal

4.2 配置 PHP

4.2.1 PHP 扩展安装

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

1
sudo 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 配置它。

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

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

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

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

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

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

1
sudo systemctl enable php-fpm --now

V. 安装 WordPress

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

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

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

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

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

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

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

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

5.1 创建 Nginx 虚拟主机

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

1
sudo vim /etc/nginx/conf.d/example.com.conf
  • HTTP 配置参考如下:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server {
    listen 80;
    server_name localhost;

    root /var/www/html/wordpress;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
   }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
  • HTTPS 配置参考如下:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name www.example.com example.com;

    location / {
        return 301 https://$host$request_uri;
    }

    if ($host != 'example.com') {
        return 444;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;

    server_name www.example.com;

    ssl_certificate /etc/ssl/example-com/fullchain.cer;
    ssl_certificate_key /etc/ssl/example-com/example.com.key;

    return 301 https://example.com$request_uri;

    if ($host != 'example.com') {
        return 444;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;

    server_name example.com;
    
    if ($host != 'example.com') {
        return 444;
    }

    ssl_certificate /etc/ssl/example-com/fullchain.cer;
    ssl_certificate_key /etc/ssl/example-com/example.com.key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions

    # intermediate configuration
    ssl_protocols TLSv1.2 TLSv1.3;
    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;
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (31536000 seconds)
    add_header Strict-Transport-Security "max-age=31536000" always;

    # log files
    access_log /var/log/nginx/example-com-access.log;
    error_log /var/log/nginx/example-com-error.log;

    root /var/www/html/wordpress;
    index index.php;

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

    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|svg)$ {
        expires max;
        log_not_found off;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

	location ~ /\. {
	    deny all;
	}

	location = /wp-config.php {
		deny all;
	}
}

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

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

1
sudo nginx -t

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

1
sudo systemctl restart nginx

5.2 配置 SELinux 和防火墙

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

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

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

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

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

1
firewall-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