编译安装Nginx

安装依赖

首先安装依赖库和编译要用到的工具:

1
sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g-dev unzip git

获取必要组件

1
2
mkdir nginx    //新建文件夹,用于存放编译nginx所需的组件
cd nginx

ngx_brotli

1
2
3
4
git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
git submodule update --init
cd ../

OpenSSL

1
2
wget https://www.openssl.org/source/openssl-1.1.1.tar.gz
tar zxf openssl-1.1.1.tar.gz

pcre

1
2
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.zip
unzip pcre-8.42.zip

zlib

1
2
wget https://zlib.net/zlib-1.2.11.tar.gz
tar zxf zlib-1.2.11.tar.gz

安装 jemalloc

1
2
3
4
5
6
7
wget https://github.com/jemalloc/jemalloc/releases/download/5.1.0/jemalloc-5.1.0.tar.bz2
tar xjf jemalloc-5.1.0.tar.bz2
./configure
make -j 8
sudo make install
sudo ln -s /usr/local/lib/libjemalloc.so.2 /usr/lib/libjemalloc.so.1
sudo ldconfig

编译并安装 Nginx

1
2
3
4
5
6
7
8
sudo groupadd www
sudo useradd www -g www -s /sbin/nologin
wget https://nginx.org/download/nginx-1.14.0.tar.gz
tar zxf nginx-1.14.0.tar.gz
cd nginx-1.14.0
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --add-module=../ngx_brotli --with-openssl=../openssl-1.1.1 --with-openssl-opt='enable-tls1_3 enable-weak-ssl-ciphers' --with-pcre=../pcre-8.42 --with-pcre-jit --with-ld-opt=-ljemalloc --with-zlib=../zlib-1.2.11
make
sudo make install

创建管理脚本与自启动

1
2
sudo vim /etc/init.d/nginx
sudo systemctl daemon-reload

输入以下内容并保存:

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
#! /bin/sh

### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DAEMON=/usr/local/nginx/sbin/nginx
DAEMON_OPTS='-c /usr/local/nginx/conf/nginx.conf'
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi

set -e

case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/nginx.pid --exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/nginx.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
--exec $DAEMON
echo "$NAME."
;;
configtest)
$DAEMON -t $DAEMON_OPTS
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|configtest|force-reload}" >&2
exit 1
;;
esac

exit 0

增加执行权限:

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

现在管理 Nginx 只需使用以下命令即可:

1
sudo service nginx start|stop|restart|reload

开机自动启动 nginx

1
sudo update-rc.d -f nginx defaults