2 *Example virtual host configurations for popular web servers*
4 - [Apache](#apache)[](.html)
5 - [Nginx](#nginx)[](.html)
9 * Shaarli is installed in a directory readable/writeable by the user
10 * the correct read/write permissions have been granted to the web server _user and/or group_
12 * a key pair (public, private) and a certificate have been generated
13 * the appropriate server SSL extension is installed and active
15 ### HTTPS, TLS and self-signed certificates
17 * [How to Create Self-Signed SSL Certificates with OpenSSL](http://www.xenocafe.com/tutorials/linux/centos/openssl/self_signed_certificates/index.php)[](.html)
18 * [How do I create my own Certificate Authority?](https://workaround.org/certificate-authority)[](.html)
19 * Generate a self-signed certificate (will trigger browser warnings) with apache2: `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`
22 If 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:
23 - `X-Forwarded-Proto`;
27 See also [proxy-related](https://github.com/shaarli/Shaarli/issues?utf8=%E2%9C%93&q=label%3Aproxy+) issues.[](.html)
33 ServerName shaarli.my-domain.org
34 DocumentRoot /absolute/path/to/shaarli/
37 ### Debug - Log all the things!
38 This configuration will log both Apache and PHP errors, which may prove useful to identify server configuration errors.
41 * [Apache/PHP - error log per VirtualHost](http://stackoverflow.com/q/176) (StackOverflow)[](.html)
42 * [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/)[](.html)
46 ServerName shaarli.my-domain.org
47 DocumentRoot /absolute/path/to/shaarli/
50 ErrorLog /var/log/apache2/shaarli-error.log
51 CustomLog /var/log/apache2/shaarli-access.log combined
53 php_flag log_errors on
54 php_flag display_errors on
55 php_value error_reporting 2147483647
56 php_value error_log /var/log/apache2/shaarli-php-error.log
60 ### Standard - Keep access and error logs
63 ServerName shaarli.my-domain.org
64 DocumentRoot /absolute/path/to/shaarli/
67 ErrorLog /var/log/apache2/shaarli-error.log
68 CustomLog /var/log/apache2/shaarli-access.log combined
72 ### Paranoid - Redirect HTTP (:80) to HTTPS (:443)
73 See [Server-side TLS](https://wiki.mozilla.org/Security/Server_Side_TLS#Apache) (Mozilla).[](.html)
77 ServerName shaarli.my-domain.org
78 DocumentRoot /absolute/path/to/shaarli/
81 SSLCertificateFile /absolute/path/to/the/website/certificate.pem
82 SSLCertificateKeyFile /absolute/path/to/the/website/key.key
84 <Directory /absolute/path/to/shaarli/>
86 Options Indexes FollowSymLinks MultiViews
92 ErrorLog /var/log/apache2/shaarli-error.log
93 CustomLog /var/log/apache2/shaarli-access.log combined
96 ServerName shaarli.my-domain.org
97 Redirect 301 / https://shaarli.my-domain.org
100 ErrorLog /var/log/apache2/shaarli-error.log
101 CustomLog /var/log/apache2/shaarli-access.log combined
109 Nginx 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.[](.html)
112 - [nginx](http://nginx.org)[](.html)
113 - [php-fpm](http://php-fpm.org) - PHP FastCGI Process Manager[](.html)
115 Official documentation:
116 - [Beginner's guide](http://nginx.org/en/docs/beginners_guide.html)[](.html)
117 - [ngx_http_fastcgi_module](http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html)[](.html)
118 - [Pitfalls](http://wiki.nginx.org/Pitfalls)[](.html)
121 - [Server-side TLS (Nginx)](https://wiki.mozilla.org/Security/Server_Side_TLS#Nginx) (Mozilla)[](.html)
122 - [PHP configuration examples](http://kbeezie.com/nginx-configuration-examples/) (Karl Blessing)[](.html)
125 Once Nginx and PHP-FPM are installed, we need to ensure:
126 - Nginx and PHP-FPM are running using the _same user and group_
127 - both these user and group have
128 - `read` permissions for Shaarli resources
129 - `execute` permissions for Shaarli directories _AND_ their parent directories
131 On a production server:
132 - `user:group` will likely be `http:http`, `www:www` or `www-data:www-data`
133 - files will be located under `/var/www`, `/var/http` or `/usr/share/nginx`
135 On a development server:
136 - files may be located in a user's home directory
137 - in this case, make sure both Nginx and PHP-FPM are running as the local user/group!
139 For all following examples, a development configuration will be used:
140 - `user:group = john:users`,
142 which corresponds to the following service configuration:
145 ; /etc/php/php-fpm.conf
155 # /etc/nginx/nginx.conf
164 _WARNING: Use for development only!_
170 worker_connections 1024;
175 default_type application/octet-stream;
176 keepalive_timeout 20;
178 index index.html index.php;
182 server_name localhost;
185 access_log /var/log/nginx/access.log;
186 error_log /var/log/nginx/error.log;
189 access_log /var/log/nginx/shaarli.access.log;
190 error_log /var/log/nginx/shaarli.error.log;
193 location ~ (index)\.php$ {
194 fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
195 fastcgi_index index.php;
196 include fastcgi.conf;
203 The previous setup is sufficient for development purposes, but has several major caveats:
204 - every content that does not match the PHP rule will be sent to client browsers:
205 - dotfiles - in our case, `.htaccess`
206 - temporary files, e.g. Vim or Emacs files: `index.php~`
207 - asset / static resource caching is not optimized
208 - if serving several PHP sites, there will be a lot of duplication: `location /shaarli/`, `location /mysite/`, etc.
210 To solve this, we will split Nginx configuration in several parts, that will be included when needed:
213 # /etc/nginx/deny.conf
215 # deny access to dotfiles
222 # deny access to temp editor files, e.g. "script.php~"
230 # /etc/nginx/php.conf
231 location ~ (index)\.php$ {
232 # filter and proxy PHP requests to PHP-FPM
233 fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
234 fastcgi_index index.php;
235 include fastcgi.conf;
239 # deny access to all other PHP scripts
245 # /etc/nginx/static_assets.conf
246 location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
248 add_header Pragma public;
249 add_header Cache-Control "public, must-revalidate, proxy-revalidate";
254 # /etc/nginx/nginx.conf
261 access_log /var/log/nginx/access.log;
262 error_log /var/log/nginx/error.log;
265 # virtual host for a first domain
267 server_name my.first.domain.org;
270 access_log /var/log/nginx/shaarli.access.log;
271 error_log /var/log/nginx/shaarli.error.log;
275 include static_assets.conf;
280 # virtual host for a second domain
282 server_name second.domain.com;
285 access_log /var/log/nginx/minigal.access.log;
286 error_log /var/log/nginx/minigal.error.log;
290 include static_assets.conf;
296 ### Redirect HTTP to HTTPS
297 Assuming you have generated a (self-signed) key and certificate, and they are located under `/home/john/ssl/localhost.{key,crt}`, it is pretty straightforward to set an HTTP (:80) to HTTPS (:443) redirection to force SSL/TLS usage.
300 # /etc/nginx/nginx.conf
306 index index.html index.php;
309 access_log /var/log/nginx/access.log;
310 error_log /var/log/nginx/error.log;
314 server_name localhost;
316 return 301 https://localhost$request_uri;
321 server_name localhost;
323 ssl_certificate /home/john/ssl/localhost.crt;
324 ssl_certificate_key /home/john/ssl/localhost.key;
327 access_log /var/log/nginx/shaarli.access.log;
328 error_log /var/log/nginx/shaarli.error.log;
332 include static_assets.conf;