Ubuntu 编译安装 Nginx 及拓展支持
本文最后更新于 1839 天前,其中的信息可能已经有所发展或是发生改变。

介绍

教程修改日期:2017.09.20 Version 1.0-stable

Nginx 是一款高性能 Web 服务器软件,其有非常有益的IO表现,而且相较于 Apache Httpd 配置更加简单上手更加容易,本文将向大家介绍编译安装 Nginx 以及其第三方扩展。

Nginx 的额外扩展:

  1. OpenSSL 1.1.0,提供 ALPN 支持,支持 HTTP/2
  2. Nginx-CT,透明证书提高 HTTPS 网站的安全性和浏览器支持
  3. ngx_PageSpeed,Google 家的网站性能优化工具
  4. Brotli,实现比 Gzip 更高的压缩率
  5. Jemalloc,优化内存管理

教程

本教程以,Ubuntu 16.04 LTS 64位版 为例。

设定版本变量

如果软件版本更新后,为了方便起见,后续修改版本号只需修改下面的变量即可。

在 SSH终端 中输入:

# Version
OpenSSLVersion='openssl-1.1.0f';
NginxCTVersion='1.3.2';
PageSpeedVersion='1.12.34.2';
SystemBit='X64';
NginxVersion='nginx-1.12.1';

上述软件版本更新查看: OpenSSLNginx-CTPageSpeedNginx

安装依赖

更新系统软件源缓存顺便升级组件:

apt update
apt upgrade -y

安装依赖组件:

apt install build-essential libreadline-dev libncurses5-dev libpcre3 libpcre3-dev libssl-dev zlib1g-dev unzip git perl make libjemalloc1 libjemalloc-dev

下载源码

这里将 Nginx 所需的源代码均放置在 /root/src 目录下,方便管理。

cd /root
mkdir src
cd src

下载 Nginx 和其拓展的源代码:

wget https://www.openssl.org/source/$OpenSSLVersion.tar.gz
tar xzf $OpenSSLVersion.tar.gz

wget https://github.com/grahamedgecombe/nginx-ct/archive/v$NginxCTVersion.tar.gz
tar xzf v$NginxCTVersion.tar.gz

git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
git submodule update --init
cd ../

wget https://github.com/pagespeed/ngx_pagespeed/archive/v$PageSpeedVersion-beta.zip
unzip v$PageSpeedVersion-beta.zip
cd ngx_pagespeed-$PageSpeedVersion-beta/
wget https://dl.google.com/dl/page-speed/psol/$PageSpeedVersion-$SystemBit.tar.gz
tar -xzvf $PageSpeedVersion-$SystemBit.tar.gz
cd ../

wget -c http://nginx.org/download/$NginxVersion.tar.gz
tar zxf $NginxVersion.tar.gz

编译 Nginx

cd $NginxVersion
./configure --prefix=/usr/local/nginx \
--user=www-data --group=www-data \
--add-module=../ngx_brotli \
--add-module=../nginx-ct-$NginxCTVersion \
--add-module=../ngx_pagespeed-$PageSpeedVersion-beta \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-openssl=../$OpenSSLVersion \
--with-ld-opt='-ljemalloc'

make && make install

设置变量

[ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=/usr/local/nginx/sbin:\$PATH" >> /etc/profile
[ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep /usr/local/nginx/ /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=/usr/local/nginx/sbin:\1@" /etc/profile
. /etc/profile

后面就可以用,nginx -t 检测配置是否正确,nginx -s reload 重载 Nginx 了。

创建相关目录

mkdir /data/wwwlogs/ -p
mkdir /data/wwwroot/default/ -p
cp /usr/local/nginx/html/index.html /data/wwwroot/default/

设置服务和开机启动

创建 /etc/init.d/nginx 文件,内容:

cd /etc/init.d/
wget https://gist.githubusercontent.com/ivmm/2006120a33f4b5c2aae596fa0fd28a31/raw/d020640ddf25c9bf2ddbf9faed0f834f505a2a76/nginx
chmod a+x /etc/init.d/nginx

编辑 /usr/local/nginx/conf/nginx.conf 文件为:

cd /usr/local/nginx/conf/
rm nginx.conf -rf
wget https://gist.githubusercontent.com/ivmm/753dc7da00674790778939720e504cdd/raw/f0645d27d3dcf8c73db39946dfde0841d4ed4b4f/nginx.conf

设置开机启动:

update-rc.d -f nginx defaults

通过 systemd 重启 Nginx:

systemctl restart nginx

打开你的服务器 IP,就能看到安装好的 Nginx 提示页了

上一篇
下一篇