Nginx+LAMP构建动静分离


动静分离是根据一定规则把静态文件(html、css、js、jpg等)和动态文件(后台应用)区分开来,采用静态文件和动态文件分开部署,以提高用户访问静态文件的速度,降低对后台应用的访问,提高服务器响应速度和性能。静态文件有Nginx服务器处理,直接获取磁盘文件,动态文件转发到应用服务器中处理。

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

针对PHP的动静分离
静态页面交给Nginx处理
动态页面交给PHP-FPM模块或Apache处理

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

两台服务器做动静分离

一台搭建LAMP,IP地址为192.168.110.15
一台搭建Nginx,IP地址为192.168.110.10

搭建LAMP架构

[root@localhost ~]# yum install httpd httpd-devel -y
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https
[root@localhost ~]# firewall-cmd --reload
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
[root@localhost ~]# systemctl start mariadb.service 
[root@localhost ~]# mysql_secure_installation
...
Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

Remove anonymous users? [Y/n] n
 ... skipping.

Disallow root login remotely? [Y/n] n
 ... skipping.

Remove test database and access to it? [Y/n] n
 ... skipping.


Reload privilege tables now? [Y/n] y
 ... Success!

[root@localhost ~]# yum install php -y
[root@localhost ~]# yum install php-mysql -y
[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 ~]# cd /var/www/html/
[root@localhost html]# vim index.php
<?php
  phpinfo();
?>
[root@localhost html]# systemctl restart httpd.service

在这里插入图片描述
更改首页
在这里插入图片描述

Nginx安装

[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# yum -y install gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib-devel \
pcre
[root@localhost 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]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx
# 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@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx 
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# yum install elinks.x86_64 -y
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# systemctl stop firewalld.service 
[root@localhost nginx-1.12.2]# setenforce 0
[root@localhost nginx-1.12.2]# elinks http://192.168.110.10

进入Nginx界面
按q退出
在这里插入图片描述
访问正常Ngnx
在这里插入图片描述
无法访问php
在这里插入图片描述

配置动静分离

[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
 #修改59-61行动静分离模板
location ~ \.php$ {
            proxy_pass   http://192.168.110.15;   #指向动态处理的服务器
}
[root@localhost nginx-1.12.2]# service nginx stop
[root@localhost nginx-1.12.2]# service nginx start

动态请求转向Apache服务器
在这里插入图片描述
静态请求还是Nginx处理
在这里插入图片描述

一台服务器基于不同端口做动静分离

192.168.110.10这台服务器搭建LAMP架构,Nginx做动静分离
80端口为Nginx静态页面,8080端口为Apache做动态页面

修改Apache主配置文件

[root@localhost opt]# vim /etc/httpd/conf/httpd.conf 
Listen 192.168.110.10:8080        #修改监听端口
#Listen 80
...
<IfModule dir_module>
    DirectoryIndex index.php     #指向php首页
</IfModule>
...
#AddType application/x-gzip .tgz
AddType application/x-httpd-php .php       
AddType application/x-httpd-php-source .phps
[root@localhost opt]# systemctl restart httpd

在这里插入图片描述
配置静态分离

[root@localhost nginx-1.12.2]# vim //usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
            proxy_pass   http://192.168.110.10:8080;     #指向Apache站点
        }
[root@localhost nginx-1.12.2]# service nginx stop
[root@localhost nginx-1.12.2]# service nginx start

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/CN_PanHao/article/details/108006874
今日推荐