How to install Wiki.js on Ubuntu 16.04

I’ve been on quite the roll as of late with integrating new services into my network. My latest addition has been the implementation of a wiki to keep track of things that I’m not publicly posting. Years ago I was a huge fan of Deki Wiki but it has seen its demise as an open source wiki alternative. For years I had a void where I just couldn’t find one that was quick, easy to edit information, and actually looked like it came from the current century (sorry MediaWiki, you just don’t do it for me).

Last night I started googling for a new wiki that offered a modern and attractive interface, was open source, and ran on Linux. I found Wiki.js and found this link with instructions to install it. The link did a pretty good job at providing these instructions, however, it was missing a few different pieces that were actually critical to the install.

Prerequisites

Installing Wiki.js is pretty simple and you will need to start with a fresh copy of Ubuntu 16.04 for these instructions to work. In addition to the OS, you’ll need to have your domain ready to go and the DNS entries in whatever you are using for DNS. This probably isn’t absolutely required, but definitely made it easier. The last thing you’ll need is an SSL certificate. You can either use a self-signed or something certbot and letsencrypt (my new favorite).

To make my life easier, I like to run all of these commands, except where noted, as root so I don’t have to remember sudo every time. In case you didn’t know, you can do this via the following:

# sudo su

Once you have Ubuntu up and running, you’ll log in and install curl, Node.js v8.x, and build-essential using the following commands:

# apt -y install curl
# curl -sL https://deb.nodesource.com/setup_8.x | bash –
# apt -y install nodejs build-essential

Once those are installed, you’ll then install MongoDB v3.4:

# apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 0C49F3730359A14518585931BC711F9BA15703C6
# echo “deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse” | tee /etc/apt/sources.list.d/mongodb-org-3.4.list
# apt update
# apt -y install mongodb-org

The referenced link for this process is missing a pretty critical step at this point. In the article, they tell you to go ahead and start and enable mongodb. However, that won’t work because, by default, mongodb doesn’t create the service file. So, before you can start it, create a new file called /etc/systemd/system/mongodb.service and add the following text within that file:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
ExecStart=/usr/bin/mongod –quiet –config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

Once that’s complete, then you can go ahead and start and enable mongodb.

# systemctl start mongodb

# systemctl enable mongodb

The final prerequisite is git, which you can install via the following:

# apt -y install git

Install Wiki.js

Now that we have the prerequisites out of the way, we can begin the install of Wiki.js. Begin by creating a new directory and moving to that directory:

# mkdir /srv/wiki.js
# cd /srv/wiki.js

Now, if you’re running this as root, you’ll want to exit root and run the following as your created user. When I ran this as root, it actually errors out stating it doesn’t have permission to a directory (I know that doesn’t make sense, but it did it repeatedly until I exited root).

# npm install wiki.js@latest

It’ll run a bunch of text on the screen and then you’ll see a prompt asking you what port you’d like to run on. I would select the top option which is the default at port 3000. Once you have selected that, you’ll see the following message:

> Browse to http://your-server:3000/ to configure your wiki! (Replaced your-server with the hostname or IP of your server!)
▐ ⠂ ▌ I’ll wait until you’re done 😉

Do what the message says, BUT don’t do anything once you get the main page loaded in your browser – we need to do some other config first. Once this is done, it’s safe to return to root.

Setup nginx

Leave the SSH session open from the previous step, with your browser open to the link that was provided, and then open up another ssh session to the server.

From this session, you will start by installing nginx.

# apt -y install nginx

Once that’s installed, create a new config file at /etc/nginx/sites-available/wiki.example.com.conf with the following information:

server {
listen [::]:80 ipv6only=off;
server_name wiki.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name wiki.example.com;

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers “EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH EDH+aRSA !RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS”;
ssl_prefer_server_ciphers on;

ssl_certificate /etc/nginx/ssl/wiki.example.com.crt;
ssl_certificate_key /etc/nginx/ssl/wiki.example.com.key;
ssl_trusted_certificate /etc/nginx/ssl/CA.crt;

location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “upgrade”;
proxy_next_upstream error timeout http_502 http_503 http_504;
}
}

If you already have an SSL certificate from letsencrypt (or some other authority), I’ve been putting a path to a symbolic link in this config file that way I don’t have to keep going into the config file when the certificate renews. Then just update a separate folder with the new cert and keep it the same name as before.

Once you have the conf file created you need to activate it by creating a symbolic link to it in the sites-enabled folder using the following commands:

# cd /etc/nginx/sites-enabled/
# ln -s ../sites-available/wiki.example.com.conf
# unlink default

Now you can restart nginx:

# systemctl restart nginx

Configure Wiki.js

Now that you have the nginx config done, you can go back to your browser and walk through the steps. I used the same answers as the link I provided and they worked pretty well for me:

  • Welcome!: Start
  • System Check (if all good): Continue
  • General:
    • Site title: ExampleWiki
    • Host: https://wiki.example.comPort: 3000
    • Site UI Language: English
    • Public Access: Not selected
    • Press: Continue
  • Important Considerations: Continue
  • Database: mongodb://localhost:27017/wiki
  • Database Check: Continue:
  • Paths:
    • Local Data Path: ./data
    • Local Repository Path: ./repo
  • Git Repository: Skip this step
  • Git Repository Check: Continue
  • Administrator Account
    • Administrator Email: admin@example.com
    • Password: MySecretCombination
    • ConfirmPassword: MySecretCombination
  • Finalizing: Start

Once you have it configured, you should be taken to the home page of the wiki site. Now that it’s ready to go, you’ll probably want to enable it to start when you boot the OS. To do so, do the following:

# npm install -g pm2
# pm2 startup

After you run pm2 startup, you’ll see a command you need to run as root. This is a part I missed. Make sure you’re running as root again and run the command the script tells you to. Once that’s complete, then you can run:

# pm2 save

Now you should have a fully functioning wiki.js instance.

Quick Notes

Here are some helpful commands to know when working with wiki.js. All of these need to be ran from the /srv/wiki.js directory:

node wiki stop

node wiki start

node wiki restart

node wiki -v