Linux Tutorials, Guides & Latest Tech Stuffs

Host a Ghost blog on Ubuntu 16.04,17.10 in Production Environment

Ghost-blog-publishing

Ghost is open source publishing or blogging platform written in JavaScript, it uses Node.js for server side executions. The Ghost platform is founded by John O’Nolan , the former deputy lead for WordPress User interface team. This blog post demonstrates, how to create a host blog with Ghost and Nginx.

Prerequisites

Download Ghost Package

You can use wget or curl command to fetch latest ghost version from its official site, We are using wget here

wget https://ghost.org/zip/ghost-latest.zip

Once latest zip file is successfully download, install unzip program to extract it:

sudo apt-get install unzip

/var/www/  is recommended location for web applications, so we will directly unzip it this location:

sudo unzip -d /var/www/ghost ghost-latest.zip

Reach to /var/www/ghost directory:

cd /var/www/ghost/

Hosting Ghost Blog in Production Environment

Ghost has started using MySQL database in its production environment, although SQLite3 is still supported in its development environment. If you don’t want to use MySQL for now you can check next section for installation instruction with SQLite3 database.

Install and secure MySQL

You can run these commands to install and secure MySQL. While running secure installation script ( last script ) you will find most of the answers to its questions are Yes ( Y ).

sudo apt-get update
sudo apt-get install mysql-server
mysql_secure_installation

After successful MySQL-server installation, you will require to create a MySQL database and database user for your blog.

Login to MySQL Console

Log in to MySQL console with root user with password created while installing mysql-server

mysql -u root -p

Create database, user and grant access

  1. Create database

for creating a new database, simply run create database <database_name> for example i am using ghost as a database name, don’t forget to use semicolon at end;

create database ghost;

2. Create User

Create a MySQL user  for accessing your Ghost blog database with password

CREATE USER 'ghostUser'@'localhost' IDENTIFIED BY 'your_strong_password';

3. Grant Access

Grant full privileges for your blog database to recently created MySQL user

GRANT ALL PRIVILEGES ON `ghost`.* TO "ghostUser"@"localhost";

Update Ghost production environment configuration

Go to Ghost blog folder and environment configuration, I am using vim for opening file you can use any suitable text editor like nano etc.

vim /var/www/ghost/core/server/config/env/config.production.json

this configuration file look something like this

{
 "database": {
 "client": "mysql",
 "connection": {
 "host" : "127.0.0.1",
 "user" : "Enter MySQL database user (ghostUser)",
 "password" : "Enter password for your MySQL user",
 "database" : "ghost"
 }
 },
 "paths": {
 "contentPath": "content/"
 },
 "logging": {
 "level": "info",
 "rotation": {
 "enabled": true
 },
 "transports": ["file", "stdout"]
 }
 }

Install Node Modules

Now go to Ghost root directory and install Node.js modules

cd /var/www/ghost/
 npm install --production

Start Ghost  Blog Server

After node_modules installation run

npm start

to start your blog for the first time, Now you can access your blog at http://localhost:2368/. For running your blog continuously, you should download daemon like forever or pm2. Forever is easy to use and can be installed by

npm install -g forever

After installing forever daemon globally set NODE_ENV to production using command

export NODE_ENV=production

Then run you ghost blog again by using

forever start index.js

Configure Nginx

For serving your Ghost blog on a domain like example.com, you need to configure Nginx. Check example configuration below.

server {
 listen 80;

if ($host ~* ^www\.(.*)) {
 set $remove_www $1;
 rewrite ^(.*)$ $scheme://$remove_www$1 permanent;
 }



server_name example.com www.example.co;
 client_max_body_size 10M;
 location / {
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header Host $http_host;
 proxy_pass http://127.0.0.1:2368;
}

}

Go to nginx configuration folder and create a .conf configuration file. Mostly Nginx are installed /etc/nginx location

cd /etc/nginx/conf.d/
sudo touch example.conf

you can use domain name or anything else for configuration file name, I am using example for tutorial purpose only.

Copy example nginx configuration above and change domain name  marked in red with your actual domain name and paste in example.conf

Restart Nginx Server

After adding new configuration to Nginx server, You need to reload it to load new configurations

sudo service nginx reload

Congratulations! Your new ghost blog is live and being served with Nginx reverse proxy. Visit your domain to find your blog.

If you have any issue, Please Leave a message from below

Pranav K

Pranav K is a software engineer and all-round computer geek. His interests include AWS, Ubuntu and Wordpress

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.