部署Nginx+Apache动静分离的实例详解
Nginx动静分离介绍
Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术
针对PHP的动静分离
- 静态页面交给Nginx处理
- 动态页面交给PHP-FPM模块或Apache处理
在Nginx的配置中,是通过location配置段配合正则匹配实现静态与动态页面的不同处理方式
反向代理原理
Nginx不仅能作为Web服务器,还具有反向代理、负载均衡和缓存的功能
Nginx通过proxy模块实现将客户端的请求代理至上游服务器,此时nginx与上游服务器的连接是通过http协议进行的
Nginx在实现反向代理功能时的最重要指令为proxy_pass,它能够并能够根据URI、客户端参数或其它的处理逻辑将用户请求调度至上游服务器
配置nginx实现动静分离
本案例根据企业需要,将配置Nginx实现动静分离,对php页面的请求转发给LAMP处理,而静态页面交给Nginx处理,以实现动静分离
架构如图所示
配置步骤
1、架设并调试后端LAMP环境
①安装Apache服务
[root@localhost~]#yuminstallhttpdhttpd-devel-y
②在防火墙设置http服务的权限
[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~]#systemctlstarthttpd
③安装mariadb
mariadb数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可mariadb的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品
[root@localhost~]#yuminstallmariadbmariadb-servermariadb-libsmariadb-devel-y [root@localhost~]#systemctlstartmariadb.service
④mysql安全配置向导
[root@localhost~]#mysql_secure_installation
⑤安装php及支持的软件
[root@localhost~]#yuminstallphp-y [root@localhost~]#yuminstallphp-mysql-y [root@localhost~]#yuminstallphp-gdphp-ldapphp-odbcphp-pearphp-xmlphp-xmlrpcphp-mbstringphp-snmpphp-soapcurlcurl-develphp-bcmath-y
⑥更改网页主页面
[root@localhost~]#cd/var/www/html [root@localhosthtml]#vimindex.php [root@localhosthtml]#systemctlrestarthttpd
⑦访问测试,输入网址http://192.168.150.214/index.php
2、编译安装nginx
①安装支持软件
[root@localhost~]#yuminstallgccgcc-c++pcre-develzlib-devel-y
②创建运行用户和组
[root@localhost~]#useradd-M-s/sbin/nologinnginx
③编译安装
[root@localhostLNMP-C7]#tarzxvfnginx-1.12.2.tar.gz-C/opt [root@localhostLNMP-C7]#cd/opt/nginx-1.12.2/ [root@localhostnginx-1.12.2]#./configure\ >--prefix=/usr/local/nginx\ >--user=nginx\ >--group=nginx\ >--with-http_stub_status_module [root@localhostnginx-1.12.2]#make&&makeinstall [root@localhostnginx-1.12.2]#ln-s/usr/local/nginx/sbin/nginx/usr/local/sbin
④服务管理控制
[root@localhost~]#vim/etc/init.d/nginx
#!/bin/bash
#chkconfig:-9920
#description:NginsServiceControlScript
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case"$1"in
start)
$PROG
;;
stop)
kill-sQUIT$(cat$PIDF)
;;
restart)
$0stop
$0start
;;
reload)
kill-sHUP$(cat$PIDF)
;;
*)
echo"Usage:$0{start|stop|restart|reload}"
exit1
esac
exit0
[root@localhost~]#chmod+x/etc/init.d/nginx
[root@localhost~]#chkconfig--addnginx
[root@localhost~]#servicenginxstart
⑤启动服务
[root@nginx~]#systemctlstopfirewalld.service [root@nginx~]#setenforce0 [root@nginx~]#servicenginxstart
⑥配置nginx处理动态页面请求
[root@nginx~]#vim/usr/local/nginx/conf/nginx.conf
location~\.php${
proxy_passhttp://192.168.150.214;
}
[root@nginx~]#servicenginxrestart
⑦访问测试
总结
以上所述是小编给大家介绍的部署Nginx+Apache动静分离的实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。