56tvmao: How-to instructions you can trust. Linux How to Self-Host and Install Ghost on Your Linux Server

How to Self-Host and Install Ghost on Your Linux Server

How to Self-Host and Install Ghost on Your Linux Server post thumbnail image
How to Self-Host and Install Ghost on Your Linux Server

Ghost is a powerful open-source content management system that enables you to effortlessly create blogs and newsletters. You have the option to either subscribe to the managed Ghost(Pro) service for a hassle-free experience or, for those with technical expertise, self-host Ghost on your own server. In this guide, we’ll walk you through the process of self-hosting Ghost on a Linux server.

Content

  • Why You Should Self-host Ghost on Your Server
  • Installing Ghost on your Server
  • Running Ghost on a Local Machine
  • Using Ghost The First Time
  • Frequently Asked Questions

Why You Should Self-host Ghost on Your Server

One of the advantages of a Ghost blog is that it provides an integrated, feature-filled platform that works right out of the box. This includes the ability to track analytics, monetize posts, and create automatic newsletters for your subscribers.

Why You Should Self-host Ghost on Your Server

Installing Ghost on your server allows you to be a publisher without relying on a third party to manage your data. This can be helpful if you are a journalist who is putting out critical articles about controversial topics.

Lastly, Ghost is easy to deploy and maintain, even for non-technical users. Unlike traditional CMS platforms, Ghost provides an all-in-one solution that you can “set and forget” on your hosting machine.

Good to know: Learn how to host a web archive on Linux using Archivebox.

Installing Ghost on your Server

Assumption: Before we start, we assume that you already have a Linux server available and a domain name to host your Ghost installation. If you need to rent a Linux server, we recommend DigitalOcean or Linode.

Since we will be using Docker, we will need to install Docker on the server first.

1. Install the Docker project’s repository public key:

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

2. Create a new repository file in your machine’s apt config directory:

sudo nano /etc/apt/sources.list.d/docker.list

3. Paste the following line of code inside your new repository file:

deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu jammy stable

4. Refresh your system’s repository listings and upgrade the existing binaries in your machine:

sudo apt update
sudo apt upgrade

5. Install Docker and Docker Compose along with their dependencies:

sudo apt install wget docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-buildx-plugin

6. Add your user to the Docker group

sudo usermod -aG docker $USER

7. Set Docker service to run on startup:

sudo systemctl enable docker.service

8. Reboot the server.

sudo reboot

Setting Up Gmail for SMTP Delivery

Ghost doesn’t come with its own email delivery client, so we need to hook it up to Gmail for email delivery.

1. Open a web browser and login to the Gmail account that you want to link to your Ghost instance.

2. Click the Profile Icon of your account, then click “Manage your Google Account.”

3. Select the “Security” category, then click the “2-Step Verification” option under the “How you sign in to Google” subcategory.

Setting Up Gmail for SMTP Delivery

4. Verify your phone number and enable 2-step Verification.

5. Go back to the “Security” category, then click the “2-Step Verification” option again. This will open a new menu screen where you can set new secondary keys for your account. Scroll to the bottom of the page, then click “App passwords.”

6. Select “Mail” for the first dropdown list, then select “Other” on the second.

7. Write “Gmail for Ghost” on the textbox, then click “Generate.” This will open a window with a yellow textbox containing your custom application password.

FYI: You can also use other external delivery agents to send mail through Ghost. Learn how to create your own email server using Mail-in-a-Box.

Building the Docker Container

1. Create a new directory for your Ghost installation’s dockerfiles:

mkdir ~/ghost
cd ~/ghost

2. Create a new docker-compose.yml file using your favorite text editor:

nano ./docker-compose.yml

3. Paste the following block of code inside your new docker-compose.yml file and save it.

---
version: "3"
services:
  ghost:
    image: ghost:latest
    restart: always
    ports:
      - 8080:2368
    environment:
      database__client: mysql
      database__connection__host: db
      database__connection__database: ghost
      database__connection__user: ghost
      database__connection__password: ghost
      url: ${GHOST_URL}
      mail__transport: ${MAIL_TRANSPORT}
      mail__options__host: ${MAIL_HOST}
      mail__options__port: ${MAIL_PORT}
      mail__options__secureConnection: ${MAIL_SECURE_CONNECTION}
      mail__options__auth__user: ${MAIL_USER}
      mail__options__auth__pass: ${MAIL_PASSWORD}
    volumes:
      - ghost-data:/var/lib/ghost/content
    links:
      - db
  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ghost
      MYSQL_USER: ghost
      MYSQL_PASSWORD: ghost
    cap_add:
      - SYS_NICE
    volumes:
      - ghost-db:/var/lib/mysql
volumes:
  ghost-data: null
  ghost-db: null

4. Create a “.env” file inside your Ghost docker directory:

nano ./.env

5. Paste the following block of code inside your new .env file:

# URL
GHOST_URL="#REPLACE_ME_WITH_DOMAIN_NAME#"
# Database
MYSQL_ROOT_PASSWORD="#REPLACE_ME_WITH_SECURE_PASSWORD#"
# SMTP variables
MAIL_SERVICE="Gmail"
MAIL_TRANSPORT="SMTP"
MAIL_USER="#REPLACE_ME_WITH_YOUR_GMAIL_ADDRESS#"
MAIL_PASSWORD="#REPLACE_ME_WITH_YOUR_APP_PASSWORD#"
MAIL_HOST="smtp.gmail.com"
MAIL_PORT="465"
MAIL_SECURE_CONNECTION="true"
  • Change the value for the “GHOST_URL” variable from “#REPLACE_ME_WITH_DOMAIN_NAME#” to your complete domain name.

  • Replace the value of the “MYSQL_ROOT_PASSWORD” with a sufficiently random password.
  • Change the value of both the “MAIL_USER” and “MAIL_PASSWORD” with the email address of your Gmail account and its Application Password.

Note: You need to write the Gmail application password with no spaces in between each segment.

6. Build the Ghost Docker container by running the following command:

sudo docker compose up -d

Setting up a Reverse Proxy

With Ghost running in the backend, we need to set up a reverse proxy to be able to access Ghost publicly. In this case, we will use Nginx’s reverse proxy.

1. Install the Nginx web server package. This will serve as your reverse proxy daemon which will take connections from the internet and redirect it to Ghost.

sudo apt install nginx

2. Create a new Nginx site config file using your favorite text editor:

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

3. Paste the following block of code inside your new site config file:

server {
   listen 80;
   listen [::]:80;
 
   root                    /var/www/html;
   server_name             ghost.myvpsserver.top;
 
   location / {
       proxy_set_header    X-Forwarded-For $remote_addr;
       proxy_set_header    Host $http_host;
       proxy_set_header    X-Forwarded-Proto $scheme;
       proxy_pass          http://127.0.0.1:8080;
   }
}

4. Create a symbolic link for your Ghost site config:

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

5. Enable the Nginx web server service using systemctl:

sudo systemctl enable --now nginx.service

Enabling SSL on Your Nginx Reverse Proxy

1. Ensure that the “core” snap package is present in your machine:

sudo snap install core

2. Install the certbot snap package:

sudo snap install certbot --classic

3. Register your certbot installation to your email address by running the following command:

sudo certbot register --agree-tos --email ramces@email.invalid

4. Request a new SSL certificate for your Ghost blog:

sudo certbot --nginx -d ghost.myvpsserver.top

5. Test whether your new Ghost instance is accessible through SSL by opening a web browser and loading your domain name.

Running Ghost on a Local Machine

If you don’t have a server and want to install Ghost on your local machine, you can too. In addition, you can make use of Tailscale to access it everywhere on your browser.

1. Install the Tailscale VPN daemon in your machine and link it to your Tailscale account. Go to your Tailscale Administrator Console and click the “DNS” tab on the console’s top bar.

Install the Tailscale VPN daemon in your machine

2. Click the “Rename tailnet…” button under the “Tailnet name” subcategory.

Click the “Rename tailnet…” button under the “Tailnet name” subcategory.

3. Set your machine’s hostname to “ghost” followed by the new subdomain for your Tailscale network:

sudo hostnamectl set-hostname ghost.your-tailnet.ts.net

Enabling Tailscale Funnel

1. Open the Tailscale Administrator Console and click the “DNS” tab.

2. Scroll down to the bottom of the page then click “Enable HTTPS…”

Enabling Tailscale Funnel

3. Scroll back up to the top of the page, then click the “Access control” tab.

Scroll back up to the top of the page, then click the “Access control” tab.

4. Click the “Add Funnel to policy” button.

Click the “Add Funnel to policy” button.

5. Run the following command to create a reverse proxy between Tailscale and your local Docker container:

sudo tailscale serve https / http://127.0.0.1:8080

6. Enable the Tailscale funnel for your reverse proxy by running the following command:

sudo tailscale funnel 443 on

7. Test whether your new Ghost instance is accessible through your Tailscale funnel by opening a web browser and loading your Tailscale address.

Test whether your new Ghost instance is accessible through your Tailscale funnel by opening a web browser and loading your Tailscale address.

Using Ghost The First Time

1. Open a web browser and navigate to your Ghost installation URL followed by the subdirectory “/ghost.”

2. Click the first field and provide a name for your new Ghost blog. Fill in the rest of the fields with details about your admin user, then click “Create account & start publishing.”

Click the first field and provide a name for your new Ghost blog. Fill in the rest of the fields with details about your admin user, then click “Create account & start publishing.”

Creating Your First Post in Ghost

1. Click the “Write your first post” button on the Ghost onboarding page.

Creating Your First Post in Ghost

2. This will bring up a plain text editor where you can write a simple text post. Click “Publish” once you are done to create a new test post.

3. Check your Ghost front page to see if the new post went through.

Adding a New User to Ghost

1. Go back to your Ghost Administrator Console, then click “Members” on the page’s left sidebar.

2. Click the “New Member” button on the page’s upper right corner.

Adding a New User to Ghost

3. Fill in the details of your new member, then click “Save” to add them on the blog’s newsletter feed.

Good to know: Learn how to deploy and host your own Twitter clone using Pleroma.


Related Post