YUM方式安裝軟件的優點就是簡單、方便、快捷,本文介紹在Linux上如何使用YUM方式快速安裝LNMP並部署WordPress。使用Linux CentOS 7.9 + Nginx 1.18 + MySQL 8.0.23 + PHP 7.4.19來搭建WordPress的運行環境。
本文的操作在華為雲ECS、阿里雲ECS以及VMware WorkStation虛擬化軟件上驗證過,成功部署WordPress 5.7。
LNMP是什么?
LNMP是指一組來運行動態網站或者服務器的開源軟件名稱的首字母縮寫。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。LNMP典型的代表就是:Linux + Nginx + MySQL + PHP這種網站服務器架構,本文中的LNMP就是指這種典型的網站服務器架構。
為什么選擇LNMP部署WordPress?
LNMP架構主要有以下幾方面的優勢:
- Linux、Nginx、MySQL都是開源軟件,可以免費使用,降低部署網站的成本。
- Linux Server比Windows Server占用更少的系統資源,穩定性更好,安全性相對更高一些。
- Nginx相比Apache占用更少的系統資源,支持高並發,支持四到七層的負載均衡,在構建大型的站點時,Nginx的優勢明顯。
- WordPress是使用PHP語言開發的,必須要在支持PHP的環境中才能運行。
環境信息
系統、軟件名稱 | 版本 | 官網下載地址 |
---|---|---|
Linux | CentOS 7.9 | CentOS 7.9 |
Nginx | Nginx 1.18.0 | Nginx 1.18.0 |
MySQL | MySQL 8.0.23 | MySQL 8.0.23 |
PHP | PHP 7.4.19 | PHP 7.4.19 |
WordPress | WordPress 5.7 | 中文版 英文版 |
操作步驟
在Linux上使用YUM方式快速安裝LNMP並部署WordPress的安裝流程如下所示:
①安裝與配置MySQL → ②安裝與配置NGINX → ③安裝與配置PHP → ④安裝WordPress前的准備 → ⑤安裝WordPress
步驟一:安裝與配置MySQL
1、卸載MySQL和MariaDB
使用SecureCRT、Xshell等工具通過SSH方式遠程連接到CentOS服務器,如果您的系統中已經安裝了MySQL或者MariaDB,請卸載MySQL和MariaDB數據庫后再安裝MySQL8.0,避免導致安裝MySQL8.0不成功。命令如下:
rpm -qa | grep mysql* # 查詢是否安裝了mysql
rpm -e --nodeps mysql* # 卸載mysql
rpm -qa | grep mariadb* # 查詢是否安裝了MariaDB
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 # 卸載MariaDB
2、添加MySQL的Yum軟件倉庫
yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
3、安裝MySQL
安裝MySQL-community-server-8.0.23需要下載的軟件包一共573M左右,安裝完成后輸出信息如下:
yum -y install mysql-community-server-8.0.23-1.el7
4、啟動MySQL服務
啟動MySQL服務
systemctl start mysqld
檢查MySQL服務的狀態,當狀態為active (running) 時說明mysql服務正常啟動了。
[root@Linux ~]# systemctl status
mysqldmysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-05-17 22:26:33 EDT; 16s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 17957 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 18031 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─18031 /usr/sbin/mysqld
May 17 22:26:28 Linux systemd[1]: Starting MySQL Server...
May 17 22:26:33 Linux systemd[1]: Started MySQL Server.
設置mysql服務開機時自動啟動
systemctl enable mysqld
5、配置數據庫
5.1 獲取MySQL數據庫root用戶的初始密碼
root用戶的初始密碼保存在/var/log/mysqld.log日志文件中(請保存好此密碼,登錄MySQL時需要使用),root用戶的初始密碼為:xs7,s>:UsfCH
[root@Linux ~]# grep 'temporary password' /var/log/mysqld.log2021-05-18T02:26:30.173361Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: xs7,s>:UsfCH
5.2更改root密碼
使用mysql -uroot -p命令連接數據庫
[root@Linux ~]# mysql -uroot -pEnter password: #輸入初始密碼
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
更改root密碼,建議密碼由大小寫字母、數字和特殊符號組成16位或以上,將Your@pass2021替換為您的數據庫密碼,命令如下:
mysql> ALTER USER root@'localhost' IDENTIFIED BY ' Your@pass2021';
Query OK, 0 rows affected (0.01 sec)
5.3 創建名稱為“wordpress”的數據庫,供WordPress程序連接使用
mysql> CREATE DATABASE wordpress;Query OK, 1 row affected (0.01 sec)
查看wordpress數據庫是否創建成功
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
5.4 創建數據庫用戶
創建名稱為wordpress(用戶名稱可自定義)的數據庫用戶,wordpress程序連接Mysql數據庫時使用wordpress這個用戶。
mysql> CREATE USER wordpress@'localhost' IDENTIFIED BY 'Your@pass2021';
Query OK, 0 rows affected (0.01 sec)
授權wordpress用戶對wordpress數據庫具有所有的操作權限
mysql> GRANT ALL privileges ON wordpress.* TO wordpress@'localhost';
Query OK, 0 rows affected (0.01 sec)
設置wordpress用戶的密碼永不過期
mysql> ALTER USER 'wordpress'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec)
刷新權限
mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)
退出MySQL
mysql> exit
Bye
[root@Linux ~]#
步驟二:安裝與配置Nginx
CentOS默認沒有安裝Nginx的軟件倉庫,需要安裝Nginx軟件倉庫才能使用Yum安裝Nginx。Nginx默認的安裝路徑為:/usr/share/nginx/html,配置文件的默認路徑為:/etc/nginx
1、安裝Nginx軟件倉庫
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2、安裝Nginx1.18
yum -y install nginx-1.18.0-2.el7.ngx
3、啟動Nginx
systemctl start nginx
查看Nginx運行狀態
[root@Linux ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2021-05-17 23:00:43 EDT; 12s ago
Docs: http://nginx.org/en/docs/
Process: 18176 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 18177 (nginx)
CGroup: /system.slice/nginx.service
├─18177 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf └─18178 nginx: worker process
May 17 23:00:43 Linux systemd[1]: Starting nginx - high performance web server...
May 17 23:00:43 Linux systemd[1]: Started nginx - high performance web server.
設置Nginx開機自動啟動
[root@Linux ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
4、驗證Nginx是否安裝成功
驗證前需要Linux防火牆放行80端口,如果服務器是華為雲ECS、阿里雲ECS,在安全組內添加規則放行80端口或者http協議即可。
Linux防火牆放行80端口
firewall-cmd --add-port=80/tcp --zone=public --permanent
重新加載防火牆規則
firewall-cmd --reload
在瀏覽器地址欄輸入Linux服務器的IP地址,看到 Welcome to ningx! 說明Nginx安裝成功。
步驟三:安裝與配置PHP
CentOS默認的軟件倉庫中沒有PHP安裝包,需要添加EPEL軟件倉庫和remi軟件倉庫后才能安裝PHP及PHP的相關擴展程序。
1、安裝軟件倉庫
安裝EPEL軟件倉庫
yum -y install epel-release
安裝REMI軟件倉庫
yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
2、安裝PHP及相關擴展模塊
yum -y install php74-php php74-php-common php74-php-fpm php74-php-mysqlnd php74-php-pdo php74-php-cli php74-php-json php74-php-mbstring php74-php-sodium php74-php-pecl-imagick php74-php-xml php74-php-gd php74-php-pecl-mcrypt php74-php-pecl-zip
3、驗證是否成功安裝
查看PHP版本信息
[root@Linux ~]# php74 -v
PHP 7.4.19 (cli) (built: May 4 2021 11:06:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
啟動PHP
systemctl start php74-php-fpm
查看PHP的運行狀態,ACTIVE狀態為 active(running)說明PHP啟動成功
[root@Linux ~]# systemctl status php74-php-fpm
● php74-php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php74-php-fpm.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2021-05-18 00:00:31 EDT; 10s ago
Main PID: 18587 (php-fpm)
Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
CGroup: /system.slice/php74-php-fpm.service
├─18587 php-fpm: master process (/etc/opt/remi/php74/php-fpm.conf)
├─18588 php-fpm: pool www
├─18589 php-fpm: pool www
├─18590 php-fpm: pool www
├─18591 php-fpm: pool www
└─18592 php-fpm: pool www May 18 00:00:31
Linux systemd[1]: Starting The PHP FastCGI Process Manager...May 18 00:00:31
Linux systemd[1]: Started The PHP FastCGI Process Manager.
設置PHP開機時自動啟動
systemctl enable php74-php-fpm
4、配置PHP
4.1 修改PHP的配置文件“www.conf”
cd /etc/opt/remi/php74/php-fpm.d/ #進入PHP配置目錄
cp -p www.conf www.conf.bak #備份配置文件
vim /etc/opt/remi/php74/php-fpm.d/www.conf #編輯配置文件www.conf
開啟vim顯示行號的功能,在非編輯狀態輸入 :set number 回車,輸入:24 回車
第24行user = apache 修改為user = nginx,26行的group = apache修改為group = nginx ,修改后保存退出。
注意:如果www.conf配置文件的user與group參數不修改為nginx的話,在WordPress發布文章的時候因為權限問題可能導致無法上傳圖片,出現“無法將上傳的文件移動至wp-content/uploads”的錯誤提示。
4.2 重啟PHP
systemctl restart php74-php-fpm
查看PHP的運行狀態,確認運行正常
systemctl status php74-php-fpm
步驟四:安裝WordPress前的准備
1、下載wordpress
新建software與wordpress目錄
mkdir /opt/softwaremkdir -p /usr/data/wordpresscd /opt/software #進入software目錄
下載wordpress5.7到 /opt/software目錄
wget -O wordpress5.7.tar.gz https://cn.wordpress.org/wordpress-5.7-zh_CN.tar.gz
解壓縮wordpress安裝包
tar -zxvf wordpress5.7.tar.gz
將解壓后的wordpress目錄下的所有子目錄和文件都復制到 /usr/data/ wordpress/目錄下
cp -R /opt/software/wordpress/* /usr/data/wordpress/
將/usr/data/wordpress目錄及子目錄下的所有文件的“擁有者”和“擁有者組”修改為nginx,命令如下:
chown -R nginx:nginx /usr/data/wordpress
將/usr/data/wordpress目錄及所有子目錄的權限設置為755, wordpress目錄下的所有文件的權限統一設置為644,命令如下:
find /usr/data/wordpress -type d -exec chmod 755 {} \;
find /usr/data/wordpress -type f -exec chmod 644 {} \;
2、配置Nginx
編輯Nginx的配置文件
vim /etc/nginx/conf.d/default.conf
default.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;
#}
}
開啟vim顯示行號的功能,在非編輯狀態輸入 :set number 回車(set前面有一個冒號,不要漏掉)。
第9行Nginx的root目錄修改為 /usr/data/wordpress 。此目錄與wordpress程序所在的目錄必須一致,否則無法成功配置wordpress,修改為你自己的wordpress目錄。
第10行 添加index.php
刪除第30至36行前面的#號注釋符
第31行的root目錄修改為 /usr/data/wordpress
第34行的/scripts$fastcgi_script_name; 改為 $document_root$fastcgi_script_name;
default.conf修改后的內容如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/data/wordpress;
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 /usr/data/wordpres;
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;
#}
}
檢查配置文件的配置是否正確,看到如下輸出,代表配置沒問題。
[root@Linux conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新加載Nginx配置文件
systemctl reload nginx
查看Nginx的運行狀態
systemctl status nginx
步驟五:安裝WordPress
現在開始安裝WordPress,在物理機的瀏覽器地址欄輸入Linux Server的IP地址192.168.1.100 回車,點【現在就開始!】按鈕開始安裝WordPress。
如下圖,填寫前面已經創建好的數據庫名,用戶名以及密碼,數據庫主機使用默認的localhost,表前綴使用默認wp_ ,點擊【提交】。
接下來點擊【現在安裝】按鈕繼續安裝
根據下圖填寫網站的基本信息,填好后點擊【安裝WordPress】進入下一步。
站點標題:站點的名稱,給你站點起一個響亮或者令人印象深刻的名字
用戶名:這個用戶是WordPress的后台管理員賬戶,具有后台最高管理權限,用戶名確認后不可更改。建議用戶名不要使用admin、administrator、root等常用的管理員賬號名稱,因為黑K很喜歡破解這類用戶的密碼。
密碼:默認會生成一個復雜的隨機密碼,請保管好,初始密碼在后台可以更改。建議在這里輸入你自己准備好的復雜密碼(設置密碼16位或以上,由大小寫字母,數字與特殊符號組成),密碼如果太簡單很容易被破解。
電子郵箱:輸入電子郵箱地址
對搜索引擎的可見性:建議不要勾選,如果你的網站不想被搜索引擎收錄的話可以勾選,就算勾選了也不能保證搜索引擎就一定不收錄。
提示WordPress安裝成功,點擊【登錄】 按鈕准備登錄WordPress。
輸入用戶名和密碼,點擊【登錄】按鈕進入WordPress后台管理系統。
已成功登錄WordPress后台管理系統,如下圖:
WordPress已經完成初始化安裝,目前使用的是默認主題而且網站只有一篇“世界,您好!”的文章,所以看起來很簡單,安裝一個精美的主題並完善網站的內容后,你期待的網站才會出現。
安裝WordPress的更多方法如下:
WordPress安裝篇(1):使用PHPStudy安裝WordPress
WordPress安裝篇(2):用寶塔面板在Windows上安裝WordPress
WordPress安裝篇(3):用寶塔面板在Linux上安裝WordPress
WordPress安裝篇(4):YUM方式安裝LNMP並部署WordPress
WordPress安裝篇(5):源碼編譯安裝LNMP並部署WordPress
本文轉自 閃電雲 sdyol.com