<!-- /\* Font Definitions \*/ @font-face {font-family:Helvetica; panose-1:2 11 6 4 2 2 2 2 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-536859905 -1073711037 9 0 511 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-536870145 1073786111 1 0 415 0;} @font-face {font-family:Consolas; panose-1:2 11 6 9 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:-520092929 1073806591 9 0 415 0;} @font-face {font-family:"\\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /\* Style Definitions \*/ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} code {mso-style-noshow:yes; mso-style-priority:99; mso-ansi-font-size:12.0pt; mso-bidi-font-size:12.0pt; font-family:宋体; mso-ascii-font-family:宋体; mso-fareast-font-family:宋体; mso-hansi-font-family:宋体; mso-bidi-font-family:宋体;} pre {mso-style-priority:99; mso-style-link:"HTML 预设格式 Char"; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} span.HTMLChar {mso-style-name:"HTML 预设格式 Char"; mso-style-priority:99; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"HTML 预设格式"; mso-ansi-font-size:12.0pt; mso-bidi-font-size:12.0pt; font-family:宋体; mso-ascii-font-family:宋体; mso-fareast-font-family:宋体; mso-hansi-font-family:宋体; mso-bidi-font-family:宋体; mso-font-kerning:0pt;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /\* Page Definitions \*/ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page WordSection1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.WordSection1 {page:WordSection1;} -->
NGINX能够用做http/https服务器、反向代理服务器、邮件代理服务器、负载平衡器、TLS终结者或缓存服务器。它的设计很是模块化。它有本地模块和由社区建立的第三方模块。它是用C语言编写的,它是一种很是快速和轻量级的软件。nginx
注意:NGINX有两个版本流并行运行——稳定和主线。两个版本均可以在生产服务器上使用。建议在生产中使用主线版本。web
从源代码中安装NGINX是相对“容易”的——下载最新版本的NGINX源代码,配置、构建和安装它。ubuntu
在本教程中,我将使用主线版本,在撰写本文时是1.13.1。当更新版本可用时,更新版本号。vim
从源代码构建NGINX的需求缓存
强制要求:服务器
OpenSSL库版本1.0.2-1.1.0app
Zlib库版本1.1.3-1.2.11。dom
PCRE库版本在4.4-8.40之间curl
GCC编译器tcp
可选的要求:
PERL
LIBATOMIC_OPS
LibGD
MaxMind GeoIP
libxml2
libxslt
在你开始以前
一、使用sudo访问建立常规用户。
二、切换到新用户:
su - <username>
三、系统更新:
sudo apt update && sudo apt upgrade -y
从源代码构建NGINX
一、NGINX是一个用C编写的程序,因此咱们须要安装C编译器(GCC)。
sudo apt install build-essential -y
二、下载最新版本的NGINX源代码并提取它:
wget https://nginx.org/download/nginx-1.13.1.tar.gz && tar zxvf nginx-1.13.1.tar.gz
三、下载NGINX依赖项的源代码并提取它们:
NGINX依赖于3个库:PCRE、zlib和OpenSSL:
# PCRE version 4.4 - 8.40
wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz && tar xzvf pcre-8.40.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.0
wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz && tar xzvf openssl-1.1.0f.tar.gz
四、删除全部. tar.gz文件。咱们再也不须要他们了:
rm -rf *.tar.gz
五、转到NGINX源目录:
cd ~/nginx-1.13.1
六、为了帮助,您能够经过运行来列出可用的配置开关:
./configure --help
七、配置、编译和安装NGINX:
./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=www-data \
--group=www-data \
--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.0f \
--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.40 \
--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 -Wdate-time -D_FORTIFY_SOURCE=2' \
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'
make
sudo make install
八、从主目录中删除全部下载的文件,在这个例子中/home/username
:
cd ~
rm -r nginx-1.13.1/ openssl-1.1.0f/ pcre-8.40/ zlib-1.2.11/
九、检查NGINX版本和编译时选项:
sudo nginx -v && sudo nginx -V
# nginx version: nginx/1.13.0 (Ubuntu)
# built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
# built with OpenSSL 1.1.0f 25 May 2017
# TLS SNI support enabled
# configure arguments: --prefix=/etc/nginx . . .
# . . .
# . . .
十、检查语法和潜在错误:
sudo nginx -t
# Will throw this error nginx: [emerg] mkdir() "/var/lib/nginx/body" failed (2: No such file or directory)
# Just create directory
mkdir -p /var/lib/nginx && sudo nginx -t
十一、为NGINX建立systemd单元文件:
sudo vim /etc/systemd/system/nginx.service
十二、复制/粘贴如下内容:
注意:根据NGINX的编译方式,PID文件和NGINX二进制文件的位置可能会有所不一样。
[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
1三、启动并启用NGINX服务:
sudo systemctl start nginx.service && sudo systemctl enable nginx.service
1四、检查NGINX是否会在从新启动后启动:
sudo systemctl is-enabled nginx.service
# enabled
1五、检查NGINX是否在运行:
sudo systemctl status nginx.service
ps aux | grep nginx
curl -I 127.0.0.1
1六、从新启动你的Ubuntu VPS,以验证NGINX自动启动:
sudo shutdown -r now
1七、建立UFW NGINX应用程序概要文件:
sudo vim /etc/ufw/applications.d/nginx
1八、复制/粘贴如下内容:
[Nginx HTTP]
title=Web Server (Nginx, HTTP)
description=Small, but very powerful and efficient web server
ports=80/tcp
[Nginx HTTPS]
title=Web Server (Nginx, HTTPS)
description=Small, but very powerful and efficient web server
ports=443/tcp
[Nginx Full]
title=Web Server (Nginx, HTTP + HTTPS)
description=Small, but very powerful and efficient web server
ports=80,443/tcp
1九、如今,验证UFW应用概要文件的建立和识别:
sudo ufw app list
# Available applications:
# Nginx Full
# Nginx HTTP
# Nginx HTTPS
# OpenSSH
结论
就是这样。您如今已经安装了NGINX的最新版本。它是静态编译的,针对一些重要的库,好比OpenSSL。一般,系统的OpenSSL版本已通过时了。经过使用新的OpenSSL版本的安装方法,您能够利用chacha20poly1305这样的新密码,以及像TLS 1.3这样的协议,这些协议将在OpenSSL 1.1.1中可用(还没有发布)。