Setting up Ghost CMS on EC2 Ubuntu using Docker with Nginx, Custom Domain, and SSL Certificate

Setting up Ghost CMS on EC2 Ubuntu using Docker with Nginx, Custom Domain, and SSL Certificate

In this comprehensive guide, we will walk you through the process of setting up Ghost CMS on an Amazon EC2 instance running Ubuntu using Docker. We will also cover the setup of Nginx as a reverse proxy, configuring a custom domain, and securing your site with an SSL certificate.

Prerequisites

Before we begin, make sure you have the following:

    1. An Amazon Web Services (AWS) account.
    2. An EC2 instance running Ubuntu Server 22.04
    3. Make sure that http & https traffic is allowed
    4. A registered domain name (e.g., example.com) and access to its DNS settings.

Step 1: Connect to Your EC2 Instance

Use SSH to connect to your EC2 instance. Replace your-instance-ip with your instance's public IP address and your-key.pem with your private key file.

ssh -i your-key.pem ubuntu@your-instance-ip

Step 2: Update and Upgrade

Once connected, update and upgrade your system packages:

sudo apt update
sudo apt upgrade

Step 3: Install Docker

Install Docker on your EC2 instance by running the following commands:

sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker

Step 4: Install Docker Compose

Docker Compose simplifies multi-container Docker applications. Install it with:

sudo apt install docker-compose

Step 5: Create a Docker Compose File for Ghost

Create a directory for your Ghost CMS and navigate to it:

mkdir ~/ghost
cd ~/ghost

Inside this directory, create a docker-compose.yml file and add the following content:

touch docker-compose.yml
vi docker-compose.yml
version: '3'

services:
  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 8lkvqzt2LaoR
    volumes:
      - dbdata:/var/lib/mysql

  ghost:
    image: ghost:latest
    restart: always
    ports:
      - 2368:2368
    environment:
      url: http://localhost:2368
      database__client: mysql
      database__connection__host: db
      database__connection__user: root
      database__connection__password: 8lkvqzt2LaoR
      database__connection__database: ghost

      NODE_ENV: production
    volumes:
      - /opt/ghost_content:/var/lib/ghost/content
    depends_on:
      - db

volumes:
  dbdata:

Save and exit the text editor.

Step 6: Start Ghost CMS

Start Ghost CMS using Docker Compose:

sudo docker-compose up -d

This command will download the Ghost image and launch the container.

Step 7: Create Nginx Docker Image

Install Nginx to act as a reverse proxy for Ghost CMS:

mkdir nginx
cd nginx
touch default.conf
touch Dockerfile

Add the following configuration to default.conf file, replacing example.com with your domain name:

server {
  listen 80;
  listen [::]:80;
  server_name localhost;

  location / {
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_pass http://ghost:2368;
  }
}

Save and exit the text editor.
Now, add to Dockerfile:

FROM nginx:latest

COPY default.conf /etc/nginx/conf.d

Step 8: Updating the Docker Compose Config

version: '3'

services:
  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 8lkvqzt2LaoR
    volumes:
      - dbdata:/var/lib/mysql

  ghost:
    image: ghost:latest
    restart: always
    ports:
      - 2368:2368
    environment:
      url: http://localhost:2368
      database__client: mysql
      database__connection__host: db
      database__connection__user: root
      database__connection__password: 8lkvqzt2LaoR
      database__connection__database: ghost

      NODE_ENV: production
    volumes:
      - /opt/ghost_content:/var/lib/ghost/content
    depends_on:
      - db
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    restart: always
    depends_on:
      - ghost
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /usr/share/nginx/html:/usr/share/nginx/html

volumes:
  dbdata:
sudo docker-compose up -d

It will start the ghost cms.

Step 8: Configure DNS Records

Go to your domain registrar's website and configure DNS records for your domain:

  • Create an A record pointing to your EC2 instance's public IP address.
  • Create a CNAME record for the www subdomain (if desired) pointing to your domain (e.g., example.com).

DNS changes may take some time to propagate, so be patient.

To secure your site with SSL, you can use Let's Encrypt. Install Certbot and obtain a certificate:

sudo apt install certbot python3-certbot-nginx
sudo certbot certonly --standalone -d example.com -d www.example.com

Follow the prompts to set up the SSL certificate.

Step 10: Access Ghost CMS

Visit your domain (e.g., https://example.com) in your web browser, and you should see your Ghost CMS site up and running. You can now log in, create content, and customize your website.

Congratulations! You've successfully set up Ghost CMS on an EC2 instance with Docker, Nginx, a custom domain, and an SSL certificate. You can now start building your blog or website using Ghost.