溫馨提示×

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

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

部署Nginx+Apache動(dòng)靜分離(實(shí)戰(zhàn)!可跟做?。?/h1>
發(fā)布時(shí)間:2020-06-07 13:03:32 來(lái)源:網(wǎng)絡(luò) 閱讀:11155 作者:JarryZ 欄目:系統(tǒng)運(yùn)維

Nginx動(dòng)靜分離介紹

1.Nginx的靜態(tài)處理能力很強(qiáng),但是動(dòng)態(tài)處理不足,因此,在企業(yè)中常用動(dòng)靜分離技術(shù)
2.針對(duì)PHP的動(dòng)靜分離

靜態(tài)頁(yè)面交給Nginx處理

動(dòng)態(tài)頁(yè)面交給PHP+FPM模塊或Apache處理

3.在Nginx的配置中,是通過(guò)location配置段配合正則匹配實(shí)現(xiàn)靜態(tài)與動(dòng)態(tài)頁(yè)面的不同處理方式

反向代理原理

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

配置Nginx實(shí)現(xiàn)動(dòng)靜分離

1.本案例根據(jù)企業(yè)需要,將配置Nginx實(shí)現(xiàn)動(dòng)靜分離,對(duì)php頁(yè)面的請(qǐng)求轉(zhuǎn)發(fā)給LAMP處理,而靜態(tài)頁(yè)面交給Nginx處理,以實(shí)現(xiàn)動(dòng)靜分離

2.架構(gòu)如圖所示:

部署Nginx+Apache動(dòng)靜分離(實(shí)戰(zhàn)!可跟做?。?></p>
<h3>配置步驟:</h3>
<p>1.配置Nginx處理動(dòng)態(tài)頁(yè)面請(qǐng)求,在server{};中加入<br/>2.在Apache.工作目錄新建test.php<br/>3.重啟Nginx并測(cè)試</p>
<pre><code>[root@nginx php5]#vim /usr/local/httpd/conf/nginx.conf
  server {
  .....
  location ~ \.php$ {
        proxy_pass http://192.168.9.237:8080;
......                                          //LAMP的IP地址</code></pre>
<h4>Demo:</h4>
<h5>環(huán)境準(zhǔn)備:兩臺(tái)CentOS 7,其中7-3做為lamp,7-4做為nginx</h5>
<h5>第一步:安裝httpd</h5>
<pre><code class=[root@localhost ~]# yum install httpd httpd-devel -y [root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http success [root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https success [root@localhost ~]# firewall-cmd --reload success [root@localhost ~]# systemctl start httpd 此時(shí)可以使用宿主機(jī)訪問(wèn)Apache的主頁(yè)如下圖所示:

部署Nginx+Apache動(dòng)靜分離(實(shí)戰(zhàn)!可跟做?。?></p>
<h5>第二步:安裝mariadb數(shù)據(jù)庫(kù)(快捷輕量化的數(shù)據(jù)庫(kù))</h5>
<pre><code class=[root@lamp ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y [root@lamp ~]# systemctl start mariadb [root@lamp ~]# systemctl start mariadb [root@lamp ~]# netstat -ntap | grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 16836/mysqld [root@lamp ~]# mysql_secure_installation //對(duì)數(shù)據(jù)庫(kù)進(jìn)行設(shè)置 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): //給root管理員設(shè)定密碼,直接回車 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 //是否設(shè)置,選擇yes New password: //輸入新密碼 Re-enter new password: //重復(fù)輸入新密碼 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 //是否刪除匿名用戶,選擇no ... 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 //是否拒絕root用戶遠(yuǎn)程登陸,選擇no ... 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 //是否刪除測(cè)試數(shù)據(jù)庫(kù),選擇no ... skipping. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y //是否加載權(quán)限列表,選擇yes ... 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 install php -y 
[root@lamp ~]# yum install php-mysql -y
[root@lamp ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath
[root@lamp ~]# cd /var/www/html/
[root@lamp html]# ls
[root@lamp html]# vim index.php
<?php
  phpinfo();
?>
輸入:wq保存退出
[root@lamp html]# systemctl restart httpd.service
這時(shí)在宿主機(jī)的瀏覽器中輸入地址:http://192.168.18.128/index.php,就可以訪問(wèn)到lamp的php主頁(yè)

部署Nginx+Apache動(dòng)靜分離(實(shí)戰(zhàn)!可跟做?。?></p>
<h5>測(cè)試準(zhǔn)備:</h5>
<pre><code class=[root@lamp html]# vim index.php <?php echo "apache web !" ?> 輸入:wq保存退出

此時(shí) http://192.168.18.128/index.php 這個(gè)地址上顯示的內(nèi)容如下:

部署Nginx+Apache動(dòng)靜分離(實(shí)戰(zhàn)!可跟做?。?></p>
<h5>Nginx上的操作:</h5>
<pre><code class=[root@nginx ~]# mkdir /aaa [root@nginx ~]# mount.cifs //192.168.10.193/rpm /aaa Password for root@//192.168.10.193/rpm: [root@nginx ~]# cd /aaa [root@nginx aaa]# ls apr-1.6.2.tar.gz error.png nginx-1.12.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2 php-7.1.10.tar.bz2 awstats-7.6.tar.gz lf.jpg php-7.1.20.tar.gz cronolog-1.6.2-14.el7.x86_64.rpm mysql-5.6.26.tar.gz Discuz_X3.4_SC_UTF8.zip mysql-boost-5.7.20.tar.gz [root@nginx aaa]# tar zxvf nginx-1.12.2.tar.gz -C /opt/ [root@nginx aaa]# cd /opt [root@nginx opt]# ls nginx-1.12.2 rh [root@nginx opt]# cd nginx-1.12.2/ [root@nginx nginx-1.12.2]# useradd -M -s /sbin/nologin nginx [root@nginx nginx-1.12.2]# yum install gcc gcc-c++ pcre-devel zlib-devel -y [root@nginx nginx-1.12.2]# ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module [root@nginx nginx-1.12.2]# make && make install [root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@nginx nginx-1.12.2]# vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 99 20 # description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0 輸入:wq保存退出 [root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx [root@nginx nginx-1.12.2]# chkconfig --add nginx [root@nginx nginx-1.12.2]# yum install elinks -y [root@nginx nginx-1.12.2]# service nginx start [root@nginx nginx-1.12.2]# netstat -ntap | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 42028/nginx: master [root@nginx nginx-1.12.2]# systemctl stop firewalld.service [root@nginx nginx-1.12.2]# setenforce 0 root@nginx nginx-1.12.2]# elinks http://192.168.18.136/

此時(shí)得到如下界面,可按q,選擇yes,回車退出

部署Nginx+Apache動(dòng)靜分離(實(shí)戰(zhàn)!可跟做?。?></p>
<h5>此時(shí)在宿主機(jī)輸入:http://192.168.18.136/index.html 這個(gè)網(wǎng)址,會(huì)得到以下界面</h5>
<p><img src=[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf location ~ \.php$ { proxy_pass http://192.168.18.128; } #以上內(nèi)容意思為:動(dòng)態(tài)請(qǐng)求轉(zhuǎn)交給誰(shuí)去處理 #我們找到以上內(nèi)容將前面的注釋去掉,并將其中的IP地址改為另外一臺(tái)7-3的地址 輸入:wq保存退出 [root@nginx nginx-1.12.2]# service nginx stop [root@nginx nginx-1.12.2]# service nginx start

此時(shí)輸入:http://192.168.18.136/index.php 這個(gè)網(wǎng)址可以得到如下界面:
因?yàn)榇颂幗唤oApache去處理php的請(qǐng)求

部署Nginx+Apache動(dòng)靜分離(實(shí)戰(zhàn)!可跟做?。?></p>
<h3>結(jié)論:后綴為html就是靜態(tài)元素,后綴為php就是動(dòng)態(tài)元素,地址是不用改變的,因?yàn)槲覀冊(cè)谄渲凶隽宿D(zhuǎn)發(fā)處理!動(dòng)靜分離實(shí)驗(yàn)成功!</h3>
													            </div>
            <div   id=向AI問(wèn)一下細(xì)節(jié)

AI