這篇文章主要介紹“Linux系統(tǒng)中怎么搭建Nginx動(dòng)靜分離”,在日常操作中,相信很多人在Linux系統(tǒng)中怎么搭建Nginx動(dòng)靜分離問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Linux系統(tǒng)中怎么搭建Nginx動(dòng)靜分離”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比雁塔網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式雁塔網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋雁塔地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。
Nginx的靜態(tài)處理能力很強(qiáng),但是動(dòng)態(tài)處理能力不足,因此,在企業(yè)中常用動(dòng)靜分離技術(shù)
針對(duì)PHP的動(dòng)靜分離
靜態(tài)頁(yè)面交給Nginx處理
動(dòng)態(tài)頁(yè)面交給PHP-FPM模塊或Apache處理
在Nginx的配置中,是通過(guò)location配置段配合正則匹配實(shí)現(xiàn)靜態(tài)與動(dòng)態(tài)頁(yè)面的不同處理方式
Nginx通過(guò)proxy模塊實(shí)現(xiàn)將客戶端的請(qǐng)求代理至上游服務(wù)器,此時(shí)nginx與. 上游服務(wù)器的連接是通過(guò)http協(xié)議進(jìn)行的
Nginx在實(shí)現(xiàn)反向代理功能時(shí)的最重要指令為proxy_ _pass, 它能夠并能夠根據(jù)URI、客戶端參數(shù)或其它的處理邏輯將用戶請(qǐng)求調(diào)度至上游服務(wù)器
架設(shè)并調(diào)試后端LAMP
環(huán)境
安裝配置Nginx
處理靜態(tài)頁(yè)面請(qǐng)求,在server {}
;段中加入
[root@nginx php5]#vim /usr/local/httpd/conf/nginx.conf location ~ .*\.(gifiglipeglbmp|swf)$ { root html; index index.html index.htm; };
配置Nginx處理動(dòng)態(tài)頁(yè)面請(qǐng)求,在server{};中加入
在Apache.工作目錄新建test.php
重啟Nginx并測(cè)試
[root@nginx php5]#vim /usr/local/httpd/conf/nginx.conf server { ..... location ~ \.php$ { proxy_ pass http://192.168.9.237:8080; //LAMP的IP地址 ......
開(kāi)啟兩臺(tái)Linux虛擬機(jī),一臺(tái)配置Nginx,一臺(tái)配置LANM架構(gòu),客戶端訪問(wèn)nginx網(wǎng)頁(yè)時(shí)Nginx服務(wù)處理靜態(tài)訪問(wèn)信息,LAMP處理動(dòng)態(tài)訪問(wèn)信息,
首先在一臺(tái)Linux系統(tǒng)中搭建lamp架構(gòu),這里使用yum安裝lamp架構(gòu)
yum install httpd httpd-devel -y 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile ...//省略部分內(nèi)容... 已安裝: httpd.x86_64 0:2.4.6-90.el7.centos httpd-devel.x86_64 0:2.4.6-90.el7.centos ...//省略部分內(nèi)容... 完畢! [root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http success //配置防火墻允許http服務(wù)在公共區(qū)域通過(guò) [root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https success //配置防火墻允許https服務(wù)在公共區(qū)域通過(guò) [root@localhost ~]# firewall-cmd --reload //重新加載防火墻 success [root@localhost ~]# systemctl start httpd.service //啟動(dòng)http服務(wù)
在客戶機(jī)訪問(wèn)httpd服務(wù),看服務(wù)是否成功開(kāi)啟
安裝mariadb(mariadb是MySQL的分支軟件,功能與MySQL相同)
[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.sjtu.edu.cn ...//省略部分內(nèi)容... 已安裝: mariadb.x86_64 1:5.5.64-1.el7 mariadb-devel.x86_64 1:5.5.64-1.el7 mariadb-server.x86_64 1:5.5.64-1.el7 完畢! [root@localhost ~]# systemctl start mariadb //啟動(dòng)mariadb服務(wù) [root@localhost ~]# netstat -ntap | grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2519/mysqld [root@localhost ~]# mysql_secure_installation //設(shè)置數(shù)據(jù)庫(kù) 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): //詢問(wèn)是否給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è)置密碼,輸入y確認(rèn) 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 //詢問(wè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 //詢問(wèn)是否拒絕root用戶遠(yuǎ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 //詢問(wèn)是否刪除測(cè)試數(shù)據(jù)庫(kù),輸入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 //詢問(wèn)是否重新加載數(shù)據(jù)庫(kù),輸入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! //完成設(shè)置
安裝PHP
[root@localhost ~]# yum -y install php //安裝php服務(wù) 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.sjtu.edu.cn ...//省略部分內(nèi)容... 已安裝: php.x86_64 0:5.4.16-46.1.el7_7 ...//省略部分內(nèi)容... 完畢! [root@localhost ~]# yum install php-mysql -y //安裝php與mysql連接包 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.sjtu.edu.cn * extras: ftp.sjtu.edu.cn ...//省略部分內(nèi)容... 已安裝: php-mysql.x86_64 0:5.4.16-46.1.el7_7 作為依賴被安裝: php-pdo.x86_64 0:5.4.16-46.1.el7_7 完畢! [root@localhost ~]# 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 //安裝php環(huán)境包 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.sjtu.edu.cn ...//省略部分內(nèi)容... 已安裝: libcurl-devel.x86_64 0:7.29.0-54.el7 php-bcmath.x86_64 0:5.4.16-46.1.el7_7 php-gd.x86_64 0:5.4.16-46.1.el7_7 php-ldap.x86_64 0:5.4.16-46.1.el7_7 php-mbstring.x86_64 0:5.4.16-46.1.el7_7 php-odbc.x86_64 0:5.4.16-46.1.el7_7 php-pear.noarch 1:1.9.4-21.el7 php-snmp.x86_64 0:5.4.16-46.1.el7_7 php-soap.x86_64 0:5.4.16-46.1.el7_7 php-xml.x86_64 0:5.4.16-46.1.el7_7 php-xmlrpc.x86_64 0:5.4.16-46.1.el7_7 ...//省略部分內(nèi)容... 完畢! [root@localhost ~]# cd /var/www/html //進(jìn)入網(wǎng)頁(yè)站點(diǎn) [root@localhost html]# vim index.php //編輯php網(wǎng)頁(yè)內(nèi)容 <?php phpinfo(); ?> :wq [root@localhost html]# systemctl restart httpd.service //重啟http服務(wù)
在客戶機(jī)中測(cè)試LAMP機(jī)構(gòu)是否安裝成功
在另一臺(tái)Linux系統(tǒng)中安裝Nginx服務(wù)
[root@localhost ~]# hostnamectl set-hostname nginx //為了區(qū)分先更改Linux主機(jī)名稱 [root@localhost ~]# su [root@nginx ~]# //更改成功 [root@nginx nginx-1.12.2]# yum install gcc gcc-c++ make pcre-devel zlib-devel -y //安裝環(huán)境包 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: ftp.sjtu.edu.cn ...//省略部分內(nèi)容... 已安裝: gcc.x86_64 0:4.8.5-39.el7 gcc-c++.x86_64 0:4.8.5-39.el7 pcre-devel.x86_64 0:8.32-17.el7 zlib-devel.x86_64 0:1.2.7-18.el7 ...//省略部分內(nèi)容... 完畢! [root@nginx ~]# useradd -M -s /sbin/nologin nginx //創(chuàng)建程序用戶 [root@nginx ~]# mount.cifs //192.168.100.10/lnmp /mnt/ //掛載宿主機(jī)源碼包目錄到mnt目錄下 Password for root@//192.168.100.10/lnmp: [root@nginx ~]# cd /mnt/ //進(jìn)入mnt目錄 [root@nginx mnt]# tar zvxf nginx-1.12.2.tar.gz -C /opt/ //解壓源碼包 ...//省略部分內(nèi)容... nginx-1.12.2/auto/cc/msvc nginx-1.12.2/auto/cc/name nginx-1.12.2/auto/cc/owc nginx-1.12.2/auto/cc/sunc [root@nginx mnt]# cd /opt/nginx-1.12.2/ //進(jìn)入解壓的nginx文件目錄 [root@nginx nginx-1.12.2]# ./configure \ //配置nginx > --prefix=/usr/local/nginx \ > --user=nginx \ > --group=nginx \ > --with-http_stub_status_module ...//省略部分內(nèi)容... nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp [root@nginx nginx-1.12.2]# make && make install //制作安裝nginx ...//省略部分內(nèi)容... test -d '/usr/local/nginx/html' \ || cp -R html '/usr/local/nginx' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' make[1]: 離開(kāi)目錄“/opt/nginx-1.12.2” [root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ //創(chuàng)建軟連接文件到系統(tǒng)命令目錄 [root@nginx nginx-1.12.2]# cd /lib/systemd/system //進(jìn)入system管理目錄 [root@nginx system]# vim nginx.service //編輯nginx啟動(dòng)腳本文件 [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/bin/kill -s HUP $MAINPID ExecStop=/usr/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target :wq [root@nginx system]# chmod 754 nginx.service //添加執(zhí)行權(quán)限 [root@nginx system]# systemctl start nginx.service //啟動(dòng)服務(wù) [root@nginx system]# systemctl stop firewalld.service //關(guān)閉防火墻 [root@nginx system]# setenforce 0 //關(guān)閉增強(qiáng)性安全功能
在客戶機(jī)測(cè)試Nginx服務(wù)
在nginx服務(wù)配置文件中開(kāi)啟功能,將動(dòng)態(tài)訪問(wèn)轉(zhuǎn)交給LAMP主機(jī)處理,以實(shí)現(xiàn)動(dòng)靜分離
[root@nginx system]# vim /usr/local/nginx/conf/nginx.conf ...//省略部分內(nèi)容... error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # location ~ \.php$ { //開(kāi)啟此模塊,并將IP地址更改為L(zhǎng)AMP的主機(jī),也就是將動(dòng)態(tài)請(qǐng)求交給LAMP的主機(jī)處理 proxy_pass http://192.168.144.137; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; ...//省略部分內(nèi)容... :wq [root@nginx system]# systemctl stop nginx.service //關(guān)閉服務(wù) [root@nginx system]# systemctl start nginx.service //重新啟動(dòng)服務(wù)
在客戶測(cè)試動(dòng)靜分離是否成功
到此,關(guān)于“Linux系統(tǒng)中怎么搭建Nginx動(dòng)靜分離”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
網(wǎng)站名稱:Linux系統(tǒng)中怎么搭建Nginx動(dòng)靜分離
瀏覽路徑:http://muchs.cn/article8/joosip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站制作、網(wǎng)站收錄、Google、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)