nginx—动静分离

本文详细介绍了如何在Linux系统中搭建LNMP(Linux + Nginx + MySQL + PHP)环境,并实现动静分离。通过创建独立的域名或使用Nginx代理,将静态文件与动态请求分开处理,提升网站性能。文中提供了详细的步骤,包括安装配置Nginx、MySQL、PHP以及修改配置文件实现静态资源和动态请求的分离。
摘要由CSDN通过智能技术生成

动静分离是什么

概念

动静分离,通过中间件将动态请求和静态请求进行分离;可以减少不必要的请求消耗,同时能减少请求的延时。
通过中间件将动态请求和静态请求分离,逻辑图如下:
在这里插入图片描述
动静分离从目前实现角度来讲大致分为两种。

第一种:纯粹把静态文件独立成单独的域名,放在独立的服务器上(主流推崇的方案);
第二种:动态跟静态文件混合在一起发布,通过 nginx 来分开。

为了加快网站的解析速度,我们可以把动态页面和静态页面交给不同的服务器来解析,来加快解析速度,提高请求的访问效率,降低原来单个服务器的压力。

主机名IP服务
nginx192.168.230.132nginx
agent192.168.153.131nginx
localhost192.168.153.139httpd

实例

准备工作
需要在lnmp架构下部署
#部署lnmp
#解压缩包及关闭防火墙
[root@localhost local]# ls
bin    include  libexec                         php-8.0.10.tar.xz  src
etc    lib      mysql-5.7.34-el7-x86_64.tar.gz  sbin
games  lib64    nginx-1.20.1.tar.gz             share
[root@localhost local]# tar xf nginx-1.20.1.tar.gz 
[root@localhost local]# tar xf mysql-5.7.34-el7-x86_64.tar.gz 
[root@localhost local]# tar xf php-8.0.10.tar.xz 
[root@localhost local]# ls /usr/local/
bin      libexec                         php-8.0.10.tar.xz
etc      mysql-5.7.34-el7-x86_64         sbin
games    mysql-5.7.34-el7-x86_64.tar.gz  share
include  nginx-1.20.1                    src
lib      nginx-1.20.1.tar.gz
lib64    php-8.0.10
[root@localhost local]# 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@localhost local]# vi /etc/selinux/config 
[root@localhost local]# setenforce 0

#nginx安装
#创建系统用户nginx
[root@localhost local]# useradd -r -M -s /sbin/nologin nginx
#安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ vim make
[root@localhost ~]# yum -y groups mark install 'Development Tools'
#创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx/

[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx/
[root@localhost ~]# cd /usr/local/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 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
[root@localhost ~]# nginx

#mysql安装
#创建MySQL用户及安装依赖包
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
#创建软连接
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin      libexec                         php-8.0.10.tar.xz
etc      mysql-5.7.34-el7-x86_64         sbin
games    mysql-5.7.34-el7-x86_64.tar.gz  share
include  nginx-1.20.1                    src
lib      nginx-1.20.1.tar.gz
lib64    php-8.0.10
[root@localhost local]# ln -sv mysql-5.7.34-el7-x86_64 mysql
'mysql' -> 'mysql-5.7.34-el7-x86_64'
[root@localhost local]# ll
总用量 724288
drwxr-xr-x.  2 root root         6 518 2020 bin
drwxr-xr-x.  2 root root         6 518 2020 etc
drwxr-xr-x.  2 root root         6 518 2020 games
drwxr-xr-x.  2 root root         6 518 2020 include
drwxr-xr-x.  2 root root         6 518 2020 lib
drwxr-xr-x.  3 root root        17 930 12:29 lib64
drwxr-xr-x.  2 root root         6 518 2020 libexec
lrwxrwxrwx.  1 root root        23 1026 07:21 mysql -> mysql-5.7.34-el7-x86_64

#修改目录/usr/local/mysql的属主属组
[root@localhost local]# ll -d /usr/local/mysql
lrwxrwxrwx. 1 mysql mysql 23 1026 07:21 /usr/local/mysql -> mysql-5.7.34-el7-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]# cat /etc/profile.d/mysql.sh 
export PATH=/usr/local/mysql/bin:$PATH
[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]# ll -d /opt/data/
drwxr-xr-x. 2 mysql mysql 6 1026 07:24 /opt/data/
#初始化数据存放目录
[root@localhost ~]# cd /usr/local/mysql/bin
[root@localhost bin]# mysqld --initialize --user=mysql --datadir=/opt/data/
2021-10-26T11:26:27.165801Z 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:26:27.502560Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-26T11:26:27.543492Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-26T11:26:27.548773Z 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: 8f42ef9f-364f-11ec-aa46-000c297af016.
2021-10-26T11:26:27.549855Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-26T11:26:27.997930Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-26T11:26:28.760242Z 1 [Note] A temporary password is generated for root@localhost: y*kZ*g3Fkx*m
#配置mysql及生成配置文件
[root@localhost bin]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@localhost bin]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost bin]# ldconfig 
#生成配置文件
[root@localhost ~]# vim /etc/my.cnf
[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

#开启mysql
[root@localhost ~]# ss -anlt
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                 [::]:22               [::]:*             
[root@localhost ~]# service mysqld start
Starting MySQL SUCCESS! 
[root@localhost ~]# ss -anlt
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                 [::]:22               [::]:*                
LISTEN   0        80                     *:3306                *:*              

#登录后设置密码
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
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> quit
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 ~]# 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@localhost]# cd /usr/local/php-8.0.10/
[root@localhost 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

config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| 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.10]# make && make install

#安装成功后进行配置
[root@localhost php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@localhost php-8.0.10]# source /etc/profile.d/php.sh 
[root@localhost php-8.0.10]# which php
/usr/local/php8/bin/php
[root@localhost 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@localhost php-8.0.10]# cp -f /usr/local/php-8.0.10/php.ini-production /etc/php.ini
[root@localhost php-8.0.10]# cp /usr/local/php-8.0.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm -f
[root@localhost php-8.0.10]# chmod +x /etc/init.d/php-fpm 
[root@localhost php-8.0.10]# cp -f /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost 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
#启动php
[root@localhost php-8.0.10]# service php-fpm start
Starting php-fpm  done
[root@localhost php-8.0.10]# ss -anlt
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                 [::]:22               [::]:*                
LISTEN   0        80                     *:3306                *:*              

#nginx配置过程

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

[root@localhost ~]# vim /usr/local/php8/etc/php-fpm.d/www.conf

; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = nginx   ##改为nginx用户
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
[root@localhost ~]# ss -anlt
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        128                 [::]:22               [::]:*                
LISTEN   0        80                     *:3306                *:*                

#每台主机开启服务,并关闭防火墙
[root@agent ~]# vi /etc/selinux/config
[root@agent ~]# 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@agent ~]# setenforce 0

#nginx和agent主机安装nginx(步骤一致,所以我就写了一遍)
[root@agent ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@agent ~]# yum -y groups mark install 'Development Tools'
#创建用户
[root@agent ~]# useradd -r -M -s /sbin/nologin nginx
[root@agent ~]# chown -R nginx.nginx /var/log/nginx
[root@agent local]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@agent src]# ls
debug  kernels  nginx-1.12.0.tar.gz
[root@agent src]# tar xf nginx-1.12.0.tar.gz
[root@agent src]# cd nginx-1.12.0
[root@agent nginx-1.12.0]# ./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.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@agent nginx-1.12.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@agent nginx-1.12.0]# . /etc/profile.d/nginx.sh
[root@agent nginx-1.12.0]# 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
[root@agent nginx-1.12.0]# nginx -v
nginx version: nginx/1.12.0
[root@agent nginx-1.12.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@agent nginx-1.12.0]# nginx
[root@agent nginx-1.12.0]# ps -ef | grep nginx
root       75578       1  0 20:48 ?        00:00:00 nginx: master process nginx
nginx      75579   75578  0 20:48 ?        00:00:00 nginx: worker process
root       75619   38505  0 20:48 pts/0    00:00:00 grep --color=auto nginx
[root@agent nginx-1.12.0]# ss -anlt
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           0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128              [::]:22             [::]:*            

#安装httpd
[root@localhost src ~]# wget http://dlcdn.apache.org/httpd/httpd-2.4.49.tar.gz
--2021-09-24 11:50:58--  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  2.83MB/s  用时 3.2s    

2021-09-24 11:51:01 (2.83 MB/s) - 已保存 “httpd-2.4.49.tar.gz” [9421895/9421895])

[root@localhost src ~]# wget http://dlcdn.apache.org/apr/apr-1.7.0.tar.gz
--2021-09-24 11:51:21--  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  2.03MB/s  用时 0.5s    

2021-09-24 11:51:22 (2.03 MB/s) - 已保存 “apr-1.7.0.tar.gz” [1093896/1093896])

[root@localhost src ~]# wget http://dlcdn.apache.org/apr/apr-util-1.6.1.tar.gz
--2021-09-24 11:51:31--  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.g 100%[=====================>] 541.31K  1.64MB/s  用时 0.3s    

2021-09-24 11:51:32 (1.64 MB/s) - 已保存 “apr-util-1.6.1.tar.gz” [554301/554301])

[root@localhost src ~]# tar xf apr-1.7.0.tar.gz 
[root@localhost src ~]# tar xf apr-util-1.6.1.tar.gz 
[root@localhost src ~]# tar xf httpd-2.4.49.tar.gz 
[root@localhost src ~]# ls
公共  文档  anaconda-ks.cfg   apr-util-1.6.1.tar.gz  lamp
模板  下载  apr-1.7.0         httpd-2.4.49
视频  音乐  apr-1.7.0.tar.gz  httpd-2.4.49.tar.gz
图片  桌面  apr-util-1.6.1    initial-setup-ks.cfg

#安装依赖包
[root@localhost src ~]# yum -y groups mark install 'Development Tools'
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:02:24 前,执行于 20210924日 星期五 120027秒。
依赖关系解决。
===================================================================================
 软件包             架构              版本                仓库                大小
===================================================================================
安装组:
 Development Tools                                                                

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

完毕!

[root@localhost src ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
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:03:18 前,执行于 20210924日 星期五 120027秒。
依赖关系解决。
===================================================================================
 软件包                   架构        版本                    仓库            大小
===================================================================================
安装:
 expat-devel              x86_64      2.2.5-3.el8             BaseOS          55 k
 gcc                      x86_64      8.3.1-5.el8             Appstream       23 M
 gcc-c++                  x86_64      8.3.1-5.el8             Appstream       12 M
 libtool                  x86_64      2.4.6-25.el8            Appstream      709 k
 make                     x86_64      1:4.2.1-10.el8          BaseOS         498 k
 openssl-devel            x86_64      1:1.1.1c-15.el8         BaseOS         2.3 M
 pcre-devel               x86_64      8.42-4.el8              BaseOS         551 k
安装依赖关系:
···

[root@localhost src ~]# cd apr-1.7.0/
[root@localhost apr-1.7.0]# vim configure

 cfgfile=${ofile}T
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"    #注释掉

#编译安装
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
Platform: x86_64-pc-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.7.0
checking for chosen layout... apr
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed

[root@localhost apr-1.7.0]# make && make install
···
/usr/bin/install -c -m 755 /root/apr-1.7.0/build/mkdir.sh /usr/local/apr/build-1
for f in make_exports.awk make_var_export.awk; do \
    /usr/bin/install -c -m 644 /root/apr-1.7.0/build/${f} /usr/local/apr/build-1; \
done
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr/build-1/apr_rules.mk
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr/bin/apr-1-config
#进行编译
[root@localhost ~]# cd /usr/src/apr-util-1.6.1/
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking for working mkdir -p... yes
APR-util Version: 1.6.1
checking for chosen layout... apr-util
checking for gcc... gcc

[root@localhost apr-util-1.6.1]# make && make install
···
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util/lib
/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util/bin/apu-1-config

[root@localhost apr-util-1.6.1]# cd /usr/src/httpd-2.4.49/
[root@localhost 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
checking for chosen layout... Apache
checking for working mkdir -p... yes
···
configure: summary of build options:

    Server Version: 2.4.49
    Install prefix: /usr/local/apache
    C compiler:     gcc
    CFLAGS:          -g -O2 -pthread  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

[root@localhost httpd-2.4.49]# make && make install
···
Installing build system files
mkdir /usr/local/apache/build
Installing man pages and online manual
mkdir /usr/local/apache/man
mkdir /usr/local/apache/man/man1
mkdir /usr/local/apache/man/man8
mkdir /usr/local/apache/manual
make[1]: 离开目录“/usr/src/httpd-2.4.49[root@localhost httpd-2.4.49]# 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@localhost httpd-2.4.49]# vim /usr/local/apache/conf/httpd.conf 

ServerName www.example.com:80

#启动apache查看是否开启
[root@localhost httpd-2.4.49]# apachectl start
[root@localhost httpd-2.4.49]# ss -anlt
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    
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                  0.0.0.0:111               0.0.0.0:*       
LISTEN    0         128                     [::]:22                   [::]:*       
LISTEN    0         5                      [::1]:631                  [::]:*       
LISTEN    0         128                     [::]:111                  [::]:*       
LISTEN    0         128                        *:80                      *:*   

[root@localhost ~]# vim /usr/lib/systemd/system/httpd.service
[root@localhost ~]# 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@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl status httpd.service 
● httpd.service - Httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset:>
   Active: inactive (dead)
     Docs: man:httpd(8)

924 12:32:30 localhost.localdomain systemd[1]: /usr/lib/systemd/system/httpd.se>
924 12:32:30 localhost.localdomain systemd[1]: /usr/lib/systemd/system/httpd.se>
924 12:32:30 localhost.localdomain systemd[1]: /usr/lib/systemd/system/httpd.se>
924 12:32:30 localhost.localdomain systemd[1]: /usr/lib/systemd/system/httpd.se>
924 12:32:30 localhost.localdomain systemd[1]: /usr/lib/systemd/system/httpd.se>
924 12:33:33 localhost.localdomain systemd[1]: /usr/lib/systemd/system/httpd.se>
lines 1-11/11 (END)
[root@localhost ~]# apachectl stop  
[root@localhost ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# systemctl status httpd.service 
● httpd.service - Httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: >
   Active: active (running) since Fri 2021-09-24 12:36:03 CST; 8s ago
     Docs: man:httpd(8)
  Process: 151521 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, st>
 Main PID: 151524 (httpd)
    Tasks: 6 (limit: 4704)
   Memory: 8.5M
   CGroup: /system.slice/httpd.service
           ├─151524 /usr/local/apache/bin/httpd -k start
           ├─151525 /usr/local/apache/bin/httpd -k start
           ├─151526 /usr/local/apache/bin/httpd -k start
           ├─151527 /usr/local/apache/bin/httpd -k start
           ├─151528 /usr/local/apache/bin/httpd -k start
           └─151529 /usr/local/apache/bin/httpd -k start
           
开启服务
[root@httpd ~]# systemctl start httpd
[root@httpd ~]# ss -anlt
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                 *:80                *:*            
LISTEN 0      128              [::]:22             [::]:*            

[root@lnmp ~]# nginx
[root@lnmp ~]# systemctl start php-fpm.service 
[root@lnmp ~]# systemctl start mysqld.service
[root@lnmp ~]# ss -anlt
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             [::]:*            

[root@agent ~]# nginx
[root@agent ~]# ss -anlt
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           0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128              [::]:22             [::]:*            

#修改agent主机配置文件
[root@agent ~]# vim /usr/local/nginx/conf/nginx.conf
......
    #gzip  on;
    upstream static {                   
        server 192.168.230.139;
    }
   
    upstream dynamic {             
        server 192.168.230.132;       
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://test;       #访问静态资源会自动跳转到进行访问
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        
        location ~ \.php$ {                            #取消注释
            proxy_pass   http://dynamic;               #访问动态资源会自动跳转到进行访问
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

......
[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
[root@agent ~]# nginx -s reload

使用agent主机IP地址访问测试
访问静态资源
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值