一、动静分离概述
动静分离是指在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 !