部署Nginx+Apache动静分离

Nginx+Apache动静分离

反向代理

(1)Nginx不仅能作为web服务器,还具有反向代理、负载均衡和缓存的功能
(2)Nginx通过proxy模块实现将客户端的请求代理至上游服务器,此时nginx与上游服务器的连接是通过http协议进行的
(3)Nginx在实现反向代理功能时的最重要指令为proxy_pass,他能够根据URL、客户端参数及其它的处理逻辑将用户请求调至上游服务器。

配置Nginx实现动静分离

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

配置Apache服务

安装httpd服务

[root@localhost ~]# yum install httpd httpd-devel -y

启动httpd服务

[root@localhost ~]# systemctl start httpd.service

配置防火墙规则

[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

安装mariadb数据库和基本组件包

[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y

启动数据库

[root@localhost ~]# systemctl start  mariadb

配置数据库信息

[root@localhost ~]# mysql_secure_installation

部署Nginx+Apache动静分离_第1张图片
部署Nginx+Apache动静分离_第2张图片
部署Nginx+Apache动静分离_第3张图片
安装php服务包

[root@localhost ~]# yum -y install php

建立php和mysql关联

[root@localhost ~]# yum install php-mysql -y

安装php插件

[root@localhost ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath

创建php网页内容

[root@localhost ~]# cd /var/www
[root@localhost www]# cd html/
[root@localhost html]# ls
[root@localhost html]# vim index.php
<?php
  phpinfo();
?>

重启httpd服务

[root@localhost html]# systemctl restart httpd

在本机测试,查看搭建情况
部署Nginx+Apache动静分离_第4张图片

安装nginx服务

将包含nginx的文件包挂载到虚拟机上,并进入挂载文件夹,解压nginx文件包,加压到/opt/目录下

[root@localhost ~]# mount.cifs //192.168.100.3/LAMP8 /mnt
Password for root@//192.168.100.3/LAMP8:  
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.0.tar.gz  php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz
[root@localhost mnt]# tar zxvf nginx-1.12.0.tar.gz -C /opt

安装安装nginx服务的基本软件包

[root@localhost nginx-1.12.0]# yum -y install gcc gcc-c++ pcre-devel zlib-devel

创建用户,并编译安装环境

[root@localhost mnt]# useradd -M -s /usr/sbin/nologin nginx
[root@localhost mnt]# cd /opt/nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make && make install

创建命令文件

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

开启nginx服务

[root@localhost init.d]#nginx

进入nginx配置文件

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

部署Nginx+Apache动静分离_第5张图片
重启nginx服务

[root@localhost init.d]# skillall -s QUIT HUPnginx

关闭防火墙和安全功能

[root@localhost init.d]# systemctl stop firewalld.service 
[root@localhost init.d]# setenforce 0

在自己主机上测试

部署Nginx+Apache动静分离_第6张图片
部署Nginx+Apache动静分离_第7张图片

你可能感兴趣的:(Linux架构)