部署Nginx+Apache动静分离的实例详解

部署Nginx+Apache动静分离的实例详解,第1张

部署Nginx+Apache动静分离的实例详解

Ngx不仅可以作为Web服务器,还具有反向代理、三层交换和缓存文件的功能。详细介绍了部署NginxApache进行动静分离的案例代码,朋友们一定要参考。

NGINX动静分离详细介绍

Nginx解决静态数据的能力很强,但解决动态数据的能力不足。所以公司普遍采用动静分离的技术
,从PHP中分离静态和动态数据。

  • 静态网页交给Nginx解决。
  • 动态网页交给PHP-FPM控制模块或Apache来解决。
  • 在Nginx的配置中,根据位置配置段的规则匹配,对静态数据和动态网页是不同的处理方式。

    反向代理的基本原理

    Nginx不仅可以作为Web服务器,还具有反向代理、三层交换和缓存文件的功能

    Nginx根据代理控制模块,完成移动客户端对上下游服务器请求的代理。此时,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和cmd,让它很容易成为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 <?php echo"thisisapachetestweb"; ?> [root@localhosthtml]#systemctlrestarthttpd

    ⑦浏览测试,输入网址http://192.168.150.214/index.PHP。

    2.编译器安装nginx

    ①安装系统升级软件。

    [root@localhost~]#yuminstallgccgcc-cpcre-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/ [[email protected]]#./configure\ >--prefix=/usr/local/nginx\ >--user=nginx\ >--group=nginx\ >--with-http_stub_status_module [[email protected]]#make&&makeinstall [[email protected]]#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~]#chmodx/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

    ⑦浏览检测


    摘要

    以上是我给大家详细介绍的部署NginxApache的案例的详细描述。我期待对你有所帮助。如果你有所有感兴趣的人帮我在留言板上留言,我会立即回复你。在此,我也要感谢大家对你们网站的申请!
    如果觉得文章对你有帮助,热烈欢迎删减,请注明出处,谢谢!

    欢迎分享,转载请注明来源:内存溢出

    原文地址: https://outofmemory.cn/zz/774691.html

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    上一篇 2022-05-03
    下一篇 2022-05-03

    发表评论

    登录后才能评论

    评论列表(0条)

    保存