目录
CentOS 8近日推出了,其LNMP环境的搭建也与CentOS7有所不同。基于CentOS 8,我重写了前一篇文章《CentOS7搭建LNMP+WordPress一篇搞定》,得到了这一本文。
为了更好地阅读体验,我将本文分成了三个部分:
以下是本文的第二个部分
首先更新已有软件
sudo dnf -y update
如果您想安装较为稳定的Nginx,可以直接使用下面的命令:
sudo duf -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
使用以下命令:
# 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 # 刷新防火墙
如果使用云服务器,须在其管理界面上放行对应端口。以阿里云为例,需要在云服务器的控制台的网络与安全组配置中添加安全组的入方向的安全组配置,放行对应端口:
使用vim打开主配置文件:
sudo vim /etc/nginx/nginx.conf
如果你想对其进行详细修改,请参阅主配置文件的介绍。将倒数第二行的include注释掉,换成我自己的配置文件,使其不适用默认的配置文件,即:
#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行后面是网页 配置文件的含义,可以参考default.conf配置文件含义。第2行是网页端口(默认80),第9、19行后面是网页文件的位置,你可以修改为自己想放的位置。在这里,我使用默认的位置,不作修改。以下的修改很重要:
/scripts
改为$document_root
。修改后的文件如下所示:
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;
}
#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 $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在Selinux中的权限:
dnf install policycoreutils-python
cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
semodule -i mynginx.pp
rm mynginx.pp
紧接着,启动Nginx:
sudo systemctl enable nginx # 将Nginx设置为开机自启
sudo systemctl start nginx # 启动Nginx
systemctl status nginx # 查看Nginx状态
如果出现绿色的active(running),就说明Nginx安装完成,并成功运行。
如果出现红色的failed,可能是配置文件出了问题,需要仔细检查。
在浏览器中,输入服务器的IP地址(如果非默认端口,需要在ip后面加上冒号再加端口号。例如你设置了1234端口,IP是123.123.123.123,你可以这么访问:123.123.123.123:1234),观察网页。如果看到了以下图片,说明Nginx配置完全成功。
如果您只是需要安装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 # 推荐安装的组件
然后配置配置文件:
vim /etc/php-fpm.d/www.conf
修改配置文件,将第24行的“user = apache”和第26行的“group = apache”分别修改为“user = nginx”和“group = nginx”,38行的“listen = /run/php-fpm/www.sock”“listen = 9000”。
保存退出后便可以启动网站了。
sudo systemctl enable php-fpm # 设为开机启动
sudo systemctl start php-fpm # 启动
systemctl status php-fpm # 查看服务状态
至此,配置完成接下来进行测试:
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
原文:https://www.cnblogs.com/mrfangd/p/LNMP-CentOS8-2.html