ubuntu centos nginx安装编译部署

**环境: **centos7 ubuntu18.04

概览:

  1. ubuntu编译部署nginx
  2. centos编译部署nginx
  3. 建议托管文件参考传送门

一. ubuntu部署nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
#软件:nginx
#环境:centos7
#/usr/local/src/nginx-1.13.0/configure --prefix=/usr/local/nginx
set -e
echo -e "\033[33m
                 1.此脚本只适用于首次安装部署,多次执行此脚本会覆盖nginx配置文件 \n
                 2.想部署其他版本的需要自行更改链接:http://nginx.org/download/nginx-1.13.0.tar.gz 找到指定版本即可 \n \033[0m"
echo -e "\033[36m ===========================start -初始化 \033[0m"
sleep 2
nginx_v="nginx-1.16.1.tar.gz" #"nginx-1.13.0.tar.gz"
dir="nginx-1.16.1"            #"nginx-1.13.0"
#yilai="gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel"
nginx_install_app() {
   echo -e "\033[36m 下载安装编译的依赖包 \033[0m"
   #sudo apt-get update
   #安装依赖:gcc、g++依赖库
   sudo apt-get install build-essential libtool -y
   #安装 pcre依赖库(http://www.pcre.org/)
   sudo apt-get install libpcre3 libpcre3-dev -y
   #安装 zlib依赖库(http://www.zlib.net)
   sudo apt-get install zlib1g-dev -y
   #安装ssl依赖库
   sudo apt-get install openssl -y
   sudo apt install zlib1g-dev openssl libssl-dev libpcre3 libpcre3-dev -y
  [ $? = 0 ] && echo -e "\033[36m 依赖:${yilai} 安装完成 \033[0m" || echo -e "\033[33m 依赖安装失败 \033[0m"
   echo -e "\033[36m =============开始下载nginx压缩包 \033[0m"
   wget http://nginx.org/download/${nginx_v}
  tar -zxvf ./${nginx_v} -C /usr/local/src/
}
nginx_make() {
   echo -e "\033[36m 开始编译安装:${nginx_v} \033[0m"
   cd /usr/local/src/${dir} && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_slice_module --with-stream
   echo -e "\033[36m make \033[0m"
   cd /usr/local/src/${dir} &&  make
   echo -e "\033[36m make install \033[0m"
   cd /usr/local/src/${dir} &&  make install
  [ $? = 0 ] && echo -e "\033[36m 依赖:编译安装完成全路径启动命令"/usr/local/nginx/sbin/nginx" \033[0m" || echo -e "\033[33m 编译安装失败 \033[0m"
  /usr/local/nginx/sbin/nginx -t
}
nginx_system() {
echo -e "\033[36m ==============================开始建立'nginx'托管文件 \033[0m"
cat <<EOF >/etc/systemd/system/nginx.service
[Unit]
Description=nginx server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
#Type=simple
#PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -SIGHUP \$MAINPID
ExecStop=/bin/kill -SIGINT \$MAINPID
Restart=always
RestartSec=5
#StartLimitInterval=0

[Install]
WantedBy=multi-user.target
EOF
  systemctl daemon-reload
  systemctl start nginx.service
  systemctl status nginx.service
  echo -e "\033[36m
  概览
  nginx安装完成访问方式-----------------------------------浏览器访问: http://localhost
  1. nginx执行文件全路径为:"/usr/local/nginx/sbin/nginx" 若要使用nginx命令需要以 全路径方式
  2. nginx 常用命令:
    /usr/local/nginx/sbin/nginx -v # -V 查看编译模块
    /usr/local/nginx/sbin/nginx -V
    /usr/local/nginx/sbin/nginx -t 等
  3. ----------------------------------------------------nginx版本为:
  \033[0m"
  /usr/local/nginx/sbin/nginx -v
}
main(){
  nginx_install_app
  nginx_make
  nginx_system
}
main

ubuntu22.04版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
# 软件:nginx
# 环境:Ubuntu
# 使用说明:首次安装部署脚本
# 优化:自动处理 OpenSSL 3.0 兼容性问题

set -e

# 颜色定义
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[36m'
RESET='\033[0m'

echo -e "${YELLOW}
1.此脚本只适用于首次安装部署,多次执行此脚本会覆盖nginx配置文件 \n
2.想部署其他版本的需要自行更改链接:http://nginx.org/download/nginx-1.13.0.tar.gz 找到指定版本即可 \n ${RESET}"

echo -e "${BLUE} ===========================start -初始化 ${RESET}"
sleep 2

# 版本配置
nginx_v="nginx-1.16.1.tar.gz"
dir="nginx-1.16.1"
nginx_url="http://nginx.org/download/${nginx_v}"

# Ubuntu 依赖包列表
yilai="build-essential libtool libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev wget curl"

# 检查是否为 root 用户
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}请使用 root 用户执行此脚本${RESET}"
exit 1
fi

# 检查是否已安装
if [ -f /usr/local/nginx/sbin/nginx ]; then
echo -e "${YELLOW}检测到 nginx 已安装,版本信息:${RESET}"
/usr/local/nginx/sbin/nginx -v 2>&1 || true
echo -e "${YELLOW}将继续覆盖安装...${RESET}"
sleep 2
fi

nginx_install_app() {
echo -e "${BLUE} 下载安装编译的依赖包 ${RESET}"

# Ubuntu 使用 apt-get
apt-get update
apt-get install -y ${yilai}

echo -e "${GREEN} 依赖安装完成 ${RESET}"

echo -e "${BLUE} =============开始下载nginx压缩包 ${RESET}"

# 创建源码目录
mkdir -p /usr/local/src

# 如果文件已存在,先删除
[ -f "/usr/local/src/${nginx_v}" ] && rm -f "/usr/local/src/${nginx_v}"
# 如果目录已存在,先清理
[ -d "/usr/local/src/${dir}" ] && rm -rf "/usr/local/src/${dir}"

cd /usr/local/src
wget --timeout=30 --tries=3 ${nginx_url} || {
echo -e "${RED} 下载失败,请检查网络或版本号${RESET}"
exit 1
}

tar -zxvf ${nginx_v} -C /usr/local/src/

echo -e "${GREEN} 下载和解压完成 ${RESET}"
}

nginx_make() {
echo -e "${BLUE} 开始编译安装:${nginx_v} ${RESET}"

cd /usr/local/src/${dir}

./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_slice_module \
--with-stream

if [ $? -ne 0 ]; then
echo -e "${RED} configure 失败,请检查依赖${RESET}"
exit 1
fi

# ============================================
# 关键修复:移除 -Werror 避免 OpenSSL 3.0 弃用 API 编译失败
# nginx 1.16.1 使用的 HMAC_Init_ex / ENGINE_by_id 等在 OpenSSL 3.0 中已弃用
# ============================================
if grep -q '\-Werror' objs/Makefile; then
echo -e "${YELLOW} 检测到 -Werror 标志,正在移除以兼容 OpenSSL 3.0...${RESET}"
sed -i 's/-Werror//g' objs/Makefile
echo -e "${GREEN} -Werror 已移除 ${RESET}"
fi

echo -e "${BLUE} make (使用 $(nproc) 核并行编译) ${RESET}"
make -j$(nproc)

if [ $? -ne 0 ]; then
echo -e "${RED} make 失败${RESET}"
exit 1
fi

echo -e "${BLUE} make install ${RESET}"
make install

if [ $? -eq 0 ]; then
echo -e "${GREEN} 编译安装完成,启动命令:/usr/local/nginx/sbin/nginx ${RESET}"
else
echo -e "${RED} 编译安装失败 ${RESET}"
exit 1
fi

# 验证安装
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -v
}

nginx_system() {
echo -e "${BLUE} ==============================开始建立 nginx systemd 服务文件 ${RESET}"

# 创建 nginx 系统用户(编译后创建,避免 UID 冲突)
if ! id nginx &>/dev/null; then
useradd -r -s /sbin/nologin nginx
echo -e "${GREEN} 已创建 nginx 系统用户 ${RESET}"
else
echo -e "${YELLOW} nginx 用户已存在,跳过创建 ${RESET}"
fi

# 确保日志目录存在(systemd PIDFile 需要)
mkdir -p /usr/local/nginx/logs

# 修改 nginx.conf 使用 nginx 用户
if [ -f /usr/local/nginx/conf/nginx.conf ]; then
sed -i 's/^user.*;/user nginx;/' /usr/local/nginx/conf/nginx.conf
fi

# 如果已有 nginx 服务在运行,先停止
if systemctl is-active --quiet nginx.service 2>/dev/null; then
echo -e "${YELLOW} 检测到运行中的 nginx 服务,正在停止...${RESET}"
systemctl stop nginx.service
fi

cat > /etc/systemd/system/nginx.service <<'EOF'
[Unit]
Description=nginx server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -SIGHUP $MAINPID
ExecStop=/bin/kill -SIGTERM $MAINPID
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable nginx.service

# 启动服务并捕获错误
if ! systemctl start nginx.service; then
echo -e "${RED} nginx 服务启动失败,请检查配置${RESET}"
systemctl status nginx.service --no-pager || true
exit 1
fi

sleep 2
systemctl status nginx.service --no-pager

echo -e "${BLUE}
==========================================
nginx 安装完成!
==========================================
nginx 执行文件路径:/usr/local/nginx/sbin/nginx
配置文件路径:/usr/local/nginx/conf/nginx.conf

nginx 常用命令:
/usr/local/nginx/sbin/nginx -t # 测试配置文件
/usr/local/nginx/sbin/nginx -v # 查看版本
/usr/local/nginx/sbin/nginx -V # 查看编译参数

systemctl 管理:
systemctl start nginx
systemctl stop nginx
systemctl reload nginx
systemctl status nginx

==========================================
浏览器访问: http://$(hostname -I | awk '{print $1}')
==========================================
${RESET}"
echo ""
}

main() {
nginx_install_app
nginx_make
nginx_system
}

main

二. centos部署nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
#软件:nginx
#环境:centos7
#/usr/local/src/nginx-1.13.0/configure --prefix=/usr/local/nginx
set -e
echo -e "\033[33m
                 1.此脚本只适用于首次安装部署,多次执行此脚本会覆盖nginx配置文件 \n
                 2.想部署其他版本的需要自行更改链接:http://nginx.org/download/nginx-1.13.0.tar.gz 找到指定版本即可 \n \033[0m"
echo -e "\033[36m ===========================start -初始化 \033[0m"
sleep 8
nginx_v="nginx-1.16.1.tar.gz" #"nginx-1.13.0.tar.gz"
dir="nginx-1.16.1" # "nginx-1.13.0"
yilai="gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel"
nginx_install_app() {
   echo -e "\033[36m 下载安装编译的依赖包 \033[0m"
  yum install ${yilai} -y
  [ $? = 0 ] && echo -e "\033[36m 依赖:${yilai} 安装完成 \033[0m" || echo -e "\033[33m 依赖安装失败 \033[0m"
   echo -e "\033[36m =============开始下载nginx压缩包 \033[0m"
   wget http://nginx.org/download/${nginx_v}
  tar -zxvf ./${nginx_v} -C /usr/local/src/
}
nginx_make() {
   echo -e "\033[36m 开始编译安装:${nginx_v} \033[0m"
   cd /usr/local/src/${dir} && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_slice_module --with-stream
   echo -e "\033[36m make \033[0m"
   cd /usr/local/src/${dir} &&  make
   echo -e "\033[36m make install \033[0m"
   cd /usr/local/src/${dir} &&  make install
  [ $? = 0 ] && echo -e "\033[36m 依赖:编译安装完成全路径启动命令"/usr/local/nginx/sbin/nginx" \033[0m" || echo -e "\033[33m 编译安装失败 \033[0m"
  /usr/local/nginx/sbin/nginx -t
}
nginx_system() {
   echo -e "\033[36m ==============================开始建立'nginx'托管文件 \033[0m"
cat <<EOF >/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID

[Install]
WantedBy=multi-user.target
EOF
  systemctl daemon-reload
  systemctl start nginx.service
  systemctl status nginx.service
  nginx_cmd=`/usr/local/nginx/sbin/nginx -v`
  echo -e "\033[36m
  概览
  nginx安装完成访问方式-----------------------------------浏览器访问: http://localhost
  1. 自动检查nginx安装成功--------------------------------nginx版本为:${nginx_cmd}
  2. nginx执行文件全路径为:"/usr/local/nginx/sbin/nginx" 若要使用nginx命令需要以 全路径方式
  3. nginx 常用命令:
    /usr/local/nginx/sbin/nginx -v
    /usr/local/nginx/sbin/nginx -t 等
  \033[0m"
    /usr/local/nginx/sbin/nginx -v
}
main(){
  nginx_install_app
  nginx_make
  nginx_system
}
main

结语fighting!