温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Nginx与Apache——动静分离实操

发布时间:2020-07-11 08:44:06 来源:网络 阅读:24622 作者:wx5d3faba330584 栏目:系统运维

Nginx动静分离介绍
Nginx的静态处理能力很强,但是动态处理能力不足,因此在企业中常用动静分离技术
针对PHP的动静分离
1、静态页面交给Nginx处理
2、动态页面交给PHP-FPM模块或Apache处理
在Nginx的配置中,是通过location配置段配合正则匹配实现静态与动态页面的不同处理方式
Nginx反向代理原理
Nginx不仅能作为Web服务器,还具有反向代理、负载均衡和缓存的功能。Nginx通过proxy模块实现将客户端的请求代理至,上游服务器,此时nginx与.上游服务器的连接是通过http协议进行的。Nginx在实现反向代理功能时的最重要指令为proxy_ _pass, 它能够并能够根据URI、客户端参数或其它的处理逻辑将用户请求调度至.上游服务器。

实操

需求与架构:
根据企业需要,将配置Nginx实现动静分离,对php页面的请求转发给L AMP处理,而静态页面交给Nginx处理,以实现动静分离
Nginx与Apache——动静分离实操
Nginx与Apache——动静分离实操
一、架设并配置LAMP环境

yum install httpd httpd-devel -y
##使用yum安装架构

systemctl start httpd.service
##启动服务

[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 ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
##使用yum安装MYSQL数据库,mariadb数据库管理系统是MYSQL数据库的分支

[root@localhost ~]# systemctl start  mariadb
##启动数据库

[root@localhost ~]# mysql_secure_installation           ##设置数据库

Enter current password for root (enter for none): 
##此处但回车键

Set root password? [Y/n] y
##此处输入y已确定设置密码
New password: 
##输入密码abc123
Re-enter new password: 
##再次确认密码输入

Remove anonymous users? [Y/n] n
##输入n以否定移除所有匿名用户

Disallow root login remotely? [Y/n] n
##此处输入n以否定使用root身份远程登录

Remove test database and access to it? [Y/n] n
##此处输入n以否定删除测试数据库并访问它

Reload privilege tables now? [Y/n] y
##此处输入n以确定重载数据库

[root@localhost ~]# yum -y install php
##使用yum安装php

[root@localhost ~]# yum install php-mysql -y
##建立php和mysql关联

[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/html
##进入站点目录

[root@localhost html]# vim index.php
##编辑php网页

<?php
  phpinfo();
?>

[root@localhost html]# systemctl restart httpd.service 
##重启服务

二、访问192.168.235.137/index.php的Apache网页
Nginx与Apache——动静分离实操

[root@localhost ~]# cd /var/www/html
[root@localhost html]# vim index.php
##修改网页输出内容
<?php
  echo "apache web";
?>

二、安装配置Nginx服务

1、在Linux上使用远程共享获取来自Windows共享的源码包

[root@localhost ~]# smbclient -L //192.168.235.1/  
##远程共享访问
Enter SAMBA\root's password: 

                Sharename       Type      Comment
                ---------       ----      -------
                LNMP         Disk       
[root@localhost ~]# mkdir /abc              
[root@localhost ~]# mount.cifs //192.168.235.1/LNMP /abc 
##挂载到/abc目录下

2、解压源码包、安装编译需要的环境组件包

[root@localhost ~]# cd /abc    ##切换到挂载点目录
[root@localhost abc]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gz
mysql-boost-5.7.20.tar.gz  php-7.1.10.tar.gz
[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt   ##解压Nginx源码包到/opt下
[root@localhost abc]# cd /opt/    ##切换到解压的目录下
[root@localhost opt]# ls
nginx-1.12.2  rh
[root@localhost opt]# yum -y install \
gcc \                                       //c语言
gcc-c++ \                               //c++语言
pcre-devel \                     //pcre语言工具
zlib-devel                       //数据压缩函数库

3、创建程序用户nginx并编译Nginx

[root@localhost opt]# useradd -M -s /sbin/nologin nginx  ##创建程序用户,限定其
[root@localhost opt]# cd nginx-1.12.2/                 ##切换到nginx目录下
[root@localhost nginx-1.12.2]# ./configure \         ##配置nginx
> --prefix=/usr/local/nginx \        ##安装路径
> --user=nginx \                         ##用户名
> --group=nginx \                       ##用户组
> --with-http_stub_status_module     ##访问状态统计模块

4、编译和安装

[root@localhost nginx-1.12.2]#make && make install

5、优化Nginx服务启动脚本,并建立命令软链接

[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 
##创建软连接让系统识别nginx启动脚本
[root@localhost nginx]# nginx -t       ##检查配置文件的语法问题
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx]# nginx      ##开启ngnix
[root@localhost nginx]# netstat -ntap | grep 80     ##查看端口,nginx已经开启
tcp   0   0 0.0.0.0:80      0.0.0.0:*   LISTEN   39620/nginx: master 
[root@localhost nginx]# systemctl stop firewalld.service    ##关闭防火墙
[root@localhost nginx]# setenforce 0 
[root@localhost nginx]# nginx                         ##开启nginx 服务

6、systemctl管理nginx脚本

[root@localhost ~]# vim /lib/systemd/system/nginx.service      ##创建配置文件

[Unit]
Description=nginx                                            ##描述
After=network.target                                        ##描述服务类型
[Service]
Type=forking                                                    ##后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid            ##PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx              ##启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID    ##根据PID重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID       ##根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service     ##设置执行权限
[root@localhost ~]# systemctl stop nginx.service       ##关闭nginx 
[root@localhost ~]# systemctl start nginx.service       ##开启

7、修改Nginx.conf配置文件

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

 location ~ \.php$ {
            proxy_pass   http://192.168.235.137;
            ##解除此三行的注释,并将地址指向Nginx服务的IP地址
        }

[root@localhost ~]# systemctl stop nginx.service   
##停止服务

[root@localhost ~]# systemctl start nginx.service  
##启动服务
[root@localhost ~]# systemctl stop firewalld.service 
##关闭防火墙
[root@localhost ~]# setenforce 0
##关闭增强型安全功能

三、验证动静分离效果

访问192.168.235.158/index.html的Nginx静态网页
Nginx与Apache——动静分离实操
访问192.168.235.158/index.php的Nginx转发给LAMP的动态网页
Nginx与Apache——动静分离实操

即实现了动态网页和静态网页的分开浏览

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI