0%

Ubuntu16-04从源码编译安装Nginx

之前是通过apt安装的nginx1.14.2,方便好用,但是因为作业ppt中展示了rtmp stat.xsl文件的数据统计功能,我在本机sudo find / 'stat.xsl'没有找到这个文件,因此才决定重新编译安装nginx。(事实上,当我安装完之后我才意识到官方事例的意思是这个stat.xsl文件放在什么地方都可以,也就是说只要去github或者哪里随便下载一下这个文件就可以了,我完全是没事找事!!!)

anyway,从源码编译安装nginx的过程如下:

创建nginx用户组以及用户

1
2
sudo groupadd -r nginx
sudo useradd -r -g nginx -s /bin/false -d /usr/local/nginx -M nginx

编译依赖

requirements:

  • OpenSSL library version between 1.0.2 - 1.1.0
  • Zlib library version between 1.1.3 - 1.2.11
  • PCRE library version between 4.4 - 8.40
  • GCC Compiler
1
2
3
4
5
6
7
8
9
10
11
# PCRE version 4.4 - 8.42
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz && tar xzvf pcre-8.42.tar.gz

# zlib version 1.1.3 - 1.2.11
wget http://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz

# OpenSSL version 1.0.2 - 1.1.1
wget https://www.openssl.org/source/openssl-1.1.1b.tar.gz && tar xzvf openssl-1.1.1b.tar.gz

# latest nginx
wget https://nginx.org/download/nginx-1.16.0.tar.gz && tar zxvf nginx-1.16.0.tar.gz

编译安装

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
./configure --prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--build=Ubuntu \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--with-openssl=../openssl-1.1.1b \
--with-openssl-opt=enable-ec_nistp_64_gcc_128 \
--with-openssl-opt=no-nextprotoneg \
--with-openssl-opt=no-weak-ssl-ciphers \
--with-openssl-opt=no-ssl3 \
--with-pcre=../pcre-8.42 \
--with-pcre-jit \
--with-zlib=../zlib-1.2.11 \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_secure_link_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-debug \
--with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' \
--add-module=../nginx-rtmp-module
make
sudo make install

这里如果加上一条配置--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'会出现一个error,尝试了好几次并不能解决,Google也搜索不到这个错误,于是把这行删掉了,编译可以通过。

检查安装

1
2
3
4
5
6
7
sudo nginx -v && sudo nginx -V
# return as following
nginx version: nginx/1.16.0 (Ubuntu)
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11)
built with OpenSSL 1.1.1b 26 Feb 2019
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --build=Ubuntu --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-openssl=../openssl-1.1.1b --with-openssl-opt=enable-ec_nistp_64_gcc_128 --with-openssl-opt=no-nextprotoneg --with-openssl-opt=no-weak-ssl-ciphers --with-openssl-opt=no-ssl3 --with-pcre=../pcre-8.42 --with-pcre-jit --with-zlib=../zlib-1.2.11 --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_slice_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_v2_module --with-http_secure_link_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-debug --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --add-module=../nginx-rtmp-module

还需要下一步处理一个报错

1
2
3
4
sudo nginx -t
# Will throw this error nginx: [emerg] mkdir() "/var/lib/nginx/body" failed (2: No such file or directory)
# Just create directory
sudo mkdir -p /var/lib/nginx && sudo nginx -t

添加service

1
sudo vim /etc/systemd/system/nginx.service

把下面这段复制粘贴进去:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

检查service状态

1
sudo systemctl status nginx

但是这里我做完之后报错没有nginx.service文件,纳闷了半天,然后其实重启就好了!

至此,nginx就编译安装好了,可以:

  • 访问localhost查看
  • sudo vim /etc/nginx/nginx.confxiugai 配置文件
  • sudo nginx -t检查配置文件合法性
  • sudo nginx -s reload在不重启服务的情况下reload配置

参考文献

How to Compile Nginx From Source on Ubuntu 16.04

Building nginx from Sources


欢迎关注我的其它发布渠道