Nginx编译安装

摘要

编译安装Nginx

创建用户

1
2
groupadd --system nginx
useradd --system -g nginx -d /usr/lib/nginx -s /sbin/nologin -c "Nginx web server" nginx

下载Nginx

1
wget http://nginx.org/download/nginx-1.15.9.tar.gz

解决依赖

1
yum -y install pcre-devel openssl-devel gcc make

解压

1
2
tar zxf nginx-1.15.9.tar.gz
cd nginx-1.15.9

生成配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-file-aio \
--with-http_realip_module \
--with-stream #支持stream代理
#--add-module=/root/fastdfs-nginx-module/src #fastdfs模块
#--add-module=/root/nginx-1.18.0/echo-nginx-module-0.62 #echo模块

编译安装

1
make && make install

自启动

创建nginx.service文件并插入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cat > /usr/lib/systemd/system/nginx.service <<'EOF'
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
WorkingDirectory=/usr/local/nginx
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

Alpine Linux

安装依赖

1
apk add gcc g++ make openssl-dev pcre-dev zlib-dev

生成配置

1
2
3
4
5
6
7
8
9
./configure \
--prefix=/usr/local/nginx \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-stream

编译

1
make && make install

创建nginx的openrc文件

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
cat /etc/init.d/nginx
#!/sbin/openrc-run

description="Nginx http and reverse proxy server"
extra_commands="checkconfig"
extra_started_commands="reload reopen upgrade"

cfgfile=${cfgfile:-/usr/local/nginx/conf/nginx.conf}
pidfile=/usr/local/nginx/logs/nginx.pid
command=${command:-/usr/local/nginx/sbin/nginx}
command_args="-c $cfgfile"
required_files="$cfgfile"

depend() {
need net
use dns logger netmount
}

start_pre() {
#checkpath --directory --owner nginx:nginx ${pidfile%/*}
$command $command_args -t -q
}

checkconfig() {
ebegin "Checking $RC_SVCNAME configuration"
start_pre
eend $?
}

reload() {
ebegin "Reloading $RC_SVCNAME configuration"
start_pre && start-stop-daemon --signal HUP --pidfile $pidfile
eend $?
}

reopen() {
ebegin "Reopening $RC_SVCNAME log files"
start-stop-daemon --signal USR1 --pidfile $pidfile
eend $?
}

upgrade() {
start_pre || return 1

ebegin "Upgrading $RC_SVCNAME binary"

einfo "Sending USR2 to old binary"
start-stop-daemon --signal USR2 --pidfile $pidfile

einfo "Sleeping 3 seconds before pid-files checking"
sleep 3

if [ ! -f $pidfile.oldbin ]; then
eerror "File with old pid ($pidfile.oldbin) not found"
return 1
fi

if [ ! -f $pidfile ]; then
eerror "New binary failed to start"
return 1
fi

einfo "Sleeping 3 seconds before WINCH"
sleep 3 ; start-stop-daemon --signal 28 --pidfile $pidfile.oldbin

einfo "Sending QUIT to old binary"
start-stop-daemon --signal QUIT --pidfile $pidfile.oldbin

einfo "Upgrade completed"

eend $? "Upgrade failed"
}

授权可执行

1
chmod +x /etc/init.d/nginx

更新Nginx

包括升级Nginx版本或者重新编译添加模块

下载Nginx

1
wget http://nginx.org/download/nginx-1.16.0.tar.gz

解决依赖

1
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ automake autoconf libtool make

解压

1
tar zxf nginx-1.14.0.tar.gz && cd nginx-1.16.0

重新配置

比如重新编译添加支持fastdfs的模块

1
2
3
4
5
6
7
8
9
10
11
12
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-file-aio \
--with-http_realip_module \
--add-module=/root/fastdfs-nginx-module/src

重新编译

注意:不进行make install操作

1
make

替换新的Nginx

make完之后在objs目录下就多了个nginx,这个就是新版本的程序了

备份旧的Nginx程序

1
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

把新的Nginx程序覆盖旧的

1
cp objs/nginx /usr/local/nginx/sbin/nginx

如果提示cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy

建议使用如下语句 cp

1
cp -rfp objs/nginx /usr/local/nginx/sbin/nginx

测试新的Nginx程序是否正确

1
/usr/local/nginx/sbin/nginx -t

平滑重启Nginx

1
/usr/local/nginx/sbin/nginx -s reload

查看Nginx版本极其编译参数

1
/usr/local/nginx/sbin/nginx -V