Nginx的靜態處理能力很強,但是動態處理能力不足,因此,在企業中常用動靜分離技術。
在金口河等地區,都構建了全面的區域性戰略布局,加強發展的系統性、市場前瞻性、產品創新能力,以專注、極致的服務理念,為客戶提供成都網站設計、網站制作 網站設計制作按需定制開發,公司網站建設,企業網站建設,成都品牌網站建設,網絡營銷推廣,外貿營銷網站建設,金口河網站建設費用合理。
針對PHP的動靜分離,靜態頁面交給Nginx處理,動態頁面交給PHP-FPM模塊或Apache處理。
在Nginx的配置中,是通過location配置段配合正則匹配實現靜態與動態頁面的不同處理方式
其簡單示意圖如下:
首先是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數據庫
概述:
MariaDB數據庫管理系統是MySQL的一個分支,主要由開源社區在維護,采用GPL授權許可 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 //對數據庫進行設置
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與數據庫的關系
[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
創建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的動態網頁的,證明LAMP架構成功,下面就是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 //創建程序性用戶
[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/ //創建軟鏈接方便識別
[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服務
此時,我們通過測試機進行訪問,即可得到如下界面:
但是,若訪問php格式的動態網頁則會出現404訪問錯誤的問題
想要解決這個問題,就引入了下面的實驗,在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
至此,動靜分離就全部完成了。
實驗驗證
動態網頁
靜態網頁
文章名稱:實現apache與nginx之間的動靜分離
URL鏈接:http://newbst.com/article4/jhsoie.html
成都網站建設公司_創新互聯,為您提供企業網站制作、小程序開發、虛擬主機、ChatGPT、外貿網站建設、網站改版
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯