鏡像下載、功能變數名稱解析、時間同步請點擊 阿裡雲開源鏡像站 LNMP是Linux + Nginx + MySQL + PHP 四個系統的首字母縮寫,相對於 LAMP(Linux + Apache + MySQL + PHP )來說的。曾經在虛擬主機建站界風靡一時,隨著新的編程語言和容器技術、微服務等發展 ...
鏡像下載、功能變數名稱解析、時間同步請點擊 阿裡雲開源鏡像站
LNMP是Linux + Nginx + MySQL + PHP 四個系統的首字母縮寫,相對於 LAMP(Linux + Apache + MySQL + PHP )來說的。曾經在虛擬主機建站界風靡一時,隨著新的編程語言和容器技術、微服務等發展,慢慢沒落了,尤其是PHP編程語言的使用量急劇下降了。
WordPress是一款能讓您建立出色網站、博客或應用程式的開源軟體。它具有美觀的設計,強大的功能,可以助您自由發揮心中所想。WordPress既是免費的,也是無價的。
# 本實踐過程中的系統及環境描述
L:Linux https://mirrors.aliyun.com/centos/
N:Nginx https://nginx.org/en/download.html
M:MySQL https://dev.mysql.com/downloads/mysql/
P:PHP http://php.net/downloads.php
Wordpress https://cn.wordpress.org/latest-zh_CN.tar.gz
#部署規劃:
192.168.250.47:Nginx php-fpm 運行web服務
192.168.250.48:運行MySQL資料庫,Redis服務
1. 架構拓撲及主機說明
# 三台主機
1 1台 Linux+Nginx+PHP+WordPress (簡稱 LNP) 伺服器 :
主機名:LNP-Server-IP47
CentOS 7.9
IP:192.168.250.47
2 1台 MySQL+Redis 伺服器 :
主機名: MySQL-Redis-IP48
CentOS 8.4
IP:192.168.250.48/24
3 1台 client主機 :
WIN10-PC機
2. 準備 MySQL 資料庫
# CentOS系統的優化,可以查以前的文章;按照架構圖修改好主機名
[root@CentOS84-IP48 ]#hostnamectl set-hostname MySQL-Redis-IP48
[root@CentOS84-IP48 ]#exit
# yum 安裝 mysql-server 資料庫
[root@MySQL-Redis-IP48 ]#yum info mysql-server
Last metadata expiration check: 19:31:21 ago on Mon 28 Mar 2022 02:34:38 AM CST.
Available Packages
Name : mysql-server
Version : 8.0.26
[root@MySQL-Redis-IP48 ]#yum -y install mysql-server
# 啟動服務並開啟自啟
[root@MySQL-Redis-IP48 ]#systemctl enable --now mysqld
# 進入資料庫
[root@MySQL-Redis-IP48 ]#mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 Source distribution
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.
# 創建 wordpress 庫
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
# 創建wordpress的資料庫賬戶名和密碼
mysql> create user wordpress@'192.168.250.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
# 資料庫授權
mysql> grant all on wordpress.* to wordpress@'192.168.250.%';
Query OK, 0 rows affected (0.01 sec)
# 本機登錄並驗證資料庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.01 sec)
mysql> use wordpress
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> quit
Bye
[root@MySQL-Redis-IP48 ]#
3. 網路驗證MySQL服務
# 通過網路在另外一臺機器上登錄上面建好的資料庫伺服器
# 安裝資料庫客戶端 mysql 包
[root@CentOS84-IP172-48 ]#yum -y install mysql
# 網路方式登錄遠程資料庫
[root@CentOS84-IP172-48 ]#mysql -uwordpress -p123456 -h192.168.250.48
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.26 Source distribution
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| wordpress |
+--------------------+
2 rows in set (0.00 sec)
mysql>
4. 配置 LNP 伺服器
基本任務: 編譯安裝和部署 php 支持 redis,並準備配置和啟動服務文件,啟動 php-fpm; 編譯安裝Nginx ,並準備配置和啟動服務文件,啟動Nginx
4.1 部署php-fpm服務
# 按照架構圖修改好主機名
[root@centos79 <sub>]# hostnamectl set-hostname LNP-Server-IP47
[root@centos79 </sub>]# exit
# 安裝編譯PHP需要的依賴包
[root@lnp-server-ip47 ]# yum -y install gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel
# 下載 php-7.4.28.tar.xz 源碼包
[root@lnp-server-ip47 src]# wget https://www.php.net/distributions/php-7.4.28.tar.xz
[root@lnp-server-ip47 src]# ll -h php-7.4.28.tar.xz
-rw-r--r-- 1 root root 10M Feb 15 21:40 php-7.4.28.tar.xz
# 解壓源碼包,進入源碼包所在目錄
[root@lnp-server-ip47 src]# tar xf php-7.4.28.tar.xz
[root@lnp-server-ip47 src]# ll
total 11220
drwxr-xr-x 9 1001 1001 186 Mar 28 17:06 nginx-1.20.2
-rw-r--r-- 1 root root 1062124 Nov 16 22:51 nginx-1.20.2.tar.gz
drwxrwxr-x 16 root root 4096 Feb 15 21:23 php-7.4.28
-rw-r--r-- 1 root root 10418352 Feb 15 21:40 php-7.4.28.tar.xz
# 準備編譯參數
[root@lnp-server-ip47 src]#cd php-7.4.28/
[root@lnp-server-ip47 php-7.4.28]# ./configure --prefix=/apps/php74 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm -enable-maintainer-zts --disable-fileinfo
....................................
Thank you for using PHP. # 需要看到這個信息才算成功了
# 查看cpu個數,作為編譯參數CPU選項輸入
[root@lnp-server-ip47 nginx-1.20.2]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 8
On-line CPU(s) list: 0-7
[root@lnp-server-ip47 php-7.4.28]#
# 編譯安裝
[root@lnp-server-ip47 php-7.4.28]# make -j 8 && make install
................... #此處刪除很多屏顯內容,需要看到下麵成功信息再進入下一步
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /apps/php74/lib/php/extensions/no-debug-zts-20190902/
Installing PHP CLI binary: /apps/php74/bin/
Installing PHP CLI man page: /apps/php74/php/man/man1/
Installing PHP FPM binary: /apps/php74/sbin/
Installing PHP FPM defconfig: /apps/php74/etc/
Installing PHP FPM man page: /apps/php74/php/man/man8/
Installing PHP FPM status page: /apps/php74/php/php/fpm/
Installing phpdbg binary: /apps/php74/bin/
Installing phpdbg man page: /apps/php74/php/man/man1/
Installing PHP CGI binary: /apps/php74/bin/
Installing PHP CGI man page: /apps/php74/php/man/man1/
Installing build environment: /apps/php74/lib/php/build/
Installing header files: /apps/php74/include/php/
Installing helper programs: /apps/php74/bin/
program: phpize
program: php-config
Installing man pages: /apps/php74/php/man/man1/
page: phpize.1
page: php-config.1
/usr/local/src/php-7.4.28/build/shtool install -c ext/phar/phar.phar /apps/php74/bin/phar.phar
ln -s -f phar.phar /apps/php74/bin/phar
Installing PDO headers: /apps/php74/include/php/ext/pdo/
##############################################################################
## 準備 php 配置文件
# 從配置文件模板複製,併進行修改
[root@lnp-server-ip47 php-7.4.28]# cp /usr/local/src/php-7.4.28/php.ini-production /etc/php.ini
# 進入當時編譯參數內定義的目錄 /apps/php74/ 從模板複製創建 php-fpm.conf
[root@lnp-server-ip47 php-7.4.28]# cd /apps/php74/etc
[root@lnp-server-ip47 etc]# cp php-fpm.conf.default php-fpm.conf
# 進入子配置文件目錄,從模板 文件創建 www.conf
[root@lnp-server-ip47 etc]# cd php-fpm.d/
[root@lnp-server-ip47 php-fpm.d]# cp www.conf.default www.conf
[root@lnp-server-ip47 php-fpm.d]#
# 按照本實踐的思路修改 www.conf
[root@lnp-server-ip47 php-fpm.d]# vim www.conf
;user = nobody
user = www
;group = nobody
group = www
;pm.status_path = /status
pm.status_path = /status
;ping.path = /ping
ping.path = /ping
;access.log = log/$pool.access.log
access.log = log/$pool.access.log
;slowlog = log/$pool.log.slow
slowlog = log/$pool.log.slow
# 修改後的 www.conf 文件去除 ; 註釋行的所有文件內容 供比對
[root@lnp-server-ip47 php-fpm.d]# grep '^[^;]' www.conf
[www]
user = www
group = www
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.status_path = /status
ping.path = /ping
access.log = log/$pool.access.log
slowlog = log/$pool.log.slow
[root@lnp-server-ip47 php-fpm.d]#
# 創建 www 用戶
[root@lnp-server-ip47 php-fpm.d]# useradd -r -s /sbin/nologin www
# 創建訪問日誌文件路徑
[root@lnp-server-ip47 php-fpm.d]# mkdir /apps/php74/log
[root@lnp-server-ip47 php-fpm.d]#
##############################################################################
## 啟動並驗證 php-fpm 服務
# 檢查配置文件語法等
[root@lnp-server-ip47 php-fpm.d]# /apps/php74/sbin/php-fpm -t
[28-Mar-2022 18:05:51] NOTICE: configuration file /apps/php74/etc/php-fpm.conf test is successful
# 準備啟動服務文件
[root@lnp-server-ip47 php-fpm.d]# cp /usr/local/src/php-7.4.28/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
# 啟動並開機自啟動 php-fpm
[root@lnp-server-ip47 php-fpm.d]# systemctl daemon-reload
[root@lnp-server-ip47 php-fpm.d]# systemctl enable --now php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
# 驗證監聽埠
[root@lnp-server-ip47 php-fpm.d]# ss -ltn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 511 127.0.0.1:9000 *:*
# 查看並跟蹤 進程信息
[root@lnp-server-ip47 php-fpm.d]# pstree -p |grep php
|-php-fpm(20700)-+-php-fpm(20701)
| `-php-fpm(20702)
[root@lnp-server-ip47 php-fpm.d]# ps -ef |grep php
root 20700 1 0 18:06 ? 00:00:00 php-fpm: master process (/apps/php74/etc/php-fpm.conf)
www 20701 20700 0 18:06 ? 00:00:00 php-fpm: pool www
www 20702 20700 0 18:06 ? 00:00:00 php-fpm: pool www
root 20707 5036 0 18:07 pts/0 00:00:00 grep --color=auto php
[root@lnp-server-ip47 php-fpm.d]#
4.2 部署 Nginx 服務
4.2.1 編譯安裝 nginx
#### 編譯安裝 nginx
# 準備Nginx編譯安裝的依賴包
[root@lnp-server-ip47 <sub>]# yum -y install gcc pcre-devel openssl-devel zlib-devel
# 下載 nginx 1.20.2 源碼包 一般/usr/local/src/ 作為源碼文件存放目錄
[root@lnp-server-ip47 </sub>]# cd /usr/local/src/
[root@lnp-server-ip47 src]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
# 解壓源碼包
[root@lnp-server-ip47 src]# tar xf nginx-1.20.2.tar.gz
[root@lnp-server-ip47 src]# ll
total 1040
drwxr-xr-x 8 1001 1001 158 Nov 16 22:44 nginx-1.20.2
-rw-r--r-- 1 root root 1062124 Nov 16 22:51 nginx-1.20.2.tar.gz
[root@lnp-server-ip47 src]#
# 進入nginx-1.20.2 目錄,準備編譯參數
[root@lnp-server-ip47 src]# cd nginx-1.20.2
[root@lnp-server-ip47 nginx-1.20.2]# ./configure --prefix=/apps/nginx \
> --user=www \
> --group=www \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module
[root@lnp-server-ip47 nginx-1.20.2]# make -j 8 && make install
##############################################################################
# 準備服務文件並啟動 nginx
[root@lnp-server-ip47 nginx-1.20.2]# vim /usr/lib/systemd/system/nginx.service
[root@lnp-server-ip47 nginx-1.20.2]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
[root@lnp-server-ip47 nginx-1.20.2]#
# 創建目錄
[root@lnp-server-ip47 nginx-1.20.2]# mkdir /apps/nginx/run/
# 修改配置文件
[root@lnp-server-ip47 nginx-1.20.2]# vim /apps/nginx/conf/nginx.conf
# 僅修改下麵這行的內容
pid /apps/nginx/run/nginx.pid;
# 啟動並開機自啟服務
[root@lnp-server-ip47 wordpress]# systemctl daemon-reload
[root@lnp-server-ip47 wordpress]# systemctl enable --now nginx
[root@lnp-server-ip47 wordpress]# ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 511 127.0.0.1:9000 *:*
LISTEN 0 511 *:80 *:*
[root@lnp-server-ip47 wordpress]#
4.2.2 配置 Nginx 支持 fastcgi
##############################################################################
#### 配置 Nginx 支持 fastcgi
[root@lnp-server-ip47 nginx-1.20.2]# vim /apps/nginx/conf/nginx.conf
# 僅僅修改下麵這些內容,其他都市預設值
worker_processes auto;
pid /apps/nginx/run/nginx.pid;
server {
listen 80;
server_name blog.shone.cn;
location / {
root /data/nginx/wordpress;
index index.php index.html index.htm;
}
location <sub> \.php$ {
root /data/nginx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location </sub> ^/(ping|pm_status)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}
# 通過命令篩選出有效的配置行
[root@lnp-server-ip47 nginx-1.20.2]# grep -Ev '#|^$' /apps/nginx/conf/nginx.conf
worker_processes auto;
pid /apps/nginx/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name blog.shone.cn; #指定功能變數名稱
location / {
root /data/nginx/wordpress; #指定數據目錄
index index.php index.html index.htm; # 指定預設主頁文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location <sub> \.php$ { #實現php-fpm
root /data/nginx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location </sub> ^/(ping|pm_status)$ { #PHP檢測狀態頁
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}
}
}
[root@lnp-server-ip47 nginx-1.20.2]#
# 重新啟動 nginx 讓新配置文件生效
[root@lnp-server-ip47 php-fpm.d]# systemctl reload nginx
[root@lnp-server-ip47 php-fpm.d]# ss -ltn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 511 127.0.0.1:9000 *:*
LISTEN 0 511 *:80 *:*
[root@lnp-server-ip47 php-fpm.d]#
4.2.3 測試PHP工作是否正常
準備測試頁面
[root@lnp-server-ip47 php-fpm.d]# mkdir -p /data/nginx/wordpress
[root@lnp-server-ip47 php-fpm.d]# vim /data/nginx/wordpress/phpinfo.php
[root@lnp-server-ip47 php-fpm.d]# cat /data/nginx/wordpress/phpinfo.php
<?php
phpinfo();
?>
測試PHP的ping
查看狀態頁
5. 部署 WordPress
5.1 準備 WordPress 文件
# 下載源文件,並複製到前面定義的網頁目錄下,並修改權屬
[root@lnp-server-ip47 <sub>]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
[root@lnp-server-ip47 </sub>]# ll
total 19012
-rw-r--r-- 1 root root 19462197 Mar 19 00:00 latest-zh_CN.tar.gz
[root@lnp-server-ip47 <sub>]# tar xf latest-zh_CN.tar.gz
[root@lnp-server-ip47 </sub>]# ll
total 19016
-rw-r--r-- 1 root root 19462197 Mar 19 00:00 latest-zh_CN.tar.gz
drwxr-xr-x 5 1006 1006 4096 Mar 19 00:00 wordpress
[root@lnp-server-ip47 <sub>]# cp -r wordpress/* /data/nginx/wordpress
[root@lnp-server-ip47 </sub>]# chown -R www.www /data/nginx/wordpress/
[root@lnp-server-ip47 ~]#
5.2 初始化 WordPress
# 修改WIN10的本地hosts文件,路徑為 C:\Windows\System32\drivers\etc\hosts 在最後添加一行
192.168.250.47 blog.shone.cn
在瀏覽器內輸入 blog.shone.cn 出現可道雲的初始化嚮導,按照嚮導完成初始化
在瀏覽器內輸入 http://blog.shone.cn
6. 優化 WordPress
6.1 允許上傳大文件
#註意:預設只支持1M以下文件上傳,要利用php程式上傳大文件,需要修改下麵的配置,最大上傳由下列項值的最小值決定,直接上傳大於1M文件,會出現下麵413錯誤
[root@lnp-server-ip47 wordpress]# vim /apps/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
client_max_body_size 100m; #nginx上傳文件大小修改成100M,預設1M
.........
[root@lnp-server-ip47 wordpress]# vim /etc/php.ini
; http://php.net/post-max-size
;post_max_size = 8M # 預設值為8M
post_max_size = 30M
;upload_max_filesize = 2M # 預設值為2M
upload_max_filesize = 20M
[root@lnp-server-ip47 wordpress]# systemctl restart nginx php-fpm
6.2 安全加固
# 關閉版本顯示
[root@lnp-server-ip47 wordpress]# grep -Ev '#|^$' /apps/nginx/conf/nginx.conf
worker_processes auto;
pid /apps/nginx/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 100m;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name blog.shone.cn;
server_tokens off; # 安全加固選項
location / {
root /data/nginx/wordpress;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location <sub> \.php$ {
root /data/nginx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By; # 安全加固選項
}
location </sub> ^/(ping|pm_status)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}
}
}
[root@lnp-server-ip47 wordpress]#
# 關閉 PHP版本暴露
[root@lnp-server-ip47 wordpress]# vim /etc/php.ini
; http://php.net/expose-php
;expose_php = On # 預設值為ON 可以在客戶端看到版本信息
expose_php = Off
6.3 配置 php 開啟 opcache 加速
[root@lnp-server-ip47 wordpress]# vim /etc/php.ini
.....................
[opcache]
; Determines if Zend OPCache is enabled
zend_extension=opcache.so
opcache.enable=1
.......................
[root@lnp-server-ip47 wordpress]#systemctl restart php-fpm
本文轉自:https://blog.51cto.com/shone/5165650