动静分离,通过中间件将动态请求和静态请求进行分离;可以减少不必要的请求消耗,同时能减少请求的延时。
通过中间件将动态请求和静态请求分离,逻辑图如下:
环境准备
| 主机名 | IP | 服务 | 系统 |
|---|---|---|---|
| nginx | 192.168.126.158 | nginx | centos7 |
| lnmp | 192.168.126.159 | lnmp架构 | centos8 |
| httpd | 192.168.126.160 | httpd | centos8 |
要求:用nginx1.20.1、php8.0.10
下载所需安装包关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld.service [root@localhost ~]# vim /etc/selinux/config [root@localhost ~]# setenforce 0 setenforce: SELinux is disabled
下载并解压nginx、mysql、php安装包
[root@localhost src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz [root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz [root@localhost src]# wget https://www.php.net/distributions/php-8.0.12.tar.xz [root@localhost src]# ls debug mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz php-8.0.12.tar.gz kernels nginx-1.20.1.tar.gz [root@localhost src]# tar xf nginx-1.20.1.tar.gz -C /usr/local/ [root@localhost src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@localhost src]# tar xf php-8.0.12.tar.xz -C /usr/local/ [root@localhost src]# cd /usr/local/ [root@localhost local]# ls bin games lib libexec nginx-1.20.1 sbin src etc include lib64 mysql-5.7.34-linux-glibc2.12-x86_64 php-8.0.12 share安装nginx
// 创建系统用户nginx [root@localhost local]# useradd -r -M -s /sbin/nologin nginx useradd:用户“nginx”已存在 // 安装依赖包 [root@localhost local]# yum -y install pcre-devel openssl openssl-devel gd-devel make gcc gcc-c++ [root@localhost local]# yum -y groups mark install 'Development Tools' // 创建日志存放目录 [root@localhost local]# mkdir -p /var/log/nginx [root@localhost local]# chown -R nginx.nginx /var/log/nginx // 编译安装 [root@localhost local]# cd nginx-1.20.1 [root@localhost 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@localhost nginx-1.20.1]# make && make install // 配置环境变量 [root@localhost nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh [root@localhost nginx-1.20.1]# . /etc/profile.d/nginx.sh安装mysql
// 创建MySQL用户
[root@localhost nginx-1.20.1]# useradd -r -M -s /sbin/nologin mysql
// 下载依赖包
[root@localhost nginx-1.20.1]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
// 创建软连接
[root@localhost local]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.34-linux-glibc2.12-x86_64/"
[root@localhost local]# ll
总用量 4
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
lrwxrwxrwx 1 root root 36 10月 27 11:19 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/
drwxr-xr-x 9 root root 129 10月 27 11:15 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x 6 root root 54 10月 27 11:19 nginx
drwxr-xr-x 9 1001 1001 186 10月 27 11:17 nginx-1.20.1
drwxrwxr-x 16 root root 4096 10月 19 18:34 php-8.0.12
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 10月 14 11:14 share
drwxr-xr-x. 3 root root 24 10月 21 10:53 src
// 修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll -d /usr/local/mysql
lrwxrwxrwx 1 mysql mysql 36 10月 27 11:19 /usr/local/mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/
// 添加环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# . /etc/profile.d/mysql.sh
[root@localhost local]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
// 创建数据存放目录
[root@localhost local]# mkdir /opt/data
[root@localhost local]# chown -R mysql.mysql /opt/data/
// 初始化数据存放目录
[root@localhost local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2021-10-27T03:20:34.201677Z 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:20:34.314736Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-27T03:20:34.334281Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-27T03:20:34.390630Z 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: d909c7a2-36d4-11ec-98c9-000c2962c3c2.
2021-10-27T03:20:34.391345Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-27T03:20:34.911166Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-27T03:20:35.077012Z 1 [Note] A temporary password is generated for root@localhost: syewyDsAF4>q <记住此密码>
// 配置mysql
[root@localhost local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@localhost local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost local]# ldconfig
// 生成配置文件
[root@localhost ~]# 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@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(basedir=).*#1/usr/local/mysql#g' /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).*#1/opt/data#g' /etc/init.d/mysqld
// 配置service文件
[root@localhost ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
[Install]
WantedBy=multi-user.target
// 重新加载
[root@localhost ~]# systemctl daemon-reload
// 启动
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
// 登录并更改密码
[root@localhost ~]# mysql -uroot -p
Enter password: ## 这里的密码是刚刚初始化数据库随机生成的那个密码
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
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('mkf');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
安装php
// 下载依赖包 [root@localhost ~]# 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@localhost]# cd /usr/local/php-8.0.12/ [root@localhost php-8.0.12]# ./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 | License: | | This software is subject to the PHP License, available in this | | distribution in the file LICENSE. By continuing this installation | | process, you are bound by the terms of this license agreement. | | If you do not agree with the terms of this license, you must abort | | the installation process at this point. | +--------------------------------------------------------------------+ Thank you for using PHP. [root@localhost php-8.0.12]# make && make install // 配置环境变量 [root@localhost php-8.0.12]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh [root@localhost php-8.0.12]# source /etc/profile.d/php.sh [root@localhost php-8.0.12]# which php /usr/local/php8/bin/php [root@localhost php-8.0.12]# php -v PHP 8.0.12 (cli) (built: Oct 27 2021 12:10:10) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.12, Copyright (c) Zend Technologies // 配置php-fpm [root@localhost php-8.0.12]# cp -f /usr/local/php-8.0.12/php.ini-production /etc/php.ini [root@localhost php-8.0.12]# cp /usr/local/php-8.0.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm -f [root@localhost php-8.0.12]# chmod +x /etc/init.d/php-fpm [root@localhost php-8.0.12]# cp -f /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf [root@localhost php-8.0.12]# cp -f /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf // 配置service文件 [root@localhost ~]# vim /usr/lib/systemd/system/php-fpm.service [Unit] Description=Php-fpm server daemon After=network.target [Service] Type=forking ExecStart=service php-fpm start ExecStop=service php-fpm stop [Install] WantedBy=multi-user.target // 重新加载 [root@localhost ~]# systemctl daemon-reload [root@localhost ~]# service php-fpm start Starting php-fpm done [root@localhost ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 5 127.0.0.1:25151 *:* LISTEN 0 128 127.0.0.1:9000 *:* LISTEN 0 5 *:873 *:* LISTEN 0 128 *:111 *:* LISTEN 0 5 192.168.122.1:53 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 5 :::873 :::* LISTEN 0 80 :::3306 :::* LISTEN 0 128 :::111 :::* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 128 ::1:631 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 128 :::443 :::* ## nginx配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
43 location / {
44 root html;
45 index index.php index.html index.htm; ##添加index.php
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;
70 include fastcgi_params;
71 }
72
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim index.php
[root@localhost html]# cat index.php
php配置
[root@localhost ~]# vim /usr/local/php8/etc/php-fpm.d/www.conf 22 ; will be used. 23 user = nginx ##改为nginx用户 24 group = nginx ##改为nginx组重启所有服务
[root@localhost ~]# nginx -s stop [root@localhost ~]# nginx [root@localhost ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@localhost ~]# service php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done访问测试 配置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.126.158;
37 }
38
39 upstream Static {
40 server 192.168.126.160;
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



