Apache和nginx实现动静分离

微笑、不失礼 提交于 2019-12-24 17:47:33

Apache和nginx实现动静分离

前言

通过之前的几篇博客,我们知道了Apache对动态网页的处理能力非常的强,然而对静态网页的处理就没有那么强了;nginx对静态网页的处理能力和处理高并发的能力非常强,然而,处理动态网页就没有Apache的处理能力强。所以,在线网当中,公司一般都是使用动静分离技术,来提高公司的业务处理。

实验环境

2台centos7虚拟机作为服务器

一台win10主机作为客户机

实验分析

根据LAMP架构和LNMP架构来看我们分为三个大步骤:LAMP架构、LNMP架构、动静分离

LNMP服务器主要就是nginx,用不到MySQL和PHP,所以,我们就可以简化一些步骤

LAMP架构、nginx服务器、动静分离

实验步骤

1.LAMP架构的搭建

1.1将服务器1的名字改成LAMP,表示这台服务器用来搭建LAMP服务

[root@web ~]# hostnamectl set-hostname LAMP
[root@web ~]# su
[root@lamp ~]# 

1.2安装LAMP架构的Apache服务,并对Apache进行相应的优化

[root@lamp ~]# yum -y install httpd httpd-devel
[root@lamp ~]# rpm -q httpd
httpd-2.4.6-90.el7.centos.x86_64
[root@lamp ~]# rpm -q httpd-devel
httpd-devel-2.4.6-90.el7.centos.x86_64
[root@lamp ~]# systemctl start httpd
[root@lamp ~]# netstat -ntap | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      3038/httpd
[root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@lamp ~]# firewall-cmd --reload
success

1.3安装LAMP架构的mysql服务,并对mysql进行相应的优化

[root@lamp ~]# yum -y intsall mariadb mariadb-server mariadb-libs mariadb-devel
[root@lamp ~]# systemctl start mariadb
[root@lamp ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):  //回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y    //设置root用户登录的密码
New password:                  //密码为abc123
Re-enter new password:         //确认密码为abc123
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n         //是否删除匿名用户,我们不删除
 ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n     //不允许root用户远程登录,我们允许远程登录
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n //是否删除测试数据库和访问数据库,选择否
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y   //是否重置数据库的权限
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

1.4安装LAMP架构的PHP服务,并对PHP服务进行优化

[root@lamp ~]# yum -y install php
[root@lamp ~]# yum -y install php-mysql
[root@lamp ~]# 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
[root@lamp ~]# rpm -q php
php-5.4.16-46.1.el7_7.x86_64
[root@lamp ~]# rpm -q php-mysql
php-mysql-5.4.16-46.1.el7_7.x86_64
[root@lamp ~]# rpm -q php-gd
php-gd-5.4.16-46.1.el7_7.x86_64
[root@lamp ~]# rpm -q php-odbc
php-odbc-5.4.16-46.1.el7_7.x86_64
[root@lamp ~]# rpm -q php-pear
php-pear-1.9.4-21.el7.noarch
[root@lamp ~]# rpm -q php-xml
php-xml-5.4.16-46.1.el7_7.x86_64
[root@lamp ~]# rpm -q php-xmlrpc
php-xmlrpc-5.4.16-46.1.el7_7.x86_64
[root@lamp ~]# rpm -q php-mbstring
php-mbstring-5.4.16-46.1.el7_7.x86_64
[root@lamp ~]# rpm -q php-snmp
php-snmp-5.4.16-46.1.el7_7.x86_64
[root@lamp ~]# rpm -q php-soap
php-soap-5.4.16-46.1.el7_7.x86_64
[root@lamp ~]# rpm -q curl
curl-7.29.0-54.el7_7.1.x86_64
[root@lamp ~]# rpm -q php-bcmath
php-bcmath-5.4.16-46.1.el7_7.x86_64
[root@lamp ~]# rpm -q libcurl-devel
libcurl-devel-7.29.0-54.el7_7.1.x86_64
[root@lamp ~]# cd /var/www/html/
[root@lamp html]# ls
[root@lamp html]# vim index.php
<?php
phpinfo();
?>

1.5重启服务,并在win10主机上面查看

[root@lamp html]# systemctl restart httpd
[root@lamp html]# netstat -ntap|grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      5941/httpd          
[root@lamp html]# systemctl restart mariadb
[root@lamp html]# netstat -ntap|grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      6172/mysqld 

在win10主机里面测试

在这里插入图片描述

测试没有问题,我们继续进行下一步。

2.nginx服务器的搭建

2.1先将nginx的源码包解压到/opt/目录下面,然后安装必要的软件包,修改主机名为nginx

[root@nginx ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  LNMP-C7  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@nginx ~]# cd LNMP-C7/
[root@nginx LNMP-C7]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gz  php-7.1.20.tar.bz2
mysql-boost-5.7.20.tar.gz  php-5.6.11.tar.bz2   php-7.1.20.tar.gz
ncurses-5.6.tar.gz         php-7.1.10.tar.bz2   zend-loader-php5.6-linux-x86_64_update1.tar.gz
[root@nginx LNMP-C7]# tar -zxvf nginx-1.12.2.tar.gz -C /opt/
[root@nginx LNMP-C7]# useradd -M -s /sbin/nologin nginx  //创建程序管理用户
[root@nginx LNMP-C7]# yum -y install pcre-devel zlib-devel gcc gcc-c++ pcre make
[root@nginx LNMP-C7]# rpm -q gcc
gcc-4.8.5-39.el7.x86_64
[root@nginx LNMP-C7]# rpm -q gcc-c++
gcc-c++-4.8.5-39.el7.x86_64
[root@nginx LNMP-C7]# rpm -q pcre
pcre-8.32-17.el7.x86_64
[root@nginx LNMP-C7]# rpm -q pcre-devel
pcre-devel-8.32-17.el7.x86_64
[root@nginx LNMP-C7]# rpm -q make
make-3.82-24.el7.x86_64
[root@nginx LNMP-C7]# rpm -q zlib
zlib-1.2.7-18.el7.x86_64

2.2开始手工编译安装nginx

[root@nginx LNMP-C7]# cd /opt
[root@nginx opt]# ls
nginx-1.12.2  rh
[root@nginx opt]# cd nginx-1.12.2/
[root@nginx nginx-1.12.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[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

2.3对nginx进行优化

[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 
# 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)
          ;;
  rstart)
      $0 stop
          $0 start
          ;;
  reload)
      kill -s HUP $(cat $PIDF)
          ;;
  *)
      echo "Usage: $0 {start|stop|restart|reload}"
      exit 1
esac
exit 0
[root@nginx nginx-1.12.2]# cd /etc/init.d/
[root@nginx init.d]# ls
functions  netconsole  network  nginx  README
[root@nginx init.d]# chmod +x nginx 
[root@nginx init.d]# ls
functions  netconsole  network  nginx  README
[root@nginx init.d]# chkconfig --add nginx
[root@nginx init.d]# chkconfig --level 35 nginx on
[root@nginx init.d]# service nginx start
[root@nginx init.d]# systemctl stop firewalld
[root@nginx init.d]# setenforce 0

在win10主机里面测试nginx主机

在这里插入图片描述

3.动静分离

[root@nginx init.d]# vim /usr/local/nginx/conf/nginx.conf
        location ~ \.php$ {
            proxy_pass   http://192.168.73.144;
        }
[root@nginx init.d]# service nginx stop
[root@nginx init.d]# service nginx start

测试动静分离的结果

在这里插入图片描述

实验总结

通过动静分离技术,我们可以看到资源的利用率和吞吐量有了一个质的提升。目前线网中,都采用了动静分离技术

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!