Nginx负载均衡实现动静分离
实验拓扑
原本应该有5台主机,2台放动态资源,2台放静态资源,一台做nginx负载均衡器,但是我们资源有限,九江4台资源机变成了2台,但是结果不受影响
实验环境:
系统
主机名
需要安装的服务
IP
Centos8
Nginx-LB
Nginx
192.168.169.139
Centos8
LNMP
Nginx+MySQL+PHP
192.168.169.140
Centos8
LAMP
Httpd+MySQL+PHP
192.168.169.142
1. 前置实验环境准备
1.1 关闭防火墙和selinux
//三台主机关闭防火墙和selinux
systemctl disable --now firewalld
sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
reboot
1.2 修改主机名
//Nginx-LB
[root@localhost ~]
[root@localhost ~]
[root@Nginx-LB ~]
//LNMP
[root@localhost ~]
[root@localhost ~]
[root@LNMP ~]
//Apache
[root@localhost ~]
[root@localhost ~]
[root@LAMP ~]
1.3 配置网络源
//三台主机配置网络源
rm -f /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
dnf clean all
dnf makecache
2. 在Nginx-LB主机上安装nginx
//安装所需工具和依赖包
[root@Nginx-LB ~]
//创建nginx用户
[root@Nginx-LB ~]
//下载nginx1.22安装包
[root@Nginx-LB ~]
[root@Nginx-LB src]
//解压nginx安装包
[root@Nginx-LB src]
[root@Nginx-LB src]
//编译nginx
[root@Nginx-LB nginx-1.22.0]
--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
[root@Nginx-LB nginx-1.22.0]
[root@Nginx-LB nginx-1.22.0]
[root@Nginx-LB nginx]
conf html logs sbin
//配置nginx全局环境变量
[root@Nginx-LB nginx]
[root@Nginx-LB nginx]
[root@Nginx-LB nginx]
/usr/local/nginx/sbin/nginx
//使用systemd风格控制nginx服务
[root@Nginx-LB nginx]
[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
[Install]
WantedBy=multi-user.target
[root@Nginx-LB nginx]
[root@Nginx-LB nginx]
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
访问测试
3. 在LNMP主机上部署LNMP
3.1 安装工具包和依赖包
[root@LNMP ~]
[root@LNMP ~]
3.2 安装nginx
//创建nginx用户
[root@LNMP ~]
//下载nginx1.22安装包
[root@LNMP ~]
[root@LNMP src]
//解压nginx
[root@LNMP src]
[root@LNMP src]
//编译nginx
[root@LNMP nginx-1.22.0]
--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
[root@LNMP nginx-1.22.0]
[root@LNMP nginx-1.22.0]
[root@LNMP nginx]
conf html logs sbin
//配置nginx全局环境变量
[root@LNMP nginx]
[root@LNMP nginx]
[root@LNMP nginx]
/usr/local/nginx/sbin/nginx
//使用systemd风格管理nginx
[root@LNMP nginx]
[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
[Install]
WantedBy=multi-user.target
[root@LNMP nginx]
[root@LNMP nginx]
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
访问测试
3.3 安装mysql
//下载mysql8.0安装包
[root@LNMP nginx]
[root@LNMP src]
//创建mysql用户
[root@LNMP src]
//创建数据存放目录
[root@LNMP src]
[root@LNMP src]
//安装mysql
[root@LNMP src]
//文件较大,解压时间较长
[root@LNMP src]
[root@LNMP local ]
[root@LNMP local ]
[root@LNMP local ]
lrwxrwxrwx 1 mysql mysql 35 Oct 19 15:31 mysql -> mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x 9 mysql mysql 129 Oct 19 15:29 mysql-8.0.28-linux-glibc2.12-x86_64
[root@LNMP local ]
[root@LNMP mysql]
bin docs include lib LICENSE man README share support-files
//配置mysql全局环境变量
[root@LNMP mysql]
[root@LNMP mysql]
[root@LNMP mysql]
/usr/local/mysql/bin/mysql
//配置mysql库文件
[root@LNMP mysql]
[root@LNMP mysql]
//配置mysql头文件
[root@LNMP mysql]
[root@LNMP mysql]
lrwxrwxrwx 1 root root 24 Oct 19 15:39 /usr/include/mysql -> /usr/local/mysql/include
//配置mysql man文档
[root@LNMP mysql]
MANDATORY_MANPATH /usr/local/mysql/man
//初始化mysql
[root@LNMP mysql]
2022-10-19T07:40:54.581530Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 35019
2022-10-19T07:40:54.596720Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-19T07:40:55.397137Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-10-19T07:40:56.374710Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: _i<g-I&/>7fS
//root@localhost: 后面的就是mysql初始化密码,要保存好
[root@LNMP mysql]
[root@LNMP mysql]
_i<g-I&/>7fS
//生成mysql配置文件
[root@LNMP mysql]
[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
//启动mysql
[root@LNMP mysql]
Starting MySQL.Logging to '/opt/data/LNMP.err' .
. SUCCESS!
//停止mysql
[root@LNMP mysql]
Shutting down MySQL. SUCCESS!
//使用systemd风格管理mysql服务
[root@LNMP mysql]
[Unit]
Description=MySQL 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
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@LNMP mysql]
[root@LNMP mysql]
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
//初始化mysql密码
//先登录到mysql
[root@LNMP mysql]
//更改密码
mysql> alter user root@'localhost' identified by '123456' ;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
//利用新密码进行登录
[root@LNMP mysql]
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
3.4 安装php
//下载php源码包
[root@LNMP mysql]
[root@LNMP src]
//解压php
[root@LNMP src]
[root@LNMP src]
//编译php
[root@LNMP php-8.1.11]
--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.1.11]
//编译安装时间较久
[root@LNMP php-8.1.11]
[root@LNMP php8]
bin etc include lib php sbin var
//配置php全局环境变量
[root@LNMP php8]
[root@LNMP php8]
//配置php头文件
[root@LNMP php8]
[root@LNMP php8]
lrwxrwxrwx 1 root root 23 Oct 19 16:18 /usr/include/php -> /usr/local/php8/include
//配置php库文件
[root@LNMP php8]
[root@LNMP php8]
//配置php-fpm
[root@LNMP php8]
cp : overwrite '/etc/php.ini' ? y
[root@LNMP php8]
[root@LNMP php8]
[root@LNMP php8]
[root@LNMP etc]
[root@LNMP etc]
//使用systemd风格管理php-fpm
[root@LNMP php8]
[Unit]
Description=php-fpm server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php8/bin/php-fpm start
ExecStop=/usr/local/php8/bin/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@LNMP php8]
[root@LNMP php8]
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
3.5 配置nginx
[root@LNMP php8]
//生成php测试页面
[root@LNMP nginx]
<?php
phpinfo();
?>
EOF
//修改nginx主配置文件
[root@LNMP nginx]
location / {
root html;
index index.html index.htm index.php; //添加index.php
}
//取消如下内容的注释
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name ;
include fastcgi_params;
}
//进行修改,如下
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;
}
//重启nginx
[root@LNMP nginx]
访问测试
4. 在LAMP主机上部署LAMP
4.1 安装工具包和依赖包
[root@LAMP ~]
[root@LAMP ~]
4.2 安装httpd
//创建apache用户
[root@LAMP ~]
//下载apr,apr-util,httpd源码包
[root@LAMP ~]
[root@LAMP src]
[root@LAMP src]
[root@LAMP src]
//编译apr
[root@LAMP src]
[root@LAMP src]
[root@LAMP apr-1.7.0]
cfgfile=${ofile} T
trap "$RM \"$cfgfile \"; exit 1" 1 2 15
[root@LAMP apr-1.7.0]
[root@LAMP apr-1.7.0]
//编译apr-util
[root@LAMP apr-1.7.0]
[root@LAMP src]
[root@LAMP src]
[root@LAMP apr-util-1.6.1]
[root@LAMP apr-util-1.6.1]
//编译httpd
[root@LAMP apr-util-1.6.1]
[root@LAMP src]
[root@LAMP src]
[root@LAMP httpd-2.4.5]
--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@LAMP httpd-2.4.5]
[root@LAMP httpd-2.4.54]
[root@LAMP apache]
bin build cgi-bin conf error htdocs icons include logs man manual modules
//配置apache全局环境变量
[root@LAMP apache]
[root@LAMP apache]
[root@LAMP apache]
/usr/local/apache/bin/apachectl
//配置头文件
[root@LAMP apache]
[root@LAMP apache]
lrwxrwxrwx 1 root root 25 Oct 19 20:33 /usr/include/httpd -> /usr/local/apache/include
//配置man 文档
[root@LAMP apache]
MANDATORY_MANPATH /usr/local/apache/man
//将主配置文件里的'#ServerName' 行的注释去掉
[root@LAMP apache]
//使用systemd风格管理apache
[root@LAMP apache]
[Unit]
Description=web server daemon
Documentation=man:httpd(5)
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@LAMP apache]
[root@LAMP apache]
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
访问测试
4.3 安装mysql
//下载mysql安装包
[root@LAMP apache]
[root@LAMP src]
//创建mysql用户
[root@LAMP src]
//创建数据存放目录
[root@LAMP src]
[root@LAMP src]
//安装mysql
[root@LAMP src]
[root@LAMP src]
[root@LAMP local ]
[root@LAMP local ]
[root@LAMP local ]
lrwxrwxrwx 1 mysql mysql 35 Oct 19 20:41 mysql -> mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x 9 mysql mysql 129 Oct 19 20:41 mysql-8.0.28-linux-glibc2.12-x86_64
[root@LAMP local ]
[root@LAMP mysql]
bin docs include lib LICENSE man README share support-files
//配置全局环境变量
[root@LAMP mysql]
[root@LAMP mysql]
[root@LAMP mysql]
/usr/local/mysql/bin/mysql
//配置mysql库文件
[root@LAMP mysql]
[root@LAMP mysql]
//配置mysql头文件
[root@LAMP mysql]
[root@LAMP mysql]
lrwxrwxrwx. 1 root root 24 Sep 7 15:21 /usr/include/mysql -> /usr/local/mysql/include
//配置mysql man文档
[root@LAMP mysql]
MANDATORY_MANPATH /usr/local/mysql/man
//初始化mysql
[root@LAMP mysql]
2022-10-19T12:44:51.639295Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 44706
2022-10-19T12:44:51.659424Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-19T12:44:52.447609Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-10-19T12:44:53.668287Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: bt/XF)uTU6Vg
//root@localhost: 后面的就是mysql初始化密码,要保存好
[root@LAMP mysql]
[root@LAMP mysql]
bt/XF)uTU6Vg
//生成mysql配置文件
[root@LAMP mysql]
[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
//使用systemd风格管理mysql服务
[root@LAMP mysql]
[Unit]
Description=MySQL 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
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@LAMP mysql]
[root@LAMP mysql]
//初始化mysql密码
//先登录到mysql
[root@LAMP mysql]
//更改密码
mysql> alter user root@'localhost' identified by '123456' ;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
//利用新密码进行登录
[root@LAMP mysql]
4.4 安装php
//下载php源码包
[root@LAMP mysql]
[root@LAMP src]
//解压php
[root@LAMP src]
[root@LAMP src]
//编译php
[root@LAMP php-8.1.11]
--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@LAMP php-8.1.11]
[root@LAMP php-8.1.11]
[root@LAMP php8]
bin etc include lib php sbin var
//配置php全局环境变量
[root@LAMP php8]
[root@LAMP php8]
//配置php头文件
[root@LAMP php8]
[root@LAMP php8]
lrwxrwxrwx 1 root root 23 Oct 19 21:02 /usr/include/php -> /usr/local/php8/include
//配置php库文件
[root@LAMP php8]
[root@LAMP php8]
//配置php-fpm
[root@LAMP php8]
cp : overwrite '/etc/php.ini' ? y
[root@LAMP php8]
[root@LAMP php8]
[root@LAMP php8]
[root@LAMP etc]
[root@LAMP etc]
//使用systemd风格管理php-fpm
[root@LAMP etc]
[Unit]
Description=php-fpm server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php8/bin/php-fpm start
ExecStop=/usr/local/php8/bin/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@LAMP etc]
[root@LAMP etc]
4.5 配置httpd
//启动代理模块
[root@LAMP etc]
[root@LAMP apache]
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
//配置虚拟主机,让php页面监听在82端口
listen 82
<VirtualHost *:82>
DocumentRoot "/usr/local/apache/htdocs/php"
ServerName php.example.com
ErrorLog "logs/php.example.com-error_log"
CustomLog "logs/php.example.com-access_log" common
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/php/$1
<Directory "/usr/local/apache/htdocs/php" >
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>、
//创建虚拟主机的目录并生成php测试页面
[root@LAMP apache]
[root@LAMP apache]
[root@LAMP php]
<?php
phpinfo();
?>
EOF
[root@LAMP php]
[root@LAMP apache]
Include conf/extra/httpd-vhosts.conf
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.php
//重启httpd
[root@LAMP php]
访问测试
5. 在Nginx-LB主机上开负载均衡和反向代理
[root@Nginx-LB ~]
[root@Nginx-LB nginx]
upstream dynamic {
server 192.168.169.140; //动态资源放在哪里就写那台主机的IP+端口
server 192.168.169.142:82;
}
upstream static {
server 192.168.169.140; //静态资源放在哪里就写哪台主机的IP+端口
server 192.168.169.142;
}
location / {
proxy_pass http://static; //因为负载均衡器不需要主页,直接修改默认的location段
}
//取消如下内容的注释
location ~ \.php$ {
proxy_pass http://127.0.0.1;
}
//进行修改
location ~ \.php$ {
proxy_pass http://dynamic;
}
//重启nginx
[root@Nginx-LB nginx]
6. 进行验证
访问Nginx-LB主机的IP
此时我们访问到的是静态网页,并且nginx和apache做轮询
刷新,重新加载
访问Nginx-LB主机的IP+index.php
现在我们访问到的是php动态网页,同样也是LNMP和LAMP两台主机轮询
刷新,重新加载