1、LNMP架構(gòu)概述
(1)什么是LNMP?
? ? ?LNMP是一套技術(shù)的組合,L=Linux、N=Nginx、M=MySQL(MyriDB)、P=PHP(Python)
(2)LNMP架構(gòu)是如何工作的?
? ? ?首先Nginx服務(wù)器是不能處理動態(tài)請求,那么當(dāng)用戶發(fā)起動態(tài)請求時,Nginx又是如何進行處理的?
? ? ?當(dāng)用戶發(fā)起http請求時,請求會被Nginx處理,如果是靜態(tài)資源請求Nginx則直接返回,如果是動態(tài)請求Nginx則通過fastcgi協(xié)議轉(zhuǎn)交給后端的PHP程序處理,具體如下圖所示:

(3)Nginx與Fast-CGI詳細工作流程如下圖所示

Nginx結(jié)合PHP FastCGI運行原理圖
注:CGI全稱通用網(wǎng)關(guān)接口 Commmon Gateway Interface、php-fpm(fcgi process mangemnt)管理進程、php-fpm配置文件為php-fpm.conf、php解析器的配置文件為php.ini。
(4)工作流程
①用戶發(fā)送http請求報文給nginx服務(wù)器
②nginx會根據(jù)文件url和后綴來判斷請求
③如果請求的是靜態(tài)內(nèi)容,nginx會將結(jié)果直接返回給用戶
④如果請求的是動態(tài)內(nèi)容,nginx會將請求交給fastcgi客戶端,通過fastcgi_pass將這個請求發(fā)送給php-fpm管理進程,php-fpm管理進程接收到后會調(diào)用具體的工作進程warrap。
⑤warrap進程收到請求會生成新的線程調(diào)用php動態(tài)程序解析器。
⑥如果只是解析代碼,php直接返回;如果有查詢數(shù)據(jù)庫操作,則由php連接數(shù)據(jù)庫發(fā)起查詢操作。
⑦最終數(shù)據(jù)由mysql->php->php-fpm->fastCGI->Nginx->user
2、LNMP架構(gòu)環(huán)境部署
(1)使用官方倉庫安裝Nginx
[root@nginx ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
#安裝Nginx,啟動并加入開機自啟。
[root@Server-1 ~]# yum -y install nginx
[root@Server-1 ~]# systemctl start nginx
[root@Server-1 ~]# systemctl enable nginx
(2)使用第三方擴展原安裝php7.1
#yum -y https://dl.fedoraprojeck.org/pub/epel/epel-release-latest-7.noarch.rpm
#yum -y https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
#安裝和啟用EPEL和Remi存儲庫 直接安裝PHP會報錯
[root@Server-1 ~]# yum remove php-mysql-5.4 php php-fpm php-common
[root@Server-1 ~]# cat /etc/yum.repos.d/php.repo
[php]
name = php Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
[root@Server-1 ~]# 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
#啟動php-fpm,并將其加入開機自啟
[root@Server-1 ~]# systemctl start php-fpm
[root@Server-1 ~]# systemctl enable php-fpm
(3)安裝Mariadb數(shù)據(jù)庫(為什么不安裝mysql------mariadb小,暫時用下數(shù)據(jù)庫而已)
[root@Server-1 ~]# yum install mariadb-server mariadb -y
3、Nginx與php實現(xiàn)原理
在將Nginx與PHP集成的過程中,需要先了解FastCGI代理配置語法
(1)設(shè)置FastCGI服務(wù)器的地址,該地址可以指定為域名或者IP地址,以及端口
Syntax:  fastcgi_pass address;
  Default: —
  Context: location,if in location
  #語法示例
  fastcgi_pass localhost:9000;            #默認端口9000(建議使用這條)
  fastcgi_pass unix:/tmp/fastcgi.socket;  #適合在nginx和php在一臺服務(wù)器上(跨網(wǎng)絡(luò)的不行)
(2)設(shè)置fastcgi默認的首頁文件,需要結(jié)合fastcgi_param一起設(shè)置
Syntax:	fastcgi_index name;
  Default:	—
  Context:	http,server,location
(3)通過fastcgi_param設(shè)置變量,并將設(shè)置的變量傳遞到后端的fastcgi服務(wù)器
Syntax:  fastcgi_param paramter value [if_not_empty];
   Default: —
   Context: http,server,location
   
   #語法示例
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME /code$fastcgi_script_name;
(4)通過圖形方式展示fastcgi_index與fastcgi_param作用

①nginx通過php.zxc.com找到對應(yīng)的服務(wù)器
②根據(jù)fastcgi_param設(shè)置的SCRIPT_FILENAME變量中的fastcgi_spript_name(客戶請求的文件)
③以上php解析的真實路徑為/code/index.php發(fā)送給FastCGI
4、Nginx與php集成實現(xiàn)
(1)創(chuàng)建匹配php的配置文件
[root@Server-1 ~]# cat /etc/nginx/conf.d/phptest.conf 
server {
 listen 80;
 server_name php.zxc.com;
 root /code;
 location / {
  index index.php index.html;
 }
 location ~ \\.php$ {
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
 }
}
(2)啟動php-fpm,并將其加入開機自啟
[root@Server-1 ~]# systemctl start php-fpm
[root@Server-1 ~]# systemctl enable php-fpm
#驗證php-fpm是否啟動,127.0.0.1:9000起來了就成功了
[root@Client-1 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1039/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1213/master
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      16441/php-fpm: mast
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      14128/mysqld
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      746/rpcbind
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      16507/nginx: master
tcp6       0      0 :::22                   :::*                    LISTEN      1039/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1213/master
tcp6       0      0 :::111                  :::*                    LISTEN      746/rpcbind
(3)測試nginx與php是否集成成功
#創(chuàng)建一個php文件
[root@server-1 ~]# cat /code/page.php
?phpspan
 phpinfo();
?>
#訪問http://php.zxc.com/page.php,能訪問就可以了
5、php與mariadb數(shù)據(jù)庫(mysql也行)集成實現(xiàn)
(1)啟動Mariadb數(shù)據(jù)庫,并將其加入開機自啟
[root@Server-1 ~]# systemctl start mariadb
[root@Server-1 ~]# systemctl enable mariadb
(2)給Mariadb配置登入密碼,并使用新密碼登入數(shù)據(jù)庫
[root@Server-1 ~]# mysqladmin password ‘P@ssw0rd’
[root@Server-1 ~]# mysql -uroot -pP@ssw0rd
(3)準(zhǔn)備一個php文件,測試能否正常連接數(shù)據(jù)庫
[root@Server-1 ~]# cat /code/mariadb.php
?php$servername = "localhost";
    $username = "root";
    $password = "P@ssw0rd";
    // 創(chuàng)建連接
    $conn = mysqli_connect($servername, $username, $password);
    // 檢測連接
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "php連接MySQL數(shù)據(jù)庫成功";
    ?>
    #只要訪問該頁面,出現(xiàn) "php連接MySQL數(shù)據(jù)庫成功"就說明成功了
- 
                                PHP
                                +關(guān)注
關(guān)注
0文章
459瀏覽量
28324 - 
                                MYSQL數(shù)據(jù)庫
                                +關(guān)注
關(guān)注
0文章
96瀏覽量
10164 - 
                                nginx
                                +關(guān)注
關(guān)注
0文章
180瀏覽量
12914 
發(fā)布評論請先 登錄
nginx重啟命令linux步驟是什么?
nginx重啟命令linux步驟是什么?
【NanoPi NEO試用體驗】之安裝配置Nginx環(huán)境WEB網(wǎng)站詳解
玩轉(zhuǎn)Firefly-RK3399資料匯總(一)
ECS配置lnmp的詳細步驟資料說明
    
Nginx如何和現(xiàn)有監(jiān)控系統(tǒng)集成
Nginx_LNMP架構(gòu)拆分
    
搭建Keepalived+Lvs+Nginx高可用集群負載均衡
    
Nginx架構(gòu)拆分集群詳解
    
          
        
        
Nginx搭建流行架構(gòu)LNMP的步驟
                
 
           
            
            
                
            
評論