Semi automated multiple ghost deployment in a single server via Shell & Docker

Hi,
I have created a simple script which will deploy multiple ghost instances in a single server almost automatically.

Prerequisite

  • Install docker
  • Install docker-compose
     sudo apt install docker-compose
    
  • Install nginx
    sudo apt install nginx
    

Now lets check create the script

Create a file script.sh & paste the below code to the file

#!/bin/bash
read -p 'Enter you domain name: ' domain
read -p 'Enter Port: ' port
block="/etc/nginx/sites-available/$domain"
docker="/home/$USER/$domain/docker-compose.yaml"

# Stack Directory
mkdir -p $domain && mkdir -p $domain/content

# create the docker compose file
touch $domain/docker-compose.yaml

# Create the Nginx server block file:
sudo tee $block > /dev/null <<EOF

server {
  listen 80;
  listen [::]:80;
  server_name $domain www.$domain;
  location / { return 301 https://$host$request_uri; }
}
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name $domain www.$domain;

  ssl_protocols TLSv1.2;
  ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;

  ssl_certificate     /etc/ssl/domain.com/cert.pem; #define your ssl cert path
  ssl_certificate_key /etc/ssl/domain.com/key.pem; #define your private key path

location / {
                proxy_pass http://0.0.0.0:$port;
        }

}



EOF

# docker-compose file configuration

tee $docker > /dev/null <<EOF

version: '3'

services:

  ghost:
    container_name: ${domain}
    image: ghost:latest
    restart: always
    ports:
      - ${port}:2368
    volumes:
      - ./content:/var/lib/ghost/content
    environment:
      url: https://${domain}
      database__client: mysql
      database__connection__host: db
      database__connection__user: root
      database__connection__password: ${password} #define a password here
      database__connection__database: ${domain}

  db:
    container_name: ${domain}_db
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${password} #define a root password for your container

EOF

# Link to make it available
sudo ln -s $block /etc/nginx/sites-enabled/

# Test configuration and reload if successful
sudo nginx -t && sudo service nginx reload

cd /home/$USER/$domain

#Deploy ghost
docker-compose up -d

Save the file & run below command

chmod +x script.sh

Now you should be able to deploy your ghost instance by running the script below

./script.sh

Now the script will ask you the domain name & Port number
Once the domain name & port number submitted the script take care of your ghost deployment from creating nginx server block to Ghost installation.

Hope it will help them who requires multiple ghost installations quickly in a single server.

2 Likes

You’ll probably want a persistent volume mounted, or to use a custom storage adapter so that you don’t blow away all your images if you need to rebuild the container

1 Like

Hey @egg Thanks for the suggestion. I will follow the suggestion & update the code.

Cheers!

1 Like

The Above script is updated now with volumes for contents

@inoryum thanks for the post and shell script. Just what I was looking for. Has anyone tried this on the default digital ocean pre-installed ghost droplet from the “marketplace”?

Thanks

You are welcome! I never tried it on DO reinstalled ghost … but you can setup this on such droplets as well

Script Update for Mysql 8

#!/bin/bash
read -p 'Enter you domain name: ' domain
read -p 'Enter Port: ' port
block="/etc/nginx/sites-available/$domain"
docker="/home/$USER/$domain/docker-compose.yaml"

# Stack Directory
mkdir -p $domain && mkdir -p $domain/content

# create the docker compose file
touch $domain/docker-compose.yaml

# Create the Nginx server block file:
sudo tee $block > /dev/null <<EOF

server {
  listen 80;
  listen [::]:80;
  server_name $domain www.$domain;
  location / { return 301 https://$host$request_uri; }
}
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name $domain www.$domain;

  ssl_protocols TLSv1.2;
  ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;

  ssl_certificate     /etc/ssl/domain.com/cert.pem; #define your ssl cert path
  ssl_certificate_key /etc/ssl/domain.com/key.pem; #define your private key path

location / {
                 proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://0.0.0.0:$port;
                proxy_set_header Host $http_host;
                proxy_set_header X-Forwarded-Proto $scheme;

        }

}



EOF

# docker-compose file configuration

tee $docker > /dev/null <<EOF

version: '3'

services:

  ghost:
    container_name: ${domain}
    image: ghost:latest
    restart: always
    ports:
      - ${port}:2368
    volumes:
      - ./content:/var/lib/ghost/content
    environment:
      url: https://${domain}
      database__client: mysql
      database__connection__host: db
      database__connection__user: root
      database__connection__password: ${password} #define a password here
      database__connection__database: ${domain}

  db:
    container_name: ${domain}_db
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${password} #define a root password for your container
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./data:/var/lib/mysql


EOF

# Link to make it available
sudo ln -s $block /etc/nginx/sites-enabled/

# Test configuration and reload if successful
sudo nginx -t && sudo service nginx reload

cd /home/$USER/$domain

#Deploy ghost
docker-compose up -d
2 Likes