nginx做负载均衡服务器,配置动静分离
1. 题目要求:
后端RS服务器⼀台部署LNMP(nginx1.22+mysql8.0+php8.1),⼀台部署 httpd。
要求nginx和php使⽤编译安装
最后要通过访问nginx负载均衡服务器的IP看到动静分离的效果。
2. 环境和提供软件包
2.1 提供软件包
wget https://nginx.org/download/nginx-1.22.0.tar.gz
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz
wgethttps://www.php.net/distributions/php-8.1.11.tar.xz
2.2 虚拟机环境要求
主机 | ip | 安装的服务 | 操作系统 |
---|---|---|---|
140 | 192.168.141.140 | lnmp,动态资源 | CentOS8 |
141 | 192.168.141.141 | nginx,静态资源 | CentOS8 |
143 | 192.168.141.143 | nginx,做负载均衡 | CentOS8 |
修改主机名,关闭防火墙,配置阿里云源
[root@Client ~]# hostnamectl set-hostname 140
[root@Client ~]# bash
[root@DR ~]# hostnamectl set-hostname 141
[root@DR ~]# bash
[root@RS1 ~]# hostnamectl set-hostname 143
[root@RS1 ~]# bash
[root@140 ~]# systemctl disable --now firewalld
[root@140 ~]# vim /etc/selinux/config
SELINUX=disabled
[root@140 ~]# setenforce 0
[root@141 ~]# systemctl disable --now firewalld
[root@141 ~]# setenforce 0
[root@141 ~]# vi /etc/selinux/config
SELINUX=disabled
[root@143 ~]# systemctl disable --now firewalld
[root@143 ~]# setenforce 0
[root@143 ~]# vi /etc/selinux/config
SELINUX=disabled
#其他的省略
[root@140 ~]# cd /etc/yum.repos.d/
[root@140 yum.repos.d]# mkdir ll
[root@140 yum.repos.d]# mv * ll/
[root@140 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@140 yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@140 yum.repos.d]#
3. 在140主机主机部署lnmp,在动态资源
3.1 源码安装nginx
创建系统用户nginx
[root@140 ~]# useradd -r -M -s /sbin/nologin nginx
[root@140 ~]# id nginx
uid=993(nginx) gid=990(nginx) groups=990(nginx)
安装依赖环境
[root@140 ~]# yum -y groups mark install 'Development Tools'
[root@140 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
创建日志存放目录
[root@140 ~]# mkdir -p /var/log/nginx
[root@140 ~]# chown -R nginx.nginx /var/log/nginx
[root@140 ~]# ll -d /var/log/nginx
drwxr-xr-x 2 nginx nginx 6 Oct 19 02:46 /var/log/nginx
编译安装
[root@140 ~]# tar xf nginx-1.22.0.tar.gz
[root@140 ~]# cd nginx-1.22.0
[root@140 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@140 nginx-1.22.0]# make
[root@140 nginx-1.22.0]# make install
nginx安装后配置
[root@140 nginx-1.22.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@140 nginx-1.22.0]# source /etc/profile.d/nginx.sh
启动测试nginx
[root@140 ~]# nginx
[root@140 ~]# 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@140 ~]# nginx -s stop
[root@140 ~]# 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@140 ~]# nginx
- 浏览器访问
3.2 二进制安装MySQL
安装依赖包,创建用户,并解压
[root@140 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
[root@140 ~]# useradd -r -M -s /sbin/nologin mysql
[root@140 ~]# id mysql
uid=992(mysql) gid=989(mysql) groups=989(mysql)
修改属主
[root@140 ~]# tar xf mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@140 ~]# cd /usr/local/
[root@140 local]# ls
bin games lib libexec nginx share
etc include lib64 mysql-8.0.30-linux-glibc2.12-x86_64 sbin src
[root@140 local]# mv mysql-8.0.30-linux-glibc2.12-x86_64/ mysql
[root@140 local]# chown -R mysql.mysql mysql
[root@140 local]# ll -d mysql
drwxr-xr-x 9 mysql mysql 129 Oct 19 03:13 mysql
[root@140 local]#
配置环境变量,man文档,lib库,头文件
[root@140 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@140 ~]# source /etc/profile.d/mysql.sh
[root@140 ~]# vim /etc/ld.so.conf.d/mysql.conf
[root@140 ~]# ldconfig
[root@140 ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@140 ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@140 ~]# vim /etc/man_db.conf
#添加如下配置
MANDATORY_MANPATH /usr/local/mysql/man
建立数据存放目录,并修改属主
[root@140 ~]# mkdir -p /opt/data
[root@140 ~]# chown -R mysql.mysql /opt/data
[root@140 ~]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Oct 19 03:18 data
初始化数据库,
[root@140 ~]# mysqld --initialize --user mysql --datadir /opt/data
2022-10-19T07:20:18.126505Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.30) initializing of server in progress as process 35901
2022-10-19T07:20:18.134799Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-19T07:20:18.394351Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-10-19T07:20:18.945482Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 9S*,w010z>>=
9S*,w010z>>=
这个是初始化密码记住
生成配置文件
[root@140 ~]# > /etc/my.cnf
[root@140 ~]# vim /etc/my.cnf
[root@140 ~]# 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@140 ~]#
配置服务启动脚本
[root@140 ~]# cd /usr/local/mysql/support-files/
[root@140 support-files]# cp mysql.server mysqld
[root@140 support-files]# vim mysqld
45行
basedir=/usr/local/mysql
datadir=/opt/data
[root@140 support-files]# chown -R mysql.mysql mysqld
[root@140 support-files]# ll -d mysqld
-rwxr-xr-x 1 mysql mysql 10601 Oct 19 03:29 mysqld
先启动测试一下
[root@140 ~]# /usr/local/mysql/support-files/mysqld start
Starting MySQL.Logging to '/opt/data/140.err'.
SUCCESS!
[root@140 ~]# 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 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
[root@140 ~]# /usr/local/mysql/support-files/mysqld stop
Shutting down MySQL.. SUCCESS!
[root@140 ~]# 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 [::]:*
[root@140 ~]#
配置service文件,设置开机自启
[root@140 ~]# cd /usr/lib/systemd/system
[root@140 system]# cp sshd.service mysqld.service
[root@140 system]# ll sshd.service
-rw-r--r--. 1 root root 456 Apr 26 2020 sshd.service
[root@140 system]# ll mysqld.service
-rw-r--r-- 1 root root 456 Oct 19 03:32 mysqld.service
[root@140 system]#
[root@140 system]# vim mysqld.service
[root@140 system]# cat mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@140 system]# systemctl enable --now mysqld.service
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@140 system]# 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 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@140 system]#
安装mysql软件包
[root@140 system]# yum -y install ncurses-compat-libs
设置密码
[root@140 system]# mysql -uroot -p'9S*,w010z>>=';
mysql> alter user 'root'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@140 support-files]# mysql -uroot -p'123';
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 MySQL Community Server - GPL
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> quit
Bye
3.3 源码安装PHP
//安装依赖包
[root@Master_B ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
//安装依赖包
[root@Master_B ~]# yum -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 readline readline-devel libxslt libxslt-devel php-mysqlnd libxml2-devel sqlite-devel https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm https://vault.centos.org/centos/8/AppStream/x86_64/os/Packages/libzip-devel-1.5.1-2.module_el8.2.0+313+b04d0a66.x86_64.rpm --nobest
//安装依赖包
[root@Masters_B]# wget https://vault.centos.org/centos/8/BaseOS/x86_64/os/Packages/libcurl-7.61.1-22.el8.x86_64.rpm
[root@Masters_B]# yum install -y libcurl-devel.x86_64
//安装php环境包
[root@140 ~]# tar xf php-8.1.11
//安装php环境包
[root@140 php-8.1.11]# yum -y install libxml2 libxml2-devel
[root@140 php-8.1.11]#./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
......等待
+--------------------------------------------------------------------+
| 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@140 php-8.1.11]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
......
[root@140 php-8.1.11]# cd /usr/local/php8/
[root@140 php8]# ls
bin etc include lib php sbin var
[root@140 php8]# echo 'export PATH=/usr/local/php8/bin:/usr/local/php8/:sbin:$PATH' > /etc/profile.d/php8.sh
[root@140 php8]# source /etc/profile.d/php8.sh
[root@140 php8]# ln -s /usr/local/php8/include/ /usr/include/php
[root@140 php8]# vim /etc/ld.so.conf.d/php.conf
[root@140 php8]# cat /etc/ld.so.conf.d/php.conf
/usr/local/php8/lib
[root@140 php8]# ldconfig
[root@140 php8]# php -v
PHP 8.1.11 (cli) (built: Oct 19 2022 04:46:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Technologies
[root@140 php8]#
配置php-fpm
[root@140 php8]# cd
[root@140 ~]# cd php-8.1.11/
[root@140 php-8.1.11]# \cp php.ini-production /etc/php.ini
[root@140 php-8.1.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@140 php-8.1.11]# chmod +x /etc/rc.d/init.d/php-fpm
[root@140 php-8.1.11]# ll /etc/rc.d/init.d/php-fpm
-rwxr-xr-x 1 root root 2402 Oct 19 04:59 /etc/rc.d/init.d/php-fpm
[root@140 php-8.1.11]# pwd
/root/php-8.1.11
[root@140 php-8.1.11]# cd /usr/local/php8/etc/
[root@140 etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@140 etc]# cp php-fpm.conf.default php-fpm.conf
[root@140 etc]# cd php-fpm.d/
[root@140 php-fpm.d]# ls
www.conf.default
[root@140 php-fpm.d]# cp www.conf.default www.conf
[root@140 php-fpm.d]# vim www.conf
36 listen = 127.0.0.1:9000
启动,并测试配置是否正确
[root@140 php-fpm.d]# cd /etc/init.d/
[root@140 init.d]# ls
functions php-fpm README
[root@140 init.d]# service php-fpm start
Starting php-fpm done
[root@140 init.d]# 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 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@140 init.d]# service php-fpm stop
Gracefully shutting down php-fpm . done
[root@140 init.d]# 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 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@140 init.d]# service php-fpm start
Starting php-fpm done
[root@140 init.d]#
3.4 配置nginx
[root@140 ~]# cd /usr/local/nginx/conf/
[root@140 conf]# vim nginx.conf
[root@140 conf]# vim nginx.conf
[root@140 conf]# cat nginx.conf
//将以下内容取消注释并修改
location / {
root html;
index index.php index.html index.htm; #添加index.php
}
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@140 conf]# nginx -s stop
[root@140 conf]# nginx
3.5 配置PHP网络界面
[root@140 ~]# cd /usr/local/nginx/html/
[root@140 html]# vi index.php
[root@140 html]# cat index.php
<?php
phpinfo();
?>
[root@140 html]#
重启
[root@140 ~]# nginx -s stop
[root@140 ~]# nginx
浏览器访问http://192.168.141.140/index.php
4. 部署
4.1 在141主机安装httpd,做静态资源
[root@141 ~]# yum -y install httpd
[root@141 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@141 ~]# 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 [::]:*
[root@141 ~]#
配置静态测试页面
[root@141 ~]# cd /var/www/html/
[root@141 html]# echo '141 jingtai ' > index.html
[root@141 html]# systemctl restart httpd.service
4.2 在143主机源码安装nginx并配置负载均衡器,进行调度
[root@143 ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
--2022-09-05 20:47:25-- https://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|: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. 100%[==========>] 1.02M 29.1KB/s in 20s
2022-09-05 20:47:47 (51.5 KB/s) - 'nginx-1.22.0.tar.gz' saved [1073322/1073322]
[root@143 ~]# useradd -r -M -s /sbin/nologin nginx
[root@143 ~]# yum -y groups mark install 'Development Tools'
[root@143 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@143 ~]# mkdir -p /var/log/nginx
[root@143 ~]# chown -R nginx.nginx /var/log/nginx
[root@143 ~]# ll -d /var/log/nginx
drwxr-xr-x. 2 nginx nginx 6 Sep 5 20:51 /var/log/nginx
[root@143 ~]# tar xf nginx-1.22.0.tar.gz
[root@143 ~]# cd nginx-1.22.0
[root@143 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@143 nginx-1.22.0]# make
[root@143 nginx-1.22.0]# make install
[root@143 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@143 ~]# source /etc/profile.d/nginx.sh
[root@143 nginx-1.22.0]# cd
[root@143 ~]# nginx
[root@143 ~]# 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@143 ~]#
5. 配置负载均衡
[root@143 ~]# cd /usr/local/nginx/conf/
[root@143 conf]# vim nginx.conf
#gzip on;
upstream backend {
server 192.168.141.140;
server 192.168.141.141;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://backend;
}
#location / {
# root html;
# index index.html index.htm;
#}
[root@143 conf]# nginx -s reload
[root@143 conf]# nginx -s stop
[root@143 conf]# nginx
[root@143 conf]# 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@143 conf]#
第一次访问 http://192.168.141.143/
第二次访问 http://192.168.141.143/ 会以轮询方式显示网页
6. 实现动静分离
[root@143 conf]# pwd
/usr/local/nginx/conf
[root@143 conf]# vim nginx.conf
#gzip on;
upstream static {
server 192.168.141.141; #httpd主机的ip
}
upstream dynamic {
server 192.168.141.140; #lnmp主机的ip
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://static; #和上面httpd设置相对应,访问静态资源会自动跳转到进行访问,
}
#location / {
# root html;
# index index.php index.html index.htm;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
proxy_pass http://dynamic;
}
[root@143 conf]# 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@143 conf]# nginx -s reload
[root@143 conf]# nginx -s stop
[root@143 conf]# nginx
[root@143 conf]# 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@143 conf]#
- 访问静态资源:http://192.168.141.143
- 访问动态资源:http://192.168.141.143/index.php