How To Install Ghost On Ubuntu 14.04 Using NGINX


Prerequisites

After you ssh into the server, The first thing we do is install wget by running the following command

sudo apt-get update
sudo aptitude install zip wget

Next we need to install Nodejs by running the following command

sudo apt-get update
sudo apt-get install nodejs-legacy

Also we now need to install the npm package manager which Node uses by default and since ghost uses npm we need it. We do this by running the following command

sudo apt-get install npm

You can now check the version of node by running

node -v

and it should return something like

v0.10.25

also check if npm is installed as follows

npm -v

and it should show something like this

1.3.0

Also you must update npm by using the following command

sudo npm install [email protected] -g

and now when you check version it should be 2.5.0

$ npm -v
2.5.0

Step 1 : Installing Ghost

Assuming you have already setup your project in a git repository.

You can now go into you target place and do a git pull but before that we need to add the server ssh key into our github account

Although this is a new server and we know that there are no ssh keys in place, if you are using an existing server you can use an existing ssh key in your github repository by doing the following

Checking for existing ssh keys

  • Open Terminal
  • Enter ls -al ~/.ssh, this will show you a list of all the ssh keys that are present
  • The ssh keys will named as follows
    • id_dsa.pub
    • id_rsa.pub
    • id_ecdsa.pub
  • If you find such such a key present then you can use this key else you can generate a new key as follows

Generating a new ssh key

  • Please make sure that you are running these commands as root user.. it is NOT recommended to use sudo with git.

  • open terminal
  • paste the text below and substitute your email address.

      ssh-keygen -t rsa -b 4096 -C "[email protected]"
      # Creates a new ssh key, using the provided email as a label
      Generating public/private rsa key pair.
    
  • when prompted to enter a file location just press enter and it will select the default file location

      Enter a file in which to save the key (/Users/you/.ssh/id_rsa):
      [Press enter]
    
  • Next you either type a passphrase or leave it blank, if you want to learn more about passphrases you can see it here working with ssh key passphrases.

  • Also, now you can finally add your ssh key to the ssh agent

Adding ssh key to ssh agent

  • First we make sure that ssh-agent is enabled by running the following command

      # start the ssh-agent in the background
      eval "$(ssh-agent -s)"
      Agent pid 29645
    
  • Next we add our ssh key to the ssh agent by running the following, also if you are using an existing key just replace the id_rsa with your key name

      $ ssh-add ~/.ssh/id_rsa
    
  • Now for the last part you need to add the key to your github account

Adding your ssh key to the github account

  • First go to you github account > click on profile > settings > ssh / gpg keys > add new ssh key

  • Now copy your key to clip board, many times xclip does not work as shown in most tutorials including the one on github’s official page. In that case just use the following command to display your ssh key ( replacing id_rsa.pub with your key name if you’re using existing keys)

      cat ~/.ssh/id_rsa.pub
    
  • Now paste your key in your github account, enter your password to confirm and you’re done.

Pulling the ghost blog from your repository

  • To use git we first need to install git and we do this in the following way

      sudo apt-get install git
    
  • Now we need to make a directory to keep are web application in which is generally the /var/www directory, we will make it by running the following command

      sudo mkdir -p /var/www/
    
  • To clone the directory we just made we need to copy the clone link on thr repository, the link will be present on the top right corner of the repository

  • Now we need to enter the directory we just made and clone are ghost blog here, this is the recommended directory to keep your web applications, run the following command

      sudo git clone < paste your repository clone link here >
    
  • This will create a clone of your repository in this location. To test if your blog is successfully installed run the following.

      sudo npm install --production
      sudo npm install sqlite3 --save
      sudo npm start
    
  • This will give you the following message

    WARNING: Ghost is attempting to use a direct method to send email.
    It is recommended that you explicitly configure an email service.
    Help and documentation can be found at http://support.ghost.org/mail.
    
    Migrations: Up-to-date at version 005
    Ghost is running in development...
    Listening on 127.0.0.1:2368
    Url configured as: http://localhost:2368
    Ctrl+C to shut down
    
  • This means that your ghost installation is successfully working on the server now you just need nginx to reverse proxy it.

Step 2: Install Nginx

This will allow us to listen on port 80 which is the default port of the internet. You can also install a SSL/TLS cerificate for free using lets encrypt

We will now install nginx using the following command

sudo apt-get install nginx

Next, we will have to configure Nginx by changing our directory to /etc/nginx and removing the default file in /etc/nginx/sites-enabled:

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

We will now create a new file in /etc/nginx/sites-available/ called ghostblog and open it with nano to edit it:

sudo touch /etc/nginx/sites-available/ghostblog
sudo nano /etc/nginx/sites-available/ghostblog

Now we will do the simplest nginx configuration possible, paste the following code in your file and replace the your_domain.tld with your domain name

server {
listen 80;
server_name your_domain.tld;
location / {
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
    proxy_pass         http://127.0.0.1:2368;
   }
}

We will now symlink our configuration in sites-enabled:

sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost

Now restarting ghost we will be able to our website

cd /var/www/<your blog name>
npm start --production

Keeping Ghost running with forever

We will now use a node module called forever to make sure that ghost is always running in the background. This also monitors that ghost always stays up. If it crashes forever starts another instance automatically

Install forever using the following command in your ghost directory , please make sure that you are NOT a root user at this point

sudo npm install -g forever

now start ghost using the following command, from the installation directory

forever start index.js

Note that to start ghost in production mode you need to set environment as production as demonstrated below. Since this is a production server the blog must always be run in production mode.

NODE_ENV=production forever start index.js

To stop the forever use the following command

forever stop index.js

Conclusion

Congratulations, you now have ghost successfully running on your own server.