Not logged in. · Lost password · Register
Forum: General Help and Support Installation and Configuration RSS
Installing on nginx in a subdirectory?
Avatar
FrenchFry #1
Member for 3 months · 2 posts
Group memberships: Members
Show profile · Link to this post
Subject: Installing on nginx in a subdirectory?
Hello everyone,

I'm trying to install Dokuwiki on my Debian 9 VPS, behind nginx. I read the official instructions and a few more tutorials, but all of them only contain instructions for installing Dokuwiki in the root domain. I already have a website in the root domain, so I want to put Dokuwiki in a subdirectory (www.domain.com/subdirectory).

I tried meddling with the nginx config a little, but the best thing I got is "File not found" when I try accessing install.php.

Here's my current nginx config for the site:

server {
  root /var/www/domain.com/html;
  index index.html;

  server_name domain.com;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
  }

  location /subdirectory {
    alias /var/www/dokuwiki;
    #index index.html index.htm index.php doku.php;

    try_files $uri $uri/ @dokuwiki;

    location ~ \.php$ {
      try_files $uri =404;
      fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
      fastcgi_index  index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
  }

  #location ^~ /subdirectory/conf/ { return 403; }
  #location ^~ /subdirectory/data/ { return 403; }
  #location ~ /subdirectory/\.ht { deny all; }

  location @dokuwiki {
    rewrite ^/subdirectory/_media/(.*) /lib/exe/fetch.php?media=$1 last;
    rewrite ^/subdirectory/_detail/(.*) /lib/exe/detail.php?media=$1 last;
    rewrite ^/subdirectory/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
    rewrite ^/subdirectory/(.*) /doku.php?id=$1 last;
  }


  listen [::]:443 ssl; # managed by Certbot
  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by $
  ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed b$
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
  if ($host = domain.com) {
        return 301 https://$host$request_uri;
      } # managed by Certbot


      listen 80;
      listen [::]:80;

      server_name domain.com;
      return 404; # managed by Certbot
}

Could anyone help? Thanks.
Avatar
schplurtz (Moderator) #2
Member since Nov 2009 · 474 posts · Location: France, Finistère
Group memberships: Global Moderators, Members
Show profile · Link to this post
Hi.

I also run a dokuwiki under a subdirectory with nginx. On my computer, the latest DW is always available at https://schplurtz.mydomain.org/latestdw . Here are the relevant part of my nginx config file. The only difference I can see between your config and mine is that you use alias, while I use root. Using root imposes one more directory. On the drive, doku.php is located at /web/mainsite/dokus/latestdw/doku.php . (latestdw directory is here to match the /latestdw of the URL)

server {
    server_name  schplurtz.mydomain.org schplurtz.mydomain.fr;

    # NEVER, EVER activate these settings, except when debugging.
    # Just too many lines (hundreds) for each and every single request.
    #DEBUG-OFF# error_log  /var/log/nginx-error-default_server-debug.log debug;
    #DEBUG-OFF# access_log  /var/log/nginx-http-default_server-access.log combined;
    #DEBUG-OFF# rewrite_log on;

    root /web/mainsite/mainapp;
    include partials/schplurtz/root-app.conf;

    # include partials/schplurtz/doku/latestdw.conf; # inlined
    #
    # DOKUWIKI-LATEST CONFIG START
    #
    
    # Deny access to data, conf etc... folders
    location ~ /latestdw/(data|conf|bin|inc)/ {
        deny all;
    }
    
    location /latestdw {
        # there must exist a directory named latestdw inside /web/mainsite/dokus.
        root /web/mainsite/dokus;
        index index.php;
        try_files $uri $uri/ @latestdw;

        # Since root is changed, php config must be set again.
        location ~ \.php$ {
                # Workaround PHP vulnerability:
                # http://forum.nginx.org/read.php?2,88845,page=3
                try_files $uri =404;

                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include fastcgi_params;
                keepalive_timeout 0;
                fastcgi_param     SCRIPT_FILENAME $document_root$fastcgi_script_name;
                # Must match php-fastcgi service options, of course...
                fastcgi_pass      unix:/run/php/php7.0-fpm.sock;
        }
    }
    
    location @latestdw {
        rewrite ^/latestdw/_media/(.*)          /latestdw/lib/exe/fetch.php?media=$1 last;
        rewrite ^/latestdw/_detail/(.*)         /latestdw/lib/exe/detail.php?media=$1 last;
        rewrite ^/latestdw/_export/([^/]+)/(.*) /latestdw/doku.php?do=export_$1&id=$2 last;
        rewrite ^/latestdw/(.*)                 /latestdw/doku.php?id=$1 last;
    }
    #
    # DOKUWIKI-LATEST CONFIG END
    #

    # php config for the main site root (/web/mainsite/mainapp)
    location ~ \.php$ {
            # Workaround PHP vulnerability:
            # http://forum.nginx.org/read.php?2,88845,page=3
            try_files $uri =404;

            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include fastcgi_params;
            keepalive_timeout 0;
            fastcgi_param     SCRIPT_FILENAME $document_root$fastcgi_script_name;
            # Must match php-fastcgi service options, of course...
            fastcgi_pass      unix:/run/php/php7.0-fpm.sock;
    }

    location /QUELBAZAR {
            autoindex on;
    }
}

nginx config is tricky. If you want to know what nginx does with the URL, try to add those lines at the start of the server section of your nginx config file.
    # NEVER, EVER activate these settings, except when debugging.
    # Just too many lines (hundreds) for each and every single request.
    error_log  /tmp/nginx-error-debug.log debug;
    access_log  /tmp/nginx-access.log combined;
    rewrite_log on;

Restart nginx try to access /subdirectory/install.php then grep -E 'http request line| using |location' /tmp/nginx-error-debug.log, (or simply browse the file with your favorite pager). grep will print things like this that may help understanding what is happening :
2019/07/21 15:08:05 [debug] 25106#25106: *1 http request line: "GET /4newdoku/foo HTTP/1.1"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "QUELBAZAR"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "4newdoku/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: ~ "/([._]ht|README$|VERSION$|COPYING$)"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: ~ "/4newdoku/(data|conf|bin|inc)/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: ~ "/latestdw/(data|conf|bin|inc)/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: ~ "/doku-test"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: ~ "\.php$"
2019/07/21 15:08:05 [debug] 25106#25106: *1 using configuration "/4newdoku/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "@4newdoku"
2019/07/21 15:08:05 [debug] 25106#25106: *1 using location: @4newdoku "/4newdoku/foo?"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "QUELBAZAR"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "4newdoku/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: ~ "/([._]ht|README$|VERSION$|COPYING$)"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: ~ "/4newdoku/(data|conf|bin|inc)/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: ~ "/latestdw/(data|conf|bin|inc)/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: ~ "/doku-test"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: ~ "\.php$"
2019/07/21 15:08:05 [debug] 25106#25106: *1 using configuration "\.php$"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "QUELBAZAR"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "4newdoku/"
2019/07/21 15:08:05 [debug] 25106#25106: *1 test location: "404.html"
2019/07/21 15:08:05 [debug] 25106#25106: *1 using configuration "=/404.html"
2019/07/21 15:10:48 [debug] 25108#25108: *3 http request line: "GET /latestdw/install.php HTTP/1.1"
2019/07/21 15:10:48 [debug] 25108#25108: *3 test location: "/"
2019/07/21 15:10:48 [debug] 25108#25108: *3 test location: "QUELBAZAR"
2019/07/21 15:10:48 [debug] 25108#25108: *3 test location: "latestdw"
2019/07/21 15:10:48 [debug] 25108#25108: *3 test location: ~ "\.php$"
2019/07/21 15:10:48 [debug] 25108#25108: *3 using configuration "\.php$"
Avatar
FrenchFry #3
Member for 3 months · 2 posts
Group memberships: Members
Show profile · Link to this post
Thank you for your help schplurtz!

I tried your config and even modified it a bit, but it still didn't work for some reason.

After some googling around for error messages, I substituted this line from the config I posted:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

with this

fastcgi_param SCRIPT_FILENAME $request_filename;

And it finally worked. Thank you for the help, again!
Avatar
schplurtz (Moderator) #4
Member since Nov 2009 · 474 posts · Location: France, Finistère
Group memberships: Global Moderators, Members
Show profile · Link to this post
Damn, you're right ! It's even documented on nginx config pitfalls page :

https://www.nginx.com/resources/wiki/start/topics/tutorial…

Glad it works, and happy DokuWiki.
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
Imprint
This board is powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2015 by Yves Goergen
Current time: 2019-10-16, 19:30:44 (UTC +02:00)