介绍
WordPress 是一个以 PHP 和 MySQL 为平台的自由开源的博客软件和内容管理系统。WordPress 具有插件架构和模板系统。截至 2018 年 4 月,排名前 1000 万的网站中超过 30.6% 使用 WordPress。WordPress 是最受欢迎的网站内容管理系统。全球有大约 30% 的网站都是使用 WordPress 架设网站的。
LNMP 一组开源软件简称,它们通常安装在一起以使服务器能够托管动态网站和 Web 应用程序。这个 L简写实际上是一个首字母缩写词,它代表Linux 操作系统,带有Nginx Web 服务器(它取代了 LAMP 堆栈的 Apache 组件), 站点数据存储在基于 mysql 的数据库中,动态内容由 PHP 处理。
前提条件
你应该在服务器上设置一个单独的非 root 用户帐户。
第 1 步 – 安装 Nginx
要添加 CentOS 8 EPEL 存储库,请运行以下命令:
> yum install epel-release
现在 EPEL 存储库已安装在你的服务器上,请使用以下yum命令安装 Nginx :
> yum install nginx
安装完成后,使用以下命令启动 Nginx 服务:
> systemctl start nginx
打开浏览器访问http://ip
设置 Nginx 开机启动
> systemctl enable nginx
第 2 步 – 安装 MariaDB
MariaDB 是一个 MySQL 的替代品。MariaDB 是 MySQL 关系数据库管理系统的社区开发分支。
> yum install mariadb-server mariadb
安装完成后,我们需要使用以下命令启动 MariaDB:
> systemctl start mariadb
现在 MariaDB 数据库正在运行,我们要运行一个安全脚本,该脚本将删除一些危险的默认值并锁定对我们数据库的访问。通过运行以下命令启动交互式脚本:
> mysql_secure_installation
提示符会询问你当前的 MariaDB root 密码。因为你刚刚安装了 MariaDB,所以你很可能没有 MariaDB,所以按回车键将其留空。然后提示符会询问你是否要设置根密码。继续输入 Y,并按照说明操作:
`mysql_secure_installation prompts:
Enter current password for root (enter for none): OK, successfully used password, moving on… Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. … Success!
`
开机启动 MariaDB
> systemctl enable mariadb
第 3 步 – 安装 PHP
CentOS 7 服务器中默认可用的 PHP 版本已过时,因此,我们需要安装第三方软件包存储库才能获取 PHP 7+ 并将其安装在你的 CentOS 7 服务器上。Remi 是一个流行的软件包存储库,为 CentOS 服务器提供最新的 PHP 版本。
wget –no-check-certificate https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm wget –no-check-certificate https://rpms.remirepo.net/enterprise/remi-release-8.rpm rpm -Uvh remi-release-8.rpm epel-release-latest-8.noarch.rpm
安装完成后,运行一个命令来启用包含首选 PHP 版本的存储库。要检查 Remi 存储库中可用的 PHP 7+ 版本
Plain Text复制代码
1
yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64
我们将安装 PHP 7.4。要启用正确的 Remi 包以安装 PHP 7.4
现在我们可以继续yum像往常一样安装 PHP 了。以下命令将安装在 Nginx 中设置 PHP 7.4 所需的所有软件包,并允许它连接到基于 MySQL 的数据库:
Plain Text复制代码
1
yum install php74 php-mysqlnd php-fpm
查看 PHP 版本
将php74 软连接 使用php命令
Plain Text复制代码
1
whereis php74
2
ln -s /usr/bin/php74 /usr/bin/php
3
php --version
4
PHP 7.4.27 (cli) (built: Dec 14 2021 17:17:06) ( NTS )
5
Copyright (c) The PHP Group
6
Zend Engine v3.4.0, Copyright (c) Zend Technologies
7
with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies
php-fpm 开机自启动
Plain Text复制代码
1
systemctl enable php74-php-fpm
2
systemctl start php74-php-fpm
PHP 现在已成功安装到你的系统上。接下来,我们需要对默认配置进行一些调整。
Plain Text复制代码
1
> vi /etc/php-fpm.d/www.conf
现在查找user和group指令。
Plain Text复制代码
1
> vim /etc/php-fpm.d/www.conf
2
3
; Unix user/group of processes
4
; Note: The user is mandatory. If the group is not set, the default user's group
5
; will be used.
6
; RPM: apache user chosen to provide access to the same directories as httpd
7
user = apache
8
; RPM: Keep a group allowed to write in log dir.
9
group = apache
10
…
你会注意到user和group变量都设置为apache。我们需要将这些更改为nginx:
Plain Text复制代码
1
> vim /etc/php-fpm.d/www.conf
2
…
3
; RPM: apache user chosen to provide access to the same directories as httpd
4
user = nginx
5
; RPM: Keep a group allowed to write in log dir.
6
group = nginx
7
…
接下来找到listen指令。默认情况下php-fpm将通过 TCP 监听主机端口。改成使其侦听本地套接字文件,这会提高服务器的整体性能。
Plain Text复制代码
1
> vim /etc/php-fpm.d/www.conf
2
listen = /var/run/php-fpm/php-fpm.sock
最后,我们需要更改我们刚刚在listen指令中定义的套接字文件的所有者和组设置。找到listen.owner,listen.group和listen.mode指令。这些行默认被注释掉。通过删除;行开头的前置符号来取消注释。然后,将所有者和组更改为nginx:
Plain Text复制代码
1
> vim /etc/php-fpm.d/www.conf
2
listen.owner = nginx
3
listen.group = nginx
4
listen.mode = 0660
启用并启动php-fpm服务
Plain Text复制代码
1
systemctl start php-fpm
你的 PHP 环境现已准备就绪。接下来,我们将配置 Nginx,以便它发送对 PHP 脚本的所有请求以供php-fpm.
第 4 步 – 配置 Nginx 处理 PHP
首先,在/etc/nginx/conf.d目录中打开一个新文件:
Plain Text复制代码
1
vim /etc/nginx/conf.d/default.conf
将以下 PHP 服务器定义块复制到你的配置文件中,不要忘记修改该server_name指令,使其指向你服务器的域名或 IP 地址:
`> vim /etc/nginx/conf.d/default.conf
server { listen 80; server_name server_domain_or_IP; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files
![](http://101.200.37.218/wp-content/uploads/2022/02/image-1.png)
uri/ =404; } error_page 404 /4html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ .php
uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME
![](http://101.200.37.218/wp-content/uploads/2022/02/image.png)
fastcgi_script_name; include fastcgi_params; } }
`
重新启动 Nginx
> systemctl restart nginx
步骤 5 — 测试 PHP 环境
现在你的 Web 服务器已设置好,我们可以创建一个测试 PHP 脚本
我们现在将创建一个测试 PHP 页面
在/usr/share/nginx/html目录中创建一个名为rumenz.php的 PHP 文件:
Plain Text复制代码
1
> vim /usr/share/nginx/html/rumenz.php
2
<?php
3
phpinfo();
完成后,保存并关闭文件。
现在我们可以测试我们的 Web 服务器是否可以正确显示由 PHP 脚本生成的内容。转到你的浏览器并访问你的服务器主机名或 IP 地址,然后是/rumenz.php:
Plain Text复制代码
1
http://ip/rumenz.php
若有收获,就点个赞吧