CentOS 8搭建LNMP + WordPress(三)

目录

CentOS 8近日推出了,其LNMP环境的搭建也与CentOS7有所不同。基于CentOS 8,我重写了前一篇文章《CentOS7搭建LNMP+WordPress一篇搞定》,得到了这一本文。

为了更好地阅读体验,我将本文分成了三个部分:

  1. 名词解释与CentOS 8操作系统安装
  2. 网页服务器的安装与配置(Nginx + PHP)
  3. 数据库(MariaDB)与WordPress的安装与配置

以下是本文的第三个部分


六、安装MariaDB

1. MariaDB安装

CentOS 8默认采用MariaDB 10.3,可以通过下面的方法直接安装:

sudo dnf install mariadb-server
sudo systemctl enable mariadb
sudo systemctl start mariadb
systemctl status mariadb

安装成功后,如果出现绿色的active(running),说明安装成功。

启动MariaDB的一个安全脚本:

sudo mysql_secure_installation

In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.

输入已有root密码,没设过密码敲回车即可。

Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
Set root password? [Y/n]

是否设置root密码?(Y,并输入密码)

By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB 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? [Y/n]

默认情况下,MariaDB安装有一个匿名用户,允许任何人登录MariaDB而不必为他们创建用户帐户。这仅用于测试,并使安装更顺利。在进入生产环境之前,应该先删除它们。
删除匿名用户?(根据需要选择,我选Y)

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? [Y/n]

通常,只允许root用户从“localhost”连接。这可以确保有人无法从网络猜出根密码。
不允许远程root登录?(根据需要选择,我选Y)

By default, MariaDB 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? [Y/n]

默认情况下,MariaDB附带一个名为“test”的数据库,任何人都可以访问该数据库。这也仅用于测试,在进入生产环境之前应该删除。
删除test数据库和对它的访问权限?(根据需要选择,我选Y)

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reload privilege tables now? [Y/n]

是否需要刷新权限表?(Y)

至此,MariaDB已经搭建完成,如需安装WordPress,还要配置相关的用户和数据表。

2. Wordpress相关用户与数据库配置

首先登陆数据库:

mysql -u root -p

接着创建Wordpress专用数据库用户,并创建对应数据库。注意,这个用户的账户和密码会以明文写在Wordpress配置文件中,因此不能使用root用户。

CREATE DATABASE [WordPress数据库名];
CREATE USER [访问WordPress用户名]@localhost IDENTIFIED BY '[WordPress数据库用户的密码]';
GRANT ALL PRIVILEGES ON [数据库名].* TO [访问WordPress用户名]@localhost IDENTIFIED BY '[WordPress数据库用户的密码]';
FLUSH PRIVILEGES;                       # 刷新数据库设置
EXIT;                                   # 退出数据库

例如:

CREATE DATABASE wordpress;                                                       # 创建一个名为wordpress的密码
CREATE USER wpUser@localhost IDENTIFIED BY '123456';                             # 创建用户名为wpUser,密码为123456的用户
GRANT ALL PRIVILEGES ON wordpress.* TO wpUser@localhost IDENTIFIED BY '123456';  # 赋予wpUser访问wordpress数据库的权限
FLUSH PRIVILEGES;                                                                # 刷新数据库设置
EXIT;                                                                            # 退出数据库

七、安装Wordpress

1. Wordpress的下载与解压

进入Nginx配置的网页路径的上一级文件夹。

cd [WordPress的上一级文件夹]

例如我的网页路径是/usr/share/nginx/html,我可以:

cd /usr/share/nginx/

然后,将路径的最后一级文件夹删掉(为接下来使用WordPress替代)

sudo rm -rf [最后一级文件夹]
# 例如:
sudo rm -rf ./html

下载并解压WordPress,然后重命名为网页最后一级目录。

wget https://wordpress.org/latest.tar.gz        # 从官网下载
tar -zxvf latest.tar.gz                         # 解压tar.gz包
mv wordpress [网页目录最后一级文件夹]             # 例如mv wordpress html

但是,在我写文章的时候,官网因为某种原因而无法访问。因此,可以采用这样的方式手动下载解压:

# 注意,别忘了将下面的链接要换成最新的版本
wget https://downloads.wordpress.org/release/zh_CN/wordpress-5.3.2.zip
unzip /wordpress-5.3.2.zip                      # 解压
mv wordpress [网页目录最后一级文件夹]             # 例如mv wordpress html
2. 文件权限与Selinux的设置

接下来要设置网页目录的读写权限:

cd [网页路径的上一级文件夹]    # 如果紧跟着上面做的话,你已经在WordPress路径的上一级文件夹了
sudo chown -R nginx:nginx [WordPress最后一级文件夹]    # 递归地将WordPress文件夹地权限赋给Nginx
sudo chmod 0755 [WordPress最后一级文件夹]              # 如果在后面出现了无法写入wp-config.php的问题,可以尝试暂时设置为0777,但是在安装完成后须重新设置为0755,并检查php-fpm是否配置正确。

例如:

cd /usr/share/nginx/
sudo chown -R nginx:nginx html
sudo chmod 0755 html

然后设置Selinux。

sudo chcon -R system_u:object_r:httpd_sys_content_t:s0 [网页目录]           # 赋予Nginx整个目录的读权限
cd [网页目录]
sudo chcon -R system_u:object_r:httpd_sys_rw_content_t:s0 wp-content        # 赋予Nginx在wp-content目录下的读写权限
3. Wordpress网站配置

然后在浏览器中输入:[主机ip][:端口](使用默认端口,可省略“:端口的内容)。如能看到WordPress的安装界面,或者是选择语言的界面,说配置成功。

安装界面

接下来,输入数据库的配置信息。根据创建MariaDB时配置的WordPress用户名、密码、数据库填写。

数据库设置

然后单击提交->安装后,配置Wordpress管理员用户。

准备安装
设置网站管理员
成功

接下来,您可以使用刚才创建的管理员用户登陆网站管理界面,也可以在网页上看看。至此,您的网站已经搭建完成。

登陆
网站

参考文献

[6] Nginx: Linux packages http://nginx.org/en/linux_packages.html#RHEL-CentOS

[7] Wordpress 官方文档对PHP的要求 https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions

猜你喜欢

转载自www.cnblogs.com/mrfangd/p/LNMP-CentOS8-3.html