Nginx 實踐案例(原始碼編譯安裝方式):利用LNMP搭建wordpress站點
# 本實踐過程中的系統及環境描述 L:Linux N:Nginx M:MySQL https://dev.mysql.com/downloads/mysql/ P:PHP Wordpress #部署規劃: 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 伺服器
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 [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 # 進入子配置檔案目錄,從模板 檔案建立 [root@lnp-server-ip47 etc]# cd php-fpm.d/ [root@lnp-server-ip47 php-fpm.d]# cp .default [root@lnp-server-ip47 php-fpm.d]# # 按照本實踐的思路修改 [root@lnp-server-ip47 php-fpm.d]# vim ;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 # 修改後的 檔案去除 ; 註釋行的所有檔案內容 供比對 [root@lnp-server-ip47 php-fpm.d]# grep '^[^;]' [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 # 解壓原始碼包 [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= 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(); ?>
5. 部署 WordPress
5.1 準備 WordPress 檔案
# 下載原始檔,並複製到前面定義的網頁目錄下,並修改權屬 [root@lnp-server-ip47 <sub>]# wget [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 [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 出現可道雲的初始化嚮導,按照嚮導完成初始化
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 ; ;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 ; ;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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70003733/viewspace-2885379/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- LNMP網站框架搭建(編譯安裝)LNMP網站框架編譯
- centos7搭建lnmp編譯安裝nginx【二】CentOSLNMP編譯Nginx
- LNMP—Nginx的編譯安裝LNMPNginx編譯
- lnmp環境安裝-原始碼編譯LNMP原始碼編譯
- [LNMP]Nginx-1.6.3編譯安裝LNMPNginx編譯
- LAMP原始碼編譯安裝配置+wordpressLAMP原始碼編譯
- 從零搭建LNMP環境(一) - 編譯原始碼安裝PHPLNMP編譯原始碼PHP
- LNMP 原始碼安裝搭建薦LNMP原始碼
- nginx原始碼編譯安裝(詳解)Nginx原始碼編譯
- centos7搭建lnmp編譯安裝php【一】CentOSLNMP編譯PHP
- LNMP架構編譯安裝(Linux、Nginx、Mysql、PHP)LNMP架構編譯LinuxNginxMySqlPHP
- nginx+php+mysql+wordpress搭建簡單站點 安裝及配置過程NginxPHPMySql
- LNMP編譯安裝配置+discuzLNMP編譯
- 通過兩種方式來安裝WordPress網站(LNMP+寶塔皮膚)網站LNMP
- [LNMP]安裝NginxLNMPNginx
- 編譯安裝Nginx編譯Nginx
- nginx編譯安裝Nginx編譯
- Nginx1.19 php8.0 原始碼編譯安裝NginxPHP原始碼編譯
- [環境搭建] 透過原始碼編譯安裝 Redis原始碼編譯Redis
- 原始碼編譯安裝Redis原始碼編譯Redis
- LAMP原始碼編譯安裝LAMP原始碼編譯
- CentOS 7版本原始碼編譯方式安裝TokuDBCentOS原始碼編譯
- Linux下原始碼編譯方式安裝MySQL5.5Linux原始碼編譯MySql
- LAMP原始碼安裝+wordpress安裝LAMP原始碼
- Shell編譯安裝nginx編譯Nginx
- centos7下編譯安裝lnmpCentOS編譯LNMP
- 基於LNMP的WordPress搭建與速度最佳化實踐LNMP
- 原始碼編譯安裝的原理原始碼編譯
- zabbix agent原始碼編譯安裝原始碼編譯
- 原始碼編譯安裝MySQL 5.7.9原始碼編譯MySql
- 【轉】MySQL原始碼編譯安裝MySql原始碼編譯
- CentOS 下編譯安裝 NginxCentOS編譯Nginx
- Linux編譯安裝NginxLinux編譯Nginx
- macOS nginx 編譯安裝教程MacNginx編譯
- 通過原始碼的方式編譯hadoop的安裝檔案原始碼編譯Hadoop
- php-7.1.0原始碼編譯安裝PHP原始碼編譯
- 詳解LAMP原始碼編譯安裝LAMP原始碼編譯
- MySQL 5.6原始碼編譯安裝流程MySql原始碼編譯