CentOS 8近日推出了,其LNMP环境的搭建也与CentOS7有所不同。基于CentOS 8,我重写了前一篇文章《CentOS7搭建LNMP+WordPress一篇搞定》,得到了这一本文。
为了更好地阅读体验,我将本文分成了三个部分:
以下是本文的第二个部分
四、安装Nginx
1. 安装Nginx
首先更新已有软件
sudo dnf -y update
如果您想安装较为稳定的Nginx,可以直接使用下面的命令:
sudo dnf -y install nginx
如果您想安装较新的版本,可以使用下面的命令6:
首先创建一个源
sudo vim /etc/yum.repos.d/nginx.repo
将下列内容加入刚才创建的文件中:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
使用命令安装:
sudo dnf clean all
sudo dnf makecache
sudo dnf -y install nginx
2. 防火墙开放端口
使用以下命令:
# http协议需要开启80端口,https协议需要开启443端口。本文只使用http协议
sudo firewall-cmd --zone=public --add-port=[端口号]/tcp --permanent
# 例如:sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
sudo firewall-cmd --reload # 刷新防火墙
如果使用云服务器,须在其管理界面上放行对应端口。以阿里云为例,需要在云服务器的控制台的网络与安全组配置中添加安全组的入方向的安全组配置,放行对应端口:
3. 配置Nginx
使用vim打开主配置文件:
sudo vim /etc/nginx/nginx.conf
如果你想对其进行详细修改,请参阅主配置文件的介绍。将“include /etc/nginx/conf.d/*.conf;”注释掉,换成我自己的配置文件,使其不适用默认的配置文件,即:
#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/conf.d/myNginx.conf;
接着输入下面的命令,打开配置文件:
sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/myNginx.conf
sudo vim /etc/nginx/conf.d/myNginx.conf
您会看到:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
配置文件的含义,可以参考default.conf配置文件含义。第2行是网页端口(默认80),第9、19行后面是网页文件的位置,你可以修改为自己想放的位置。在这里,我使用默认的位置,不作修改。以下的修改很重要:
- 将第30-36行(含36行)前面表示注释的“#”号去掉(除非您不想使用PHP)。
- 将第34行的
/scripts
改为$document_root
。 - 将31行的root路径改成与第9行的root路径一致。
- 在第10行前加入index.php(除非您不想使用PHP)
- 在第10行下插入
try_files $uri $uri/ /index.php?$args;
,以支持伪静态 - (可选)第二行可以控制Nginx服务使用的端口。
- (可选)将第9行修改为网页文件的位置(否则使用默认/usr/share/nginx/html)。注意,第31行也应随之更改。
- 如果您有域名,将第3行改为网站的域名。
- 将32行的“fastcgi_pass”后改为“unix:/run/php-fpm/www.sock;”
修改后的文件如下所示:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args; # 伪静态
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
紧接着,启动Nginx:
sudo systemctl enable nginx # 将Nginx设置为开机自启
sudo systemctl start nginx # 启动Nginx
systemctl status nginx # 查看Nginx状态
如果出现绿色的active(running),就说明Nginx安装完成,并成功运行。
如果出现红色的failed,可能是配置文件出了问题,需要仔细检查。
5. 测试安装情况
在浏览器中,输入服务器的IP地址(如果非默认端口,需要在ip后面加上冒号再加端口号。例如你设置了1234端口,IP是123.123.123.123,你可以这么访问:123.123.123.123:1234),观察网页。如果看到了以下图片,说明Nginx配置完全成功。
如果无法正常访问,但Nginx处于正常运行的状态,则有可能是由于Selinux中没有赋予Nginx相关的权限。可以使用sestatus
这个命令检查Selinux的状态。如果它处于enforcing状态,则说明它正常开启;否则,则可能是其它的原因。另外,为了服务器的安全,在公网的服务器中,建议将Selinux开启并处于enforcing状态。注意,Selinux在阿里云提供的CentOS 8系统中是默认关闭的,需要编辑/etc/selinux/config文件,将“SELINUX=”设置为“enforcing”,然后重启服务器。
对于处于Enforcing状态的Selinux,我们可以采用使用如下的命令,根据Selinux对Nginx的拒绝日志,设置Nginx在Selinux中的权限:
dnf install policycoreutils
cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
semodule -i mynginx.pp
rm mynginx.pp
五、安装PHP
1. 安装
如果您只是需要安装Wordpress7.2,那么直接使用CentOS8默认的PHP安装即可。
如果您需要安装Wordpress,官方文档7,推荐安装PHP7.3及以上版本的PHP并安装一些依赖的模块:
sudo dnf install epel-release
sudo dnf update epel-release
#sudo rpm -Uvh https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-8.rpm # 安装remi源
sudo dnf clean all
sudo dnf makecache
sudo dnf module enable php:7.3 # 使用PHP7.3版本,如使用更高版本,需安装remi源
# sudo dnf module enable php:remi-7.4 # 使用remi安装7.4版本
sudo dnf install php php-curl php-dom php-exif php-fileinfo php-fpm php-gd php-hash php-json php-mbstring php-mysqli php-openssl php-pcre php-xml php-zip libsodium # 必须安装组件
# php-pecl-imagick的安装需要remi源
sudo dnf install php-filter php-iconv php-simplexml php-xmlreader php-zlib php-pecl-imagick # 推荐安装的组件
然后配置配置PHP:
sudo systemctl enable php-fpm # 设为开机启动
sudo systemctl start php-fpm # 启动
systemctl status php-fpm # 查看服务状态
2. 测试安装
至此,配置完成接下来进行测试:
cd [网页路径] # 之前在myNginx.conf设置路径。若没修改该,则使用默认路径/usr/share/nginx/html。
vim test.php # 使用vim创建test.php文件
输入:
<?php
phpinfo();
?>
然后在浏览器中输入:[主机ip][:端口]/test.php(使用默认端口,可省略“:端口”的内容。若出现类似下图的界面,说明安装成功。
至此,LNMP环境安装完毕,别忘了将test.php删掉哦。
参考文献
[6] Nginx: Linux packages http://nginx.org/en/linux_packages.html#RHEL-CentOS