基于openEuler 24.03 LTS,基于X86 CPU实现。
服务器信息:
共计使用4台VM。
序号 | 虚拟机名称 | IP地址 | 用途 |
---|---|---|---|
1 | VM-Project-05-Task-01 | 10.10.2.51192.168.0.254 | Nginx |
2 | VM-Project-05-Task-02 | 192.168.0.2 | Apache Http Server,内部网站-A |
3 | VM-Project-05-Task-03 | 192.168.0.3 | Apache Http Server,内部网站-B-镜像-1 |
4 | VM-Project-05-Task-04 | 192.168.0.4 | Apache Http Server,内部网站-B-镜像-2 |
VM-Project-05-Task-01:
虚拟机名称:VM-Project-05-Task-01-10.10.2.51
内存:1GB
CPU:1颗1核心
虚拟硬盘:20GB
网卡:2块,网卡1为NAT,网卡2为仅主机网络
主机名:Project-05-Task-01
网卡1:
IP地址:10.10.2.51 /24
网关:10.10.2.254
DNS:10.10.2.254
网卡2:
IP地址:192.168.0.254 /24
VM-Project-05-Task-02
虚拟机名称:VM-Project-05-Task-02-192.168.0.2
内存:1GB
CPU:1颗1核心
虚拟硬盘:20GB
网卡:1块,仅主机网络
主机名:Project-05-Task-02
IP地址:192.168.0.2 /24
VM-Project-05-Task-03:
虚拟机名称:VM-Project-05-Task-03-192.168.0.3
内存:1GB
CPU:1颗1核心
虚拟硬盘:20GB
网卡:1块,仅主机网络
主机名:Project-05-Task-03
IP地址:192.168.0.3 /24
VM-Project-05-Task-04:
虚拟机名称:VM-Project-05-Task-04-192.168.0.4
内存:1GB
CPU:1颗1核心
虚拟硬盘:20GB
网卡:1块,仅主机网络
主机名:Project-05-Task-04
IP地址:192.168.0.4 /24
部署指令:
完成Project-05-Task-02部署,发布网站Sita-A。
#配置内容
#1. IP:192.168.0.2
#2. Hostname:Project-05-Task-02
#3. Install Apache Http Server
#4. Firewall
yum -y install httpd
systemctl start httpd
systemctl enable httpd
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
cat << EOF > /var/www/html/index.html
<h1>Site-A</h1>
<h3>IP:192.168.0.2</h3>
EOF
完成Project-05-Task-03部署,发布网站Sita-B Clone-1。
#配置内容
#1. IP:192.168.0.3
#2. Hostname:Project-05-Task-03
#3. Install Apache Http Server
#4. Firewall
yum -y install httpd
systemctl start httpd
systemctl enable httpd
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
cat << EOF > /var/www/html/index.html
<h1>Site-B Clone-1</h1>
<h3>IP:192.168.0.3</h3>
EOF
完成Project-05-Task-04部署,发布网站Sita-B Clone-2。
#配置内容
#1. IP:192.168.0.4
#2. Hostname:Project-05-Task-04
#3. Install Apache Http Server
#4. Firewall
yum -y install httpd
systemctl start httpd
systemctl enable httpd
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
cat << EOF > /var/www/html/index.html
<h1>Site-B Clone-2</h1>
<h3>IP:192.168.0.4</h3>
EOF
Project-05-Task-01:通过源代码编译安装Nginx
#安装编译安装所需要的工具
yum install -y tar gcc make zlib-devel pcre-devel openssl-devel
#添加nginx服务所需要的用户和用户组
groupadd nginx
useradd nginx -g nginx -s /sbin/nologin -M
tail /etc/group
tail /etc/passwd
#获取nginx软件源代码
wget https://nginx.org/download/nginx-1.27.2.tar.gz
tar -zxvf nginx-1.27.2.tar.gz
cd nginx-1.27.2
#编译安装nginx
#编译
./configure --user=nginx --group=nginx --with-stream --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_sub_module --with-http_realip_module --with-http_stub_status_module
#执行安装
make && make install
#查看nginx版本
/usr/local/nginx/sbin/nginx -v
#检查配置文件语法是否正确
/usr/local/nginx/sbin/nginx -t
#将Nginx配置为系统服务
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
#对服务的说明
Description=nginx - high performance web server
#服务的一些具体运行参数的设置
[Service]
#后台运行的形式
Type=forking
#PID文件的路径
PIDFile=/usr/local/nginx/logs/nginx.pid
#启动准备
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
#启动命令
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#重新载入命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#停止命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop
#快速停止
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
#给服务分配临时空间
PrivateTmp=true
[Install]
#服务用户的模式
WantedBy=multi-user.target
EOF
#配置Nginx系统服务配置文件的权限
chmod +x /usr/lib/systemd/system/nginx.service
#重新加载systemctl
systemctl daemon-reload
Project-05-Task-01:Nginx的配置文件
#对默认配置文件进行备份
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
#文件地址 vi /usr/local/nginx/conf/nginx.conf
html {
#配置负载均衡业务组
upstream Site-B-LB {
server 192.168.0.3:80 weight=1;
server 192.168.0.4:80 weight=3;
}
#配置业务发布
server {
location /status {
#展示Nginx基本状态信息
stub_status;
}
location /site-a {
#将所有请求转发到http://192.168.0.2:80
proxy_pass http://192.168.0.2:80/;
}
location /site-b {
#将所有请求转发到定义的网站服务器组Site-B-LB中
proxy_pass http://Site-B-LB/;
}
}
#配置日志
#定义名称为Nginx-pub-log的日志格式
log_format Nginx-pub-log
#客户端来源IP - 服务器本地时间 - 完整请求信息
'$remote_addr - [$time_local] - "$request"'
#负载均衡转发地址信息
'$upstream_addr '
#网站服务器节点返回总耗时
'ups_resp_time: $upstream_response_time '
#请求总耗时
'request_time: $request_time';
#访问日志存放在/var/log/nginx/access.log目录下,日志记录格式为main1定义的格式
access_log /var/log/nginx/access.log Nginx-pub-log;
}
#创建日志存放的路径
mkdir /var/log/nginx/
#检查配置文件语法是否正确
/usr/local/nginx/sbin/nginx -t
Project-05-Task-01:配置服务
#配置防火墙
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
firewall-cmd --list-all
#启动Nginx服务
systemctl start nginx
systemctl enable nginx
systemctl status nginx
#查看Nginx的访问日志
cat /var/log/nginx/access.log
业务测试
在本地主机,使用浏览器访问发布的业务:
在本地主机,使用浏览器访问,以查看Nginx的状态监控: