目 录CONTENT

文章目录

动静分离

ZiChen D
2021-10-30 / 0 评论 / 0 点赞 / 247 阅读 / 20,948 字 / 正在检测是否收录...

为什么要做动静分离?

Nginx是当下最热的Web容器,网站优化的重要点在于静态化网站,网站静态化的关键点则是是动静分离,动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们则根据静态资源的特点将其做缓存操作。

让静态的资源只走静态资源服务器,动态的走动态的服务器

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

  • 对于静态资源比如图片,js,css等文件,我们则在反向代理服务器nginx中进行缓存。这样浏览器在请求一个静态资源时,代理服务器nginx就可以直接处理,无需将请求转发给后端服务器tomcat。
  • 若用户请求的动态文件,比如servlet,jsp则转发给Tomcat服务器处理,从而实现动静分离。这也是反向代理服务器的一个重要的作用。

动静分离配置

环境介绍:

NAMEIP安装的服务
lnmp192.168.159.12lnmp
nginx192.168.159.10nginx
httpd192.168.159.11httpd

构建lnmp

//下载安装包
[root@lnmp ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@lnmp ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@lnmp ~]# wget https://www.php.net/distributions/php-8.0.10.tar.gz

//关闭防火墙
[root@lnmp ~]# systemctl disable --now firewalld
[root@lnmp ~]# setenforce 0
[root@lnmp ~]# cat /etc/sysconfig/selinux 
7 SELINUX=disabled
[root@lnmp ~]# reboot

//解压安装包
[root@lnmp ~]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@lnmp ~]# tar xf nginx-1.20.1.tar.gz -C /usr/local/
[root@lnmp ~]# tar xf php-8.0.10.tar.gz -C /usr/local/

编译nginx

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

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

//创建日志存放目录并修改属主
[root@lnmp ~]# mkdir -p /var/log/nginx
[root@lnmp ~]# chown -R nginx.nginx /var/log/nginx

//编译安装nginx
[root@lnmp ~]# cd /usr/local/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 nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp nginx-1.20.1]# . /etc/profile.d/nginx.sh

//启动nginx
[root@lnmp nginx-1.20.1]# nginx
[root@lnmp nginx-1.20.1]# ss -anlt
State       Recv-Q Send-Q                                                   Local Address:Port                                                                  Peer Address:Port              
LISTEN      0      128                                                                  *:80                                                                               *:*                  
LISTEN      0      128                                                                  *:22                                                                               *:*                  
LISTEN      0      100                                                          127.0.0.1:25                                                                               *:*                  
LISTEN      0      128                                                                 :::22                                                                              :::*                  
LISTEN      0      100                                                                ::1:25                                                                              :::*

编译mysql

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

//下载依赖包
[root@lnmp ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs

//软链接
[root@lnmp ~]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.34-linux-glibc2.12-x86_64/"

//修改mysql的属主
[root@lnmp local]# chown -R mysql.mysql /usr/local/mysql
[root@lnmp local]# ll
lrwxrwxrwx   1 mysql mysql        36 10月 27 11:26 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/

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

//创建数据存放目录
[root@lnmp local]# mkdir /opt/data
[root@lnmp local]# chown -R mysql.mysql /opt/data/

//初始化
[root@lnmp local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2021-10-27T03:34:41.828146Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-10-27T03:34:42.048489Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-27T03:34:42.089923Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-27T03:34:42.151286Z 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: d257c91b-36d6-11ec-8604-000c29057182.
2021-10-27T03:34:42.152827Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-27T03:34:42.889509Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-10-27T03:34:42.889524Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-10-27T03:34:42.890141Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-27T03:34:43.216315Z 1 [Note] A temporary password is generated for root@localhost: rx*t>>UsK9z.

//配置mysql
[root@lnmp local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@lnmp local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@lnmp local]# ldconfig

//生成配置文件
[root@lnmp local]# cat /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 local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lnmp local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@lnmp local]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

//启动mysql并修改密码
[root@lnmp local]# service mysqld start
Starting MySQL.Logging to '/opt/data/lnmp.err'.
 SUCCESS! 
[root@lnmp local]# mysql -uroot -p
Enter password: rx*t>>UsK9z.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.34

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('1');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye

编译PHP

//下载依赖包
[root@lnmp local]# yum -y install sqlite-devel libzip-devel libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel
[root@lnmp local]# 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 local]# cd /usr/local/php-8.0.10/
[root@lnmp php-8.0.10]# ./configure --prefix=/usr/local/php8 \
    --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.10]# make && make install

//安装后配置
[root@lnmp php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@lnmp php-8.0.10]# source /etc/profile.d/php.sh 
[root@lnmp php-8.0.10]# which php
/usr/local/php8/bin/php
[root@lnmp php-8.0.10]# php -v
PHP 8.0.10 (cli) (built: Oct 26 2021 03:20:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies

//配置php-fpm
[root@lnmp php-8.0.10]# cp -f /usr/local/php-8.0.10/php.ini-production /etc/php.ini
[root@lnmp php-8.0.10]# cp /usr/local/php-8.0.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm -f
[root@lnmp php-8.0.10]# chmod +x /etc/init.d/php-fpm 
[root@lnmp php-8.0.10]# cp -f /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@lnmp php-8.0.10]# cp -f /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

//启动
[root@lnmp php-8.0.10]# service php-fpm start
Starting php-fpm  done
[root@lnmp php-8.0.10]# ss -antl
State    Recv-Q   Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128                0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128              127.0.0.1:9000            0.0.0.0:*                
LISTEN   0        128                0.0.0.0:80              0.0.0.0:*                
LISTEN   0        128                   [::]:22                 [::]:*                
LISTEN   0        80                       *:3306                  *:*

创建web访问页面

[root@lnmp ~]# cat /usr/local/nginx/html/index.php
<?php
   phpinfo();
?>

修改nginx配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
 41         #access_log  logs/host.access.log  main;
 42 
 43         location / {
 44             root   html;
 45             index  index.html index.php index.htm;	//在中间添加index.php
 46         }
 47 
 48         #error_page  404              /404.html;


63         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 64         #
 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;	//将/script改为/usr/local/nginx/html
 70             include        fastcgi_params;
 71         }

重启服务并访问:

[root@lnmp php-8.0.10]# nginx -s stop;nginx 
[root@lnmp php-8.0.10]# ss -anlt
State                     Recv-Q                    Send-Q                                       Local Address:Port                                         Peer Address:Port                    
LISTEN                    0                         128                                              127.0.0.1:9000                                              0.0.0.0:*                       
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                         80                                                       *:3306                                                    *:*                       
LISTEN                    0                         128                                                   [::]:22                                                   [::]:*

配置nginx

//创建系统用户nginx
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
useradd:用户“nginx”已存在

//安装依赖环境
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
完毕!

[root@nginx ~]# yum -y groups mark install 'Development Tools'
已加载插件:fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.cqu.edu.cn
 * epel: mirror.sabay.com.kh
 * extras: mirrors.dgut.edu.cn
 * updates: mirrors.aliyun.com
Marked install: Development Tools

//创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx

//下载nginx
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
--2021-10-26 11:29:00--  http://nginx.org/download/nginx-1.20.1.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1061461 (1.0M) [application/octet-stream]
正在保存至: “nginx-1.20.1.tar.gz”

100%[=======================================================================================================================================================>] 1,061,461    653KB/s 用时 1.6s   

2021-10-26 11:29:02 (653 KB/s) - 已保存 “nginx-1.20.1.tar.gz” [1061461/1061461])

//编译安装
[root@nginx src]# ls
debug  kernels  nginx-1.20.1.tar.gz
[root@nginx src]# tar xf nginx-1.20.1.tar.gz
[root@nginx src]# 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@nginx nginx-1.20.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

//配置环境变量
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# . /etc/profile.d/nginx.sh

//启动nginx
[root@nginx ~]# nginx
[root@nginx ~]# ss -anlt
LISTEN      0      128                                  :::22                                      :::*                  
LISTEN      0      100                                  ::1:25                                     :::*      
LISTEN      0      128                                  *:22                                       *:*         
LISTEN      0      128                                  *:80                                       *:*

image.png

配置httpd

[root@httpd src]# wget http://dlcdn.apache.org/httpd/httpd-2.4.49.tar.gz
--2021-09-23 06:02:19--  http://dlcdn.apache.org/httpd/httpd-2.4.49.tar.gz
正在解析主机 dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
正在连接 dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:9421895 (9.0M) [application/x-gzip]
正在保存至: “httpd-2.4.49.tar.gz”

httpd-2.4.49.tar.gz                       100%[=====================================================================================>]   8.99M  1.03MB/s  用时 9.0s    

2021-09-23 06:02:28 (1022 KB/s) - 已保存 “httpd-2.4.49.tar.gz” [9421895/9421895])

//下载和安装apr以及apr-util
[root@httpd src]# wget http://dlcdn.apache.org/apr/apr-1.7.0.tar.gz
--2021-09-23 06:02:44--  http://dlcdn.apache.org/apr/apr-1.7.0.tar.gz
正在解析主机 dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
正在连接 dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1093896 (1.0M) [application/x-gzip]
正在保存至: “apr-1.7.0.tar.gz”

apr-1.7.0.tar.gz                          100%[=====================================================================================>]   1.04M  1.06MB/s  用时 1.0s    

2021-09-23 06:02:45 (1.06 MB/s) - 已保存 “apr-1.7.0.tar.gz” [1093896/1093896])

[root@httpd src]# wget http://dlcdn.apache.org/apr/apr-util-1.6.1.tar.gz
--2021-09-23 06:02:53--  http://dlcdn.apache.org/apr/apr-util-1.6.1.tar.gz
正在解析主机 dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
正在连接 dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:554301 (541K) [application/x-gzip]
正在保存至: “apr-util-1.6.1.tar.gz”

apr-util-1.6.1.tar.gz                     100%[=====================================================================================>] 541.31K  1.44MB/s  用时 0.4s    

2021-09-23 06:02:54 (1.44 MB/s) - 已保存 “apr-util-1.6.1.tar.gz” [554301/554301])

[root@httpd apr-1.7.0]# yum -y install epel-release
[root@httpd apr-1.7.0]# ls /etc/yum.repos.d/
epel-modular.repo     epel.repo                  epel-testing.repo  dzc.repo
epel-playground.repo  epel-testing-modular.repo  redhat.repo

[root@httpd src]# tar xf apr-1.7.0.tar.gz 
[root@httpd src]# tar xf apr-util-1.6.1.tar.gz 
[root@httpd src]# tar xf httpd-2.4.49.tar.gz 
[root@httpd src]# ls
apr-1.7.0         apr-util-1.6.1         debug         httpd-2.4.49.tar.gz
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.49  kernels

//安装开发工具包
[root@httpd apr-1.7.0]# yum -y groups mark install 'Development Tools'

//安装依赖包
[root@httpd ~]#  yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make

[root@httpd src]# cd apr-1.7.0/
[root@httpd apr-1.7.0]# vim configure
    cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"  //删掉或注释掉此行

//配置    
[root@httpd apr-1.7.0]# ./configure --prefix=/usr/local/apr
//编译安装
[root@httpd apr-1.7.0]# make && make install

[root@httpd apr-1.7.0]# cd /usr/src/apr-util-1.6.1
[root@httpd apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[root@httpd apr-util-1.6.1]# make && make install

[root@httpd httpd-2.4.49]# ./configure --prefix=/usr/local/apache \
> --enable-so \
> --enable-ssl \
> --enable-cgi \
> --enable-rewrite \
> --with-zlib \
> --with-pcre \
> --with-apr=/usr/local/apr \
> --with-apr-util=/usr/local/apr-util/ \
> --enable-modules=most \
> --enable-mpms-shared=all \
> --with-mpm=prefork

[root@httpd httpd-2.4.49]#  make && make install

[root@httpd ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@httpd ~]# source /etc/profile.d/httpd.sh 

[root@httpd ~]# ln -s /usr/local/apache/include/ /usr/include/httpd

[root@httpd ~]# vim /etc/man_db.conf
#
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/apache/man

//去掉注释
[root@httpd ~]# vim /usr/local/apache/conf/httpd.conf 
ServerName www.example.com:80 

//启动apache
[root@httpd ~]# apachectl start
[root@httpd ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   

LISTEN    0         128                      *:80                    *:*  


[root@httpd ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/httpd.service
[root@httpd ~]# vim /usr/lib/systemd/system/httpd.service 
[root@httpd ~]# cat /usr/lib/systemd/system/httpd.service 
[Unit]
Description=Httpd server daemon
Documentation=man:httpd(8)
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

[root@httpd ~]# systemctl daemon-reload 
[root@httpd ~]# apachectl stop    
[root@httpd ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

配置动静分离

//修改Nginx配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
 35     upstream Dynamic {
 36         server 192.168.159.12;
 37     }
 38 
 39     upstream Static {
 40         server 192.168.159.11;
 41     }
 42 
 43     server {
 44         listen       80;
 45         server_name  localhost;


 51        #location / {		#注释掉
 52         #    root   html;		#注释掉
 53         #    index  index.html index.htm;		#注释掉
 54         #}		#注释掉
 55 
 56         location ~ \.php$ {		# 添加
 57             proxy_pass http://Dynamic;		# 添加
 58         }		# 添加
 59         
 60         location / {		# 添加
 61             proxy_pass http://Static;		# 添加
 62         }		# 添加

[root@nginx ~]# nginx -s reload

成功!

0

评论区