How to Install Ghost on Amazon EC2

Here's how to get a self-hosted instance of the Ghost blogging platform up and running on an Amazon EC2 instance. Part 1 covers launching an EC2 instance, if you already have an instance launched, skip to Part 2.

Part 1: Launch a new EC2 instance.
  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. From the console dashboard, click Launch Instance.
  3. Choose an AMI. This guide assumes Amazon Linux.
    • The latest as of this writing was Amazon Linux AMI 2015.09.1 (HVM), SSD Volume Type - ami-60b6c60a.
  4. Choose an Instance Type:
    • Select t2.micro. If you need more horsepower, you don't need this guide.
  5. Click Review and Launch.
  6. On the Review Instance Launch page, under Security Groups, click Edit security groups.
    • Restrict SSH to only you:
      By default, SSH is enabled from anywhere. That's not good. Under, Source, change Anywhere to My IP.
    • Allow HTTP:
      Click Add Rule and under Type select HTTP .
  7. Click Review and Launch.
  8. On the Review Instance Launch page, click Launch.
  9. Select or create a key pair, check the acknowledgment check box, and then click Launch Instance.

Part 2: Install Ghost
  1. Log into your instance:

    ssh -i /path/to/your/keypair [email protected]
    
  2. Update the pre-installed packages:

    [ec2-user ~]$ sudo yum update
    
  3. Install dependencies:

    [ec2-user ~]$ sudo yum install gcc gcc-c++
    
  4. Configure the account used to run the Ghost instance:

    [ec2-user ~]$ sudo useradd ghost  
    [ec2-user ~]$ sudo passwd ghost
    
  5. Create the directory for the Ghost install:

    [ec2-user ~]$ sudo mkdir -p /var/www/ghost
    
  6. Grant the ghost user rights to the directory:

    [ec2-user ~]$ sudo chown -R ghost:ghost /var/www/ghost/
    
  7. Switch to the ghost user:

    [ec2-user ~]$ su - ghost
    
  8. Install nvm:

    [ghost ~]$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash  
    [ghost ~]$ source .bash_profile
    
  9. Download and extract the latest Ghost release:

    [ghost ~]$ wget https://ghost.org/zip/ghost-0.7.1.zip  
    [ghost ~]$ unzip ghost-0.7.1.zip -d /var/www/ghost
    
  10. Change to the Ghost directory:

    [ghost ~]$ cd /var/www/ghost/
    
  11. Install node. Ghost wants the 0.10.x series:

    [ghost ~]$ nvm install 0.10
    [ghost ~]$ nvm use 0.10
    [ghost ~]$ nvm alias default 0.10
    
  12. Install Ghost:

    [ghost ~]$ npm install --production
    
  13. Install pm2 to ensure Ghost survives crashes and reboots:

    [ghost ~]$ npm install -g pm2
    [ghost ~]$ echo "export NODE_ENV=production" >> ~/.profile
    [ghost ~]$ source ~/.profile
    [ghost ~]$ pm2 kill
    [ghost ~]$ pm2 start index.js --name ghost
    [ghost ~]$ pm2 dump
    [ghost ~]$ exit
    

    This last command must be run as ec2-user:

    [ec2-user ~]$ sudo su -c "env PATH=$PATH:/home/ghost/.nvm/v0.10.40/bin pm2 startup amazon -u ghost --hp /home/ghost"
    
  14. Install nginx to proxy port 80:

    [ec2-user ~]$ sudo yum install nginx -y
    [ec2-user ~]$ sudo service nginx start
    [ec2-user ~]$ sudo chkconfig nginx on
    

    Create /etc/nginx/conf.d/ghost.conf and add this:

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

    Restart nginx:

    [ec2-user ~]$ sudo service nginx restart
    
Part 3: Configure Ghost

You should now have a working Ghost install accessible via your EC2 instance's public ip. You can login in and configure Ghost by going to http://your-ec2-public-ip/ghost.

You'll still need to configure your outgoing mail, but don't worry Ghost Support has you covered. Just make sure and do a pm2 restart ghost as the ghost user after you update your config.js.

Part 4: Credits

This post builds on the work of others. See:

Thanks to all and Happy blogging!