CentOS 8搭建LNMP + WordPress(二)


CentOS 8近日推出了,其LNMP環境的搭建也與CentOS7有所不同。基於CentOS 8,我重寫了前一篇文章《CentOS7搭建LNMP+WordPress一篇搞定》,得到了這一本文。

為了更好地閱讀體驗,我將本文分成了三個部分:

  1. 名詞解釋與CentOS 8操作系統安裝
  2. 網頁服務器的安裝與配置(Nginx + PHP)
  3. 數據庫(MariaDB)與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行后面是網頁文件的位置,你可以修改為自己想放的位置。在這里,我使用默認的位置,不作修改。以下的修改很重要:

  1. 將第30-36行(含36行)前面表示注釋的“#”號去掉(除非您不想使用PHP)。
  2. 將第34行的/scripts改為$document_root
  3. 將31行的root路徑改成與第9行的root路徑一致。
  4. 在第10行前加入index.php(除非您不想使用PHP)
  5. 在第10行下插入try_files $uri $uri/ /index.php?$args;,以支持偽靜態
  6. (可選)第二行可以控制Nginx服務使用的端口。
  7. (可選)將第9行修改為網頁文件的位置(否則使用默認/usr/share/nginx/html)。注意,第31行也應隨之更改。
  8. 如果您有域名,將第3行改為網站的域名。
  9. 將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(使用默認端口,可省略“:端口”的內容。若出現類似下圖的界面,說明安裝成功。

phpinfo

至此,LNMP環境安裝完畢,別忘了將test.php刪掉哦。

參考文獻

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM