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密碼。
不允許遠程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


免責聲明!

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



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