<p><em>Example virtual host configurations for popular web servers</em></p>
<ul>
<li><ahref="#apache">Apache</a></li>
<li><ahref="#nginx">Nginx</a></li>
</ul>
<h2id="prerequisites">Prerequisites</h2>
<h3id="shaarli">Shaarli</h3>
<ul>
<li>Shaarli is installed in a directory readable/writeable by the user</li>
<li>the correct read/write permissions have been granted to the web server <em>user and/or group</em></li>
<li>for HTTPS / SSL:</li>
<li>a key pair (public, private) and a certificate have been generated</li>
<li>the appropriate server SSL extension is installed and active</li>
</ul>
<h3id="https-tls-and-self-signed-certificates">HTTPS, TLS and self-signed certificates</h3>
<p>Related guides:
<em><ahref="http://www.xenocafe.com/tutorials/linux/centos/openssl/self_signed_certificates/index.php">How to Create Self-Signed SSL Certificates with OpenSSL</a>
</em><ahref="https://workaround.org/certificate-authority">How do I create my own Certificate Authority?</a>
* Generate a self-signed certificate (will trigger browser warnings) with apache2: <code>make-ssl-cert generate-default-snakeoil --force-overwrite</code> will create <code>/etc/ssl/certs/ssl-cert-snakeoil.pem</code> and <code>/etc/ssl/private/ssl-cert-snakeoil.key</code></p>
<h3id="proxies">Proxies</h3>
<p>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:
- <code>X-Forwarded-Proto</code>;
- <code>X-Forwarded-Host</code>;
- <code>X-Forwarded-For</code>.</p>
<p>See also <ahref="https://github.com/shaarli/Shaarli/issues?utf8=%E2%9C%93&q=label%3Aproxy+">proxy-related</a> issues.</p>
<h2id="apache">Apache</h2>
<h3id="minimal">Minimal</h3>
<pre><codeclass="apache"><VirtualHost *:80>
ServerName shaarli.my-domain.org
DocumentRoot /absolute/path/to/shaarli/
</VirtualHost>
</code></pre>
<h3id="debug-log-all-the-things">Debug - Log all the things!</h3>
<p>This configuration will log both Apache and PHP errors, which may prove useful to identify server configuration errors.</p>
<p>See:
<em><ahref="http://stackoverflow.com/q/176">Apache/PHP - error log per VirtualHost</a> (StackOverflow)
</em><ahref="https://ma.ttias.be/php-php_value-vs-php_admin_value-and-the-use-of-php_flag-explained/">PHP: php_value vs php_admin_value and the use of php_flag explained</a></p>
<p>Shaarli use <code>.htaccess</code> Apache files to deny access to files that shouldn't be directly accessed (datastore, config, etc.). You need the directive <code>AllowOverride All</code> in your virtual host configuration for them to work.</p>
<p><strong>Warning</strong>: If you use Apache 2.2 or lower, you need <ahref="https://httpd.apache.org/docs/current/mod/mod_version.html">mod_version</a> to be installed and enabled.</p>
<p>Apache module <code>mod_rewrite</code><strong>must</strong> be enabled to use the REST API. URL rewriting rules for the Slim microframework are stated in the root <code>.htaccess</code> file.</p>
<h2id="lighthttpd">LightHttpd</h2>
<h2id="nginx">Nginx</h2>
<h3id="foreword">Foreword</h3>
<p>Nginx does not natively interpret PHP scripts; to this effect, we will run a <ahref="https://en.wikipedia.org/wiki/FastCGI">FastCGI</a> service, to which Nginx's FastCGI module will proxy all requests to PHP resources.</p>
<p>Required packages:
- <ahref="http://nginx.org">nginx</a>
- <ahref="http://php-fpm.org">php-fpm</a> - PHP FastCGI Process Manager</p>
<p>Once Nginx and PHP-FPM are installed, we need to ensure:
- Nginx and PHP-FPM are running using the <em>same user and group</em>
- both these user and group have
- <code>read</code> permissions for Shaarli resources
- <code>execute</code> permissions for Shaarli directories <em>AND</em> their parent directories</p>
<p>On a production server:
- <code>user:group</code> will likely be <code>http:http</code>, <code>www:www</code> or <code>www-data:www-data</code>
- files will be located under <code>/var/www</code>, <code>/var/http</code> or <code>/usr/share/nginx</code></p>
<p>On a development server:
- files may be located in a user's home directory
- in this case, make sure both Nginx and PHP-FPM are running as the local user/group!</p>
<p>For all following configuration examples, this user/group pair will be used:
- <code>user:group = john:users</code>,</p>
<p>which corresponds to the following service configuration:</p>
<pre><codeclass="ini">; /etc/php/php-fpm.conf
user = john
group = users
[...]
listen.owner = john
listen.group = users
</code></pre>
<pre><codeclass="nginx"># /etc/nginx/nginx.conf
user john users;
http {
[...]
}
</code></pre>
<h3id="optional-increase-the-maximum-file-upload-size">(Optional) Increase the maximum file upload size</h3>
<p>Some bookmark dumps generated by web browsers can be <em>huge</em> due to the presence of Base64-encoded images and favicons, as well as extra verbosity when nesting links in (sub-)folders.</p>
<p>To increase upload size, you will need to modify both nginx and PHP configuration:</p>
<pre><codeclass="nginx"># /etc/nginx/nginx.conf
http {
[...]
client_max_body_size 10m;
[...]
}
</code></pre>
<pre><codeclass="ini"># /etc/php5/fpm/php.ini
[...]
post_max_size = 10M
[...]
upload_max_filesize = 10M
</code></pre>
<h3id="minimal_1">Minimal</h3>
<p><em>WARNING: Use for development only!</em></p>
<pre><codeclass="nginx">user john users;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 20;
index index.html index.php;
server {
listen 80;
server_name localhost;
root /home/john/web;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /shaarli/ {
try_files $uri /shaarli/index.php$is_args$args;
access_log /var/log/nginx/shaarli.access.log;
error_log /var/log/nginx/shaarli.error.log;
}
location ~ (index)\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
</code></pre>
<h3id="modular">Modular</h3>
<p>The previous setup is sufficient for development purposes, but has several major caveats:
- every content that does not match the PHP rule will be sent to client browsers:
- dotfiles - in our case, <code>.htaccess</code>
- temporary files, e.g. Vim or Emacs files: <code>index.php~</code>
- asset / static resource caching is not optimized
- if serving several PHP sites, there will be a lot of duplication: <code>location /shaarli/</code>, <code>location /mysite/</code>, etc.</p>
<p>To solve this, we will split Nginx configuration in several parts, that will be included when needed:</p>
<pre><codeclass="nginx"># /etc/nginx/deny.conf
location ~ /\. {
# deny access to dotfiles
access_log off;
log_not_found off;
deny all;
}
location ~ ~$ {
# deny access to temp editor files, e.g. "script.php~"
access_log off;
log_not_found off;
deny all;
}
</code></pre>
<pre><codeclass="nginx"># /etc/nginx/php.conf
location ~ (index)\.php$ {
# Slim - split URL path into (script_filename, path_info)
# serve the Shaarli favicon from its custom location
alias /var/www/shaarli/images/favicon.ico;
}
include deny.conf;
include static_assets.conf;
include php.conf;
}
server {
# virtual host for a second domain
listen 80;
server_name second.domain.com;
location /minigal/ {
access_log /var/log/nginx/minigal.access.log;
error_log /var/log/nginx/minigal.error.log;
}
include deny.conf;
include static_assets.conf;
include php.conf;
}
}
</code></pre>
<h3id="redirect-http-to-https">Redirect HTTP to HTTPS</h3>
<p>Assuming you have generated a (self-signed) key and certificate, and they are located under <code>/home/john/ssl/localhost.{key,crt}</code>, it is pretty straightforward to set an HTTP (:80) to HTTPS (:443) redirection to force SSL/TLS usage.</p>
<pre><codeclass="nginx"># /etc/nginx/nginx.conf
[...]
http {
[...]
index index.html index.php;
root /home/john/web;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server {
listen 80;
server_name localhost;
return 301 https://localhost$request_uri;
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /home/john/ssl/localhost.crt;
ssl_certificate_key /home/john/ssl/localhost.key;
location /shaarli/ {
# Slim - rewrite URLs
try_files $uri /index.php$is_args$args;
access_log /var/log/nginx/shaarli.access.log;
error_log /var/log/nginx/shaarli.error.log;
}
location = /shaarli/favicon.ico {
# serve the Shaarli favicon from its custom location
Built with <ahref="http://www.mkdocs.org">MkDocs</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.