Skip to content

Instantly share code, notes, and snippets.

@Cdaprod
Created June 13, 2024 19:32
Show Gist options
  • Save Cdaprod/321ac38cbba5df950b73962971d6e399 to your computer and use it in GitHub Desktop.
Save Cdaprod/321ac38cbba5df950b73962971d6e399 to your computer and use it in GitHub Desktop.
To deploy a Ghost blog using the command line interface (CLI), follow these steps:

To deploy a Ghost blog using the command line interface (CLI), follow these steps:

Step 1: Install Docker and Docker Compose

Make sure Docker and Docker Compose are installed on your machine. If not, follow the installation instructions from the Docker website and Docker Compose installation page.

Step 2: Set Up the Project Directory

Create a directory for your Ghost blog project:

mkdir ghost-blog
cd ghost-blog

Step 3: Create a Docker Compose File

Create a docker-compose.yml file in the project directory with the following content:

version: "3.3"

services:
  ghost:
    image: ghost:latest
    restart: always
    ports:
      - "2368:2368"
    volumes:
      - ./content:/var/lib/ghost/content
    environment:
      url: http://localhost:2368

  db:
    image: mariadb:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: your_mysql_root_password
      MYSQL_USER: ghost
      MYSQL_PASSWORD: ghostdbpass
      MYSQL_DATABASE: ghostdb
    volumes:
      - ./mysql:/var/lib/mysql

Step 4: Start the Containers

Navigate to the project directory and run Docker Compose to start the Ghost blog and database containers:

docker-compose up -d

This command will download the necessary Docker images, create the containers, and start them. Your Ghost blog will be accessible at http://localhost:2368.

Step 5: Configure the Ghost Blog

Open a web browser and go to http://localhost:2368/ghost. You will be prompted to set up your Ghost admin account and configure your blog.

Step 6: Adding SSL with Let's Encrypt and Nginx (Optional)

To secure your blog with SSL, you can use Let's Encrypt and Nginx. Here's how to set it up:

  1. Install Certbot:

    sudo apt-get update
    sudo apt-get install certbot python3-certbot-nginx
  2. Obtain an SSL Certificate:

    sudo certbot certonly --standalone -d yourdomain.com
  3. Create Nginx Configuration: Create a new Nginx configuration file for your Ghost blog:

    sudo nano /etc/nginx/sites-available/ghost

    Add the following content to the file, replacing yourdomain.com with your actual domain:

    server {
        listen 80;
        server_name yourdomain.com;
        location / {
            proxy_pass http://localhost:2368;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
        location /.well-known/acme-challenge/ {
            root /var/www/html;
        }
    }
    
    server {
        listen 443 ssl;
        server_name yourdomain.com;
    
        ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
        include /etc/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    
        location / {
            proxy_pass http://localhost:2368;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
  4. Enable the Nginx Configuration:

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

Step 7: Automate Deployment with GitHub Actions (Optional)

To automate the deployment, you can use GitHub Actions by creating a workflow file at .github/workflows/deploy.yml:

name: Deploy Ghost Blog

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: your-dockerhub-username/ghost-blog:latest

      - name: Deploy to Server
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
        run: |
          ssh -o StrictHostKeyChecking=no user@your-server-ip << 'EOF'
            cd /path/to/your/project
            git pull origin main
            docker-compose down
            docker-compose up -d
          EOF

Replace the placeholders with your actual values and ensure your Docker Hub credentials and SSH private key are stored as GitHub secrets.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment