溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Nginx動靜分離(實戰(zhàn)?。?/h1>
發(fā)布時間:2020-10-25 00:48:05 來源:網(wǎng)絡(luò) 閱讀:57669 作者:一拳超人007 欄目:系統(tǒng)運維

Nginx動靜分離介紹

  • Nginx的靜態(tài)處理能力很強,但是動態(tài)處理能力不足,因此,在企業(yè)中常用動靜分離技術(shù)
  • 針對PHP的動靜分離
    靜態(tài)頁面交給Nginx處理
    動態(tài)頁面交給PHP-FPM模塊或Apache處理
  • 在Nginx的配置中,是通過location配置段配個正則匹配實現(xiàn)靜態(tài)與動態(tài)頁面的不同處理方式

反向代理原理

  • Nginx不僅能作為web服務(wù)器,還具有反向代理,負載均衡和緩存的功能
  • Nginx通過proxy模塊實現(xiàn)將客戶端的請求代理至上游服務(wù)器,此時Nginx與上游服務(wù)器的連接是通過http協(xié)議進行的
  • Nginx在實現(xiàn)反向代理功能時最重要指令為proxy_pass,它能夠根據(jù)URI,客戶端參數(shù)或其他的處理邏輯將用戶請求調(diào)度至上游服務(wù)器

實驗環(huán)境

LAMP服務(wù)器(192.168.13.139)
Nginx服務(wù)器(192.168.13.140)

一,搭建LAMP(簡易搭建)

1,安裝Apache服務(wù),并允許通過防火墻進行訪問

[root@lamp ~]# yum install httpd httpd-devel -y   ##安裝http服務(wù)及開發(fā)包
[root@lamp ~]# systemctl start httpd.service   ##開啟服務(wù)
[root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=http   ##防火墻允許http
success
[root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@lamp ~]# firewall-cmd --reload   ##重啟防火墻
success

2,安裝mariadb數(shù)據(jù)庫

[root@lamp ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
##安裝數(shù)據(jù)庫
[root@lamp ~]# systemctl start  mariadb   ##開啟數(shù)據(jù)庫
[root@lamp ~]# netstat -ntap | grep 3306   ##查看端口號3306
tcp    0     0 0.0.0.0:3306       0.0.0.0:*     LISTEN     2200/mysqld  
[root@lamp ~]# mysql_secure_installation   ##設(shè)置數(shù)據(jù)庫

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y    ##設(shè)置root密碼
New password:       ##輸入密碼
Re-enter new password:     ##確認密碼

Remove anonymous users? [Y/n] n   ##允許匿名訪問
 ... skipping.

Disallow root login remotely? [Y/n] n  ##允許遠程登錄
 ... skipping.

Remove test database and access to it? [Y/n] n    ##保留測試數(shù)據(jù)庫
 ... skipping.

Reload privilege tables now? [Y/n] y     ##重啟數(shù)據(jù)庫
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

3,安裝PHP,并進行數(shù)據(jù)庫關(guān)聯(lián)

[root@lamp ~]# yum install php -y             ##安裝PHP
[root@lamp ~]# yum install php-mysql -y   ##建立php和mysql關(guān)聯(lián)
[root@lamp ~]# yum install -y \
> php-gd                                                    ##GD庫是php處理圖形的擴展庫
> php-ldap                                                ##輕量級目錄訪問協(xié)議
> php-odbc                                              ##應(yīng)用程序編程接口
> php-pear                                             ##擴展應(yīng)用代碼庫
> php-xml php-xmlrpc                           ##xml文件
> php-mbstring                                     ##多字節(jié)字符串
> php-snmp                                          ##管理端開發(fā)
> php-soap                                           ##SOAP 擴展可以用來提供和使用 Web Services
> curl curl-devel                                   ##支持數(shù)據(jù)文件下載工具
> php-bcmath                                      ##BCMath庫來支持更加精確的計算

4,切換到站點,編輯網(wǎng)頁內(nèi)容

[root@lamp ~]# cd /var/www/html/   ##切換到站點
[root@lamp html]# vim index.php    ##編輯php網(wǎng)頁內(nèi)容
<?php
    phpinfo();
?>

5,測試網(wǎng)頁

Nginx動靜分離(實戰(zhàn)?。?></p>
<pre><code>[root@lamp html]# vim index.php    ##編輯php網(wǎng)頁內(nèi)容
<?php
echo

Nginx動靜分離(實戰(zhàn)?。?></p>
<h3>二,安裝Nginx</h3>
<h4>1,在Linux上使用遠程共享獲取文件并掛載到mnt目錄下</h4>
<pre><code>[root@localhost ~]# smbclient -L //192.168.100.3/   ##遠程共享訪問
Enter SAMBA\root's password: 

                                Sharename       Type      Comment
                                ---------       ----      -------
                                LNMP-C7         Disk       
[root@localhost ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt  ##掛載到/mnt目錄下</code></pre>
<h4>2,解壓源碼包到/opt下,并查看</h4>
<pre><code>[root@localhost ~]# cd /mnt    ##切換到掛載點目錄
[root@localhost mnt]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gz
mysql-boost-5.7.20.tar.gz  php-7.1.20.tar.gz
[root@localhost mnt]# tar zxvf nginx-1.12.2.tar.gz -C /opt   ##解壓Nginx源碼包到/opt下
[root@localhost mnt]# cd /opt/    ##切換到解壓的目錄下
[root@localhost opt]# ls
nginx-1.12.2  rh</code></pre>
<h4>3,安裝編譯需要的環(huán)境組件包</h4>
<pre><code>[root@localhost opt]# yum -y install \
gcc \                                       //c語言
gcc-c++ \                        //c++語言
pcre-devel \                     //pcre語言工具
zlib-devel                       //數(shù)據(jù)壓縮用的函式庫</code></pre>
<h4>4,創(chuàng)建程序用戶nginx并編譯Nginx</h4>
<pre><code>[root@localhost opt]# useradd -M -s /sbin/nologin nginx  ##創(chuàng)建程序用戶,安全不可登陸狀態(tài)
[root@localhost opt]# id nginx
uid=1001(nginx) gid=1001(nginx) 組=1001(nginx)
[root@localhost opt]# cd nginx-1.12.0/                 ##切換到nginx目錄下
[root@localhost nginx-1.12.0]# ./configure \         ##配置nginx
> --prefix=/usr/local/nginx \        ##安裝路徑
> --user=nginx \                         ##用戶名
> --group=nginx \                       ##用戶組
> --with-http_stub_status_module     ##狀態(tài)統(tǒng)計模塊</code></pre>
<h4>5,編譯和安裝</h4>
<pre><code>[root@localhost nginx-1.12.0]# make     ##編譯
...
[root@localhost nginx-1.12.0]# make install   ##安裝
...
[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 
##創(chuàng)建軟連接讓系統(tǒng)識別nginx啟動腳本</code></pre>
<h4>6,制作管理腳本,便于使用service管理使用</h4>
<pre><code>[root@localhost nginx]# cd /etc/init.d/   ##切換到啟動配置文件目錄
[root@localhost init.d]# ls
functions  netconsole  network  README
[root@localhost init.d]# vim nginx         ##編輯啟動腳本文件

#!/bin/bash
# chkconfig: - 99 20                                    ##注釋信息
# description: Nginx Service Control Script
PROG=

7,安裝elinks測試Nginx網(wǎng)頁

[root@localhost init.d]# yum install elink -y  ##安裝軟件
[root@localhost init.d]# netstat -ntap | grep 80   ##查看Nginx端口
tcp     0   0 0.0.0.0:80    0.0.0.0:*    LISTEN    44663/nginx: master 
[root@localhost init.d]# systemctl stop firewalld.service   ##關(guān)閉防火墻
[root@localhost init.d]# setenforce 0
[root@localhost init.d]# elinks http://192.168.13.140/  ##測試網(wǎng)頁

Nginx動靜分離(實戰(zhàn)?。?></p>
<h4>8,訪問Nginx網(wǎng)頁</h4>
<p><img src=

三,修改Nginx配置文件開啟動靜分離

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf  ##修改配置文件

     location ~ \.php$ {    ##找到此處將注釋去除,開啟動靜分離
        proxy_pass   http://192.168.13.139;   ##填寫動態(tài)處理的Apache的服務(wù)器地址
        }
[root@localhost init.d]# service nginx stop   ##關(guān)閉
[root@localhost init.d]# service nginx start   ##開啟

四,測試網(wǎng)頁(192.168.13.140)

Nginx動靜分離(實戰(zhàn)!)
Nginx動靜分離(實戰(zhàn)?。?></p>
<h3>實現(xiàn)了動靜分離處理,Nginx處理靜態(tài),Apache處理動態(tài)信息</h3>
<h2>謝謝閱讀!</h2>
													            </div>
            <div   id=向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI