Nginx实现动静分离
文章目录
简介:
环境说明:
系统 | IP | 服务 |
---|---|---|
CentOS8 调度器 DR | 192.168.220.9 | Nginx nginx-1.20.1.tar.gz |
CentOS8 动态页面处理 Dynamic(动态) | 192.168.220.10 | LNMP nginx-1.20.1.tar.gz mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz php-8.0.10.tar.gz |
CentOS8 静态页面处理 Static(静态) | 192.168.220.17 | httpd apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.48.tar.gz |
关闭三台防火墙和selinux,改名
# DR
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# hostnamectl set-hostname DR
[root@localhost ~]# bash
[root@DR ~]#
# Dynamic
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# hostnamectl set-hostname Dynamic
[root@localhost ~]# bash
[root@Dynamic ~]#
# Static
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# hostnamectl set-hostname Static
[root@localhost ~]# bash
[root@Static ~]#
DR上安装Nginx
# 安装***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
# 下载nginx,解压
root@DR ~]# mkdir /usr/src/soft
[root@DR ~]# cd /usr/src/soft/
[root@DR soft]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@DR soft]# ls
nginx-1.20.1.tar.gz
[root@DR soft]# tar xf nginx-1.20.1.tar.gz -C /usr/local/
# 编译安装Nginx
[root@DR soft]# cd /usr/local/nginx-1.20.1/
[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]# which nginx
/usr/local/nginx/sbin/nginx
# 尝试启动Nginx
[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 Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80(Nginx默认端口) 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
# 设置开机自启
[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
[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 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 [::]:*
访问页面是否正常
Dynamic安装LNMP
# 安装***Nginx***
# 安装依赖包和需要的工具包
[root@Dynamic ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@Dynamic ~]# yum -y group mark install "Development Tools"
# 创建用户nginx
[root@Dynamic ~]# useradd -r -M -s /sbin/nologin nginx
# 创建nginx的日志存放目录
[root@Dynamic ~]# mkdir -p /var/log/nginx
# 下载nginx并解压
[root@Dynamic ~]# mkdir /usr/src/soft
[root@Dynamic ~]# cd /usr/src/soft/
[root@Dynamic ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@Dynamic soft]# ls
nginx-1.20.1.tar.gz
[root@Dynamic soft]# tar xf nginx-1.20.1.tar.gz -C /usr/local/
# 开始编译安装nginx
[root@Dynamic soft]# cd /usr/local/nginx-1.20.1/
[root@Dynamic nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --user&