nginx动静分离配置部署

Nginx动静分离

一、概述

动态页面与静态页面区别
  • 静态资源: 当用户多次访问这个资源,资源的源代码永远不会改变的资源。
  • 动态资源:当用户多次访问这个资源,资源的源代码可能会发送改变。
什么是动静分离
  • 动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路
  • 动静分离简单的概括是:动态文件与静态文件的分离。
  • 伪静态:网站如果想被搜索引擎搜素到,动态页面静态技术freemarker等模版引擎技术
为什么要用动静分离
  • 在我们的软件开发中,有些请求是需要后台处理的(如:.jsp,.do等等),有些请求是不需要经过后台处理的(如:css、html、jpg、js等等文件),这些不需要经过后台处理的文件称为静态文件,否则动态文件。因此我们后台处理忽略静态文件。这会有人又说那我后台忽略静态文件不就完了吗。当然这是可以的,但是这样后台的请求次数就明显增多了。在我们对资源的响应速度有要求的时候,我们应该使用这种动静分离的策略去解决。
  • 动静分离将网站静态资源(HTML,JavaScript,CSS,img等文件)与后台应用分开部署,提高用户访问静态代码的速度,降低对后台应用访问。这里我们将静态资源放到nginx中,动态资源转发到tomcat服务器中。
  • 因此,动态资源转发到tomcat服务器我们就使用到了前面讲到的反向代理了。

二、Nginx实现动静分离

主机IP需要安装的服务
nginx服务端192.168.10.201nginx
动态页面端192.168.10.202lnmp
静态页面端192.168.10.203httpd

服务安装

nginx服务端

nginx部署

//创建用户
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖包
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@nginx ~]# yum -y groups mark install 'Development Tools'

//编译安装nginx
[root@nginx ~]# ls
anaconda-ks.cfg  nginx-1.20.1.tar.gz
[root@nginx ~]# tar xf nginx-1.20.1.tar.gz 
[root@nginx ~]# cd nginx-1.20.1/
[root@nginx nginx-1.20.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@lnmp nginx-1.20.1]# make && make install

//修改日志文件属组
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx/

//添加环境变量并启动
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# bash
[root@nginx ~]# nginx 
[root@nginx ~]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process      
LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*
LISTEN      0           128                       [::]:22                     [::]:*
[root@nginx ~]#
动态页面端

部署lnmp

nginx

//创建用户
[root@lnmp ~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖包
[root@lnmp ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@lnmp ~]# yum -y groups mark install 'Development Tools'

//编译安装nginx
[root@lnmp ~]# ls
anaconda-ks.cfg  nginx-1.20.1.tar.gz
[root@lnmp ~]# tar xf nginx-1.20.1.tar.gz 
[root@lnmp ~]# cd nginx-1.20.1/
[root@lnmp nginx-1.20.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@lnmp nginx-1.20.1]# make && make install

//修改日志文件属组
[root@lnmp ~]# chown -R nginx.nginx /var/log/nginx/

//添加环境变量并启动
[root@lnmp ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp ~]# bash
[root@lnmp ~]# nginx 
[root@lnmp ~]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process      
LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*
LISTEN      0           128                       [::]:22                     [::]:*
[root@lnmp ~]#

mysql部署

//安装依赖包
[root@lnmp ~]# yum -y install ncurses-compat-libs

//创建用户
[root@lnmp ~]# useradd -M -r -s /sbin/nologin mysql

//解压并改名
[root@lnmp ~]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@lnmp ~]# mv /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/ /usr/local/mysql

//添加环境变量
[root@lnmp ~]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@lnmp ~]# bash

//创建数据存放目录并修改属组
[root@lnmp ~]# mkdir -p /opt/data
[root@lnmp ~]# chown -R mysql.mysql /usr/local/mysql/
[root@lnmp ~]# chown -R mysql.mysql /opt/data/

//初始化mysql
[root@lnmp ~]# mysqld --initialize --user=mysql --datadir=/opt/data
2021-11-01T07:03:59.250884Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-11-01T07:03:59.438276Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-11-01T07:03:59.488055Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-11-01T07:03:59.548403Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e33346e5-3ae1-11ec-b6f6-000c292100bb.
2021-11-01T07:03:59.549430Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-11-01T07:04:00.085196Z 0 [Warning] CA certificate ca.pem is self signed.
2021-11-01T07:04:00.238760Z 1 [Note] A temporary password is generated for root@localhost: yqaov,iQR6qf    //记住此密码
[root@lnmp ~]#

//写入配置文件
[root@lnmp ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

//配置启动脚本
[root@lnmp ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/usr/local/mysql/support-files/mysql.server stop

[Install]
WantedBy=multi-user.target
[root@lnmp ~]# systemctl daemon-reload 
[root@lnmp ~]# systemctl enable --now mysqld.service

//修改mysql的root用户密码
[root@lnmp ~]# mysql -uroot -p'yqaov,iQR6qf' -e "set password = password('123')" --connect-expired-password

php部署

//安装依赖包
[root@lnmp ~]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@lnmp ~]# yum -y install libxml2 libxml2-devel sqlite sqlite-devel libcurl-devel readline-devel libzip libzip-devel

//编译安装
[root@lnmp ~]# tar xf php-8.0.11.tar.gz 
[root@lnmp ~]# cd php-8.0.11/
[root@lnmp php-8.0.11]# ./configure --prefix=/usr/local/php  \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@lnmp php-8.0.11]# make && make install

//添加环境变量
[root@lnmp php-8.0.11]# echo "export PATH=/usr/local/php/bin:$PATH" > /etc/profile.d/php.sh
[root@lnmp php-8.0.11]# bash

//配置并启动php
[root@lnmp php-8.0.11]# cp php.ini-production /etc/php.ini
[root@lnmp php-8.0.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@lnmp php-8.0.11]# chmod +x /etc/init.d/php-fpm
[root@lnmp php-8.0.11]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@lnmp php-8.0.11]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@lnmp php-8.0.11]# service php-fpm start
Starting php-fpm  done

配置php页面

[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf
……………………略
		#access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;		##添加index.php
        }

……………………略

		# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
		location ~ \.php$ {													##取消注释
            root           html;											##取消注释
            fastcgi_pass   127.0.0.1:9000;									##取消注释
            fastcgi_index  index.php;										##取消注释
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;	##修改
            include        fastcgi_params;									##取消注释
        }
……………………略

[root@lnmp ~]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
静态页面端

apache部署

//直接用yum安装apache
[root@apache ~]# yum -y install httpd

//启动apache
[root@apache ~]# systemctl enable --now httpd
nginx服务端配置动静分离
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    #tcp_nopush     on;
##配置一下两段内容
    upstream static {
        server 192.168.10.203;    ##静态
    }

    upstream dynamic {
        server 192.168.10.202;    ##动态
    }
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
##配置一下两段内容
        location / {
            proxy_pass http://static;     ##访问静态资源找static
        }

        location ~ \.php$ {
            proxy_pass http://dynamic;    ##访问动态资源找dynamic
        }
        #error_page  404              /404.html;

[root@nginx ~]# nginx -s reload
浏览器访问测试

访问静态页面


访问动态页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值