成都创新互联网站制作重庆分公司

CentOS7系统配置Nginx服务+Apache动静分离(实战!)

Nginx动静分离介绍

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

    成都一家集口碑和实力的网站建设服务商,拥有专业的企业建站团队和靠谱的建站技术,十余年企业及个人网站建设经验 ,为成都近千家客户提供网页设计制作,网站开发,企业网站制作建设等服务,包括成都营销型网站建设,品牌网站建设,同时也为不同行业的客户提供成都做网站、成都网站设计的服务,包括成都电商型网站制作建设,装修行业网站制作建设,传统机械行业网站建设,传统农业行业网站制作建设。在成都做网站,选网站制作建设服务商就选成都创新互联公司

  • 静态页面交给Nginx处理,动态页面交给PHP-FPM模块或Apache处理;

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

搭建LAMP架构

为方便实验直接用yum安装,不用手工编译安装。用两台虚拟机,分别搭建LAMP架构和Nginx服务。

1.安装Apache服务

[root@localhost ~]# yum install httpd httpd-devel -y
.........//省略安装过程
[root@localhost ~]#

2.开启服务,配置Firewalld防火墙

[root@localhost ~]# systemctl start httpd.service   //开启服务
[root@localhost ~]# 
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http  //放通http服务
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https   //放通https服务
success
[root@localhost ~]# firewall-cmd --reload    //重载防火墙
success
[root@localhost ~]#

3.安装mariadb数据库

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

[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
...........//省略安装过程
[root@localhost ~]#

4.开启数据库服务

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

5.进行数据库设置

[root@localhost ~]# 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):    //给root管理员设定密码,直接回车
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   //是否设置,选择y
New password:    //输入新密码
Re-enter new password:    //再次输入新密码
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   //是否删除匿名用户,选择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用户远程登陆,选择n
 ... 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   //是否删除测试数据库,选择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   //是否加载权限列表,选择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!
[root@localhost ~]# 

6.安装PHP

[root@localhost ~]# yum -y install php
.......//省略过程
[root@localhost ~]#

7.安装PHP与MySQL的连接包

[root@localhost ~]# yum install php-mysql -y
........//省略过程
[root@localhost ~]#

8.安装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
........//省略过程
[root@localhost ~]#

9.测试php首页

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.php

[root@localhost html]#

CentOS7系统配置Nginx服务+Apache动静分离(实战!)

10.测试完毕,修改主页文件

[root@localhost html]# vim /var/www/html/index.php

[root@localhost html]# systemctl restart httpd.service 
[root@localhost html]# 

CentOS7系统配置Nginx服务+Apache动静分离(实战!)

搭建Nginx服务

1.修改主机名

[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# su
[root@nginx ~]# 

2.解压nginx源码包到“/opt/”目录

[root@nginx ~]# mkdir /mnt/tools
[root@nginx ~]# mount.cifs //192.168.100.50/tools /mnt/tools/
Password for root@//192.168.100.50/tools: 
[root@nginx ~]# cd /mnt/tools/LNMP/
[root@nginx LNMP]# ls
Discuz_X3.4_SC_UTF8.zip  mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.10.tar.bz2  php-7.1.20.tar.gz
[root@nginx LNMP]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
........//省略过程
[root@nginx LNMP]#

3.安装环境包

[root@nginx LNMP]# 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]# 
[root@nginx nginx-1.12.2]# yum install -y gcc gcc-c++ pcre-devel zlib-devel
.........//省略过程
[root@nginx nginx-1.12.2]#

3.创建nginx用户

[root@nginx nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@nginx nginx-1.12.2]# 

4.配置nginx服务,并编译安装

[root@nginx 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 && make install   //编译安装
.........//省略编译过程
[root@localhost nginx-1.12.2]# 

5.优化nginx服务的管理

[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  //优化nginx命令路径
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx    //制作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)
    ;;
  restart)
    $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]#
[root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx    //添加执行权限
[root@nginx nginx-1.12.2]# chkconfig --add nginx   //添加让service能够识别
[root@nginx nginx-1.12.2]# 

6.开启nginx服务

[root@nginx nginx-1.12.2]# service nginx start     //开启服务
[root@nginx nginx-1.12.2]# netstat -ntap | grep 80   //查看端口
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      58696/nginx: master 
[root@nginx nginx-1.12.2]#
[root@nginx nginx-1.12.2]# systemctl stop firewalld.service    //关闭防火墙
[root@nginx nginx-1.12.2]# setenforce 0
[root@nginx nginx-1.12.2]# 

7.安装elinks工具测试nginx网站

[root@nginx nginx-1.12.2]# yum install elinks -y
..........//省略安装过程
[root@nginx nginx-1.12.2]#
[root@nginx nginx-1.12.2]# elinks http://192.168.52.132/
[root@nginx nginx-1.12.2]#

CentOS7系统配置Nginx服务+Apache动静分离(实战!)

8.用宿主机测试nginx网站

CentOS7系统配置Nginx服务+Apache动静分离(实战!)

实现动静分离

1.在nginx服务配置文件中添加代理

[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
        location ~ \.php$ {
            proxy_pass   http://192.168.52.131;   //代理地址
        }
[root@nginx nginx-1.12.2]# service nginx stop 
[root@nginx nginx-1.12.2]# service nginx start 
[root@nginx nginx-1.12.2]# 

2.访问nginx服务的地址(静态资源)

CentOS7系统配置Nginx服务+Apache动静分离(实战!)

3.访问nginx服务地址(动态资源)

CentOS7系统配置Nginx服务+Apache动静分离(实战!)


分享题目:CentOS7系统配置Nginx服务+Apache动静分离(实战!)
路径分享:http://cxhlcq.com/article/ihdpcg.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部