How To Host A Ghost Blog On Ubuntu VPS

How To Host A Ghost Blog On Ubuntu VPS
Photo by Karsten Winegeart / Unsplash

In this tutorial, I will guide you through the process of setting up a Ghost blog on an Ubuntu VPS. We will be using the latest version of Ghost, which is 0.7.1 as of this writing. We will also be using Nginx as our web server, and we will be securing our site with SSL using a free Let's Encrypt certificate.

Before we begin, you will need to have a domain name that you can use for your Ghost blog. In this guide, we will be using the domain name example.com. You will also need to have a VPS running Ubuntu 14.04 or 16.04.

If you do not have a domain name yet, you can register one from a domain name registrar such as Namecheap or GoDaddy. If you do not have a VPS, you can get one from a VPS provider such as DigitalOcean or Vultr.

If you are using DigitalOcean, you can use this link to get a $10 credit, which is enough to get started with a $5/month VPS.

Once you have a domain name and a VPS, you can log in to your server via SSH. If you are using a Windows computer, you can use PuTTY to connect to your server.

Once you are logged in, the first thing you need to do is update the package index and upgrade all of the installed packages to their latest versions:

sudo apt-get update
sudo apt-get upgrade

After the packages have been updated, you can install Nginx by running the following command:

sudo apt-get install nginx

Once Nginx has been installed, we need to create a server block for our Ghost blog.

You can create the server block file by running the following command:

sudo nano /etc/nginx/sites-available/example.com

Paste the following into the file, replacing example.com with your domain name:

server {
listen 80;
listen [::]:80;
server_name [example.com](<http://example.com/>);
return 301 https://$host$request_uri;
}

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name [example.com](<http://example.com/>);
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_pass [<http://127.0.0.1:2368/>](<http://127.0.0.1:2368/>);
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}

Save and close the file when you are finished.

Next, we need to enable the server block by creating a symbolic link from the sites-available directory to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Then, we need to remove the default server block file:

sudo rm /etc/nginx/sites-enabled/default

Finally, we need to test our Nginx configuration for syntax errors:

sudo nginx -t

If no errors are reported, we can go ahead and restart Nginx for the changes to take effect:

sudo systemctl restart nginx

Now that Nginx is up and running, we can install Ghost.

The first thing we need to do is download the latest version of Ghost:

curl -L [<https://ghost.org/zip/ghost-latest.zip>](<https://ghost.org/zip/ghost-latest.zip>) -o ghost.zip

Unzip the downloaded file:

unzip -uo ghost.zip -d /var/www/html/

Change the ownership of the unzipped directory to the www-data user and group:

sudo chown -R www-data:www-data /var/www/html/ghost/

Next, we need to install the Node.js dependencies for Ghost:

cd /var/www/html/ghost/
npm install --production

Once the dependencies have been installed, we can start Ghost:

npm start --production

You can now access your Ghost blog by going to http://example.com in your web browser.

If you want to stop Ghost, you can run the following command:

npm stop

If you want to start Ghost again, you can run the following command:

npm start --production

Or

Install Packages

Before we get started, we need to install some dependencies.

sudo apt-get install -y curl wget unzip

Install Node.js and npm

Ghost uses Node.js and npm, so we need to install those too.

curl -sL <https://deb.nodesource.com/setup_4.x> | sudo -E bash -
sudo apt-get install -y nodejs

Install MariaDB

MariaDB is a fork of MySQL, so it should be compatible with any MySQL database.

sudo apt-get install -y mariadb-server

Configure MariaDB

After installing MariaDB, we need to secure it and create a database for our Ghost blog.

sudo mysql_secure_installation

You will need to answer some questions. Here are some recommended answers:

Set root password? [Y/n] y
New password: [your password]
Re-enter new password: [your password]

Remove anonymous users? [Y/n] y

Disallow root login remotely? [Y/n] y

Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y

Now that MariaDB is secured, we can create a database for our Ghost blog.

sudo mariadb

You should now be logged in to the MariaDB console. Run the following commands to create a database and user for Ghost.

CREATE DATABASE ghost;

CREATE USER 'ghost'@'localhost' IDENTIFIED BY '[your password]';

GRANT ALL PRIVILEGES ON ghost.* TO 'ghost'@'localhost';

FLUSH PRIVILEGES;

EXIT;

Install Ghost

Now we can install Ghost.

curl -L <https://ghost.org/zip/ghost-latest.zip> -o ghost.zip;

unzip -uo ghost.zip -d /var/www/ghost;

rm -rf ghost.zip;

Configure Ghost

Now that Ghost is installed, we need to configure it.

cd /var/www/ghost;

npm install --production;

cp config.example.js config.js;

Open the config file in your favorite text editor.

nano config.js;

Find the following lines and change them to match your database and user.

database: {
    client: 'sqlite3',
    connection: {
        filename: path.join(__dirname, '/content/data/ghost.db')
    },
    debug: false
},

server: {
    host: '127.0.0.1',
    port: '2368'
}

Save and close the file.

Install Forever

Forever is a Node.js process manager. It will keep Ghost running even if the server is rebooted.

npm install -g forever;

Start Ghost

Now we can start Ghost.

NODE_ENV=production forever start index.js;

Install Nginx

We will use Nginx to proxy requests to our Ghost blog.

sudo apt-get install -y nginx;

Configure Nginx

Now we need to configure Nginx to proxy requests to our Ghost blog.

sudo nano /etc/nginx/sites-available/default;

Find the following lines and comment them out by adding a # at the beginning of each line.

# listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;

# root /usr/share/nginx/html;
# index index.html index.htm;

Find the following lines and change them to match the server block below.

server_name localhost;

location / {
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
    proxy_pass         <http://127.0.0.1:2368>;
}

Your server block should now look like this:

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

    server_name localhost;

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         <http://127.0.0.1:2368>;
    }
}

Save and close the file.

Now we need to restart Nginx for the changes to take effect.

sudo service nginx restart;

You should now be able to access your Ghost blog at http://your_server_ip.

In this tutorial, we have successfully created or self-hosted Ghost Blog on an Ubuntu VPS using Nginx Server.