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 ~]
[root@Client ~]
[root@DR ~]
[root@DR ~]
[root@RS1 ~]
[root@RS1 ~]
[root@140 ~]
[root@140 ~]
SELINUX=disabled
[root@140 ~]
[root@141 ~]
[root@141 ~]
[root@141 ~]
SELINUX=disabled
[root@143 ~]
[root@143 ~]
[root@143 ~]
SELINUX=disabled
[root@140 ~]
[root@140 yum.repos.d]
[root@140 yum.repos.d]
[root@140 yum.repos.d]
[root@140 yum.repos.d]
[root@140 yum.repos.d]
3. 在140主机主机部署lnmp,在动态资源
3.1 源码安装nginx
创建系统用户nginx
[root@140 ~]
[root@140 ~]
uid=993(nginx) gid=990(nginx) groups=990(nginx)
安装依赖环境
[root@140 ~]
[root@140 ~]
创建日志存放目录
[root@140 ~]
[root@140 ~]
[root@140 ~]
drwxr-xr-x 2 nginx nginx 6 Oct 19 02:46 /var/log/nginx
编译安装
[root@140 ~]
[root@140 ~]
[root@140 nginx-1.22.0]
--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]
[root@140 nginx-1.22.0]
nginx安装后配置
[root@140 nginx-1.22.0]
[root@140 nginx-1.22.0]
启动测试nginx
[root@140 ~]
[root@140 ~]
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 ~]
[root@140 ~]
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 ~]
- 浏览器访问
3.2 二进制安装MySQL
安装依赖包,创建用户,并解压
[root@140 ~]
[root@140 ~]
[root@140 ~]
uid=992(mysql) gid=989(mysql) groups=989(mysql)
修改属主
[root@140 ~]
[root@140 ~]
[root@140 local]
bin games lib libexec nginx share
etc include lib64 mysql-8.0.30-linux-glibc2.12-x86_64 sbin src
[root@140 local]
[root@140 local]
[root@140 local]
drwxr-xr-x 9 mysql mysql 129 Oct 19 03:13 mysql
[root@140 local]
配置环境变量,man文档,lib库,头文件
[root@140 ~]
[root@140 ~]
[root@140 ~]
[root@140 ~]
[root@140 ~]
/usr/local/mysql/lib
[root@140 ~]
[root@140 ~]
MANDATORY_MANPATH /usr/local/mysql/man
建立数据存放目录,并修改属主
[root@140 ~]
[root@140 ~]
[root@140 ~]
total 0
drwxr-xr-x 2 mysql mysql 6 Oct 19 03:18 data
初始化数据库,
[root@140 ~]
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 ~]
[root@140 ~]
[root@140 ~]
[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 ~]
[root@140 support-files]
[root@140 support-files]
45行
basedir=/usr/local/mysql
datadir=/opt/data
[root@140 support-files]
[root@140 support-files]
-rwxr-xr-x 1 mysql mysql 10601 Oct 19 03:29 mysqld
先启动测试一下
[root@140 ~]
Starting MySQL.Logging to '/opt/data/140.err'.
SUCCESS!
[root@140 ~]
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 ~]
Shutting down MySQL.. SUCCESS!
[root@140 ~]
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 ~]
[root@140 system]
[root@140 system]
-rw-r--r--. 1 root root 456 Apr 26 2020 sshd.service
[root@140 system]
-rw-r--r-- 1 root root 456 Oct 19 03:32 mysqld.service
[root@140 system]
[root@140 system]
[root@140 system]
[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]
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@140 system]
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]
设置密码
[root@140 system]
mysql> alter user 'root'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@140 support-files]
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 ~]
//安装依赖包
[root@Master_B ~]
//安装依赖包
[root@Masters_B]
[root@Masters_B]
//安装php环境包
[root@140 ~]
//安装php环境包
[root@140 php-8.1.11]
[root@140 php-8.1.11]
......等待
+--------------------------------------------------------------------+
| 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]
......
[root@140 php-8.1.11]
[root@140 php8]
bin etc include lib php sbin var
[root@140 php8]
[root@140 php8]
[root@140 php8]
[root@140 php8]
[root@140 php8]
/usr/local/php8/lib
[root@140 php8]
[root@140 php8]
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]
[root@140 ~]
[root@140 php-8.1.11]
[root@140 php-8.1.11]
[root@140 php-8.1.11]
[root@140 php-8.1.11]
-rwxr-xr-x 1 root root 2402 Oct 19 04:59 /etc/rc.d/init.d/php-fpm
[root@140 php-8.1.11]
/root/php-8.1.11
[root@140 php-8.1.11]
[root@140 etc]
pear.conf php-fpm.conf.default php-fpm.d
[root@140 etc]
[root@140 etc]
[root@140 php-fpm.d]
www.conf.default
[root@140 php-fpm.d]
[root@140 php-fpm.d]
36 listen = 127.0.0.1:9000
启动,并测试配置是否正确
[root@140 php-fpm.d]
[root@140 init.d]
functions php-fpm README
[root@140 init.d]
Starting php-fpm done
[root@140 init.d]
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]
Gracefully shutting down php-fpm . done
[root@140 init.d]
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]
Starting php-fpm done
[root@140 init.d]
3.4 配置nginx
[root@140 ~]
[root@140 conf]
[root@140 conf]
[root@140 conf]
//将以下内容取消注释并修改
location / {
root html;
index index.php index.html index.htm;
}
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]
[root@140 conf]
3.5 配置PHP网络界面
[root@140 ~]
[root@140 html]
[root@140 html]
<?php
phpinfo();
?>
[root@140 html]
重启
[root@140 ~]
[root@140 ~]
浏览器访问http://192.168.141.140/index.php
4. 部署
4.1 在141主机安装httpd,做静态资源
[root@141 ~]
[root@141 ~]
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@141 ~]
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 ~]
[root@141 html]
[root@141 html]
4.2 在143主机源码安装nginx并配置负载均衡器,进行调度
[root@143 ~]
--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 ~]
[root@143 ~]
[root@143 ~]
[root@143 ~]
[root@143 ~]
[root@143 ~]
drwxr-xr-x. 2 nginx nginx 6 Sep 5 20:51 /var/log/nginx
[root@143 ~]
[root@143 ~]
[root@143 nginx-1.22.0]
--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]
[root@143 nginx-1.22.0]
[root@143 ~]
[root@143 ~]
[root@143 nginx-1.22.0]
[root@143 ~]
[root@143 ~]
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 ~]
[root@143 conf]
upstream backend {
server 192.168.141.140;
server 192.168.141.141;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend;
}
[root@143 conf]
[root@143 conf]
[root@143 conf]
[root@143 conf]
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]
/usr/local/nginx/conf
[root@143 conf]
upstream static {
server 192.168.141.141;
}
upstream dynamic {
server 192.168.141.140;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://static;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
proxy_pass http://dynamic;
}
[root@143 conf]
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]
[root@143 conf]
[root@143 conf]
[root@143 conf]
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]
|