您好,登錄后才能下訂單哦!
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
至此,動靜分離就全部完成了。
實驗驗證
動態網頁
靜態網頁
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。