Free Ghost Host Using Heroku and Any Flavor of Linux

This step by step guide will teach you how to install the popular blogging platform ghost and host it for free using Heroku and any flavor of Linux. This guide was written for the casual computer user in mind, and developers should find these step by step procedures a piece of cake.

First, and foremost, I am no developer, and thus this guide was crafted by compiling sources, and experimenting all day. The sources used can be acquired at the end of the guide, and is to be found under ‘ Recommended Reading ‘.

Prerequisites

Linux (Any flavor will do.) A live DVDV/USB was not tested with this guide.

Basic Linux skills. This guide uses 80% of command line work, so the user should be comfortable using the terminal.

(Optional) Custom Domain Name: Heroku allows the user to map their own custom domain or subdomain at no cost, however, the user must have a verified account, which requires the input of a credit card. A verified account is not a must to follow the steps in this guide.

1. Create an Account on heroku.com

Navigate to heroku.com to sign up for a free account. You will need the login credentials to complete this tutorial. An email is needed for verification purposes.

2. Install the Heroku Toolbelt

The easiest way to install the Heroku toolbelt is via the terminal. From this point forth, and every time a new terminal window is open, we will execute all commands using administrative rights. This command is executed once and remains active until you close the terminal.

Launch the terminal application and type:

sudo su

* The password will be the same one you use when you log into your computer.

Feel free to copy and paste the following commands into the terminal to install the Heroku Toolbelt.

sudo apt-get install software-properties-common # debian only
sudo add-apt-repository "deb https://cli-assets.heroku.com/branches/stable/apt ./"
curl -L https://cli-assets.heroku.com/apt/release.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install heroku

3. Create a Heroku App

Type the following command to login Heroku:

heroku login

You will be asked for the email and password that was created on step 1.Next, we will create our first application. Note: The name must be unique and currently not in use. For example, blog is one of the first words to go. Type the following command to create your first application:heroku create testdevReplace ‘ testdev ‘ with the name of your actual app. Please note, the name of your app will be what defines your default URL. You will receive a message saying your application was created and you will be given the URLS where your app can be accessed from. It will look something like this:

https://testdev.herokuapp.com/ | https://git.heroku.com/testdev.git

The URL marked is the URL you will need for later steps.

4. Install Postgresql

Postgresql is the type of database our Heroku application functions with, so we need to create it. Type the following command once to install the addon, and a second time to create a database.

If at any point it asks you to specify the app, append the following to the end of the command: –app testdev (Remember to replace testdev with the name of your actual application.)

heroku addons:create heroku-postgresql:hobby-dev

You will receive a specific string that will contain a COLOR_URL. It will look like this:

Created postgresql-cubic-18331 as HEROKU_POSTGRESQL_AQUA_URL

Heroku requires that the user promotes the database so type the following command to complete that step:

heroku pg:promote HEROKU_POSTGRESQL_AQUA_URL

It will create an additional database, but we are only interested in our primary database, which in my case is the AQUA Database.

5. Extract Remote Connection Information from the Database

We need a very specific snippet of information found inside our heroku account. Navigate to your dashboard, click on your newly created app, resources, followed by ‘ Heroku Postgres :: Aqua ‘ (Look for your specific color). Click on the link, and you will be redirected to a new window. Scroll down to ‘ Database Credentials ‘ and click on view credentials. Open a notepad and copy down the following and fill in the appropiate information after the ‘ = ‘.

heroku config:set POSTGRES_DATABASE=Database Info goes here.
heroku config:set POSTGRES_HOST=Postgres Host info. goes here.
heroku config:set POSTGRES_PASSWORD=Postgres_user password info. goes here.
heroku config:set POSTGRES_USER=Postgres_user name goes here.
heroku config:set MY_URL=https://testdev.herokuapp.com (If you are mapping a custom domain, input that url instead of your herokuapp url.)
heroku config:set NODE_ENV=production

Keep this information handy because we will need it in later steps.

6. Install Nodejs

Nodejs is the core framework of what makes up the ghost blogging platform. First, we must install nodejs to avoid errors later on. Type the following commands:

apt install nodejs
apt install nodejs-legacy

7. Install Npm

Next, we must install Npm, so type the following command:

sudo apt install npm

8. Verify Nodejs and Npm

We must verify that the installation of nodejs and npm went without a hitch. Type the following commands:

node -v
npm -v

You may have to hit ‘ Enter ‘ to get the second command executed. In both instances, you should be greeted by the version number.

9. Install Ghost Locally

It’s time to install ghost as localhost. First, and foremost, the developers of ghost recommend installing ghost in /var/www/ghost, however, that install location gave me severe problems, so you can change the name of your directory to whatever you want, but do not change the location to /var for your own sanity.

Download the latest version of ghost

Copy the following command to download the latest version of ghost from the developers’ site:

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

Unpack, Create Install Directory

Type the following command to unpack ghost to our directory. I have chosen to create a directory called ‘ testdev ‘. Replace ‘ testdev ‘ for the name of the directory  you want to create, and that’s where the compressed folder will be unpacked to.

unzip -uo ghost.zip -d testdev

Install Production Dependencies

Type the following command to install the dependencies needed for a production environment. Remember to swap out ‘ ghost ‘ for the name of your directory.

cd testdev && npm install --production

You shouldn’t receive any errors.

Install, Test Live Environment

npm start --production

We need to verify that ghost was successfully activated. Navigate to http://127.0.0.1:2368/ on your browser. You should be greeted with the typical default ghost theme. Congratulations. Now it’s time to shut down the local install. We were just making sure everything was installed correctly. To shut down ghost, return to the terminal window and execute CTRL+C. (Make sure both buttons are pressed at the same time.) You will receive a message that your blog has gone offline.

Create a .Gitignore File

Execute the following command:

sudo echo node_modules > .gitignore

Verify node_modules isn’t listed anywhere by executing:

git status

Initialize

We are going to create an empty git repository by inputting the following commands:

git init
git add --all  
git commit -m "Initial Commit"

10. (Optional) Map a Custom Domain

If you are going to map a custom domain, now would be the time to do it. If you are not interested in this step, skip to step 11.

To map an existing domain, we need to go back to the dashboard, and click on our existing application, followed by settings, and scroll down to where it says ‘ Domains and certificates ‘. Click on ‘ Add Domain ‘. Type the domain or subdomain of your choosing and click ‘ Okay ‘. You need to copy the string under DNS.

I will be mapping a subdomain via Cpanel. The steps would be for ‘ Advanced Zone Editor ‘ found on Cpanel. Every domain registrar has their own given instructions.

Type: CNAME
Name: example.yourdomain.com
TTL: 1400
CNAME: The string you copied under DNS.

Let’s navigate to our custom address to test that it is mapped correctly. In my case: publications.catloverr.com. shows perfectly on the address bar. Next we are greeted by a screen with the following message:

Heroku | Welcome to your new app!

The domain/subdomain is working. Time to move to step 11.

11. Configure the Database

Now, we need the information we extracted from step 5. Copy and paste it in the terminal. Remember to keep in mind: If you have a custom domain: write your custom address in MY URL. If you don’t, write your herokuapp URL. The information you are going to paste in the terminal should look something like this:

heroku config:set POSTGRES_DATABASE=d54ij5aom96bvb
heroku config:set POSTGRES_HOST=ec2-54-225-107-107.compute-1.amazonaws.com
heroku config:set POSTGRES_PASSWORD=ba133efb9f6148ec206d56012e629f24deec88bc61f54cac94712969668f5cd7
heroku config:set POSTGRES_USER=eccmhkfqxdafvo 
heroku config:set MY_URL=http://publications.catloverr.com/ 
heroku config:set NODE_ENV=production

If for some reason you receive an error, append –app –appname to each command. Example:

heroku config:set POSTGRES_DATABASE=d54ij5aom96bvb --app testdev

12. Create a Procfile

To create a procfile for our ghost installation, type the following command:

echo "web: NODE_ENV=production node index.js" > Procfile

13. Configure Ghost

We are almost done. It’s time to point our ghost installation to the correct place. We will also be disabling the upload mode. You will have to link images from an external site. Any themes must be inside the ghost installation folder and not uploaded via the ‘upload’ function or they will be wiped by Heroku once your application resets. Open a new terminal window without closing the first one, and type:

gksu nautilus

This will allow us to access the file system with administrative rights via visual mode. Navigate to your ghost install location, and open config.js. Under production, change the URL to the same one we set up on our database, and under type: fileStorage: false,

 production: {
 url: 'http://publications.catloverr.com',
 fileStorage: false,

Next, replace the ‘database section’ with this one:

database: {  
  client: 'postgres',
  connection: {
    host: process.env.POSTGRES_HOST,
    user: process.env.POSTGRES_USER,
    password: process.env.POSTGRES_PASSWORD,
    database: process.env.POSTGRES_DATABASE,
    port: '5432'
  },
  debug: false
},

Finally, replace the ‘ server ‘ bit with this one:

server: {  
  host: '0.0.0.0',
  port: process.env.PORT
}

Save and close config.js and the second terminal window we just opened. It’s time to return to our original window.

13. Deploy

It’s time to deploy our heroku app. Keep in mind, every time you make changes, you must execute the following command so that the changes stick (themes, etc.):

git add --all  
git commit -m "Configured Ghost for Heroku + Postgres"  
git push heroku master

If for some reason you get an error, execute the following commands and type y when prompted:

heroku keys:clear
heroku keys:add

Next, try to execute the command to deploy once more. The application should be deployed successfully, and to check that you are live, type your custom domain or herokuapp url to check.


Recommended Reading Sources

Source 1 – How to install Heroku Toolbelt

Source 2 – Installing Ghost Locally

Source 3 – The original guide I followed