服务器群集——Nginx+Apache动静分离

一、动静分离概述

动静分离是指在web服务器架构中,将静态页面与动态页面或者静态内容接口和动态内容接口分开不同系统访问的架构设计方法,进而提升整个服务访问性能和可维护性。

二、实验

实验目的:使用nginx搭建静态页面,Apache搭建动态页面,实现动静分离。
实验环境:主机1Apache服务器192.168.200.60,主机2nginx服务器
实验步骤:
1、配置主机1Apache服务器192.168.200.60

[root@localhost ~]# yum install -y httpd httpd-devel
[root@localhost ~]# systemctl start httpd
[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 ~]# systemctl start httpd
  浏览器访问192.168.200.60,成功
[root@localhost ~]# yum -y install mariadb mariadb-server mariadb-lib mariadb-devel   //安装数据库
[root@localhost ~]# systemctl start mariadb.service   //启动mariadb服务
[root@localhost ~]# netstat -antp | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      13789/mysqld

[root@localhost ~]# mysql_secure_installation                  //新建数据库
Enter current password for root (enter for none):             //回车
Set root password? [Y/n] y             //设置密码
New password:                                //abc123
Re-enter new password: 
Remove anonymous users? [Y/n] n      //不删除匿名用户
Disallow root login remotely? [Y/n] n   //不拒绝root用户远程登录
Remove test database and access to it? [Y/n] n   //不删除测试数据库
Reload privilege tables now? [Y/n] y          //是否重新加载权限列表 

[root@localhost ~]# yum -y install php    //安装PHP
[root@localhost ~]# yum -y install php-mysql    //安装php与mysql连接包
安装环境包,php的插件
[root@localhost ~]# 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-bcmach
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.php
<?php
  phpinfo();
?>
[root@localhost html]# systemctl restart httpd
win10网页访问192.168.200.60/index.php

[root@localhost html]# vim index.php
<?php
  echo "apache web !"
?>
[root@localhost html]# systemctl restart httpd
win10网页访问192.168.200.60/index.php显示apache web !

2、配置主机2nginx服务器

[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# su
[root@nginx ~]# cd /opt/
[root@nginx opt]# ls               //xshell拖入nginx压缩包
nginx-1.12.2.tar.gz
[root@nginx opt]# tar zxvf nginx-1.12.2.tar.gz -C  /opt/
[root@nginx nginx-1.12.2]# useradd -M -s /sbin/nologin nginx    //创建管理用户
[root@nginx nginx-1.12.2]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
[root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@nginx nginx-1.12.2]#make && make install

[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/     //路径优化
[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20                                           //一定要注意99前空格
# 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
  ;;
 reloard)
  kill -s HUP $(cat $PIDF)
  ;;
 *)
  echo "Usage:$0{start|stop|restart|reload}"
  exit 1
esac
exit 0
[root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx 

[root@nginx nginx-1.12.2]# chkconfig --add nginx
[root@nginx nginx-1.12.2]# yum -y install elinks
[root@nginx nginx-1.12.2]# service nginx start
[root@nginx nginx-1.12.2]# netstat -antp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      81741/nginx: master 
[root@nginx nginx-1.12.2]# systemctl stop firewalld.service     //关闭防火墙
[root@nginx nginx-1.12.2]# setenforce 0
[root@nginx nginx-1.12.2]# elinks 192.168.200.70      //文本登录网页,按q退出
[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
            proxy_pass   http://192.168.200.60;                   //开启动静分离,并指向lamp架构,lamp相当于fpm模块
        }
[root@nginx nginx-1.12.2]# service nginx stop
[root@nginx nginx-1.12.2]# service nginx start

使用win10 浏览器访问http://192.168.200.70/index.php显示apache web !
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值