溫馨提示×

溫馨提示×

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

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

實現(xiàn)apache與nginx之間的動靜分離

發(fā)布時間:2020-07-22 16:11:10 來源:網(wǎng)絡 閱讀:197 作者:qq5d47f509174fe 欄目:系統(tǒng)運維

實現(xiàn)apache與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)頁面的不同處理方式

其簡單示意圖如下:

實現(xiàn)apache與nginx之間的動靜分離

本次實驗將使用兩臺虛擬機,分別模擬LNMP端與NGINX端

首先是LAMP的架設

LAMP端

安裝http服務

[root@localhost ~]# hostnamectl set-hostname LAMP       //更改主機名
[root@localhost ~]# su
[root@lamp ~]# yum install httpd httpd-devel -y
[root@lamp ~]# systemctl start httpd.service
[root@lamp ~]# firewall-cmd --zone=public --add-service=http --permanent    //防火墻設定允許通過
success
[root@lamp ~]# firewall-cmd --zone=public --add-service=https --permanent 
success
[root@lamp ~]# firewall-cmd --reload 
success

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

概述:

MariaDB數(shù)據(jù)庫管理系統(tǒng)是MySQL的一個分支,主要由開源社區(qū)在維護,采用GPL授權(quán)許可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能輕松成為MySQL的代替品。

[root@lamp ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
[root@lamp ~]# systemctl start mariadb.service 
[root@lamp ~]# mysql_secure_installation                     //對數(shù)據(jù)庫進行設置

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):        //回車進行下一步
OK, successfully used password, moving on...

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

Set root password? [Y/n] y                             //建立新的root管理密碼,選擇y
New password:                                          //輸入兩遍同樣密碼即可
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n                         //刪除匿名用戶?(選擇n)
 ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n                 //是否允許遠程根登錄,選擇n
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n        //選擇n即可
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y                 //是否進行刷新?(選擇y)
 ... 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!

安裝PHP工具

[root@lamp ~]# yum -y install php              //安裝PHP工具
[root@lamp ~]# yum install php-mysql -y        //建立php與數(shù)據(jù)庫的關(guān)系
[root@lamp ~]# yum -y install \
php-gd \
php-ldap \
php-odbc \
php-pear \
php-xml \
php-xmlrpc \
php-mbstring \
php-snmp \
php-soap \
curl curl-devel \
php-bcmath

創(chuàng)建http站點

[root@lamp ~]# cd /var/www/html/          //前往http站點目錄
[root@lamp html]# ll       //此時為空
總用量 0
[root@lamp html]# vim index.php           //新建首頁
添加
<?php
  phpinfo();
?>
[root@lamp html]# systemctl restart httpd.service

此時,使用測試機進行訪問是可以查看到php的動態(tài)網(wǎng)頁的,證明LAMP架構(gòu)成功,下面就是nginx方面的設置。

實現(xiàn)apache與nginx之間的動靜分離

Nginx端

開啟另一臺虛擬機作為nginx端

手工編譯安裝nginx

[root@localhost ~]# hostnamectl set-hostname nginx      //更改主機名
[root@localhost ~]# su
[root@nginx mnt]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@nginx mnt]# cd /opt/nginx-1.12.0/
[root@nginx nginx-1.12.0]# useradd -M -s /sbin/nologin nginx     //創(chuàng)建程序性用戶
[root@nginx nginx-1.12.0]# yum -y install \
gcc gcc-c++ \
pcre pcre-devel \
zlib-devel \
expat-devel
[root@nginx nginx-1.12.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@nginx nginx-1.12.0]# make && make install     //編譯安裝

編寫啟動腳本

[root@nginx nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/    //創(chuàng)建軟鏈接方便識別
[root@nginx nginx-1.12.0]# vim /etc/init.d/nginx
添加
#!/bin/bash
wenjian="/usr/local/nginx/sbin/nginx"
pid="/usr/local/nginx/logs/nginx.pid"
case $1 in
start)
    $wenjian ;;
stop)
    kill -s QUIT $(cat $pid) ;;
restart)
    $0 stop
    $0 start
;;
reload)
    kill -s HUP $(cat $pid) ;;
*)
    echo "Please,try again"
    exit 1 ;;
esac
exit 0
[root@nginx nginx-1.12.0]# chmod +x /etc/init.d/nginx
[root@nginx init.d]# service nginx start 
[root@nginx init.d]# netstat -atnp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8638/nginx: master
[root@nginx init.d]# systemctl stop firewalld.service 
[root@nginx init.d]# setenforce 0

驗證nginx服務

此時,我們通過測試機進行訪問,即可得到如下界面:

實現(xiàn)apache與nginx之間的動靜分離

但是,若訪問php格式的動態(tài)網(wǎng)頁則會出現(xiàn)404訪問錯誤的問題

實現(xiàn)apache與nginx之間的動靜分離

想要解決這個問題,就引入了下面的實驗,在nginx配置文件中進行動靜分離的設置

nginx端

[root@nginx init.d]# cd /usr/local/nginx/conf
[root@nginx conf]# vim nginx.conf     //修改配置文件
59到61行,取消注釋,并按照下面進行修改
  location ~ \.php$ {
        proxy_pass   http://192.168.142.128;    #apache服務器所在地址
  }
wq保存退出
[root@nginx nginx]# service nginx stop       //重啟服務
[root@nginx nginx]# service nginx start

至此,動靜分離就全部完成了。

實驗驗證

動態(tài)網(wǎng)頁

實現(xiàn)apache與nginx之間的動靜分離

靜態(tài)網(wǎng)頁

實現(xiàn)apache與nginx之間的動靜分離

向AI問一下細節(jié)

免責聲明:本站發(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