Ubuntu 16.04下编译安装nginx

文章是记录自己安装的一个过程,其中的配置完全是个人的需求。

(你可能对[Ubuntu 16.04下编译安装php7.2] 也感兴趣~)

前期准备

  • 安装一大堆东西
1
sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g-dev unzip git

这是必须的,因为编译前的配置需要它们的支持。

  • 新建nginx用户组和nginx用户
1
2
sudo groupadd -r nginx
sudo useradd -r -g nginx -s /bin/false -d /usr/local/nginx -M nginx

获取必要组件

nginx-ct 和 ngx-brotli 与本文主题无关,不过都是常用的 Nginx 组件,一并记录在这里。

  • nginx-ct

nginx-ct 模块用于启用 Certificate Transparency 功能。直接从 github 上获取源码:

1
2
wget -O nginx-ct.tar.gz -c https://github.com/grahamedgecombe/nginx-ct/archive/v1.3.2.tar.gz
tar -zxvf nginx-ct.tar.gz
  • ngx_brotli

Google 开发的 Brotli 压缩格式,它通过内置分析大量网页得出的字典,实现了更高的压缩比率,同时几乎不影响压缩 / 解压速度。 以前要想支持 ngx_brotli 模块,需要先手动编译 libbrotli。经评论里的朋友提醒,现在已经不用了。 直接获取源码即可:

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

由于系统自带的 OpenSSL 库往往不够新,推荐在编译 Nginx 时指定 OpenSSL 源码目录,而不是使用系统自带的版本,这样更可控。

1
2
3
wget -c https://github.com/openssl/openssl/archive/OpenSSL_1_0_2n.tar.gz
tar -zxvf OpenSSL_1_0_2n.tar.gz
mv openssl-OpenSSL_1_0_2n openssl

编译并安装 Nginx

接着就可以获取 Nginx 源码:

1
2
wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
tar zxvf nginx-1.12.2.tar.gz

配置编译和安装:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
./configure \
--user=nginx \
--group=nginx \
--add-module=../ngx_brotli \
--add-module=../nginx-ct-1.3.2 \
--with-openssl=../openssl \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_realip_module

提示如下信息就可以往下编译安装了

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Configuration summary
  + using system PCRE library
  + using OpenSSL library: ../openssl
  + 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"

编译安装:

1
make && make install

除了 http_v2 和 http_ssl 这两个 HTTP/2 必备模块之外,我还额外启用了 http_gzip_static,需要启用哪些模块需要根据自己实际情况来决定(注:从 Nginx 1.11.5 开始,ipv6 模块已经内置,故 –with-ipv6 配置项已被移除)。 以上步骤会把 Nginx 装到 /usr/local/nginx/ 目录,如需更改路径可以在 configure 时指定。

启动、停止、重载配置、测试配置文件

1
2
3
4
5
6
sudo /usr/local/nginx/sbin/nginx    #启动
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #选定配置文件启动
sudo /usr/local/nginx/sbin/nginx -t #检测配置文件是否正确 
sudo /usr/local/nginx/sbin/nginx -s stop #停止服务
sudo /usr/local/nginx/sbin/nginx -s reload #重载配置文件
sudo /usr/local/nginx/sbin/nginx -s stop, quit, reopen, reload

配置Nginx

  • 配置环境变量

配置Nginx_HOME

1
vim /etc/profile

在最底下一行的上面添加如下内容:

1
2
3
#set for Nginx
export Nginx_HOME=/usr/local/nginx
export PATH=$Nginx_HOME/sbin:$PATH

保存并退出,编译/etc/profile 使配置生效

1
source /etc/profile
  • 配置nginx.conf
1
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

以下内容是我的配置

 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
user www-data;
worker_processes auto;
pid logs/nginx.pid;
events {
    worker_connections  1024;
}
http {

        ##
        # Basic Settings
        ##
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        include mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log logs/access.log;
        error_log logs/error.log;

        ##
        # Gzip Settings
        ##
        gzip on;
        gzip_disable "msie6";
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_types text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype font/ttf application/x-font-ttf application/vnd.ms-fontobject image/x-icon;

        ##
        # brotli Settings
        ##
        brotli on;
        brotli_comp_level 6;
        brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml

        ##
        # Virtual Host Configs
        ##
        include /srv/www/nginx/conf.d/*.conf;
        include /srv/www/nginx/sites-enabled/*;
}

Nginx开机启动

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
vim /etc/systemd/system/nginx.service

[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
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

保存以后,设置开机自启以及开启服务:

1
2
systemctl enable nginx.service
systemctl start nginx.service

P.s:卸载Nginx命令 make uninstall&&make clean

参考: Jerry Qu博客 Nginx 配置之完整篇 NGINX systemd service file

Licensed under CC BY-NC-SA 4.0
最后更新于 May 10, 2019 19:20 UTC
点击刷新🚌