DokuWiki

It's better when it's simple

User Tools

Site Tools


install:nginx

nginx

FIXME this page needs some clean up. Preferably by an experienced nginx user.

There is a recipe in the nginx wiki and several examples below. Ideally they should be combined into a single coherent setup.

An explanation on the different setups using (F)CGI and FPM should be added.

Installation of the needed components for different Operating Systems should be moved into a separate section - the configuration shouldn't depend on OS specifics anyway.

DokuWiki with nginx on Ubuntu

You need to install nginx and php-fpm, if you are on a current release, this will be at least php7.0.

The nginx config below is based mostly on the sample configuration in this site here updated to work with current releases (tested with 16.04 LTS). With the image caching config from this site

On the initial setup you won't be able to start unless you comment the deny to the install.php file This is the full configuration we use you can simply copy paste and change the names to your liking. please be aware this is running as a vhost. You'll likely need to put it in /etc/nginx/sites-enabled by default (or somewhere else if you've changed the configuration)

Note this is https, so you will either need a certificate or use a self-signed certificate to configure this way. The configuration for the https certificate is typical like this (to get a good security rating):

# file with the cert + intermediates
ssl_certificate /etc/ssl/certs/example_com_pack.crt;
ssl_certificate_key /etc/ssl/private/example_com.key;
ssl_session_timeout 5m;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
# Use 2048 bit Diffie-Hellman RSA key parameters
# (otherwise Nginx defaults to 1024 bit, lowering the strength of encryption # when using PFS)
# Generated by OpenSSL with the following command:
# openssl dhparam -outform pem -out /etc/nginx/ssl/dhparam2048.pem 2048
ssl_dhparam /etc/ssl/private/dhparam2048.pem;
server {
    listen               80;
    listen                   [::]:80;
    server_name          wiki.domain.example;
    return 301 https://$server_name$request_uri;
}
 
server {
    listen [::]:443 ssl;
    listen 443 ssl;
 
    server_name wiki.domain.example;
 
    # Maximum file upload size is 4MB - change accordingly if needed
    client_max_body_size 4M;
    client_body_buffer_size 128k;
 
    root /dokuwiki;
    index doku.php;
 
    #Remember to comment the below out when you're installing, and uncomment it when done.
    location ~ /(conf/|bin/|inc/|vendor/|install.php) { deny all; }
 
    #Support for X-Accel-Redirect
    location ~ ^/data/ { internal ; }
 
    location ~ ^/lib.*\.(js|css|gif|png|ico|jpg|jpeg)$ {
        expires 365d;
    }
 
    location / { try_files $uri $uri/ @dokuwiki; }
 
    location @dokuwiki {
        # rewrites "doku.php/" out of the URLs if you set the userwrite setting to .htaccess in dokuwiki config page
        rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last;
        rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last;
        rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
        rewrite ^/(.*) /doku.php?id=$1&$args last;
    }
 
    location ~ \.php$ {
        try_files $uri $uri/ /doku.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param REDIRECT_STATUS 200;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        # fastcgi_pass unix:/var/run/php5-fpm.sock; #old php version
    }
}

Once this file has been placed you should restart nginx and php-fpm.

service php7.0-fpm reload && service nginx reload
# service php5-fpm reload && service nginx reload # for the older php5 version

If you get a 502 gateway issue, it usually means there is a problem with your phpfpm socket configuration (usually wrong path to the socket). Please see the docs for phpfpm and nginx.

DokuWiki with nginx on Windows

Dokuwiki has been extracted to E:\www\dokuwiki.

This is an example for dokuwiki entry in nginx.conf running on Windows:

location ~ ^/dokuwiki/.*\.php {
    root           E:/www;	         
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME    E:/www$fastcgi_script_name;
    fastcgi_param  QUERY_STRING       $query_string;
    include        fastcgi_params;
}
# serve static files
location ~ ^/dokuwiki/lib/  {
    root         E:/www;		
    expires 30d;
}
 
location ~ ^/dokuwiki/conf/ { deny all; }
location ~ ^/dokuwiki/data/ { deny all; }
location ~ /\.ht            { deny all; }

FastCGI and nginx have been started using this batch file:

ECHO Starting PHP FastCGI...
RunHiddenConsole.exe E:\appl\php-5.2.9-2-Win32\php-cgi.exe -b 127.0.0.1:9000
ECHO Starting nginx...
cd /d D:\nginx-0.8.39 && start nginx.exe

Or you could try Start-Nginx if you prefer a normal windows GUI.

Linux configuration for Debian 8

You need to have already installed nginx and php5-fpm.

Depending of your choices your php part in sites documents could be with socks or ports.

You just copy paste what's working for you.

This site configuration is without ssl and use URL rewrite

in that example dokuwiki is at the root of a domain called exempl.net

As root :

create a new nginx site file :

nano /etc/nginx/sites-available/exempl.net

you paste in the code below of course change the path to your doku wiki and change the server_name :

server {
      listen 80;
      root /var/www/exempl.net;
      server_name exempl.net;
      index index.php index.html doku.php;
      location ~ ^/(data|conf|bin|inc) {
              return 404;
      }
      location ~ ^/lib.*\.(gif|png|ico|jpg)$ {
              expires 31d;
      }
      location / {
              try_files $uri $uri/ @dokuwiki;
      }
      location @dokuwiki {
              rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last;
              rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last;
              rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
              rewrite ^/tag/(.*) /doku.php?id=tag:$1&do=showtag&tag=tag:$1 last;
              rewrite ^/(.*) /doku.php?id=$1&$args last;
      }
	  
	 # here you paste your location ~ \.php$ 
	 # here is mine commented
     # location ~ \.php$ {
     #         include /etc/nginx/fastcgi_params;
     #         fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     #         fastcgi_pass unix:/var/run/php5-fpm.sock;
     # }
 }

and make it enable and restart ngnix :

ln -s /etc/nginx/sites-available/exempl.net /etc/nginx/sites-enabled/
 
service nginx restart

Rewrite

See nginx documentation. In the following example, our server root is /var/www, and we extract dokuwiki to /var/www/wiki.

server {
    listen 80;
    server_name example.com www.example.com;
 
    #maximum file upload size is 4MB - change accordingly if needed
    client_max_body_size 4M;
    client_body_buffer_size 128k;
 
    root /var/www/wiki;
    index doku.php;
 
    location / { try_files $uri $uri/ @dokuwiki; }
 
    location @dokuwiki {
        rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last;
        rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last;
        rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
        rewrite ^/(.*) /doku.php?id=$1&$args last;
    }
 
    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param REDIRECT_STATUS 200;
        fastcgi_pass 127.0.0.1:9000;
    }
}

Notes

If using https the HTTPS server variable has to be set to allow proper linking in dokuwiki. This can be done in the server section with:

fastcgi_param HTTPS on;

The last keyword of rewrite rules before location setup make sure that rewrite only happens once. You should replace all /dokuwiki/ appeared above to you wiki directory relative to web server root directory.

If you are copy-pasting these configs, make sure you are adding locations to secure some of the directories as described on Security page.

install/nginx.txt · Last modified: 2023-09-22 19:44 by andi

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki