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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
<meta charset='utf-8'>
<title>Nginx</title>
</head>
<body>
<a href="index.html">Tools Index</a>
<h1>Nginx</h1>
<h2 id="install">1. Install Nginx</h2>
<pre>
$ prt-get depinst nginx
</pre>
<p>Allow minimal privileges via mount options, view /etc/fstab;</p>
<pre>
UID=xxxxx-xxx-xxx-xxx-xxxxxxxx /srv/www ext4 defaults,nosuid,noexec,nodev,noatime 1 2
</pre>
<p>Add user nginx to www group;</p>
<pre>
# usermod -a -G www nginx
</pre>
<p>Change default home directory of nginx user;</p>
<pre>
# usermod -m -d /srv/www nginx
</pre>
<p>Create configuration directory's for better organization;</p>
<pre>
$ sudo mkdir /etc/nginx/conf.d
$ sudo mkdir /etc/nginx/sites-enable
$ sudo mkdir /etc/nginx/sites
</pre>
<h2 id="certs">2. Certificates</h2>
<h3 id="letsencrypt">2.1. Lets encrypt</h3>
<p>Example of nginx location block on public pmwiki setup;</p>
<pre>
#ACME challenge
location ^~ /.well-known {
allow all;
alias /srv/www/machine-pmwiki/pub/cert/.well-known/;
default_type "text/plain";
try_files $uri =404;
}
</pre>
<p>First run dryrun to test if everything is ok;</p>
<pre>
# certbot certonly --dry-run --email user@mail.org --webroot -w /srv/www/machine-pmwiki/pub/cert/-d machine.example.org
</pre>
<pre>
# certbot certonly --email user@mail.org --webroot -w /srv/www/machine-pmwiki/pub/cert/-d machine.example.org
</pre>
<h3 id="mancert">2.2. Self certificate</h2>
<p>Certificates allow a more secure connection. Lets create
self-signed certificate;</p>
<p>Create private key;</p>
<pre>
$ sudo openssl genrsa -des3 -out /etc/ssl/keys/nginx.key 2048
Password:
Generating RSA private key, 2048 bit long modulus
..............................+++
............+++
e is 65537 (0x10001)
Enter pass phrase for /etc/ssl/keys/nginx.key:
Verifying - Enter pass phrase for /etc/ssl/keys/nginx.key:
</pre>
<p>Create ceritificate signing request. For "Common Name"
provide domain name or ip address, leave challange password
and optional company name blank;</p>
<pre>
$ sudo openssl req -new -key /etc/ssl/keys/nginx.key -out /etc/ssl/certs/nginx.csr
Enter pass phrase for /etc/ssl/keys/nginx.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:PT
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:machine.example.org
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
$
</pre>
<p>Having password is a good idea, but requires it every
time nginx is restarted. To remove;</p>
<pre>
$ sudo cp /etc/ssl/keys/nginx.key /etc/ssl/keys/nginx.key.pass
$ sudo openssl rsa -in /etc/ssl/keys/nginx.key.pass -out /etc/ssl/keys/nginx.key
</pre>
<pre>
Enter pass phrase for /etc/ssl/keys/nginx.key.pass:
writing RSA key
</pre>
<pre>
$ sudo chown nginx /etc/ssl/keys/nginx.key*
$ sudo chmod 0600 /etc/ssl/keys/nginx.key*
# chmod 644 /etc/ssl/certs/exim.cert
</pre>
<p>Sign SSL cetificate;</p>
<pre>
$ sudo openssl x509 -req -days 365 \
-in /etc/ssl/certs/nginx.csr \
-signkey /etc/ssl/keys/nginx.key \
-out /etc/ssl/certs/nginx.crt
</pre>
Signature ok
subject=/C=PT/ST=Some-State/O=Internet Widgits Pty Ltd/CN=machine.example.org
Getting Private key
Enter pass phrase for /etc/ssl/keys/nginx.key:
</pre>
<pre>
$ sudo chown nginx:nginx /etc/ssl/keys/nginx.key*
$ sudo chmod 0600 /etc/ssl/keys/nginx.key*
$ sudo chmod 644 /etc/ssl/certs/nginx.crt
</pre>
<h2 id="nginxconf">3. Nginx Configuration</h2>
<p>Read <a href="http://wiki.nginx.org/Pitfalls">nginx pitfalls</a>,
for more information about optimization
<a href="https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration">digitalocean</a>,
<p>Number of worker_processes must be equal or less than
the number of available cpu cores. This is set to auto.</p>
<pre>
$ nproc
2
</pre>
<p>Number of worker_connections must be equal or less than
the number file-size writing limit, you can get it by;</p>
<pre>
$ nlimit -n
1024
</pre>
<p>Edit ngnix configutarion;</p>
<pre>
#
# /etc/nginx/nginx.conf - nginx server configuration
#
user www;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
#tcp_nopush on;
# Allow attach iso to wiki
#client_max_body_size 8M;
client_max_body_size 30M;
#keepalive_timeout 65;
keepalive_timeout 120;
#client_body_timeout 12;
client_body_timeout 24;
#client_header_timeout 12;
client_header_timeout 24;
#client_max_body_size 10000M;
#keepalive_timeout 10000;
#client_body_timeout 10000;
#client_header_timeout 10000;
send_timeout 65;
gzip on;
gzip_vary on;
#gzip_proxied any;
gzip_comp_level 9;
# 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;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
}
# End of file
</pre>
<h2 id="server">4. Server with PHP</h2>
<p>To debug configurations check logs and;</p>
<pre>
nginx -V
</pre>
<h3>4.1. Setup PHP</h3>
<p> Install php and setup php.ini as development mode;</p>
<pre>
$ sudo prt-get depinst php php-fpm php-gd php-pdo-pgsql php-postgresql
</pre>
<p>Setup php ini in development mode;<p/>
<pre>
$ sudo cp /etc/php/php.ini-development /etc/php/php.ini
</pre>
<pre>
$ php --ini
Configuration File (php.ini) Path: /etc/php
Loaded Configuration File: /etc/php/php.ini
Scan for additional .ini files in: /etc/php/conf.d
Additional .ini files parsed: /etc/php/conf.d/extensions.ini,
/etc/php/conf.d/pdo_pgsql.ini
</pre>
<h3 id="virtual-host">4.2. Setup Virtual Host</h3>
<p>Server (virtual host) with pmwiki and flyspray, check
<a href="conf/etc/nginx/sites/">/etc/nginx/sites</a>
for more examples. Install pmwiki and flyspray;</p>
<pre>
$ sudo prt-get depinst pmwiki flyspray
</pre>
<p> This server is configured in a way that
root serves pmwiki and /tasks serves flyspray. In order to
flyspray to link correctly change index is needed. Create /etc/nginx/sites-enabled/machine.example.org.conf;</p>
<pre>
server {
listen 443 ssl;
listen 80;
server_name machine.example.org;
# listen [::]:443 ssl http2;
ssl_certificate /etc/letsencrypt/live/machine.example.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/machine.example.org/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/machine.example.org/chain.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
ssl_protocols TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
access_log /var/log/nginx/example_access.log;
error_log /var/log/nginx/example_error.log;
root /srv/www/;
location /mirror {
#alias /usr/ports/releases;
proxy_pass http://10.0.0.3:80/;
}
location /builder {
rewrite ^/blog(.*) /$1 break;
proxy_pass http://10.0.0.3:80;
}
location /doc {
alias /srv/www/doc;
index index.html;
}
location /git/static {
# static files (png/css) served from /usr/share/gitweb/static
alias /srv/www/gitweb/static;
}
location /git {
alias /srv/www/gitweb;
index gitweb.cgi;
fastcgi_split_path_info ^/git()(/?.+)$;
fastcgi_param GITWEB_CONFIG /etc/gitweb.conf;
fastcgi_param DOCUMENT_ROOT /srv/www/gitweb;
fastcgi_param SCRIPT_NAME /gitweb.cgi$fastcgi_path_info;
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgiwrap.sock;
}
location /chat {
index index.php;
alias /srv/www/chat;
try_files $uri $uri/ index.php$is_args$args;
}
location ~ ^/chat(.+\.php)$ { ### This location block was the solution
alias /srv/www/chat;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
try_files $uri /index.php =404;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$1;
# fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
}
location /task {
index index.php;
alias /srv/www/flyspray;
try_files $uri $uri/ index.php$is_args$args;
}
location ~ ^/task(.+\.php)$ { ### This location block was the solution
alias /srv/www/flyspray;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
try_files $uri /index.php =404;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$1;
# fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
}
location /pub {
alias /srv/www/pmwiki/pub;
}
location /wiki {
alias /srv/www/pmwiki/;
index pmwiki.php;
try_files $uri $uri/ /pmwiki.php$is_args$args;
}
location ~ ^/wiki(.+\.php)$ {
alias /srv/www/pmwiki;
index pmwiki.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index pmwiki.php;
try_files $uri /pmwiki.php =404;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
}
# ACME challenge
location ^~ /.well-known {
allow all;
alias /srv/www/pmwiki/pub/cert/.well-known/;
default_type "text/plain";
try_files $uri =404;
}
location / {
alias /srv/www/frontpage/;
index index.html;
try_files $uri $uri/ /index.html$is_args$args;
}
}
</pre>
<p>Change /srv/www/default/flyspray/index.php to;</p>
<pre>
<?php
/*
This is the main script that everything else is included
in. Mostly what it does is check the user permissions
to see what they have access to.
*/
define('IN_FS', true);
$_SERVER['SCRIPT_NAME'] = "/bug/index.php";
require_once(dirname(__FILE__).'/header.php');
</pre>
<h2 id="userdir">5. User Directory</h2>
<p><a href="http://wiki.nginx.org/UserDir">Nginx Wiki UserDir</a></p>
<pre>
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm;
autoindex on;
}
</pre>
<p>Directories should have 644 or 664 and
files chmod 755 or 775;</p>
<pre>
$ sudo find . -type f -print0 | xargs -0 chmod 644
$ sudo find . -type d -print0 | xargs -0 chmod 755
</pre>
<h2 id="logs">6. Logs</h2>
<pre>
$ sudo grep "login" /var/log/nginx/access.log
$ sudo grep "etc/passwd" /var/log/nginx/access.log
$ sudo egrep -i "denied|error|warn" /var/log/nginx/error.log
</pre>
<a href="index.html">Tools Index</a>
<p>This is part of the Hive System Documentation.
Copyright (C) 2018
Hive Team.
See the file <a href="../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
for copying conditions.</p>
</body>
</html>
|