一.LNMP簡介
1.LNMP的組成
LNMP分別代表Linux啦撮、 Nginx蒜绽、MySQL和PHP舟肉。
2.LNMP工作流程
Nginx作為web服務(wù)器,當(dāng)它接收到請(qǐng)求后燕酷,Nginx是不支持對(duì)外部程序的直接調(diào)用或者解析籍凝,必須通過FastCGI進(jìn)行調(diào)用周瞎。如果是PHP請(qǐng)求,則交給PHP解釋器處理饵蒂,并把結(jié)果返回給客戶端声诸。PHP-FPM是支持解析PHP的一個(gè)FastCGI進(jìn)程管理器,提供了更好管理PHP進(jìn)程的方式苹享,可以有效控制內(nèi)存和進(jìn)程、可以平滑重載PHP配置浴麻。
3.nginx與fast-cgi工作流程圖解
說明:php-fpm是控制php-fpm守護(hù)進(jìn)程的(包括某個(gè)域名的訪問日志得问、錯(cuò)誤日志、session存放位置等)
Nginx FastCGI的運(yùn)行原理
nginx fastcgi 訪問PHP:
1.用戶發(fā)送http請(qǐng)求報(bào)文給nginx服務(wù)器
2.nginx會(huì)根據(jù)文件url和后綴來判斷請(qǐng)求
3.如果請(qǐng)求的是靜態(tài)內(nèi)容,nginx會(huì)將結(jié)果直接返回給用戶
4.如果請(qǐng)求的是動(dòng)態(tài)內(nèi)容,nginx會(huì)將請(qǐng)求交給fastcgi客戶端,通過fastcgi_pass將這個(gè)請(qǐng)求發(fā)送給php-fpm
5.php-fpm收到請(qǐng)求后會(huì)通過本地監(jiān)聽的socket交給wrapper
6.wrapper收到請(qǐng)求會(huì)生成新的線程調(diào)用php動(dòng)態(tài)程序解析服務(wù)器
7.如果用戶請(qǐng)求的是博文软免、或者內(nèi)容宫纬、PHP會(huì)請(qǐng)求MySQL查詢結(jié)果
8.如果用戶請(qǐng)求的是圖片、附件膏萧、PHP會(huì)請(qǐng)求nfs存儲(chǔ)查詢結(jié)果
9.php會(huì)將查詢到的結(jié)果交給Nginx
10.nginx會(huì)生成一個(gè)響應(yīng)報(bào)文返還給用戶
二.安裝環(huán)境裝備
1. 關(guān)閉防火墻漓骚。
1. 運(yùn)行systemctl status firewalld命令查看當(dāng)前防火墻的狀態(tài)。
[root@web01 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2019-11-07 23:00:45 CST; 12s ago
Docs: man:firewalld(1)
Main PID: 7515 (firewalld)
CGroup: /system.slice/firewalld.service
└─7515 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
Nov 07 23:00:44 web01 systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 07 23:00:45 web01 systemd[1]: Started firewalld - dynamic firewall daemon.
如果防火墻的狀態(tài)參數(shù)是inactive榛泛,則防火墻為關(guān)閉狀態(tài)蝌蹂。
如果防火墻的狀態(tài)參數(shù)是active,則防火墻為開啟狀態(tài)曹锨。本示例中防火墻為開啟狀態(tài)孤个,因此需要關(guān)閉防火墻。
2. 關(guān)閉防火墻沛简。如果防火墻為關(guān)閉狀態(tài)可以忽略此步驟齐鲤。
如果您想臨時(shí)關(guān)閉防火墻,運(yùn)行命令systemctl stop firewalld椒楣。
說明 :這只是暫時(shí)關(guān)閉防火墻给郊,下次重啟Linux后,防火墻還會(huì)開啟捧灰。
如果您想永久關(guān)閉防火墻淆九,運(yùn)行命令systemctl disable firewalld。
說明: 如果您想重新開啟防火墻毛俏,請(qǐng)參見https://firewalld.org/
3. 關(guān)閉SELinux吩屹。
1. 運(yùn)行g(shù)etenforce命令查看SELinux的當(dāng)前狀態(tài)。
[root@web01 ~]# getenforce
Disabled
如果SELinux狀態(tài)參數(shù)是Disabled拧抖, 則SELinux為關(guān)閉狀態(tài)煤搜。
如果SELinux狀態(tài)參數(shù)是Enforcing,則SELinux為開啟狀態(tài)唧席。本示例中SELinux為開啟狀態(tài)擦盾,因此需要關(guān)閉SELinux嘲驾。
2. 關(guān)閉SELinux。如果SELinux為關(guān)閉狀態(tài)可以忽略此步驟迹卢。
如果您想臨時(shí)關(guān)閉SELinux辽故,運(yùn)行命令setenforce 0。
說明 :這只是暫時(shí)關(guān)閉SELinux腐碱,下次重啟Linux后誊垢,SELinux還會(huì)開啟。
如果您想永久關(guān)閉SELinux症见,運(yùn)行命令vi /etc/selinux/config編輯SELinux配置文件喂走。回車后谋作,把光標(biāo)移動(dòng)到`SELINUX=enforcing`這一行芋肠,按`i`鍵進(jìn)入編輯模式,修改為`SELINUX=disabled`遵蚜, 按`Esc`鍵帖池,然后輸入`:wq`并按`Enter`鍵以保存并關(guān)閉SELinux配置文件。
說明: 如果您想重新開啟SELinux吭净,請(qǐng)參見https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/5/html/deployment_guide/ch-selinux#s1-SELinux-resources)睡汹。
3. 重啟系統(tǒng)使設(shè)置生效。
4.系統(tǒng)以及軟件版本介紹
使用了以下版本的軟件:
操作系統(tǒng):公共鏡像 CentOS 7.6 64位
Nginx版本:Nginx 1.16.1-1
MySQL版本:MySQL 5.7.28
PHP版本:PHP 7.1
三.安裝LNMP
1.創(chuàng)建統(tǒng)一用戶
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -s /sbin/nologin -M -u 666 -g 666
[root@web01 ~]# id www
uid=666(www) gid=666(www) 組=666(www)
2.安裝nginx
1)使用nginx官方源
nginx官方源請(qǐng)參考:http://nginx.org/en/linux_packages.html
使用vim /etc/yum.repos.d/nginx.repo創(chuàng)建nginx.repo源文件寂殉,按i輸入如下內(nèi)容:
[root@web01 ~]# 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
按ESC輸入:wq保存文件并退出帮孔。
2)yum安裝nginx
[root@web01 ~]# yum -y install nginx
3)啟動(dòng)nginx并加入開機(jī)自啟動(dòng)
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
3.使用第三方擴(kuò)展epel源安裝PHP
1)移除舊版本PHP
[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common
2)安裝擴(kuò)展源
[root@web01 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@web01 ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
3)安裝PHP7.1版本
[root@web ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
4)配置php-fpm用戶與nginx用戶保持一致
[root@web01 ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
[root@web01 ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
5)啟動(dòng)php-fpm并設(shè)置開機(jī)自啟動(dòng)
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm
3.安裝mysql數(shù)據(jù)庫
1)下載官方擴(kuò)展源, 擴(kuò)展源集成mysql5.6、5.7不撑、8.0,僅5.7倉庫是開啟
[root@web01 ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
2)安裝mysql數(shù)據(jù)庫
[root@web01 ~]# yum install mysql-community-server -y
3)啟動(dòng)mysql并設(shè)置開機(jī)自啟動(dòng)
[root@web01 ~]# systemctl start mysqld
[root@web01 ~]# systemctl enable mysqld
4)設(shè)置數(shù)據(jù)庫的密碼
注意:mysql5.7版本之后文兢,安裝后的默認(rèn)密碼不再是空密碼了,如果是剛安裝的焕檬,可以在mysql的日志文件找到臨時(shí)的登錄密碼姆坚!之后就可以自行設(shè)置密碼了,另外实愚,到了5.7版本兼呵,user表里就沒有了password這個(gè)字段了,要想修改密碼則需要用authentication_string這個(gè)字段腊敲。
[root@web01 ~]# grep 'temporary password' /var/log/mysqld.log
2019-11-08T23:39:39.839217Z 1 [Note] A temporary password is generated for root@localhost: Qjt2;DfHI0py
[root@web01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.28
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> alter user 'root'@'localhost' identified by 'Weiaixiong-123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
四.配置LNMP架構(gòu)
1.配置nginx實(shí)現(xiàn)動(dòng)態(tài)請(qǐng)求php請(qǐng)求請(qǐng)參考nginx官方示例說明:http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html
使用vim /etc/nginx/conf.d/wordpress.conf創(chuàng)建wordpress.conf配置文件击喂,按i輸入如下內(nèi)容:
[root@web01 ~]# vim /etc/nginx/conf.d/wordpress.conf
server {
server_name blog.wordpress.com;
listen 80;
root /code;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /code$fastcgi_script_name;
include fastcgi_params;
}
}
按ESC輸入:wq保存文件并退出。
2.測(cè)試nginx與PHP的連通
1)創(chuàng)建站點(diǎn)目錄
[root@web01 ~]# mkdir /code
[root@web01 ~]# chown -R www.www /code/
2)在站點(diǎn)下創(chuàng)建測(cè)試連通性文件
使用vim /code/info.php,創(chuàng)建info.php文件碰辅,按i輸入如下內(nèi)容:
[root@web01 ~]# vim /code/info.php
<?php
phpinfo();
?>
按ECS輸入:wq退出并保存文件懂昂。
3)檢查nginx的語法并重啟
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx
4)配置本地hosts文件之后測(cè)試
5)測(cè)試PHP連接mysql數(shù)據(jù)庫
使用vim /code/mysql.php創(chuàng)建mysql.php文件,按i 輸入如下內(nèi)容:
[root@web01 ~]# vim /code/mysql.php
<?php
$servername = "localhost";
$username = "root";
$password = "Weiaixiong-123456";
// 創(chuàng)建連接
$conn = mysqli_connect($servername, $username, $password);
// // 檢測(cè)連接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "php 連接 MySQL 數(shù)據(jù)庫成功";
?>
按ECS輸入:wq保存文件并退出
測(cè)試訪問結(jié)果如下:
五.部署WordPress博客
說明:之前的環(huán)境已經(jīng)部署完成没宾,這里只需創(chuàng)建一個(gè)數(shù)據(jù)庫以及下載源碼包安裝即可凌彬。
1.創(chuàng)建一個(gè)名為wordpress的數(shù)據(jù)庫
[root@web01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> create database wordpress;
Query OK, 1 row affected (0.00 sec)
2.下載源碼包并解壓到站點(diǎn)目錄下
[root@web01 ~]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@web01 ~]# ls
anaconda-ks.cfg hostname_ip.sh wordpress-4.9.4-zh_CN.tar.gz
[root@web01 ~]# tar xf wordpress-4.9.4-zh_CN.tar.gz -C /code/
[root@web01 ~]# cd /code/
[root@web01 /code]# ls
info.php mysql.php wordpress
3.修改站點(diǎn)目錄并修改屬主和屬組
[root@web01 ~]# cat /etc/nginx/conf.d/wordpress.conf
server {
server_name blog.wordpress.com;
listen 80;
root /code/wordpress;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 ~]# chown -R www.www /code/
[root@web01 ~]# ll /code/
total 12
-rw-r--r-- 1 www www 24 Nov 9 09:32 info.php
-rw-r--r-- 1 www www 339 Nov 9 09:39 mysql.php
drwxr-xr-x 5 www www 4096 Feb 8 2018 wordpress
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx
4.瀏覽器訪問并進(jìn)一步部署wordpress
15.png
16.png
17.png
18.png
19.png
20.png
21.png
22.png
至此wordpress部署完成
說明:其他網(wǎng)站部署操作也和以上布置大同小異沸柔,你可以自行搭建使用即可。
六.php-fpm優(yōu)化
1.php.init優(yōu)化
1.打開php的安全模式,控制php執(zhí)行危險(xiǎn)函數(shù), 默認(rèn)是Off,改為On
sql.safe_mode = Off
2.關(guān)閉php頭部信息, 隱藏版本號(hào), 默認(rèn)是On,改為Off
expose_php = On
3錯(cuò)誤信息輸出控制
display_error = Off
error_reporting = E_WARNING & E_ERROR
4記錄錯(cuò)誤日志至后臺(tái), 方便追溯
log_errors = On
error_log = /var/log/php_error.log
5.每個(gè)腳本時(shí)間最大內(nèi)存
memory_limit = 128M
6.上傳文件最大許可,默認(rèn)2M, 建議調(diào)整為16,32M
upload_max_filesize = 2M
7.禁止遠(yuǎn)程執(zhí)行phpshell,默認(rèn)On, 建議Off
allow_url_fopen = On
8.時(shí)區(qū)調(diào)整,默認(rèn)PRC, 建議調(diào)整為Asia/Shanghai
date.timezone = PRC
整體優(yōu)化后配置文件
sql.safe_mode = Off
expose_php = Off
display_error = Off
error_reporting = E_WARNING & E_ERROR
log_errors = On
error_log = /var/log/php_error.log
upload_max_filesize = 50M
allow_url_fopen = Off
date.timezone = Asia/Shanghai
2.php-fpm配置詳解
[global]
#pid設(shè)置, 記錄程序啟動(dòng)后pid
pid = /var/run/php-fpm.pid
#php-fpm程序啟動(dòng)錯(cuò)誤日志路徑
error_log = /soft/log/php/php-fpm_error.log
# 錯(cuò)誤級(jí)別. 可用級(jí)別為: alert(必須立即處理),error(錯(cuò)誤情況), warning(警告情況), notice(一般重要信息), debug(調(diào)試信息). 默認(rèn): notice.
log_level = warning
#設(shè)置文件打開描述符的rlimit限制.
rlimit_files = 65535
events.mechanism = epoll
#啟動(dòng)進(jìn)程的用戶和組
[www]
user = www
group = www
# fpm監(jiān)聽端口
listen = 127.0.0.1:9000
# unix socket設(shè)置選項(xiàng)铲敛,如果使用tcp方式訪問褐澎,這里注釋即可。
listen.owner = www
listen.group = www
# 允許訪問FastCGI進(jìn)程的IP伐蒋,any不限制
listen.allowed_clients = 127.0.0.1
# pm設(shè)置動(dòng)態(tài)調(diào)度
pm = dynamic
# 同一時(shí)刻最大的php-fpm子進(jìn)程數(shù)量
pm.max_children = 50
# 動(dòng)態(tài)方式下的起始php-fpm進(jìn)程數(shù)量
pm.start_servers = 20
# 動(dòng)態(tài)方式下服務(wù)器空閑時(shí)最小php-fpm進(jìn)程數(shù)量
pm.min_spare_servers = 10
# 動(dòng)態(tài)方式下服務(wù)器空閑時(shí)最大php-fpm進(jìn)程數(shù)量
pm.max_spare_servers = 30
# 最大請(qǐng)求
pm.max_requests = 1024
pm.process_idle_timeout = 15s;
# FPM狀態(tài)頁面,用于監(jiān)控php-fpm狀態(tài)使用
pm.status_path = /status
# 錯(cuò)誤日志
php_flag[display_errors] = off
php_admin_value[error_log] = /soft/log/php/php-www_error.log
php_admin_flag[log_errors] = on
# 配置php慢查詢, 以及慢查詢記錄日志位置
request_slowlog_timeout = 5s
slowlog = /soft/log/php/php-slow.log
3.優(yōu)化案例
php-fpm配置文件 服務(wù)器配置4核16G 8核16G
[root@nginx ~]# cat /etc/php-fpm.d/www.conf
[global]
pid = /var/run/php-fpm.pid
#php-fpm程序錯(cuò)誤日志
error_log = /var/log/php/php-fpm.log
log_level = warning
rlimit_files = 655350
events.mechanism = epoll
[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 512 #一個(gè)對(duì)應(yīng)內(nèi)存30m內(nèi)存左右
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.process_idle_timeout = 15s;
pm.max_requests = 2048
#php-www模塊錯(cuò)誤日志
php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php/php-www.log
php_admin_flag[log_errors] = on
#php慢查詢?nèi)罩?request_slowlog_timeout = 5s
slowlog = /var/log/php/php-slow.log