⽤nginx做负载均衡服务 器,配置动静分离
文章目录
- ⽤nginx做负载均衡服务 器,配置动静分离
- 部署LNMP ,做动态资源
- nginx安装
- mysql安装
- 安装php
- 配置nginx
- 配置PHP网络界面
- node2安装httpd,做静态资源
- node3源码安装nginx并配置负载均衡器,进行调度
- 配置负载均衡
- 实现动静分离
题目:
后端RS服务器⼀台部署LNMP(nginx1.22+mysql8.0+php8.1),⼀台部署
httpd。
要求nginx和php使⽤编译安装
最后要通过访问nginx负载均衡服务器的IP看到动静分离的效果。
环境准备:
主机 | ip | 安装的服务 |
---|---|---|
node1 | 192.168.174.170 | lnmp,动态资源 |
node2 | 192.168.174.175 | nginx,静态资源 |
node3 | 192.168.174.177 | nginx,做负载均衡 |
部署LNMP ,做动态资源
nginx安装
//首先关闭防火墙和selinux
[root@node1 ~]# setenforce 0
[root@node1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@node1 ~]# systemctl disable --now firewalld.service
//创建用户
[root@node1 ~]# useradd -rMs /sbin/nologin nginx
//安装所需要的依赖包
[root@node1 ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
//下载nginx源码包,并解压
[root@node1 ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@node1 ~]# tar -xf nginx-1.22.0.tar.gz
[root@node1 ~]# cd nginx-1.22.0/
[root@node1 nginx-1.22.0]#
//编译安装
[root@node1 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
//开启编译安装
[root@node1 nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//安装成功
[root@node1 nginx-1.22.0]# cd /usr/local/nginx/
[root@node1 nginx]# ls
conf html logs sbin
nginx配置
//配置环境变量
[root@node1 nginx]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@node1 nginx]# source /etc/profile
//启动服务
[root@node1 nginx]# nginx
[root@node1 nginx]# 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 [::]:22 [::]:*
//关闭服务,编写service文件,并设置服务开机自启
[root@node1 nginx]# cat > /usr/lib/systemd/system/nginx.service << EOF
> [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
> EOF
[root@node1 nginx]# systemctl daemon-reload
[root@node1 nginx]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@node1 nginx]# 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 [::]:22 [::]:*
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YxRm2o2D-1682564464646)(null)]
mysql安装
//安装依赖包
[root@node1 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建apache服务的用户和组
[root@node1 ~]# useradd -r -M -s /sbin/nologin mysql[root@node1 ~]# cd /usr/local/
[root@node1 local]# chown -R mysql.mysql mysql*
[root@node1 local]# ll |gerp mysql
bash: gerp: command not found
[root@node1 local]# ll |grep mysql
lrwxrwxrwx 1 mysql mysql 36 Oct 19 04:26 mysql -> mysql-8.0.30-linux-glibc2.12-x86_64/
drwxr-xr-x 4 mysql mysql 28 Oct 19 04:23 mysql-8.0.30-linux-glibc2.12-x86_64
[root@node1 ~]# id mysql
uid=992(mysql) gid=989(mysql) groups=989(mysql)
//下载或导入mysql二进制软件包
[root@node1 ~]# ls
mysql-8.0.30-linux-glibc2.12-x86_64.tar
//解压软件至/usr/local/
[root@node1 ~]# tar -xf mysql-8.0.30-linux-glibc2.12-x86_64.tar -C /usr/local/
[root@node1 ~]# cd /usr/local/
[root@node1 local]# ls
bin games lib libexec nginx share
etc include lib64 mysql-8.0.30-linux-glibc2.12-x86_64 sbin src
//建立软链接
[root@node1 local]# ln -sv mysql-8.0.30-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-8.0.30-linux-glibc2.12-x86_64/'
[root@node1 local]# ll mysql
lrwxrwxrwx 1 root root 36 Oct 19 04:26 mysql -> mysql-8.0.30-linux-glibc2.12-x86_64/
//修改目录/usr/local/mysql的属主属组
[root@node1 ~]# cd /usr/local/
[root@node1 local]# chown -R mysql.mysql mysql*
[root@node1 local]# ll |grep mysql
lrwxrwxrwx 1 mysql mysql 36 Oct 19 04:26 mysql -> mysql-8.0.30-linux-glibc2.12-x86_64/
drwxr-xr-x 4 mysql mysql 28 Oct 19 04:23 mysql-8.0.30-linux-glibc2.12-x86_64
[root@node1 local]# ls /usr/local/mysql
bin include LICENSE mysql-8.0.30-linux-glibc2.12-x86_64 share
docs lib man README support-files
[root@node1 local]# ln -s /usr/local/mysql/include/ /usr/include/mysql/
[root@node1 local]# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf
[root@node1 local]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
//添加环境变量
[root@node1 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@node1 ~]# source /etc/profile.d/mysql.sh
[root@node1 ~]# which mysql
/usr/local/mysql/bin/mysql
//创建数据存放目录
[root@node1 ~]# mkdir /opt/data
[root@node1 ~]# chown -R mysql.mysql /opt/data/
[root@node1 ~]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Oct 19 04:46 data
//初始化数据库
[root@node1 ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-10-19T08:47:34.299683Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.30-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.30) initializing of server in progress as process 27184
2022-10-19T08:47:34.420194Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-19T08:47:47.324357Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-10-19T08:47:53.257519Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: B=wdzUpHZ4hq
//保存临时密码,方便后面使用
[root@node1 ~]# echo 'B=wdzUpHZ4hq' > pass
//生成配置文件
[root@node1 ~]# vim /etc/my.cnf
[root@node1 ~]# 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@node1 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@node1 ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@node1 ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
[root@node1 ~]#
//启动mysql
[root@node1 ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/node1.err'.
............ SUCCESS!
[root@node1 ~]# 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 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 70 *:33060 *:*
//服务开机自启
[root@node1 ~]# chkconfig --add mysqld
//修改密码
//使用临时密码登录
[root@node1 ~]# mysql -uroot -p'B=wdzUpHZ4hq'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.30
Copyright (c) 2000, 2022, 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.
[root@node1 ~]# mysql -uroot -p'B=wdzUpHZ4hq'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.30
Copyright (c) 2000, 2022, 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> ALTER user 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
安装php
//安装依赖包
[root@node1 ~]# dnf -y install epel-release
[root@node1 ~]# dnf -y install 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 libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip-devel wget gcc gcc-c++ make
[root@node1 ~]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
//下载php源码包,并解压
[root@node1 ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
[root@node1 ~]# tar -xf php-8.1.11.tar.gz -C /usr/local/src/
[root@node1 ~]# cd /usr/local/src/php-8.1.11/
[root@node1 php-8.1.11]#
//编译安装php
[root@node1 php-8.1.11]# ./configure --prefix=/usr/local/php \
--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@node1 php-8.1.11]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@node1 php-8.1.11]# cd /usr/local/php/
[root@node1 php]# ls
bin etc include lib php sbin var
//配置服务
(复制默认配置文件,使配置文件生效)
[root@node1 php]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[root@node1 php]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
//编写service文件
[root@node1 php]# cat > /usr/lib/systemd/system/php.service << EOF
> [Unit]
> Description=php server daemon
> After=network.target
>
> [Service]
> Type=forking
> ExecStart=/usr/local/php/sbin/php-fpm
> ExecStop=ps -ef |grep php |grep -v grep|awk '{print$2}'|xargs kill -9
> ExecReload=/bin/kill -HUP $MAINPID
>
> [Install]
> WantedBy=multi-user.target
> EOF
[root@node1 php]# systemctl daemon-reload
[root@node1 php]# systemctl enable --now php.service
Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.
[root@node1 php]# 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 128 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 70 *:33060 *:*
配置nginx
[root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf
......
location / {
root html;
index index.html index.html index.php; //在index后面添加index.php,表示优先访问php页面
}
......
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;
}
......
配置PHP网络界面
[root@node1 ~]# cd /usr/local/nginx/html/
[root@node1 html]# vim index.php
[root@node1 html]# cat index.php
<?php
phpinfo();
?>
[root@node1 html]#
//重启服务
[root@node1 ~]# nginx -s stop
[root@node1 ~]# nginx
访问:http://192.168.174.170/index.php
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TMTvYHNl-1682564464613)(null)]
node2安装httpd,做静态资源
[root@node2 ~]# yum -y install httpd
[root@node2 ~]# systemctl enable --now httpd
[root@node2 ~]# 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 [::]:*
node3源码安装nginx并配置负载均衡器,进行调度
//创建nginx用户
[root@node3 ~]# useradd -r -M -s /sbin/nologin nginx
[root@node3 ~]# id nginx
uid=994(nginx) gid=991(nginx) groups=991(nginx)
//下载依赖包
[root@node3 ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
//下载nginx源码包,并解压
[root@node3 ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
--2022-10-19 09:42:47-- https://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073322 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.22.0.tar.gz’
nginx-1.22.0.tar.gz 100%[===================================>] 1.02M 731KB/s in 1.4s
2022-10-19 09:42:50 (731 KB/s) - ‘nginx-1.22.0.tar.gz’ saved [1073322/1073322]
[root@node3 ~]# tar -xf nginx-1.22.0.tar.gz
[root@node3 ~]# cd nginx-1.22.0/
[root@node3 nginx-1.22.0]#
//开始配置
[root@node3 ~]# tar -xf nginx-1.22.0.tar.gz
[root@node3 ~]# cd nginx-1.22.0/
[root@node3 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
//开启编译安装
[root@node3 nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//安装完成
[root@node3 nginx-1.22.0]# cd /usr/local/nginx/
[root@node3 nginx]# ls
conf html logs sbin
//配置环境变量
[root@node3 nginx]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@node3 nginx]# source /etc/profile.d/nginx.sh
//启动服务
[root@node3 nginx]# nginx
[root@node3 nginx]# 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 [::]:22
[::]:*
//关闭服务,编写service文件,并设置服务开机自启
[root@node3 nginx]# nginx -s stop
[root@node3 nginx]# 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@node3 nginx]# cat > /usr/lib/systemd/system/nginx.service << EOF
> [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
> EOF
[root@node3 nginx]# systemctl daemon-reload
[root@node3 nginx]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@node3 nginx]# 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 [::]:22 [::]:*
配置负载均衡
[root@node3 ~]# vim /usr/local/nginx/conf/nginx.conf
......
upstream backend {
server 192.168.174.170;
server 192.168.174.175;
}
......
location / {
proxy_pass http://backend;
}
[root@node3 ~]# 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@node3 ~]# systemctl restart nginx.service
访问:http://192.168.174.177/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XFoClpzb-1682564464640)(null)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Dflb5gqU-1682564464653)(null)]
实现动静分离
[root@node3 ~]# vim /usr/local/nginx/conf/nginx.conf
......
upstream static {
server 192.168.174.175;
}
upstream dynamic {
server 192.168.174.170;
}
......
location / {
proxy_pass http://static;
}
location ~ \.php$ {
proxy_pass http://dynamic;
}
......
[root@node3 ~]# 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@node3 ~]# systemctl restart nginx.service
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vB0SmiV3-1682564464623)(null)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4s1BpVhk-1682564464632)(null)]
upstream dynamic {
server 192.168.174.170;
}
…
location / {
proxy_pass http://static;
}
location ~ .php$ {
proxy_pass http://dynamic;
}
…
[root@node3 ~]# 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@node3 ~]# systemctl restart nginx.service
[外链图片转存中...(img-ZM0HyZLJ-1682564464389)]
[外链图片转存中...(img-3msue4FN-1682564464390)]