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