Self Host a Ghost blog with anonymous comments via Commento on one server on Ubuntu 18.04

There are lots of guides on self-hosting ghost out there, but in this one I will focus on hosting Ghost and commento on one server, to add commenting functionality to your Ghost blog.

Prerequisites

Setting up your domain

You will need two DNS records pointing to your server, in this tutorial I will be using commento.roettgers.co and roettgers.co.

Optional: To make life easier when changing your server, I recommend setting one A record pointing to your server, and one ALIAS record pointing to the subdomain with the A record.

Setting up your webserver

First we will install nginx and certbot for automatic, easy and free SSL certificates from Let's Encrypt.

sudo apt update
sudo add-apt-repository ppa:certbot/certbot
sudo apt install nginx python-certbot-nginx

Now, we'll need to allow incoming web connections if we haven't already:

sudo ufw allow 'Nginx Full'
This opens port 80 and 443 in the firewall

Ghost sets up it's Nginx configuration automatically, but we'll need to write our own for Commento. Create a file at /etc/nginx/sites-available/commento.yourdomain.com.conf and paste the following:

server {
    listen 80;
    listen [::]:80 ipv6only=on;

    server_name commento.yourdomain.com;
    root /var/www/commento.yourdomain.com;

    index index.html;
    location / {
            proxy_set_header Host $host;
            proxy_pass http://localhost:8081;
    }
}

This proxies all requests to commento.yourdomain.com to port 8081 (we will set up Commento on this port). The only thing left to do is to create a SSL certificate with certbot. Execute these commands and select commento.yourdomain.com when you're asked.

sudo ln -s /etc/nginx/sites-available/commento.yourdomain.com.conf  /etc/nginx/sites-enabled/commento.yourdomain.com.conf
sudo systemctl reload nginx
sudo certbot --rsa-key-size 4096 --nginx
sudo systemctl reload nginx

Awesome 🎉, we have set up nginx and can now install Ghost.

Further Reading: Refer to this guide to set up another reverse proxy. If you want to read more about how to set up nginx I recommend this blog post on dennisnotes.com.

Installing Ghost

Before we can set up Ghost, we need to install it and its dependencies.

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash
sudo apt install mysql-server
sudo apt-get install -y nodejs
sudo npm install ghost-cli@latest -g
Install MySQL, Node and finally Ghost

After we installed the dependencies, we also need to update our database user with a more secure user.

# login to the MySQL shell
sudo mysql

# update the password of the root user
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yoursupersecurepassword';

# exit the MySQL shell
quit

# change back to your ubuntu user (replace <user> with your username)
su - <user>

Finally, we can start the Ghost set up process

sudo mkdir -p /var/www/ghost

# Allow us to modify this directory
# Replace <user> with your username
sudo chown <user>:<user> /var/www/ghost
sudo chmod 775 /var/www/ghost

cd /var/www/ghost

# Install Ghost
# answer yes to all questions
ghost install

Great, now we have a Ghost website live 🚀, which we can already visit. We'll do that after we have set up and installed Commento.

Further reading: If you encounter any errors, check the official Ghost documentation this part of the tutorial is based on.

Installing Commento

Before we can install Commento, we have to install another database, as Commento uses PostgreSQL and not MySQL.

sudo apt install postgres postgres-contrib

After we installed PostgreSQL we can create a new database user for Commento

# login to the postgres shell
sudo --user=postgres psql

# in the postgres shell
CREATE USER commento SUPERUSER WITH PASSWORD 'yoursupersecurepassword';

# exit the postgres shell
\q

Sadly, Commento does not provide their app via apt yet, so we need to manually download and install the binary. Copy the download link of the latest release .tar.gz from the releases page, you will need it for these commands

# run this in a directory where you want to install commento
wget <binary_link_here>
mkdir commento
tar xvf <binary_filename> -C commento

We want to easily be able to automatically start Commento on start up, as well as manually start and stop it. To achieve this we will create a systemd service. Create the file /etc/systemd/system/commento.service and paste the following

[Unit]
Description=Commento daemon service
After=network.target postgresql.service

[Service]
Type=simple
ExecStart=/usr/bin/commento
Environment=COMMENTO_PORT=8081
Environment=COMMENTO_STATIC=/usr/share/commento

# Fill in these with the correct values of your installation
Environment=COMMENTO_POSTGRES=postgres://commento:<yourdatabasepassword>@db:5432/commento?sslmode=disable
Environment=COMMENTO_ORIGIN=https://commento.yourdomain.com

# Uncomment the next line after you created your commento user and reload the service
# Environment=COMMENTO_FORBID_NEW_OWNERS=true

# We will add more variables later here

[Install]
WantedBy=multi-user.target

This is sufficient for running Commento. We will add SMTP credentials later to send emails about comment replies, but lets start Commento first.

# run this to start and enable commento to run on system start up
sudo chmod 644 /etc/systemd/system/commento.service
sudo systemctl start commento
sudo systemctl enable commento

# to reload commento after changes to the service file run:
sudo systemctl stop commento
sudo systemctl daemon-reload
sudo systemctl start commento

# if something doesn't work, run this to see what's wrong:
sudo systemctl status commento

Now we need an SMTP server, I recommend using an ESP (Email Service Provider) like mailgun.com, but you could even use your Gmail Account. To enable emails, add these lines to your service file and reload the service:

Environment=COMMENTO_SMTP_HOST=<your_smtp_host>
Environment=COMMENTO_SMTP_USERNAME=<your_smtp_username>
Environment=COMMENTO_SMTP_PASSWORD=<your_smtp_password>
Environment=COMMENTO_SMTP_PORT=<your_smtp_port>
Environment=COMMENTO_SMTP_FROM_ADDRESS=<email_address_to_send_form>

Further reading: Check the Commento documentation, to see what else you can customise and how to add social login.

Putting it all together

Now we've installed Ghost and Commento, the only thing left to do is to integrate the two.

First, visit the URL of your Ghost site and create your account if you haven't already. It will walk you through the admin panel and explain how Ghost works.

Next, visit the URL of your Commento instance and create an account. You will be greeted by an admin panel. Click "New Domain" and enter the information of your Ghost blog. After you've done this, don't forget to edit the service file to prevent the creation of further admin accounts.

To show the comments on our blog we need to add them to our theme. Download the zip file of your Ghost theme from the "Design" section of the admin panel and unzip it on your computer.

This is the tricky part:

  1. Find the page where you want your comments (usually this is post.hbs)
  2. Find the part of the file where your comment section should be (refer to the documentation of your theme)
  3. Insert the snippet from your Commento Dashboard
  4. Save and zip the theme folder.
  5. Upload your edited theme to your Ghost site.

Congratulations 🎉, you've just added privacy-respecting comments to your freshly created blog.

Show Comments