部署Nginx+Apache动静分离详解


前言

  • nginx的静态处理能力很强,但是动态处理能力不足,因此在企业中常采用动静分离技术
  • 针对PHP,将静态页面交给nginx处理,动态页面交给PHP-FPM模块或Apache处理
  • 在nginx的配置文件中,是通过location配置段配合正则匹配来实现静态与动态页面的不同处理方式
  • 在企业信息化应用环境中,服务器的安全性和响应速度需要根据实际情况进行相应参数配置,以达到最优的用户体验
  • 默认的nginx安装参数只能供最基本的服务,还需要调整如网页缓存时间,连接超时,网页压缩等相应参数,餐能发挥出服务器的最大作用

mark

1.1:什么是Nginx动静分离?

  • Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术

  • 针对PHP的动静分离

    静态页面交给 Nginx处理

    动态页面交给 PHP-FPM模块或 Apache处理

  • 在 Nginx的配置中,是通过 ocation配置段配合正则匹配实现静态与动态页面的不同处理方式

1.2:反向代理的原理

  • Nginx不仅能作为Web服务器,还具有反向代理、负载均衡和缓存的功能
  • Nginx通过 proxy模块实现将客户端的请求代理至上游服务器,此时ngInx与上游服务器的连接是通过http协议进行的
  • Nginx在实现反向代理功能时的最重要指令为proxy pass,它能够并能够根据UR、客户端参数或其它的处理逻辑将用户请求调度至上游服务器

1.3:Nginx实现动静分离最重要的是配置

1.3.1:需求

  • 根据需要,将配置 Nginx实现动静分离,对php页面的请求转发给LAMP处理,而静态页面交给Nginx处理,以实现动静分离

二:实验步骤

  • 假设并调用后端LAMP环境

  • 准备两台虚拟机一台作为Apache Web服务,另一台作为Nginx Web服务器

2.1:首先安装Apache服务

[root@tom03 ~]# sentenforce 0   #关闭核心防护
[root@tom03 ~]# iptables -F
[root@tom03 ~]# yum -y install httpd httpd-devel

2.2:设置防火墙权限并开启服务

[root@tom03 ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@tom03 ~]# firewall-cmd --permanent --zone=public --add-service=https
success
#重新加载防火墙
[root@tom03 ~]# firewall-cmd --reload
success
#开启服务
[root@tom03 ~]# systemctl start httpd

2.3:客户机测试:输入服务器IP进行访问

mark

客户机tom03 IP:20.0.0.42作为nginx架构

客户机20.0.0.43作为lamp架构

2.4:安装数据库

  • 安装mairadb

  • mariadb数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 mariadb的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品

[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
#重启服务
[root@tom03 ~]# systemctl start mariadb.service 
#查看端口
[root@tom03 ~]# netstat -ntap | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      85026/mysqld        
#安全配置向导
[root@tom03 ~]# mysql_secure_installation
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n]    y
下面设置密码
确认新密码
#是否删除弥明用户
Remove anonymous users? [Y/n]     n
#是否拒绝root远程登陆
Disallow root login remotely? [Y/n] 
#删除测试数据库
Remove test database and access to it? [Y/n]   n

Reload privilege tables now? [Y/n] y
  • 安装php
[root@tom03 ~]# yum -y install php
  • 安装php与mysql连接包
[root@tom03 ~]# yum -y install php-mysql
  • 安装环境包
yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-soap curl curl-devel php-bcmath
  • 配置php首页信息
[root@tom03 ~]# cd /var/www/html/
[root@tom03 html]# vim index.php
#填写
<?php
 phpinfo();
?>
#php要支持apache 要重启
[root@tom03 html]# systemctl restart httpd.service

客户机输入http://20.0.0.43/index.php

mark

2.5:安装nginx架构

切换到20.0.0.44主机

[root@localhost ~]# cd /opt
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz 
  • 创建程序用户
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
  • 安装环境
[root@localhost nginx-1.12.2]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
  • 进行编译安装
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module

#编译
[root@localhost nginx-1.12.2]# make 
[root@localhost nginx-1.12.2]# make install
  • 优化路径
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/
  • 创建启动脚本
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
   $PROG
   ;;
  stop)
   kill -s QUIT $(cat $PIDF)
   ;;
  restart)
   $0 stop
   $0 start
   ;;
  reload)
   kill -s HUP $(cat $PIDF)
   ;;
  *)
                echo "Usage: $0 {start|stop|restart|reload}"
                exit 1
esac
exit 0

[root@localhost nginx-1.12.2]# chmod -x /etc/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add nginx
#重启服务
[root@localhost init.d]# service nginx restart
  • 安装网页验证
[root@localhost init.d]# yum install elinks -y
#重启服务
[root@localhost init.d]# service nginx start
[root@localhost init.d]# systemctl stop firewalld.service 
[root@localhost init.d]# setenforce 0
[root@localhost init.d]# elinks http://20.0.0.42/

mark
客户端进行访问测试
mark
mark

三:配置动静分离

3.1:编写Nginx配置文件

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf

mark

3.2:重启Nginx服务

[root@localhost init.d]# service nginx stop
[root@localhost init.d]# service nginx start
[root@localhost init.d]:

3.3:客户端测试 输入http://20.0.0.42/index.php

mark

本次实验结束 感谢观看

猜你喜欢

转载自blog.csdn.net/weixin_47151643/article/details/108053196