在Vultr VPS上的CentOS 8系统中,使用Nginx+PHP 7.4+MySQL 8安装WordPress

首先

有很多关于安装WordPress的文章,但是我会备忘录下在没有使用LAMP堆栈(Linux,Apache,MySQL,PHP)的情况下的单独安装步骤。虽然Vultr可以选择预装WordPress的VPS,但是因为基于CentOS7,所以如果想使用CentOS8,就需要自己进行安装。

就我所知,没有比Vultr更便宜且可以运行WordPress的VPS。如果通过推广链接进行新申请,Vultr会提供100美元的信用额。所以请务必使用。有了100美元,你可以创建多个VPS,尝试集群配置,做很多不同的事情。推荐者也会得到一点点信用额。

你可以通过这个链接获得100美元。

スクリーンショット 2020-03-09 午後7.03.25.png
スクリーンショット 2020-03-09 午後7.17.41.png

我们在Vultr VPS上配置了CentOS8。

由于只是暂时尝试,所以选择了最便宜的服务器。由于可能会使用价格低廉的GB单价的Block Storage来存放WordPress的图片,所以将Block Storage选项设为ON。为了能够使用先前在其他文章中使用过的SSH密钥来方便SSH连接,需要事先做出准备。另外,如果预计图片文件将超过250GB,使用Vultr对象存储将会更便宜(从250GB/$5开始)。但是考虑到这篇文章的目标读者规模,我决定省略Vultr对象存储相关内容。

(1) 创建虚拟私有服务器

my.vultr.com_deploy_(iPad Pro).png
$ ssh -i ~/.ssh/vultr2 root@VultrIPアドレス
The authenticity of host 'IPアドレス (IPアドレス)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'IPアドレス' (ECDSA) to the list of known hosts.
root@IPアドレス's password: 
Activate the web console with: systemctl enable --now cockpit.socket

(2) 操作系统的组成

我需要为我的过去5到6篇文章进行Swap区域和防火墙的配置。

在Vultr VPS上使用CentOS7,Ruby on Rails 6,Puma,Capistrano3进行生产环境部署的过去记录。

2. 安装Nginx

(1) 使用yum进行安装。

# yum update
# yum info nginx
Last metadata expiration check: 0:00:58 ago on Mon 01 Jun 2020 02:22:09 AM UTC.
Available Packages
Name         : nginx
Epoch        : 1
Version      : 1.14.1
Release      : 9.module_el8.0.0+184+e34fea82
Architecture : x86_64
Size         : 570 k
Source       : nginx-1.14.1-9.module_el8.0.0+184+e34fea82.src.rpm
Repository   : AppStream
Summary      : A high performance web server and reverse proxy server
URL          : http://nginx.org/
License      : BSD
Description  : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
             : IMAP protocols, with a strong focus on high concurrency, performance and low
             : memory usage.

# yum install nginx

(2)在Centos 8上启动Nginx。

# systemctl start nginx
# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service  /usr/lib/systemd/system/nginx.service.
# systemctl status nginx
 nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-06-01 02:31:37 UTC; 1min 23s ago
 Main PID: 2820 (nginx)
    Tasks: 2 (limit: 2864)
   Memory: 7.3M
   CGroup: /system.slice/nginx.service
           ├─2820 nginx: master process /usr/sbin/nginx
           └─2821 nginx: worker process

Jun 01 02:31:36 vultrguest systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jun 01 02:31:36 vultrguest nginx[2816]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jun 01 02:31:36 vultrguest nginx[2816]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jun 01 02:31:37 vultrguest systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jun 01 02:31:37 vultrguest systemd[1]: Started The nginx HTTP and reverse proxy server.

给出一些基本的命令,因为操作系统的版本可能会有些差异。

# systemctl stop nginx
# systemctl restart nginx
# systemctl reload nginx

由于Nginx可以在默认配置下进行操作确认,所以请将Vultr VPS的IP地址输入到浏览器中进行确认。

スクリーンショット 2020-05-31 午後10.37.21.png

(3) Nginx的配置

配置檔案的位置 de

    • Nginx configuration directory: /etc/nginx

 

    • Nginx root directory: /usr/share/nginx/html

 

    Master/Global configuration file: /etc/nginx/nginx.conf

3. 安装 PHP 7.4

(1) 添加 EPEL 和 Remi 仓库

使用dnf命令添加软件仓库。

# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
# rpm -qa | grep epel
epel-release-8-8.el8.noarch
# dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
# rpm -qa | grep remi
remi-release-8.1-2.el8.remi.noarch

(2) PHP安装

检查可用的PHP模块。

# dnf module list php
Remi's Modular repository for Enterprise Linux 8 - x86_64                                                                                              474 kB/s | 569 kB     00:01    
Safe Remi's RPM repository for Enterprise Linux 8 - x86_64                                                                                             982 kB/s | 1.5 MB     00:01    
CentOS-8 - AppStream
Name                               Stream                                 Profiles                                                 Summary                                             
php                                7.2 [d]                                common [d], devel, minimal                               PHP scripting language                              
php                                7.3                                    common, devel, minimal                                   PHP scripting language                              

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name                               Stream                                 Profiles                                                 Summary                                             
php                                remi-7.2                               common [d], devel, minimal                               PHP scripting language                              
php                                remi-7.3                               common [d], devel, minimal                               PHP scripting language                              
php                                remi-7.4                               common [d], devel, minimal                               PHP scripting language                              

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

由于Remi-7.4是最新版本,所以我们将其启用。

# dnf module enable php:remi-7.4
Last metadata expiration check: 0:01:40 ago on Mon 01 Jun 2020 02:48:32 AM UTC.
Dependencies resolved.
=======================================================================================================================================================================================
 Package                                     Architecture                               Version                                      Repository                                   Size
=======================================================================================================================================================================================
Enabling module streams:
 php                                                                                    remi-7.4                                                                                      

Transaction Summary
=======================================================================================================================================================================================

Is this ok [y/N]: y
Complete!

启用PHP remi-7.4模块后,安装PHP及其相关软件包。

# dnf install php php-cli php-common
Last metadata expiration check: 0:03:08 ago on Mon 01 Jun 2020 02:48:32 AM UTC.
Dependencies resolved.
=======================================================================================================================================================================================
 Package                                    Architecture                   Version                                                          Repository                            Size
=======================================================================================================================================================================================
Installing:
 php                                        x86_64                         7.4.6-1.el8.remi                                                 remi-modular                         3.0 M
 php-cli                                    x86_64                         7.4.6-1.el8.remi                                                 remi-modular                         4.6 M
 php-common                                 x86_64                         7.4.6-1.el8.remi                                                 remi-modular                         1.2 M
Installing dependencies:
 apr                                        x86_64                         1.6.3-9.el8                                                      AppStream                            125 k
 apr-util                                   x86_64                         1.6.1-6.el8                                                      AppStream                            105 k
 centos-logos-httpd                         noarch                         80.5-2.el8                                                       AppStream                             24 k
 httpd                                      x86_64                         2.4.37-16.module_el8.1.0+256+ae790463                            AppStream                            1.7 M
 httpd-filesystem                           noarch                         2.4.37-16.module_el8.1.0+256+ae790463                            AppStream                             35 k
 httpd-tools                                x86_64                         2.4.37-16.module_el8.1.0+256+ae790463                            AppStream                            103 k
 mod_http2                                  x86_64                         1.11.3-3.module_el8.1.0+213+acce2796                             AppStream                            158 k
 oniguruma                                  x86_64                         6.8.2-1.el8                                                      AppStream                            188 k
 libsodium                                  x86_64                         1.0.18-2.el8                                                     epel                                 162 k
 php-json                                   x86_64                         7.4.6-1.el8.remi                                                 remi-modular                          74 k
Installing weak dependencies:
 apr-util-bdb                               x86_64                         1.6.1-6.el8                                                      AppStream                             25 k
 apr-util-openssl                           x86_64                         1.6.1-6.el8                                                      AppStream                             27 k
 php-fpm                                    x86_64                         7.4.6-1.el8.remi                                                 remi-modular                         1.6 M
 php-mbstring                               x86_64                         7.4.6-1.el8.remi                                                 remi-modular                         527 k
 php-opcache                                x86_64                         7.4.6-1.el8.remi                                                 remi-modular                         334 k
 php-pdo                                    x86_64                         7.4.6-1.el8.remi                                                 remi-modular                         143 k
 php-sodium                                 x86_64                         7.4.6-1.el8.remi                                                 remi-modular                          87 k
 php-xml                                    x86_64                         7.4.6-1.el8.remi                                                 remi-modular                         215 k
Enabling module streams:
 httpd                                                                     2.4                                                                                                        

Transaction Summary
=======================================================================================================================================================================================
Install  21 Packages

Total download size: 14 M
Installed size: 65 M
Is this ok [y/N]: y

检查已安装的PHP和PHP-FPM版本。截至2020年6月,已安装最新版本的7.4。

# php -v
PHP 7.4.6 (cli) (built: May 12 2020 08:09:15) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies

# php-fpm -v
PHP 7.4.6 (fpm-fcgi) (built: May 12 2020 08:09:15)
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies

(3) 更改最大文件大小设置

默认情况下,PHP只能上传最大为2MB的文件,所以我们需要修改php.ini中的upload_max_filesize和nginx.conf,以便能够上传更大的文件。

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize =256M

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size
post_max_size = 256M
http {
....

    client_max_body_size 256M;

....
}
# systemctl restart php-fpm
# systemctl restart nginx

(4) 验证Nginx和PHP的协作

配置phpinfo();以便在浏览器中访问和查看。

# echo "<?php phpinfo(); ?>" > /var/www/html/index.php
# chown nginx.nginx /var/www/html/index.php

在没有任何设置的情况下进行浏览器访问时,会出现错误。

スクリーンショット 2020-05-31 午後11.15.25.png

确认nginx和PHP的套接字连接。

# cat /etc/nginx/conf.d/php-fpm.conf
# PHP-FPM FastCGI server
# network or unix domain socket configuration

upstream php-fpm {
        server unix:/run/php-fpm/www.sock; これをメモしておく。
}
server {
   server_name VPSIPアドレス;
   root /var/www/html;

   location / {
       index index.html index.htm index.php;
   }

   location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_pass unix:/run/php-fpm/www.sock; ここはさっき確認したソケットのフルパス
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
}

把/etc/nginx/conf.d/wordpress.conf的更改加载到nginx中。

# systemctl reload nginx

在php-fpm的/etc/php-fpm.d/www.conf配置文件中,将用户和组更改为nginx。

- user = apache
- group = apache

+ user = nginx
+ group = nginx

为了使/etc/php-fpm.d/www.conf的变更生效,将重新启动php-fpm。

# systemctl restart php-fpm

设置完毕后,可以通过浏览器确认是否能够读取index.php文件。如果出现以下类似的界面,则表示设置正确。

スクリーンショット 2020-05-31 午後11.29.29.png

4. 安装MySQL 8

(1) 卸载MariaDB

如果已经安装了MariaDB,我们将首先卸载它。

# dnf list installed | grep mariadb
mariadb.x86_64            3:10.3.11-2.module_el8.0.0+35+6f2527ed
mariadb-common.x86_64     3:10.3.11-2.module_el8.0.0+35+6f2527ed
# dnf remove mariadb

(2) 安装MySQL8.0

我們可以通過MySQL的Yum存儲庫來確認可以安裝的MySQL版本。

# dnf info mysql
Last metadata expiration check: 0:09:51 ago on Mon 01 Jun 2020 02:48:32 AM UTC.
Available Packages
Name         : mysql
Version      : 8.0.17
Release      : 3.module_el8.0.0+181+899d6349
Architecture : x86_64
Size         : 11 M
Source       : mysql-8.0.17-3.module_el8.0.0+181+899d6349.src.rpm
Repository   : AppStream
Summary      : MySQL client programs and shared libraries
URL          : http://www.mysql.com
License      : GPLv2 with exceptions and LGPLv2 and BSD
Description  : MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
             : client/server implementation consisting of a server daemon (mysqld)
             : and many different client programs and libraries. The base package
             : contains the standard MySQL client programs and generic MySQL files.

开始安装版本8.0.17。

# dnf install @mysql:8.0
# dnf install php-mysqlnd

以上完成了MySQL 8.0的安装,现在我们需要进行自动启动配置并启动MySQL。

# systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
# systemctl start mysqld

(3) MySQL的初始设置

MySQL的初始设置很简单。我全部都用“y”进行了设置。

# mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

创建WordPress用的MySQL用户。

事先创建一个可以登录WordPress初始设置界面的用户。我选择了用户名为”wpuser”,但可以根据个人喜好任意命名。

# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.17 Source distribution

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE USER 'wpuser'@'localhost' IDENTIFIED BY '任意のパスワード';
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.01 sec)

mysql> GRANT ALL ON wordpress.* TO `wpuser `@`localhost`;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

5. 安装WordPress

(1) 安装前提软件

# dnf install tar curl php-json
Last metadata expiration check: 0:46:02 ago on Mon 01 Jun 2020 02:48:32 AM UTC.
Package tar-2:1.30-4.el8.x86_64 is already installed.
Package curl-7.61.1-11.el8.x86_64 is already installed.
Package php-json-7.4.6-1.el8.remi.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

(2) WordPress的安装

下载WordPress,并解压到Nginx的根目录。我已把它下载到/tmp文件夹,但可以选择任何地方。解压后,将整个目录复制到nginx的根目录中。

# cd /tmp
# curl https://wordpress.org/latest.tar.gz --output wordpress.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11.6M  100 11.6M    0     0  32.9M      0 --:--:-- --:--:-- --:--:-- 32.9M
# tar xf wordpress.tar.gz
# cp -r wordpress /var/www/html

最终将更改权限和SELinux安全上下文。

# chown -R nginx.nginx /var/www/html/wordpress
# chcon -t httpd_sys_rw_content_t /var/www/html/wordpress -R

WordPress的安装已完成。请在浏览器中确认WordPress的首页(http://IP地址/wordpress),如果显示以下页面则表示安装成功。

スクリーンショット 2020-05-31 午後11.41.02.png

(3) WordPress的配置

点击WordPress初始页面上的”Let’s go”按钮开始配置WordPress。

スクリーンショット 2020-05-31 午後11.45.38.png

输入必要信息后,点击提交按钮,将会跳转至以下界面,然后点击“运行安装”进行执行。

スクリーンショット 2020-05-31 午後11.46.50.png

设置WordPress的用户名和密码,然后点击安装WordPress。

スクリーンショット 2020-05-31 午後11.47.31.png

一旦看到下面的界面,表示 WordPress 安装完成,然后登录进入 WordPress 管理页面。

スクリーンショット 2020-05-31 午後11.50.26.png

登录页面为http://IP地址/wordpress/wp-login.php。

スクリーンショット 2020-05-31 午後11.53.07.png

从这里开始就是WordPress的世界了,请使用你喜欢的主题来创建内容。

スクリーンショット 2020-05-31 午後11.55.33.png

请参阅该文章。

    • How To Install And Configure Nginx On CentOS 8

 

    • How to Install PHP 7.4 on CentOS 8

 

    • CentOS8.0にMySQL8.0をインストールする

 

    • How to install WordPress on RHEL 8 / CentOS 8 Linux

 

    • How To Install Linux, Nginx, MariaDB, PHP (LEMP Stack) On CentOS 8 / RHEL 8

 

    • How to Install MySQL 8.0 on CentOS 8 / RHEL 8

 

    How To Install WordPress with Nginx on CentOS 8 / RHEL 8