nginx动静分离

一、动静分离是什么

1. 概念

动静分离,通过中间件将动态请求和静态请求进行分离;可以减少不必要的请求消耗,同时能减少请求的延时。
通过中间件将动态请求和静态请求分离,逻辑图如下:
在这里插入图片描述

二、配置动静分离

1.1环境准备

主机名IP服务系统
lnmp192.168.47.169lnmp架构centos8
agent192.168.47.160nginxcentos7
httpd192.168.47.130httpdredhat8

1.2 部署lnmp

1.2.1 准备工作

// 关闭防火墙和selinux
[root@lnmp ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@lnmp ~]# vi /etc/selinux/config 
[root@lnmp ~]# setenforce 0

// 下载并解压nginx、mysql、php安装包
[root@lnmp ~]# cd /usr/src/
[root@lnmp src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
--2021-10-26 18:44: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”

nginx-1.20.1.tar 100%[=========>]   1.01M   560KB/s  用时 1.9s    

2021-10-26 18:44:03 (560 KB/s) - 已保存 “nginx-1.20.1.tar.gz” [1061461/1061461])
[root@lnmp src]# ls
debug                                       nginx-1.20.1.tar.gz
kernels                                     php-8.0.10.tar.xz
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

[root@lnmp src]# tar xf nginx-1.20.1.tar.gz -C /usr/local/
[root@lnmp src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@lnmp src]# tar xf php-8.0.10.tar.xz -C /usr/local/
[root@lnmp src]# cd /usr/local/
[root@lnmp local]# ls
bin      lib                                  nginx-1.20.1  src
etc      lib64                                php-8.0.10
games    libexec                              sbin
include  mysql-5.7.34-linux-glibc2.12-x86_64  share

1.2.2 安装nginx

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

// 安装依赖包
[root@lnmp ~]# yum -y install pcre-devel openssl openssl-devel gd-devel make 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

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

1.2.3 安装mysql

// 创建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 ~]# cd /usr/local/
[root@lnmp local]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.34-linux-glibc2.12-x86_64/'
[root@lnmp local]# ll
总用量 4
drwxr-xr-x.  2 root root    6 519 2020 bin
drwxr-xr-x.  2 root root    6 519 2020 etc
drwxr-xr-x.  2 root root    6 519 2020 games
drwxr-xr-x.  2 root root    6 519 2020 include
drwxr-xr-x.  2 root root    6 519 2020 lib
drwxr-xr-x.  3 root root   17 930 01:33 lib64
drwxr-xr-x.  2 root root    6 519 2020 libexec
lrwxrwxrwx   1 root root   36 1026 18:59 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/
drwxr-xr-x   9 root root  129 1026 18:46 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x   6 root root   54 1026 18:55 nginx
drwxr-xr-x   9 1001 1001  186 1026 18:51 nginx-1.20.1
drwxrwxr-x  16 root root 4096 824 23:40 php-8.0.10
drwxr-xr-x.  2 root root    6 519 2020 sbin
drwxr-xr-x.  5 root root   49 930 01:33 share
drwxr-xr-x.  2 root root    6 519 2020 src

// 修改目录/usr/local/mysql的属主属组
[root@lnmp local]# chown -R mysql.mysql /usr/local/mysql
[root@lnmp local]# ll -d /usr/local/mysql
lrwxrwxrwx 1 mysql mysql 36 1026 18:59 /usr/local/mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/

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

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

// 初始化数据存放目录
[root@lnmp ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2021-10-26T11:01:48.559986Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-10-26T11:01:48.695190Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-26T11:01:48.726168Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-26T11:01:48.781123Z 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: 1dd8e669-364c-11ec-9de0-000c29e9a550.
2021-10-26T11:01:48.781747Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-26T11:01:49.167292Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-26T11:01:49.351122Z 1 [Note] A temporary password is generated for root@lnmp: HqwG6gph!>4d    // 记住这个密码
[root@lnmp ~]# vim password
[root@lnmp ~]# cat password 
HqwG6gph!>4d

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

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

// 配置service文件
[root@lnmp ~]# 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@lnmp ~]# systemctl  daemon-reload

// 启动
[root@lnmp ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 

// 登录并更改密码
[root@lnmp ~]# 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('wjj');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye

1.2.4 安装php

// 下载依赖包
[root@lnmp ~]# 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 ~]# 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]# 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
........此处省略
+--------------------------------------------------------------------+
| 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@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 19:28:33) ( 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

// 配置service文件
[root@lnmp ~]# 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@lnmp ~]# systemctl  daemon-reload

// 启动
[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      80                 *:3306             *:*            
LISTEN 0      128             [::]:22            [::]:*      

1.2.5 配置nginx、php

// nginx配置
[root@lnmp ~]# 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@lnmp ~]# cd /usr/local/nginx/html/
[root@lnmp html]# vim index.php
[root@lnmp html]# cat index.php 
<?php 
  phpinfo(); 
?>

// php配置
[root@lnmp ~]# vim /usr/local/php8/etc/php-fpm.d/www.conf
 22 ;       will be used.
 23 user = nginx    ##改为nginx用户
 24 group = nginx   ##改为nginx组

// 重启所有服务
[root@lnmp ~]# nginx -s stop
[root@lnmp ~]# nginx
[root@lnmp ~]# service mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@lnmp ~]# service php-fpm restart 
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[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        127.0.0.1:9000       0.0.0.0:*            
LISTEN 0      80                 *:3306             *:*            
LISTEN 0      128             [::]:22            [::]:*        

1.2.6 访问测试

在这里插入图片描述

1.3部署nginx

// 关闭防火墙和selinux
[root@agent ~]# systemctl disable --now firewalld.service
[root@agent ~]# setenforce 0

// 安装epel源、vim、wget
[root@agent ~]# yum -y install epel-release vim wget

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

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

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

//下载nginx
[root@agent ~]# cd /usr/src/
[root@agent src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
--2021-10-25 18:34:46--  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
长度:980831 (958K) [application/octet-stream]
正在保存至: “nginx-1.20.1.tar.gz”

100%[=========================>] 980,831      498KB/s 用时 1.9s   

2021-10-25 18:34:48 (498 KB/s) - 已保存 “nginx-1.20.1.tar.gz” [980831/980831])

//编译安装
[root@agent src]# ls
debug  kernels  nginx-1.20.1.tar.gz
[root@agent src]# tar xf nginx-1.20.1.tar.gz
[root@agent src]# cd nginx-1.20.1
[root@agent 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@agent nginx-1.20.1]# make && make install

// nginx安装后配置
[root@agent ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@agent ~]# cat /etc/profile.d/nginx.sh 
export PATH=/usr/local/nginx/sbin:$PATH
[root@agent ~]# bash
//启动nginx
[root@agent ~]# nginx
[root@agent ~]# ss -antl
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                 :::*                  

1.4 安装httpd服务

//下载和解压httpd、apr以及apr-util
[root@httpd src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.49.tar.gz
[root@httpd src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@httpd src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@httpd src]# tar xf httpd-2.4.49.tar.gz 
[root@httpd src]# tar xf apr-util-1.6.1.tar.gz 
[root@httpd src]# tar xf apr-1.7.0.tar.gz 
[root@httpd apr-1.7.0]# vim configure
31878     cfgfile=${ofile}T
31879     trap "$RM \"$cfgfile\"; exit 1" 1 2 15
31880     # $RM "$cfgfile"    将此行加上注释,或者删除此行
31881 
31882     cat <<_LT_EOF >> "$cfgfile"


// 安装EPEL rpm[root@httpd apr-1.7.0]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
[root@httpd apr-1.7.0]# ls /etc/yum.repos.d/
epel-modular.repo     epel-testing-modular.repo  wjj.repo
epel-playground.repo  epel-testing.repo
epel.repo             redhat.repo
[root@httpd apr-1.7.0]# yum clean all    //清理缓存
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
33 文件已删除
[root@httpd apr-1.7.0]# yum makecache   //重新建立缓存
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Extra Packages for Enterprise Linux  563 kB/s | 955 kB     00:01    
Extra Packages for Enterprise Linux  616 kB/s |  10 MB     00:17    
BaseOS                               164 MB/s | 2.3 MB     00:00    
AppStream                            126 MB/s | 5.8 MB     00:00    
元数据缓存已建立。

// 安装开发工具包
[root@httpd apr-1.7.0]# yum groups mark install 'Development Tools' -y
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:0:01:26 前,执行于 2021年09月23日 星期四 17时49分10秒。
依赖关系解决。
=====================================================================
 软件包         架构          版本              仓库            大小
=====================================================================
安装组:
 Development Tools
                                                                    

事务概要
=====================================================================

完毕!

// 创建apache服务的用户和组
[root@httpd src]# useradd -r -M -s /sbin/nologin apache

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

// 编译安装apr-1.7.0、apr-util-1.6.1、httpd-2.4.49
[root@httpd apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@httpd apr-1.7.0]# make && make install
[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 
20 MANDATORY_MANPATH                       /usr/man
21 MANDATORY_MANPATH                       /usr/share/man
22 MANDATORY_MANPATH                       /usr/local/share/man
23 MANDATORY_MANPATH                       /usr/local/apache/man     // 把apache加进去

//取消ServerName前面的注释
[root@httpd ~]# vim /usr/local/apache/conf/httpd.conf
203 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       80                     *:3306                *:*     

// 配置开机自启
[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 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@httpd ~]# systemctl status httpd.service 
● httpd.service - Httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; v>
   Active: active (running) since Thu 2021-09-23 18:47:11 CST; 10s a>
     Docs: man:httpd(8)

// 配置apache
## 启用代理模块
// 启用httpd的相关模块
[root@httpd ~]# vim /usr/local/apache/conf/httpd.conf
119 #LoadModule remoteip_module modules/mod_remoteip.so
120 LoadModule proxy_module modules/mod_proxy.so   // 取消注释
121 #LoadModule proxy_connect_module modules/mod_proxy_connect.so
122 #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
123 #LoadModule proxy_http_module modules/mod_proxy_http.so
124 LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so      // 取消注释
125 #LoadModule proxy_scgi_module modules/mod_proxy_scgi.so

1.5 开启服务

//httpd主机
[root@httpd ~]# systemctl start httpd
[root@httpd ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port     Peer Address:Port  
LISTEN  0       128            0.0.0.0:111           0.0.0.0:*     
LISTEN  0       128            0.0.0.0:80            0.0.0.0:*     
LISTEN  0       32       192.168.122.1:53            0.0.0.0:*     
LISTEN  0       128            0.0.0.0:22            0.0.0.0:*     
LISTEN  0       5            127.0.0.1:631           0.0.0.0:*     
LISTEN  0       128               [::]:111              [::]:*     
LISTEN  0       128               [::]:22               [::]:*     
LISTEN  0       5                [::1]:631              [::]:*  

//lnmp主机
[root@lnmp ~]# nginx
[root@lnmp ~]# systemctl start php-fpm.service 
[root@lnmp ~]# systemctl start mysqld.service
[root@lnmp ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port  Peer Address:Port Process 
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主机
[root@agent ~]# nginx
[root@agent ~]# ss -antl
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                 :::*           

1.6 修改agent主机配置文件

[root@agent ~]# vim /usr/local/nginx/conf/nginx.conf
 33     #gzip  on;
 ## 添加以下6行
 34     upstream static {
 35         server 192.168.47.130;    ## httpd的IP
 36     }
 37 
 38     upstream dynamic {
 39         server 192.168.47.169;   ## lnmp的IP
 40     }
 41 
 42     server {
 43         listen       80;
 44         server_name  localhost;
 45 
 46         #charset koi8-r;
 47 
 48         #access_log  logs/host.access.log  main;
 49 
 50         location / {
 51             #root   html;                   # 注释或删掉
 52             #index  index.html index.htm;   # 注释或删掉
 53             proxy_pass http://static;       #访问静态资源会自动跳转到进行访问
 54         }
 55         
 56         #error_page  404              /404.html;
 57 
 58         # redirect server error pages to the static page /50x.h    tml
 59         #
 60         error_page   500 502 503 504  /50x.html;
 61         location = /50x.html {
 62             root   html; 
 63         }
 64     
 65         # proxy the PHP scripts to Apache listening on 127.0.0.    1:80
 66         #
 ## 取消以下三行注释,并修改
 67         location ~ \.php$ {
 68             proxy_pass   http://dynamic;     #访问动态资源会自动跳转到进行访问
 69         }
[root@agent ~]# nginx -s reload
[root@agent ~]# 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

1.7 使用agent主机IP地址访问测试

1.7.1 访问静态资源

在这里插入图片描述

1.7.2 访问动态资源

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值