]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - doc/md/Server-configuration.md
Merge pull request #1152 from ArthurHoaro/hotfix/install-error
[github/shaarli/Shaarli.git] / doc / md / Server-configuration.md
CommitLineData
992af0b9
V
1*Example virtual host configurations for popular web servers*
2
53ed6d7d 3- [Apache](#apache)
4- [Nginx](#nginx)
992af0b9
V
5
6## Prerequisites
5409ade2 7### Shaarli
43ad7c8e
V
8- Shaarli is installed in a directory readable/writeable by the user
9- the correct read/write permissions have been granted to the web server _user and/or group_
10- for HTTPS / SSL:
11 - a key pair (public, private) and a certificate have been generated
12 - the appropriate server SSL extension is installed and active
992af0b9 13
5409ade2 14### HTTPS, TLS and self-signed certificates
992af0b9 15Related guides:
43ad7c8e
V
16
17- [How to Create Self-Signed SSL Certificates with OpenSSL](http://www.xenocafe.com/tutorials/linux/centos/openssl/self_signed_certificates/index.php)
18- [How do I create my own Certificate Authority?](https://workaround.org/certificate-authority)
19- Generate a self-signed certificate (will trigger browser warnings) with apache2:
20 `make-ssl-cert generate-default-snakeoil --force-overwrite` will create `/etc/ssl/certs/ssl-cert-snakeoil.pem` and `/etc/ssl/private/ssl-cert-snakeoil.key`
5409ade2
A
21
22### Proxies
23If Shaarli is served behind a proxy (i.e. there is a proxy server between clients and the web server hosting Shaarli), please refer to the proxy server documentation for proper configuration. In particular, you have to ensure that the following server variables are properly set:
43ad7c8e
V
24
25- `X-Forwarded-Proto`
26- `X-Forwarded-Host`
27- `X-Forwarded-For`
5409ade2 28
53ed6d7d 29See also [proxy-related](https://github.com/shaarli/Shaarli/issues?utf8=%E2%9C%93&q=label%3Aproxy+) issues.
992af0b9
V
30
31## Apache
32### Minimal
33```apache
34<VirtualHost *:80>
35 ServerName shaarli.my-domain.org
36 DocumentRoot /absolute/path/to/shaarli/
37</VirtualHost>
38```
39### Debug - Log all the things!
40This configuration will log both Apache and PHP errors, which may prove useful to identify server configuration errors.
41
42See:
43ad7c8e
V
43
44- [Apache/PHP - error log per VirtualHost](http://stackoverflow.com/q/176) (StackOverflow)
45- [PHP: php_value vs php_admin_value and the use of php_flag explained](https://ma.ttias.be/php-php_value-vs-php_admin_value-and-the-use-of-php_flag-explained/)
992af0b9
V
46
47```apache
48<VirtualHost *:80>
49 ServerName shaarli.my-domain.org
50 DocumentRoot /absolute/path/to/shaarli/
51
52 LogLevel warn
53 ErrorLog /var/log/apache2/shaarli-error.log
54 CustomLog /var/log/apache2/shaarli-access.log combined
55
56 php_flag log_errors on
57 php_flag display_errors on
58 php_value error_reporting 2147483647
59 php_value error_log /var/log/apache2/shaarli-php-error.log
60</VirtualHost>
61```
62
63### Standard - Keep access and error logs
64```apache
65<VirtualHost *:80>
66 ServerName shaarli.my-domain.org
67 DocumentRoot /absolute/path/to/shaarli/
68
69 LogLevel warn
70 ErrorLog /var/log/apache2/shaarli-error.log
71 CustomLog /var/log/apache2/shaarli-access.log combined
72</VirtualHost>
73```
74
75### Paranoid - Redirect HTTP (:80) to HTTPS (:443)
53ed6d7d 76See [Server-side TLS](https://wiki.mozilla.org/Security/Server_Side_TLS#Apache) (Mozilla).
992af0b9
V
77
78```apache
79<VirtualHost *:443>
80 ServerName shaarli.my-domain.org
81 DocumentRoot /absolute/path/to/shaarli/
82
83 SSLEngine on
5409ade2 84 SSLCertificateFile /absolute/path/to/the/website/certificate.pem
992af0b9
V
85 SSLCertificateKeyFile /absolute/path/to/the/website/key.key
86
87 <Directory /absolute/path/to/shaarli/>
88 AllowOverride All
89 Options Indexes FollowSymLinks MultiViews
90 Order allow,deny
91 allow from all
92 </Directory>
93
94 LogLevel warn
95 ErrorLog /var/log/apache2/shaarli-error.log
96 CustomLog /var/log/apache2/shaarli-access.log combined
97</VirtualHost>
98<VirtualHost *:80>
99 ServerName shaarli.my-domain.org
100 Redirect 301 / https://shaarli.my-domain.org
101
102 LogLevel warn
103 ErrorLog /var/log/apache2/shaarli-error.log
104 CustomLog /var/log/apache2/shaarli-access.log combined
105</VirtualHost>
106```
107
3cc8c898
A
108### .htaccess
109
110Shaarli use `.htaccess` Apache files to deny access to files that shouldn't be directly accessed (datastore, config, etc.). You need the directive `AllowOverride All` in your virtual host configuration for them to work.
111
53ed6d7d 112**Warning**: If you use Apache 2.2 or lower, you need [mod_version](https://httpd.apache.org/docs/current/mod/mod_version.html) to be installed and enabled.
b230bf20
A
113
114Apache module `mod_rewrite` **must** be enabled to use the REST API. URL rewriting rules for the Slim microframework are stated in the root `.htaccess` file.
3cc8c898 115
992af0b9
V
116## LightHttpd
117
118## Nginx
119### Foreword
53ed6d7d 120Nginx does not natively interpret PHP scripts; to this effect, we will run a [FastCGI](https://en.wikipedia.org/wiki/FastCGI) service, to which Nginx's FastCGI module will proxy all requests to PHP resources.
992af0b9
V
121
122Required packages:
43ad7c8e 123
53ed6d7d 124- [nginx](http://nginx.org)
125- [php-fpm](http://php-fpm.org) - PHP FastCGI Process Manager
992af0b9
V
126
127Official documentation:
43ad7c8e 128
53ed6d7d 129- [Beginner's guide](http://nginx.org/en/docs/beginners_guide.html)
130- [ngx_http_fastcgi_module](http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html)
131- [Pitfalls](http://wiki.nginx.org/Pitfalls)
992af0b9
V
132
133Community resources:
43ad7c8e 134
53ed6d7d 135- [Server-side TLS (Nginx)](https://wiki.mozilla.org/Security/Server_Side_TLS#Nginx) (Mozilla)
136- [PHP configuration examples](http://kbeezie.com/nginx-configuration-examples/) (Karl Blessing)
992af0b9
V
137
138### Common setup
139Once Nginx and PHP-FPM are installed, we need to ensure:
43ad7c8e 140
992af0b9
V
141- Nginx and PHP-FPM are running using the _same user and group_
142- both these user and group have
143 - `read` permissions for Shaarli resources
144 - `execute` permissions for Shaarli directories _AND_ their parent directories
145
146On a production server:
43ad7c8e 147
992af0b9
V
148- `user:group` will likely be `http:http`, `www:www` or `www-data:www-data`
149- files will be located under `/var/www`, `/var/http` or `/usr/share/nginx`
150
151On a development server:
43ad7c8e 152
992af0b9
V
153- files may be located in a user's home directory
154- in this case, make sure both Nginx and PHP-FPM are running as the local user/group!
155
3cc8c898 156For all following configuration examples, this user/group pair will be used:
43ad7c8e 157
992af0b9
V
158- `user:group = john:users`,
159
160which corresponds to the following service configuration:
161
162```ini
163; /etc/php/php-fpm.conf
164user = john
165group = users
166
53ed6d7d 167[...]
992af0b9
V
168listen.owner = john
169listen.group = users
170```
171
172```nginx
173# /etc/nginx/nginx.conf
174user john users;
175
176http {
53ed6d7d 177 [...]
992af0b9
V
178}
179```
180
3cc8c898
A
181### (Optional) Increase the maximum file upload size
182Some bookmark dumps generated by web browsers can be _huge_ due to the presence of Base64-encoded images and favicons, as well as extra verbosity when nesting links in (sub-)folders.
183
184To increase upload size, you will need to modify both nginx and PHP configuration:
185
186```nginx
187# /etc/nginx/nginx.conf
188
189http {
53ed6d7d 190 [...]
3cc8c898
A
191
192 client_max_body_size 10m;
193
53ed6d7d 194 [...]
3cc8c898
A
195}
196```
197
198```ini
199# /etc/php5/fpm/php.ini
200
53ed6d7d 201[...]
3cc8c898 202post_max_size = 10M
53ed6d7d 203[...]
3cc8c898
A
204upload_max_filesize = 10M
205```
206
992af0b9
V
207### Minimal
208_WARNING: Use for development only!_
209
210```nginx
211user john users;
212worker_processes 1;
213events {
214 worker_connections 1024;
215}
216
217http {
218 include mime.types;
219 default_type application/octet-stream;
220 keepalive_timeout 20;
221
222 index index.html index.php;
223
224 server {
225 listen 80;
226 server_name localhost;
227 root /home/john/web;
228
229 access_log /var/log/nginx/access.log;
230 error_log /var/log/nginx/error.log;
231
232 location /shaarli/ {
b230bf20 233 try_files $uri /shaarli/index.php$is_args$args;
992af0b9
V
234 access_log /var/log/nginx/shaarli.access.log;
235 error_log /var/log/nginx/shaarli.error.log;
236 }
237
238 location ~ (index)\.php$ {
b230bf20
A
239 try_files $uri =404;
240 fastcgi_split_path_info ^(.+\.php)(/.+)$;
992af0b9
V
241 fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
242 fastcgi_index index.php;
243 include fastcgi.conf;
244 }
245 }
246}
247```
248
249### Modular
250The previous setup is sufficient for development purposes, but has several major caveats:
43ad7c8e 251
992af0b9
V
252- every content that does not match the PHP rule will be sent to client browsers:
253 - dotfiles - in our case, `.htaccess`
254 - temporary files, e.g. Vim or Emacs files: `index.php~`
255- asset / static resource caching is not optimized
256- if serving several PHP sites, there will be a lot of duplication: `location /shaarli/`, `location /mysite/`, etc.
257
258To solve this, we will split Nginx configuration in several parts, that will be included when needed:
259
260```nginx
261# /etc/nginx/deny.conf
262location ~ /\. {
263 # deny access to dotfiles
264 access_log off;
265 log_not_found off;
266 deny all;
267}
268
269location ~ ~$ {
270 # deny access to temp editor files, e.g. "script.php~"
271 access_log off;
272 log_not_found off;
273 deny all;
274}
275```
276
277```nginx
278# /etc/nginx/php.conf
279location ~ (index)\.php$ {
b230bf20
A
280 # Slim - split URL path into (script_filename, path_info)
281 try_files $uri =404;
282 fastcgi_split_path_info ^(.+\.php)(/.+)$;
283
f8b936e7 284 # filter and proxy PHP requests to PHP-FPM
992af0b9
V
285 fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
286 fastcgi_index index.php;
287 include fastcgi.conf;
288}
f8b936e7
V
289
290location ~ \.php$ {
291 # deny access to all other PHP scripts
292 deny all;
293}
992af0b9
V
294```
295
296```nginx
297# /etc/nginx/static_assets.conf
298location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
299 expires max;
300 add_header Pragma public;
301 add_header Cache-Control "public, must-revalidate, proxy-revalidate";
302}
303```
304
305```nginx
306# /etc/nginx/nginx.conf
53ed6d7d 307[...]
992af0b9
V
308
309http {
53ed6d7d 310 [...]
992af0b9
V
311
312 root /home/john/web;
313 access_log /var/log/nginx/access.log;
314 error_log /var/log/nginx/error.log;
315
316 server {
317 # virtual host for a first domain
318 listen 80;
319 server_name my.first.domain.org;
320
321 location /shaarli/ {
b230bf20
A
322 # Slim - rewrite URLs
323 try_files $uri /shaarli/index.php$is_args$args;
324
992af0b9
V
325 access_log /var/log/nginx/shaarli.access.log;
326 error_log /var/log/nginx/shaarli.error.log;
327 }
328
3cc8c898
A
329 location = /shaarli/favicon.ico {
330 # serve the Shaarli favicon from its custom location
331 alias /var/www/shaarli/images/favicon.ico;
332 }
333
992af0b9
V
334 include deny.conf;
335 include static_assets.conf;
336 include php.conf;
337 }
338
339 server {
340 # virtual host for a second domain
341 listen 80;
342 server_name second.domain.com;
343
344 location /minigal/ {
345 access_log /var/log/nginx/minigal.access.log;
346 error_log /var/log/nginx/minigal.error.log;
347 }
348
349 include deny.conf;
350 include static_assets.conf;
351 include php.conf;
352 }
353}
354```
355
356### Redirect HTTP to HTTPS
43ad7c8e
V
357Assuming you have generated a (self-signed) key and certificate, and they are
358located under `/home/john/ssl/localhost.{key,crt}`, it is pretty straightforward
359to set an HTTP (:80) to HTTPS (:443) redirection to force SSL/TLS usage.
992af0b9
V
360
361```nginx
362# /etc/nginx/nginx.conf
53ed6d7d 363[...]
992af0b9
V
364
365http {
53ed6d7d 366 [...]
992af0b9
V
367
368 index index.html index.php;
369
370 root /home/john/web;
371 access_log /var/log/nginx/access.log;
372 error_log /var/log/nginx/error.log;
373
374 server {
375 listen 80;
376 server_name localhost;
377
378 return 301 https://localhost$request_uri;
379 }
380
381 server {
382 listen 443 ssl;
383 server_name localhost;
384
385 ssl_certificate /home/john/ssl/localhost.crt;
386 ssl_certificate_key /home/john/ssl/localhost.key;
387
388 location /shaarli/ {
b230bf20
A
389 # Slim - rewrite URLs
390 try_files $uri /index.php$is_args$args;
391
992af0b9
V
392 access_log /var/log/nginx/shaarli.access.log;
393 error_log /var/log/nginx/shaarli.error.log;
394 }
395
3cc8c898
A
396 location = /shaarli/favicon.ico {
397 # serve the Shaarli favicon from its custom location
398 alias /var/www/shaarli/images/favicon.ico;
399 }
400
992af0b9
V
401 include deny.conf;
402 include static_assets.conf;
403 include php.conf;
404 }
405}
406```