Skip to content

Commit 5f3f5cb

Browse files
committed
在Ubuntu 16.04中如何从源代码编译Nginx
1 parent 229f263 commit 5f3f5cb

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

Nginx/nginx-install.md

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
## 在Ubuntu 16.04中如何从源代码编译Nginx
2+
+ NGINX可用作HTTP / HTTPS服务器,反向代理服务器,邮件代理服务器,负载均衡器,TLS终结器或缓存服务器。它是相当模块化的设计。它具有由社区创建的本机模块和第三方模块。以C编程语言编写,它是一个非常快速和轻便的软件。
3+
## 从源头构建NGINX的要求
4+
#### 强制性要求:
5+
+ OpenSSL库版本介于1.0.2 - 1.1.0之间
6+
+ Zlib库版本介于1.1.3 - 1.2.11之间
7+
+ PCRE库版本在4.4 - 8.40之间
8+
+ GCC编译器
9+
#### 可选要求:
10+
+ PERL
11+
+ LIBATOMIC_OPS
12+
+ 的libgd
13+
+ MaxMind GeoIP
14+
+ libxml2的
15+
+ 的libxslt
16+
#### 开始之前
17+
+ 创建普通用户`sudo`访问。
18+
+ 切换到新用户:`su - <username>`
19+
+ 更新系统:`sudo apt update && sudo apt upgrade -y`
20+
#### 从源代码构建NGINX
21+
+ 1、NGINX是用C编写的程序,所以我们需要安装C编译器(GCC)。
22+
`sudo apt install build-essential -y`
23+
+ 2、下载最新版本的NGINX源代码并解压缩:
24+
`wget https://nginx.org/download/nginx-1.13.1.tar.gz && tar zxvf nginx-1.13.1.tar.gz`
25+
+ 3、下载NGINX依赖项的源代码并解压缩
26+
> NGINX依赖于3个库:PCRE,zlib和OpenSSL:
27+
```bash
28+
# PCRE version 4.4 - 8.40
29+
wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz && tar xzvf pcre-8.40.tar.gz
30+
31+
# zlib version 1.1.3 - 1.2.11
32+
wget http://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz
33+
34+
# OpenSSL version 1.0.2 - 1.1.0
35+
wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz && tar xzvf openssl-1.1.0f.tar.gz
36+
```
37+
+ 4、删除所有.tar.gz文件。我们不再需要它们了,`rm -rf *.tar.gz`
38+
+ 5、转到NGINX源目录:`cd ~/nginx-1.13.1`
39+
+ 6、有关帮助,您可以通过运行以下列出可用的配置开关:`./configure --help`
40+
+ 7、配置,编译和安装NGINX:
41+
```bash
42+
./configure --prefix=/usr/share/nginx \
43+
--sbin-path=/usr/sbin/nginx \
44+
--modules-path=/usr/lib/nginx/modules \
45+
--conf-path=/etc/nginx/nginx.conf \
46+
--error-log-path=/var/log/nginx/error.log \
47+
--http-log-path=/var/log/nginx/access.log \
48+
--pid-path=/run/nginx.pid \
49+
--lock-path=/var/lock/nginx.lock \
50+
--user=www-data \
51+
--group=www-data \
52+
--build=Ubuntu \
53+
--http-client-body-temp-path=/var/lib/nginx/body \
54+
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
55+
--http-proxy-temp-path=/var/lib/nginx/proxy \
56+
--http-scgi-temp-path=/var/lib/nginx/scgi \
57+
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
58+
--with-openssl=../openssl-1.1.0f \
59+
--with-openssl-opt=enable-ec_nistp_64_gcc_128 \
60+
--with-openssl-opt=no-nextprotoneg \
61+
--with-openssl-opt=no-weak-ssl-ciphers \
62+
--with-openssl-opt=no-ssl3 \
63+
--with-pcre=../pcre-8.40 \
64+
--with-pcre-jit \
65+
--with-zlib=../zlib-1.2.11 \
66+
--with-compat \
67+
--with-file-aio \
68+
--with-threads \
69+
--with-http_addition_module \
70+
--with-http_auth_request_module \
71+
--with-http_dav_module \
72+
--with-http_flv_module \
73+
--with-http_gunzip_module \
74+
--with-http_gzip_static_module \
75+
--with-http_mp4_module \
76+
--with-http_random_index_module \
77+
--with-http_realip_module \
78+
--with-http_slice_module \
79+
--with-http_ssl_module \
80+
--with-http_sub_module \
81+
--with-http_stub_status_module \
82+
--with-http_v2_module \
83+
--with-http_secure_link_module \
84+
--with-mail \
85+
--with-mail_ssl_module \
86+
--with-stream \
87+
--with-stream_realip_module \
88+
--with-stream_ssl_module \
89+
--with-stream_ssl_preread_module \
90+
--with-debug \
91+
--with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' \
92+
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'
93+
make
94+
sudo make install
95+
```
96+
+ 8、从主目录中删除所有下载的文件,在这种情况下/home/username:
97+
```bash
98+
cd ~
99+
rm -r nginx-1.13.1/ openssl-1.1.0f/ pcre-8.40/ zlib-1.2.11/
100+
```
101+
+ 9、检查NGINX版本和编译时间选项:
102+
```bash
103+
sudo nginx -v && sudo nginx -V
104+
105+
# nginx version: nginx/1.13.0 (Ubuntu)
106+
# built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
107+
# built with OpenSSL 1.1.0f 25 May 2017
108+
# TLS SNI support enabled
109+
# configure arguments: --prefix=/etc/nginx . . .
110+
# . . .
111+
# . . .
112+
```
113+
+ 10、检查语法和潜在错误:
114+
```bash
115+
sudo nginx -t
116+
# Will throw this error nginx: [emerg] mkdir() "/var/lib/nginx/body" failed (2: No such file or directory)
117+
# Just create directory
118+
mkdir -p /var/lib/nginx && sudo nginx -t
119+
```
120+
+ 11、为NGINX创建systemd单元文件:
121+
```bash
122+
sudo vim /etc/systemd/system/nginx.service
123+
```
124+
+ 12、复制/粘贴以下内容:
125+
> 注意:根据NGINX的编译方式,PID文件和NGINX二进制文件的位置可能不同。
126+
```bash
127+
[Unit]
128+
Description=A high performance web server and a reverse proxy server
129+
After=network.target
130+
131+
[Service]
132+
Type=forking
133+
PIDFile=/run/nginx.pid
134+
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
135+
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
136+
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
137+
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
138+
TimeoutStopSec=5
139+
KillMode=mixed
140+
141+
[Install]
142+
WantedBy=multi-user.target
143+
```
144+
+ 13、启动并启用NGINX服务:
145+
```bash
146+
sudo systemctl start nginx.service && sudo systemctl enable nginx.service
147+
```
148+
+ 14、检查NGINX是否在重启后启动:
149+
```bash
150+
sudo systemctl is-enabled nginx.service
151+
# enabled
152+
```
153+
+ 15、检查NGINX是否正在运行:
154+
```bash
155+
sudo systemctl status nginx.service
156+
ps aux | grep nginx
157+
curl -I 127.0.0.1
158+
```
159+
+ 16、重新启动Ubuntu VPS以验证NGINX是否自动启动:`sudo shutdown -r now`
160+
+ 17、创建UFW NGINX应用程序配置文件:`sudo vim /etc/ufw/applications.d/nginx`
161+
+ 18、复制/粘贴以下内容:
162+
```bash
163+
[Nginx HTTP]
164+
title=Web Server (Nginx, HTTP)
165+
description=Small, but very powerful and efficient web server
166+
ports=80/tcp
167+
168+
[Nginx HTTPS]
169+
title=Web Server (Nginx, HTTPS)
170+
description=Small, but very powerful and efficient web server
171+
ports=443/tcp
172+
173+
[Nginx Full]
174+
title=Web Server (Nginx, HTTP + HTTPS)
175+
description=Small, but very powerful and efficient web server
176+
ports=80,443/tcp
177+
```
178+
+ 19、现在,验证UFW应用配置文件是否被创建和识别:
179+
```bash
180+
sudo ufw app list
181+
182+
# Available applications:
183+
# Nginx Full
184+
# Nginx HTTP
185+
# Nginx HTTPS
186+
# OpenSSH
187+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## <a name="index"/>目录
22
+ Nginx 教程
3+
+ [Nginx安装](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/nginx-install.md)
34
+ [Nginx基础知识](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/nginx-basic.md)
45
+ [Nginx高性能WEB服务器详解](https://github.com/Tinywan/Lua-Nginx-Redis/blob/master/Nginx/nginx-high-basic.md)
56
+ [nginx 并发数问题思考](#Nginx_base_knowledge)

0 commit comments

Comments
 (0)