There is a new version of this tutorial available for Ubuntu 20.04 (Focal Fossa).

How to deploy Ghost Blog with Nginx on Ubuntu 18.04 LTS

Ghost is a completely open source (MIT license) blogging platform, that is gaining popularity among developers and ordinary users since its 2013 release. Ghost source code is publicly available on GitHub. Ghost generally takes around ~300MB of RAM to run well, so it can run on modest hardware. It puts focus on content and blogging. The most attractive thing about Ghost is its simple, clean, elegant and responsive design. You can write your blog posts from a mobile phone. Content for Ghost is written and formatted using the Markdown language. Ghost is a perfect fit for individuals or small groups of writers. Ghost also has built-in support for Accelerated Mobile Pages (AMP) project, which will make loading of your blog lightning fast on mobile phones.


Recently, Ghost developers released the first major, stable release of the Ghost - 1.0.0. Ghost 1.0.0 introduced many new features and the most notable among them are: brand new Markdown editor, refreshed user interface (UI), new default theme design, new and improved installation and update process with Ghost-CLI tool.

Ghost Blog and CMS


In this tutorial, we are going to set up and deploy a secure Ghost blog with the help of Ghost's Ghost-CLI tool on an Ubuntu 18.04 LTS server using Let's Encrypt, Acme.sh, Node.js, npm, Yarn, NGINX and MySQL/MariaDB.

Requirements

  • Domain name. This tutorial will use example.com domain. 
  • A server running Ubuntu 18.04 LTS with 1GB or RAM.
  • A non-root user with sudo privileges.

Initial Steps

Check your Ubuntu version:

lsb_release -ds 
# Ubuntu 18.04 LTS

Set up the timezone:

timedatectl list-timezones
sudo timedatectl set-timezone 'Region/City'

Update your operating system’s packages:

sudo apt update && sudo apt upgrade -y

Install build-essential package:

sudo apt install -y build-essential

Step 1 - Install Node.js and npm

NOTE: Latest Ghost 1.0.0 currently supports Node.js versions 8.9+ and 6.9+ only.

Ghost is built on Node.js. We are going to install recommended version for Ghost which is v8 Carbon LTS at the time of this writing tutorial. On Linux you have a few installation options: Linux Binaries (x86/x64), Source Code or via Package Managers. We will use Package Manager method by utlilzing Nodesource repository.

Download and install the latest Long-Term Support (LTS) version (release) of Node.js:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt install -y nodejs

NOTE: npm is distributed with Node.js - which means that when you download Node.js, you automatically get npm installed on your computer.

Check Node.js and npm version:

node -v && npm -v
# v8.11.2
# 5.6.0

Npm is a separate project from Node.js, and tends to update more frequently. As a result, even if you’ve just downloaded Node.js (and therefore npm), you’ll probably need to update your npm. Luckily, npm knows how to update itself! To update your npm, type this into your terminal:

sudo npm install -g npm@latest

Recheck npm version, it should be latest version:

npm -v
# 6.1.0

Step 2 - Install MariaDB server

Ghost supports MySQL/MariaDB and SQLite databases. In this tutorial, however, we will be using the MariaDB database. If you prefer, you can use MySQL instead of MariaDB.

Download and install the latest stable version of MariaDB server from the MariaDB repository on your machine:

sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64] https://mirrors.nxthost.com/mariadb/repo/10.3/ubuntu bionic main'
sudo apt update
sudo apt install -y mariadb-server

During the MariaDB installation process, you will be promted to enter MariaDB root user password. You should enter strong password.

Install MariaDB database

Check MariaDB version:

mysql --version && sudo mysqld --version
# mysql  Ver 15.1 Distrib 10.3.7-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
# mysqld  Ver 10.3.7-MariaDB-1:10.3.7+maria~bionic-log for debian-linux-gnu on x86_64 (mariadb.org binary distribution)

Check if MariaDB daemon has started and is running:

sudo systemctl status mysql.service
sudo systemctl is-enabled mysql.service

Run the mysql_secure_installation utility (script) to improve the security of your MariaDB installation:

sudo mysql_secure_installation


# Enter current password for root (enter for none):

# Change the root password? [Y/n] N
# Remove anonymous users? [Y/n] Y
# Disallow root login remotely? [Y/n] Y
# Remove test database and access to it? [Y/n] Y
# Reload privilege tables now? [Y/n] Y
# Success.

# All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

# Thanks for using MariaDB!

Login (connect) to the MariaDB command line as the MariaDB root user:

mysql -u root -p

Create a new MariaDB database and user for Ghost installation. This step is optional, because Ghost-CLI tool in a later step can create database for you, you just need to enter MariaDB root user credentials (username & password) when asked by Ghost-CLI during the installation of Ghost:

mysql> CREATE DATABASE dbname;
mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL ON dbname.* TO 'username'@'localhost';
mysql> FLUSH PRIVILEGES;

Exit (disconnect) from MariaDB:

mysql> EXIT;

Step 3 - Install NGINX

NGINX (engine-x) is a high performance web server, load balancer, cache and proxy server that works well in all environments: Bare Metal, Public/Private/Hybrid Cloud and Containers. NGINX will be used as a reverse proxy for our Ghost application.

NGINX can be installed differently, depending on the operating system. For Linux, NGINX packages from nginx.org can be used.

Download and install latest mainline (recommended for most deployments) release of NGINX and dynamically loadable modules directly from the official NGINX repository:

wget https://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key
rm nginx_signing.key
sudo -s
printf "deb https://nginx.org/packages/mainline/ubuntu/ $(lsb_release -sc) nginx\ndeb-src https://nginx.org/packages/mainline/ubuntu/ $(lsb_release -sc) nginx\n" >> /etc/apt/sources.list.d/nginx_mainline.list
exit
sudo apt update
sudo apt install -y nginx nginx-module-geoip nginx-module-image-filter nginx-module-njs nginx-module-perl nginx-module-xslt

Verify that NGINX is installed by checking its version:

sudo nginx -v && sudo nginx -V
# nginx version: nginx/1.15.0
# nginx version: nginx/1.15.0

Start and enable NGINX:

sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Create /etc/nginx/snippets, /etc/nginx/ssl, /etc/nginx/sites-available and /etc/nginx/sites-enabled directories. These directories are needed for Ghost-CLI tool to work correctly:

sudo mkdir -p /etc/nginx/{snippets,ssl,sites-available,sites-enabled}

Add to include /etc/nginx/sites-enabled/*.conf; directive to nginx.conf file, run:

sudo vim /etc/nginx/nginx.conf

Install Nginx web server

Save file and exit from Vim editor.

Step 4 - Install Yarn (optional)

Download and install Yarn package manager on your system:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install -y yarn

Check Yarn version:

yarn --version
# 1.7.0

Step 5 - Installing Ghost-CLI

Ghost-CLI is a command line interface (CLI) tool that makes installing and updating Ghost easy. It sets up the database, configures NGINX as a reverse proxy, enables TLS/SSL security using Let’s Encrypt CA, automatically renews your SSL, and initializes Ghost as a systemd service. Ghost-CLI is an npm module that can be installed via either npm or yarn.

Download and install Ghost-CLI tool:

sudo npm install -g ghost-cli@latest
# or with Yarn
sudo yarn global add ghost-cli@latest

Check Ghost-CLI version:

ghost version
# Ghost-CLI version: 1.8.1

Troubleshoot the system for any potential issues when installing or updating Ghost:

ghost doctor install

Step 6 - Install Ghost

Next, we will install Ghost using the Ghost-CLI tool.

First, create an empty document root directory:

sudo mkdir -p /var/www/example.com

Change ownership of the /var/www/example.com directory to the non-root user with sudo privileges that you created. In this example, johndoe:

sudo chown johndoe:johndoe /var/www/example.com

Your installation folder must have the correct permissions:

sudo chmod 755 /var/www/example.com

Navigate to the Ghost root directory:

cd /var/www/example.com

Ensure that the directory is empty to avoid file conflicts:

ls -a

Install Ghost in production mode by running:

ghost install

Answer each question as prompted:

? Enter your blog URL: https://example.com
? Enter your MySQL hostname: localhost
? Enter your MySQL username: root
? Enter your MySQL password: your_mysql_root_user_password_here
? Enter your Ghost database name: database_name_for_ghost
? Do you wish to set up "ghost" mysql user? yes ? Do you wish to set up Nginx? yes ? Do you wish to set up SSL? yes ? Enter your email (used for Let's Encrypt notifications) [email protected] ? Do you wish to set up Systemd? yes ? Do you want to start Ghost? yes

After installation is complete, run

ghost ls

to view running Ghost processes.

In future, to update your Ghost blog when newer version is released, you just need to run

ghost update

command, from Ghost installation directory and Ghost-CLI will take care of everything for you.

Step 7 - Complete Ghost setup

To complete the setup process, navigate to the Ghost configuration page by appending /ghost to the end of your blog’s URL or IP. This example uses https://example.com/ghost.

On the welcome screen, click Create your account:

Ghost web installer

Enter your email, create a user, password, and blog title:

Create account in Ghost

Invite additional members to your team. If you’d prefer to skip this step, click I’ll do this later, take me to my blog! at the bottom of the page.

Invite team members

Navigate the Ghost admin area to create your first post, change your site’s theme, or configure additional settings:

Ghost stories

Congratulations! You have successfully installed and deployed Ghost blogging platform on Ubuntu 18.04 LTS server.

Share this page:

4 Comment(s)