Install-EMQX-Offline-In-CentOS8
一、相关链接
- CentOS官网
- CentOS下载-阿里云镜像站
- Linux依赖包查询 rpm find
- Linux依赖包查询 pkgs
- Linux依赖包查询 aliyun
- Linux依赖包查询 pbone
- EMQX 开源版下载
- EMQX 使用指南
- EMQX 集群配置
- EMQX 集群架构
- EMQX 配置手册
- MQTTX 官网
- Nginx 官网
- HAProxy官网
二、环境介绍
| 名称 | 版本 | 备注 |
|---|---|---|
| 操作系统 | CentOS 8.5.2111 | 阿里云镜像站 CentOS 8.5 |
| CentOS依赖 | - | 阿里云镜像站 BaseOS Packages |
| EMQX安装包 | EMQX-v5.8.5 | RHEL 8(CentOS 8) amd64 / tar.gz |
| 集群网关 | Nginx-1.26.3 | nginx-1.26.3 |
| 集群网关 | HAproxy-3.1.5 | haproxy-3.1.5 |
三、准备工作
由于生产服务器无法连接外网,可以先在本地搭建一套与生产版本一致的虚拟机,所有需要离线安装的软件包都预先在虚拟机中进行安装测试,保证兼容性及依赖包的完整性,避免到生产服务器出现不兼容或缺少依赖的请况。
1. 系统安装
系统安装与CentOS7之前有较大区别,可以参考这篇文章
重点是在安装信息摘要页面,按照以下顺序设置
- Root用户密码
- 网络和主机名 此处的MAC地址需要和虚拟机
设置-网络页面的MAC地址一致 - 安装目的地
- 安装源
http://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/ - 软件选择 最小安装(yum ssh curl ping ip)

如果要设置固定IP,可以修改网络配置文件,手动设置IP、MAC地址、网关、子网掩码、DNS等信息
# 查看网络配置
cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
# 修改网络配置
vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
BOOTPROTO=static
IPADDR=192.168.137.101
# MACADDR=08:00:27:D7:88:87
HWADDR=08:00:27:D7:88:87
GATEWAY=192.168.137.1
NETMASK=255.255.255.0
DNS1=114.114.114.114
# 重启网络服务
systemctl restart NetworkManager
# 测试与宿主机 公网的网络互通
ping -c4 192.168.137.1
ping -c4 jd.com
2. 修改yum源
Errors during downloading metadata for repository 'appstream':
- Curl error (6): Couldn't resolve host name for http://mirrorlist.centos.org/?release=8&arch=x86_64&repo=AppStream&infra=stock [Could not resolve host: mirrorlist.centos.org]
错误:为仓库 'appstream' 下载元数据失败 : Cannot prepare internal mirrorlist: Curl error (6): Couldn't resolve host name for http://mirrorlist.centos.org/?release=8&arch=x86_64&repo=AppStream&infra=stock [Could not resolve host: mirrorlist.centos.org]
由于 CentOS 团队已于 2021 年 12 月 31 日对 CentOS Linux 8 停止更新和维护,并从官方镜像中移除 CentOS 8 的所有包,因此导致在使用 yum 源安装或更新会报上述错误,解决方案可参考这篇文章,下面直接用阿里云软件源替换
- 备份软件源
# 进入仓库源文件目录下
cd /etc/yum.repos.d/
# 备份
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/* /etc/yum.repos.d/backup/
- 使用阿里云软件源
# 如果没有wget,可以用curl下载
# wget http://mirrors.aliyun.com/repo/Centos-8.repo -O /etc/yum.repos.d/CentOS-Linux-BaseOS.repo
curl http://mirrors.aliyun.com/repo/Centos-8.repo -o /etc/yum.repos.d/CentOS-Linux-BaseOS.repo
- 更新软件源
# 清空原有缓存并重新生成
yum clean all && yum makecache
# 更新软件包(可选)
yum update -y
3. 查找依赖包
由于离线安装时无法在线安装依赖包,故需要提前准备好相关的依赖包,如何查询并安装依赖包可以参考这篇文章
- 安装依赖
# 如果无法在线安装,可以在阿里云镜像站找到 https://mirrors.aliyun.com/centos/8.5.2111/BaseOS/x86_64/os/Packages/
yum -y install yum-utils
# 测试下载依赖
mkdir -p /root/lib/{tar,unzip,wget,vim,net-tools}
yumdownloader --resolve --destdir=/root/lib/tar tar
yumdownloader --resolve --destdir=/root/lib/unzip unzip
yumdownloader --resolve --destdir=/root/lib/wget wget
yumdownloader --resolve --destdir=/root/lib/vim vim
yumdownloader --resolve --destdir=/root/lib/net-tools net-tools
- 查看提供某个命令/value的rpm包
yum provides unzip
- 查看 rpm 包所需的全部依赖
yum deplist unzip-6.0-45.el8_4
四、测试机安装EMQX
1. 下载依赖包
yumdownloader --resolve --destdir=/root/lib/tar tar
yumdownloader --resolve --destdir=/root/lib/unzip unzip
yumdownloader --resolve --destdir=/root/lib/wget wget
yumdownloader --resolve --destdir=/root/lib/vim vim
yumdownloader --resolve --destdir=/root/lib/net-tools net-tools
yumdownloader --resolve --destdir=/root/lib/libatomic libatomic
yumdownloader --resolve --destdir=/root/lib/chrony chrony
# 如果openssl版本低于1.1,需要进行升级 openssl version
yumdownloader --resolve --destdir=/root/lib/epel-release epel-release
yumdownloader --resolve --destdir=/root/lib/openssl11 openssl11
yumdownloader --resolve --destdir=/root/lib/openssl11-devel openssl11-devel
# 打包为压缩包,方便下载到本地
tar -zcvf lib.tar.gz ./lib/*
# 将依赖同步到其他服务器
scp -r /root/lib/* root@192.168.137.102:/root/lib/
# 上传到服务器之后解压
tar -zxvf lib.tar.gz -C ./
2. rpm 安装
# 下载 wget https://www.emqx.com/zh/downloads/broker/5.8.5/emqx-5.8.5-el8-amd64.rpm
curl https://packages.emqx.net/emqx-ce/v5.8.5/emqx-5.8.5-el8-amd64.rpm -o emqx-5.8.5-el8-amd64.rpm
# 安装
sudo yum install -y emqx-5.8.5-el8-amd64.rpm
# 启动 停止命令
sudo systemctl start emqx
sudo systemctl status emqx
sudo systemctl stop emqx
sudo systemctl restart emqx
# 查看配置文件
cat /etc/emqx/emqx.conf
# 查询安装的包
sudo rpm -qa | grep emqx
# 卸载
sudo rpm -e emqx-5.8.5-1.el8.x86_64
安装完成后相关文件可以从下面位置找到
/usr/lib/emqx安装文件路径/usr/bin/emqx执行文件路径(软链接)/etc/emqx/配置文件路径/var/log/emqx日志文件路径
3. tar.gz 安装
# 下载 wget https://www.emqx.com/zh/downloads/broker/5.8.5/emqx-5.8.5-el8-amd64.tar.gz
curl https://packages.emqx.net/emqx-ce/v5.8.5/emqx-5.8.5-el8-amd64.tar.gz -o emqx-5.8.5-el8-amd64.tar.gz
# 安装 tar
rpm -ivh ./lib/tar-1.30-5.el8.x86_64.rpm
# 安装
mkdir -p /opt/emqx && tar -zxvf emqx-5.8.5-el8-amd64.tar.gz -C /opt/emqx
# 启动 停止命令
cd /opt/emqx
./bin/emqx start
./bin/emqx stop
./bin/emqx console
安装完成后相关文件可以从下面位置找到
/opt/emqx安装文件路径/opt/emqx/bin执行文件路径/opt/emqx/etc/配置文件路径/opt/emqx/log日志文件路径
4. 将EMQX注册为系统服务
cat /etc/systemd/system/emqx.service
cat > /etc/systemd/system/emqx.service << 'EOF'
[Unit]
Description=EMQX Broker
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/opt/emqx/bin/emqx start
ExecStop=/opt/emqx/bin/emqx stop
ExecReload=/opt/emqx/bin/emqx restart
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
EOF
# 设置开机自启
systemctl enable emqx
# 启停命令
service emqx start
service emqx stop
service emqx restart
五、集群安装配置
假设现在有三台服务器,其主机名和ip分别如下
| 主机名 | IP |
|---|---|
| emqx-01 | 192.168.137.101 |
| emqx-02 | 192.168.137.102 |
| emqx-03 | 192.168.137.103 |
1. 测试节点网络
修改hosts文件,确保通过域名(主机名)可以相互访问
cat >> /etc/hosts << EOF
192.168.137.101 emqx-01
192.168.137.102 emqx-02
192.168.137.103 emqx-03
EOF
检查网络互通及延迟情况
ping -c4 emqx-01
ping -c4 emqx-02
ping -c4 emqx-03
注意 官方文档要求各节点之间的延迟不高于10ms,过高的延迟可能造成服务不可用
2. 开放服务器端口
为保证集群节点之间的通讯,需要开放以下端口
| 端口 | 描述 |
|---|---|
| 1883 | MQTT监听端口 |
| 8083 | WebSocket监听端口 |
| 18083 | Web控制台端口 |
| 4370 | Erlang 分布式传输端口 |
| 5370 | 集群 RPC 端口,适用于物理机环境 |
| 5369 | 集群 RPC 端口,适用于 Docker 环境 |
firewall-cmd --zone=public --add-port=1883/tcp --permanent
firewall-cmd --zone=public --add-port=8083/tcp --permanent
firewall-cmd --zone=public --add-port=18083/tcp --permanent
firewall-cmd --zone=public --add-port=4370/tcp --permanent
firewall-cmd --zone=public --add-port=5370/tcp --permanent
firewall-cmd --zone=public --add-port=5369/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-port
3. 时间同步配置
由于EMQX集群对服务器的网络延迟有严格要求,所以必须保证各节点之间的时间同步正常且延迟低于10ms,如果服务器上没有时间同步配置,需要安装配置chrony,可以参考这篇文章
- 安装chrony服务
# 检查是否有 ntpd 或 chronyd 服务
systemctl status ntpd
systemctl status chronyd
# 安装chrony
rpm -ivh /root/lib/chrony/timedatex-0.5-3.el8.x86_64.rpm
rpm -ivh /root/lib/chrony/chrony-4.1-1.el8.x86_64.rpm
# 配置文件
cat /etc/chrony.conf
- 启动并设置开机自启
# 启动
systemctl start chronyd
systemctl status chronyd
# 开机自启
systemctl enable chronyd
- 配置chrony
# 修改配置文件
vim /etc/chrony.conf
# 允许所有客户端同步时间
allow all
# 上游NTP服务
server 210.72.145.44 iburst
server cn.pool.ntp.org iburst
server cn.ntp.org.cn iburst
# 阿里
pool ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
server ntp5.aliyun.com iburst
server ntp6.aliyun.com iburst
server ntp7.aliyun.com iburst

- 状态检查
# 检查时区
timedatectl
# 修改时区
timedatectl set-timezone Asia/Shanghai
# 查看时间源
chronyc sources -v
# 查看当前时间状态
chronyc tracking
# 手动同步时间
chronyc -a makestep

4. 检测时钟回拨
时钟回拨会导致Erlang虚拟机退出,在正式部署前可以先执行脚本检测以下,避免正式使用时出现这个问题
实测 5.8.5 版本在时钟回拨会退出程序,可以将EMQX版本退回到 5.0.26,这个版本不会因为时钟回拨而退出
- 监测时钟回拨脚本
# 写入脚本
cat > timestamp.sh << 'EOF'
#!/bin/bash
# 基础配置
LOG_DIR="$(dirname "$0")/logs" # 日志存储目录
RETENTION_DAYS=7 # 日志保留天数
MONITOR_INTERVAL=600 # 性能监控间隔(秒)
NTP_CHECK_INTERVAL=7200 # NTP检查间隔(秒)
# 初始化目录
mkdir -p "$LOG_DIR"
# 动态日志路径管理
current_date=$(date +"%Y-%m-%d")
time_log="${LOG_DIR}/time_info_${current_date}.log"
error_log="${LOG_DIR}/time_error_${current_date}.log"
perf_log="${LOG_DIR}/performance_${current_date}.log"
# 初始化跟踪变量
last_timestamp=0
last_ntp_check=$(date +%s)
last_perf_check=$(date +%s)
# 函数:日志切割检查
check_log_rotation() {
local today=$(date +"%Y-%m-%d")
if [ "$today" != "$current_date" ]; then
current_date="$today"
time_log="${LOG_DIR}/time_info_${current_date}.log"
error_log="${LOG_DIR}/time_error_${current_date}.log"
perf_log="${LOG_DIR}/performance_${current_date}.log"
echo "[INFO] Rotated log files to ${current_date}" >> "$time_log"
fi
}
# 函数:性能监控
monitor_performance() {
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local process_stats=$(ps -p $$ -o %cpu,%mem --no-headers | tr -d ' ')
local loadavg=$(awk '{print $1,$2,$3}' /proc/loadavg)
local mem_info=$(free -m | awk '/Mem/{printf "%.2f%%", $3/$2*100}')
echo "[PERF] $timestamp | CPU: ${process_stats%%,*}% | MEM: ${process_stats##*,}% | Load: $loadavg | MemUsage: $mem_info" >> "$perf_log"
}
# 函数:清理旧日志
cleanup_old_logs() {
find "$LOG_DIR" -maxdepth 1 -type f \( -name "*.log" \) \
-mtime +$RETENTION_DAYS \
-exec rm -fv {} \; >> "$time_log" 2>&1
}
# 函数:NTP状态检测
check_ntp_status() {
# 优先检查ntpstat命令
if command -v ntpstat >/dev/null 2>&1; then
if ! ntpstat >/dev/null 2>&1; then
echo "[WARNING] NTP sync abnormal (ntpstat check failed)" >> "$ERROR_LOG_FILE"
fi
# 次选检查chrony服务
elif systemctl is-active chronyd >/dev/null 2>&1; then
if ! chronyc tracking >/dev/null 2>&1; then
echo "[WARNING] Chrony sync abnormal (chronyc check failed)" >> "$ERROR_LOG_FILE"
fi
# 最后检查ntpd服务
elif systemctl is-active ntpd >/dev/null 2>&1; then
ntpq -pn | grep -q '^+'
if [ $? -ne 0 ]; then
echo "[WARNING] NTPD sync abnormal (no reachable servers)" >> "$ERROR_LOG_FILE"
fi
else
echo "[WARNING] No NTP service found (chronyd/ntpd not running)" >> "$ERROR_LOG_FILE"
fi
}
# 主监控循环
while true; do
now=$(date +%s)
# ========== 日志切割检查 ==========
check_log_rotation
# ========== 时间记录与回拨检测 ==========
current_time=$(date +"%Y-%m-%d %H:%M:%S.%3N")
now_seconds=$(date +%s)
now_millis=$(date +%3N | sed 's/^0*//') # 移除前导零
# 关键修复:显式声明十进制数值
current_timestamp=$(( now_seconds * 1000 + 10#$now_millis ))
echo "[TIME] $current_time" >> "$time_log"
# 时钟回拨检测
if [ $last_timestamp -gt 0 ] && [ $current_timestamp -lt $last_timestamp ]; then
error_msg="[ERROR] Clock rollback - ${last_timestamp} - ${current_timestamp} ms | Details: $current_time"
echo "$error_msg" >> "$error_log"
fi
last_timestamp=$current_timestamp
# ========== 定期任务调度 ==========
# 性能监控(每10分钟)
if [ $((now - last_perf_check)) -ge $MONITOR_INTERVAL ]; then
monitor_performance
last_perf_check=$now
fi
# NTP检查(每2小时)
if [ $((now - last_ntp_check)) -ge $NTP_CHECK_INTERVAL ]; then
check_ntp_status
last_ntp_check=$now
fi
# 修复点:使用字符串比较代替数值比较
if [ "$(date +"%H%M")" = "0005" ]; then
cleanup_old_logs
sleep 10
fi
sleep 0.9
done
EOF
- 运行脚本,检查日志
# 运行脚本
chmod +x ./timestamp.sh
nohup ./timestamp.sh > /dev/null 2>&1 &
# 查看打印记录
tail -f ./logs/time_info_2025-03-13.log
tail -f ./logs/time_error_2025-03-13.log
tail -f ./logs/performance_2025-03-13.log
5. 试运行EMQX
- 下载运行EMQX
# 下载安装包
# curl https://packages.emqx.net/emqx-ce/v5.0.26/emqx-5.0.26-el8-amd64.rpm -o emqx-5.0.26-el8-amd64.rpm
curl https://packages.emqx.net/emqx-ce/v5.0.26/emqx-5.0.26-el8-amd64.tar.gz -o emqx-5.0.26-el8-amd64.tar.gz
# 安装
mkdir -p /opt/emqx && tar -zxvf emqx-5.0.26-el8-amd64.tar.gz -C /opt/emqx
# 控制台启动验证
cd /opt/emqx
./bin/emqx console
# 如果报错缺少 libatomic.so.1,需要手动安装
scp /root/lib/libatomic/* root@192.168.137.102:/root/lib/libatomic/
rpm -ivh /root/lib/libatomic/libatomic-8.5.0-4.el8_5.x86_64.rpm
- 查看EMQX状态
# 启动停止命令
./bin/emqx start
./bin/emqx stop
# 查看服务运行状态
ps -ef | grep emqx
# 查看集群状态
./emqx/bin/emqx ctl cluster status
- 将EMQX注册为系统服务
cat /etc/systemd/system/emqx.service
cat > /etc/systemd/system/emqx.service << 'EOF'
[Unit]
Description=EMQX Broker
After=network.target
[Service]
Type=forking
ExecStart=/opt/emqx/bin/emqx start
ExecStop=/opt/emqx/bin/emqx stop
ExecReload=/opt/emqx/bin/emqx restart
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
EOF
# 设置开机自启
systemctl enable emqx
6. 集群配置
- 基于 static 节点列表自动集群
- 基于 DNS 自动集群
- 配置手册-Cluster
- 获取真实 IP 和客户端 TSL 证书信息
由于开源版本只能部 署core集群,所以三个节点 role 都定义为 core,
修改
node.name为当前节点的ip,设置集群发现策略discovery_strategy为static以及 节点地址static.seeds
# 备份配置文件
cp ./etc/emqx.conf ./etc/emqx.conf.bakup
# 修改配置文件
vim ./etc/emqx.conf
# 修改以下内容
node {
name = "emqx@192.168.137.101"
}
cluster{
autoheal = true
discovery_strategy = static
static {
seeds = ["emqx@192.168.137.101", "emqx@192.168.137.102", "emqx@192.168.137.103"]
}
}
listeners.tcp.default {
bind = "0.0.0.0:1883"
max_connections = 102400
proxy_protocol = true
}
rm -rf /opt/emqx/etc/emqx.conf
cat > /opt/emqx/etc/emqx.conf << 'EOF'
## NOTE:
## This config file overrides data/configs/cluster.hocon,
## and is merged with environment variables which start with 'EMQX_' prefix.
##
## Config changes made from EMQX dashboard UI, management HTTP API, or CLI
## are stored in data/configs/cluster.hocon.
## To avoid confusion, please do not store the same configs in both files.
##
## See https://docs.emqx.com/en/enterprise/v5.0/configuration/configuration.html
## Configuration full example can be found in emqx.conf.example
node {
name = "emqx@192.168.137.101"
cookie = "emqxsecretcookie"
data_dir = "data"
}
cluster {
name = emqxcl
autoheal = true
discovery_strategy = static
static {
seeds = ["emqx@192.168.137.101", "emqx@192.168.137.102", "emqx@192.168.137.103"]
}
}
listeners.tcp.default {
bind = "0.0.0.0:1883"
max_connections = 102400
proxy_protocol = true
}
dashboard {
listeners {
http.bind = 18083
# 5.0.26 不能有以下配置
# https.bind = 18084
# https {
# ssl_options {
# certfile = "${EMQX_ETC_DIR}/certs/cert.pem"
# keyfile = "${EMQX_ETC_DIR}/certs/key.pem"
# }
# }
}
}
authorization {
deny_action = ignore
no_match = allow
cache = { enable = true }
}
EOF

7. 启动EMQX服务
配置完成后启动服务
yum install -y emqx-5.8.5-el8-amd64.rpm
systemctl start emqx
systemctl status emqx
mv /etc/emqx/emqx.conf /etc/emqx/emqx.conf.bakup
mv ./emqx/etc/emqx.conf /etc/emqx/emqx.conf
cat /etc/emqx/emqx.conf
tail -f /var/log/emqx/emqx.log.1
systemctl restart emqx

访问 Dashboard
- 账号密码 admin /public

六、验证集群发布与订阅
-
打开MQTTX客户端,连接到集群的各个节点

-
每个节点订阅相同的Topic

-
从一个节点发出测试消息,可以看到三个节点都收到了订阅消息

七、Nginx网关负载均衡
负载均衡(Load Balancing)用于均衡多个网络组件的负载,从而优化资源的使用,避免由于组件过载造成故障。负载均衡虽然不是集群中的必备组件,但是能给集群带来一些非常有用的特性,例如当配置在 EMQX 集群中时,将能带来如下优势:
- 均衡 EMQX 的负载,避免出现单节点过载的情况;
- 简化客户端配置,客户端只需连接到负载均衡器上,无需关心集群内部伸缩变化;
- TLS/SSL 终结,减轻 EMQX 集群的负担;
- 提高安全性,有了负载均衡在集群前端,能够通过设置阻止不需要的流量,保护 EMQX 集群免受恶意攻击。
本节我们选择用 Nginx 来作为 EMQX 集群的网关,最终搭建的效果如下所示:

1. 环境准备
1. 安装编译依赖库
# 安装nginx必备依赖库
yum install -y gcc glibc-devel pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 下载依赖包
yumdownloader --resolve --destdir=/root/lib/nginx/make make
yumdownloader --resolve --destdir=/root/lib/nginx/gcc gcc
yumdownloader --resolve --destdir=/root/lib/nginx/glibc-devel glibc-devel
yumdownloader --resolve --destdir=/root/lib/nginx/pcre pcre
yumdownloader --resolve --destdir=/root/lib/nginx/pcre-devel pcre-devel
yumdownloader --resolve --destdir=/root/lib/nginx/zlib zlib
yumdownloader --resolve --destdir=/root/lib/nginx/zlib-devel zlib-devel
yumdownloader --resolve --destdir=/root/lib/nginx/openssl openssl
yumdownloader --resolve --destdir=/root/lib/nginx/openssl-devel openssl-devel
# 安装依赖
rpm -ivh /root/lib/nginx/make/*
rpm -ivh /root/lib/nginx/gcc/*
rpm -ivh /root/lib/nginx/glibc-devel/*
rpm -ivh /root/lib/nginx/pcre/*
rpm -ivh /root/lib/nginx/pcre-devel/*
rpm -ivh /root/lib/nginx/zlib/*
rpm -ivh /root/lib/nginx/zlib-devel/*
rpm -ivh /root/lib/nginx/openssl/*
rpm -ivh /root/lib/nginx/openssl-devel/*
2. 开放代理所需端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --zone=public --add-port=1883/tcp --permanent
firewall-cmd --zone=public --add-port=8883/tcp --permanent
firewall-cmd --zone=public --add-port=1080/tcp --permanent
firewall-cmd --zone=public --add-port=1443/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-port
| 端口 | 描述 |
|---|---|
| 80 | Nginx HTTP端口 |
| 443 | Nginx HTTPS端口 |
| 1883 | 反向代理 MQTT端口 |
| 8883 | 反向代理 MQTT SSL端口 |
| 1080 | 反向代理 MQTT WebSocket端口 |
| 1443 | 反向代理 MQTT WebSocket SSL端口 |
2. 下载安装Nginx
1. 下载
# 下载
curl https://nginx.org/download/nginx-1.26.3.tar.gz -o nginx-1.26.3.tar.gz
# 解压
tar -zxvf nginx-1.26.3.tar.gz
cd nginx-1.26.3
2. 编译配置
# 查看configure 支持的参数,其中包含指定某些路径、开启某些模块、编译中特殊参数
./configure --help | more
# 编译配置
./configure \
--with-threads \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-stream \
--with-stream_ssl_module \
--prefix=/usr/local/nginx
# 输出各种文件的路径
Configuration summary
+ using threads
+ using system PCRE2 library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
--with-http_ssl_module参数用于添加 SSL 功能支持,--with-stream与--with-stream_ssl_module参数用于添加 TCP 反向代理支持--prefix=/nginx指定安装目录
3. 安装
# 编译
make
# 安装
make install
# 创建 Nginx 用户和组
groupadd -r nginx
useradd -s /sbin/nologin -r -g nginx nginx
4. 将Nginx注册为服务
cat > /etc/systemd/system/nginx.service << 'EOF'
[Unit]
Description=The Nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
5. 启动服务并设置开机自启
# 启动
service nginx start
# 停止
service nginx stop
# 状态
service nginx status
# 开机自启
systemctl enable nginx
6. 前端页面访问
-
浏览器打开网页
http://192.168.137.110
-
在
nginx.conf中加入监控配置
# NGINX 状态监控
location /stats {
stub_status on;
access_log off;
}
浏览器打开网页 http://192.168.137.110/stats

3. 配置Nginx
1. 创建配置文件夹,生成SSL证书
mkdir -p /usr/local/nginx/{conf.d,stream,certs}
cd /usr/local/nginx/certs
# 生成私钥,需要输入密码 123456
openssl genrsa -des3 -out emqx.pass.key 2048
# 删除私钥中的密码
openssl rsa -in emqx.pass.key -out emqx.key
# 生成CSR
openssl req -new -key emqx.key -out emqx.csr -subj "/C=CN/ST=Shanghai/L=Shanghai/O=PISX/OU=Diginn/CN=*.example.com"
# 生成证书
openssl x509 -req -days 3650 -in emqx.csr -signkey emqx.key -out emqx.pem
2. 主配置文件 nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
# NGINX 状态监控
location /stats {
stub_status on;
access_log off;
}
#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;
}
}
include /usr/local/nginx/conf.d/*.conf;
}
stream {
log_format proxy '$remote_addr - [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time - "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log logs/tcp_access.log proxy;
error_log logs/tcp_error.log;
# 引入 stream 配置
include /usr/local/nginx/stream.d/*.conf;
}