nginx做负载均衡服务器,配置动静分离
目录
部署LNMP(nginx1.22+mysql8.0+php8.1)
部署LAMP(httpd2.4+mysql8.0+php8.1)
要求nginx和php使?编译安装最后要通过访问nginx负载均衡服务器的IP看到动静分离的效果
环境说明:
系统 | ip | 主机名 | 服务 |
---|---|---|---|
centos8 | 192.168.222.250 | RS1 | LNMP,动态资源,静态资源 |
centos8 | 192.168.222.137 | RS2 | LAMP,静态资源,动态资源 |
centos8 | 192.168.222.139 | node1 | nginx,负载均衡服务器 |
关闭防火墙
RS1
[root@localhost ~]# hostnamectl set-hostname RS1
[root@localhost ~]# bash
[root@RS1 ~]# systemctl stop firewalld.service
[root@RS1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@RS1 ~]# setenforce 0
[root@RS1 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
RS2:
[root@localhost ~]# hostnamectl set-hostname RS2
[root@localhost ~]# bash
[root@RS2 ~]# systemctl stop firewalld.service
[root@RS2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@RS2 ~]# setenforce 0
[root@RS2 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
node1:
[root@localhost ~]# hostnamectl set-hostname node1
[root@localhost ~]# bash
[root@node1 ~]# systemctl stop firewalld.service
[root@node1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@node1 ~]# setenforce 0
[root@node1 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
部署LNMP
源码安装nginx
[root@RS1 ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
//安装nginx
[root@RS1 ~]# ls
anaconda-ks.cfg nginx-1.22.0.tar.gz
[root@RS1 ~]# useradd -r -M -s /sbin/nologin nginx
//创建用户
[root@RS1 ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make vim
//安装依赖包
[root@RS1 ~]# mkdir -p /var/log/nginx //创建日志存放目录
[root@RS1 ~]# chown -R nginx.nginx /var/log/nginx
[root@RS1 ~]# ll -d /var/log/nginx/
drwxr-xr-x. 2 nginx nginx 6 Oct 19 14:47 /var/log/nginx/
[root@RS1 ~]# tar xf nginx-1.22.0.tar.gz //解压
[root@RS1 ~]# cd nginx-1.22.0/
[root@RS1 nginx-1.22.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@RS1 nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install //安装
[root@RS1 nginx-1.22.0]# cd
[root@RS1 ~]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh //配置环境变量
[root@RS1 ~]# source /etc/profile.d/nginx.sh //使其生效
[root@RS1 ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service //将其加入systemd服务中
[root@RS1 ~]# vim /usr/lib/systemd/system/nginx.service
[roo