Nginx实现动静分离

目录

    • 环境:
    • DR上安装Nginx
    • Dynamic安装LNMP
    • Static安装httpd
    • 配置动静分离
    • 访问测试:

环境:

系统 IP 服务
CentOS8 动态 192.168.235.160 LNMP
CentOS8 静态 192.168.235.169 Nginx
Redhat8 调度器 DR 192.168.235.135 Httpd

关闭三台防火墙和selinux、修改主机名

DR上安装Nginx

//安装依赖包和工具包
[root@dr ~]#  yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@dr ~]# yum -y group mark install "Development Tools"

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

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

[root@dr ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
--2021-10-30 20:08:25--  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    629KB/s 用时 1.6s   

2021-10-30 20:08:27 (629 KB/s) - 已保存 “nginx-1.20.1.tar.gz” [1061461/1061461])

[root@dr ~]# tar xf nginx-1.20.1.tar.gz -C /usr/local/
[root@dr ~]# cd /usr/local/nginx-1.20.1/

//编译安装Nginx
[root@dr 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@dr nginx-1.20.1]# make && make install

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

[root@dr nginx-1.20.1]# /usr/local/nginx/sbin/nginx 
[root@dr nginx-1.20.1]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128          *:22                       *:*                                
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::*  

//设置nginx开机自启
[root@dr nginx-1.20.1]# cd /usr/lib/systemd/system/
[root@dr system]# cp sshd.service nginxd.service
[root@dr system]# vi nginxd.service
[root@dr system]# cat nginxd.service 
[Unit]
Description=Nginx server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
KillMode=process

[Install]
WantedBy=multi-user.target

[root@dr system]# /usr/local/nginx/sbin/nginx -s stop
[root@dr system]# systemctl daemon-reload
[root@dr system]# systemctl start nginxd.service
[root@dr system]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128          *:22                       *:*                        
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::*                  
[root@dr system]# 

访问测试:
Nginx实现动静分离_第1张图片

Dynamic安装LNMP

安装包

[root@localhost ~]# ls
php-8.0.10.tar.xz
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
nginx-1.20.1.tar.gz

解压

[root@localhost ~]# tar xf nginx-1.20.1.tar.gz -C /usr/local/
[root@localhost ~]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# tar xf php-8.0.10.tar.xz -C /usr/local/

关闭防火墙和seliunx

[root@localhost ~]# 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 ~]# setenforce 0
setenforce: SELinux is disabled

nginx安装

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

//安装依赖环境
[root@localhost ~]#  yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ 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 ~]# cd /usr/local/nginx-1.20.1
[root@localhost ~]# ./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
[root@localhost ~]# 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@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginxd.service
[root@localhost ~]# vim /usr/lib/systemd/system/nginxd.service
[root@localhost ~]# cat /usr/lib/systemd/system/nginxd.service
[Unit]
Description=nginx server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl enabled 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]# 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 5月  19 2020 bin
drwxr-xr-x.  2 root root    6 5月  19 2020 etc
drwxr-xr-x.  2 root root    6 5月  19 2020 games
drwxr-xr-x.  2 root root    6 5月  19 2020 include
drwxr-xr-x.  2 root root    6 5月  19 2020 lib
drwxr-xr-x.  3 root root   17 10月 19 05:03 lib64
drwxr-xr-x.  2 root root    6 5月  19 2020 libexec
lrwxrwxrwx   1 root root   36 10月 26 19:17 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root  129 10月 26 19:04 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x.  8 1001 1001  158 7月  11 2017 nginx-1.12.1
drwxr-xr-x   9 1001 1001  186 10月 26 19:13 nginx-1.20.1
drwxrwxr-x. 16 root root 4096 8月  24 23:40 php-8.0.10
drwxr-xr-x.  2 root root    6 5月  19 2020 sbin
drwxr-xr-x.  5 root root   49 10月 19 05:03 share
drwxr-xr-x.  2 root root    6 5月  19 2020 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月 26 19:17 /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 ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/

//配置mysql
[root@localhost ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# 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

//启动
[root@localhost ~]# service mysqld start 
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 
[root@localhost ~]# 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          80                         *:3306                    *:*                    
LISTEN     0          128                     [::]:22                   [::]:*     
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.34 MySQL Community Server (GPL)

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)

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
[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

//启动
[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     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@localhost ~]# 

修改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
 46         }

//取消注释并更改69行路径
 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         }



[root@localhost ~]# vim /usr/local/nginx/html/index.php
[root@localhost ~]# cat /usr/local/nginx/html/index.php


//重启nginx
[root@localhost ~]# nginx -s reload
[root@localhost ~]# 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实现动静分离_第2张图片

Static安装httpd

//下载工具包
[root@Static ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make wget vim
[root@Static ~]# yum -y  groups mark install 'Development Tools'

[root@Static ~]# useradd -r -M -s /sbin/nologin apache

//下载包
[root@Static ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@Static ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@Static ~]# wget https://downloads.apache.org/httpd/httpd-2.4.48.tar.gz
[root@Static ~]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.48.tar.gz   

//解压并编译安装apr
[root@Static ~]# tar xf apr-1.7.0.tar.gz -C /usr/local/
[root@Static ~]# tar xf apr-util-1.6.1.tar.gz  -C /usr/local/
[root@Static ~]# tar xf httpd-2.4.48.tar.gz  -C /usr/local/
[root@Static ~]# cd /usr/local/apr-1.7.0/
[root@Static apr-1.7.0]# vim configure

31879     trap "$RM \"$cfgfile\"; exit 1" 1 2 15
31880 #    $RM "$cfgfile"    //删除或注释掉此行

[root@Static apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@Static apr-1.7.0]# make && make install

//编译安装apr-util
[root@Static local]# cd apr-util-1.6.1/
[root@Static apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@Static apr-util-1.6.1]# make && make install

//编译安装httpd
[root@Static apr-util-1.6.1]# cd ../httpd-2.4.48/
[root@Static httpd-2.4.48]# ./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@Static httpd-2.4.48]# make && make install

//配置环境变量
[root@Static httpd-2.4.48]# echo "export PATH=/usr/local/apache/bin:$PATH" > /etc/profile.d/httpd.sh
[root@Static httpd-2.4.48]# bash
[root@Static httpd-2.4.48]# which httpd
/usr/local/apache/bin/httpd

[root@Static httpd-2.4.48]# cd /usr/lib/systemd/system/
[root@Static system]# cp sshd.service httpd.service
[root@Static system]# vi httpd.service
[root@Static system]# cat httpd.service 
[Unit]
Description=httpd server daemon
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
KillMode=process

[Install]
WantedBy=multi-user.target

[root@Static system]#  systemctl daemon-reload
[root@Static system]# systemctl start httpd
[root@Static system]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port        
LISTEN    0         128                      *:80                     *:*       

访问测试:
Nginx实现动静分离_第3张图片

配置动静分离

修改nginx配置文件

[root@dr ~]# vim /usr/local/nginx/conf/nginx.conf
33     #gzip  on;
34         upstream Dynamic {  	//添加
35            server 192.168.235.160;	//添加
36         }		//添加
37     
38         upstream Static {		//添加
39             server 192.168.235.135;   	//添加
40         } 		//添加

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         #}
54         location ~ \.php$ {		//添加
55              proxy_pass   http://Dynamic;		//添加
56         }
57         
58         #error_page  404              /404.html;
59 
60         # redirect server error pages to the static page /50x.html
61         #
62         error_page   500 502 503 504  /50x.html;
63         location = /50x.html {
64             root   html;
65         }
66 
67         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
68         #
69         #location ~ \.php$ {
70         #    proxy_pass   http://127.0.0.1;
71         #}
72          location / {		//添加
73             proxy_pass http://Static;		//添加
74         }		//添加

访问测试:

Nginx实现动静分离_第4张图片
Nginx实现动静分离_第5张图片

你可能感兴趣的:(Linux自动化,笔记,nginx,centos,运维)