Installing and configuring Ghost blogging platform on CentOS 7.

Installing and configuring Ghost blogging platform on CentOS 7.


I been a user of WordPress for couple years, but after having couple issues with it I decided to look for an alternative for my new blog (http://www.juliosblog.com).

After a little of research, I found Ghost and decided to give it a try. Ghost is a free and open source blogging platform written in JavaScript (Node.js) and is becoming really popular. In this blog post, I will show how to install and configure Ghost on CentOS 7 to run behind Nginx.

Installing

First, we are going to install Node.js and the npm tool:

[root@www~]$ yum install nodejs npm -y

We are going to install Ghost using npm, you could also download Ghost from their website . Please notice that this procedure will take a little bit to run.

[root@www~]$ npm install ghost -y

After Ghost is downloaded, we are going to proceed to install it:

Create a directory where you want your Ghost packages to live, in my case the directory will be /var/www/virtual_hosts/www.juliosblog.com and copy the software that npm downloaded to that folder:

[root@www~]$ mkdir -p /var/www/virtual_hosts/www.juliosblog.com

[root@www~]$ cp -av /root/.npm/ghost/0.9.0-beta.2/package/* .

[root@www~]$ npm install --production

[root@www~]$ npm start --production

You need to make sure that on the output of this command Ghost is running. The output will look like this:

[root@www~]$ npm start --production

\> [email protected] start /var/www/virtual_hosts/www.juliosblog.com

\> node index

WARNING: Ghost is attempting to use a direct method to send email.
It is recommended that you explicitly configure an email service.
Help and documentation can be found at http://support.ghost.org/mail.

Migrations: Database initialisation required for version 006
Migrations: Creating tables...
Migrations: Creating table: posts
Migrations: Creating table: users
Migrations: Creating table: roles
Migrations: Creating table: roles_users
Migrations: Creating table: permissions
Migrations: Creating table: permissions_users
Migrations: Creating table: permissions_roles
Migrations: Creating table: permissions_apps
Migrations: Creating table: settings
Migrations: Creating table: tags
Migrations: Creating table: posts_tags
Migrations: Creating table: apps
Migrations: Creating table: app_settings
Migrations: Creating table: app_fields
Migrations: Creating table: clients
Migrations: Creating table: client_trusted_domains
Migrations: Creating table: accesstokens
Migrations: Creating table: refreshtokens
Migrations: Creating table: subscribers
Migrations: Running fixture populations
Migrations: Creating owner
Migrations: Ensuring default settings
Ghost is running in production...
**Your blog is now available on http://www.juliosblog.com**
Ctrl+C to shut down

Configuring Nginx with Ghost

After this, we are going to configure Nginx to serve the newly installed Ghost instance. In order to do that, we are going to go ahead and create a new virtual host for it.

Go to /etc/nginx/conf.d/ and create a new configuration file for the virtual host. In my case is the site running this blog. Also, make sure that the location (directory) that you are defining to keep your access and errors logs exist.

[root@www~]$ vim www.juliosblog.com.conf

server {
listen  80;
server_name www.juliosblog.com juliosblog.com;
access_log /var/www/virtual_hosts/www.juliosblog.com/logs/access.log;
error_log /var/www/virtual_hosts/www.juliosblog.com/logs/error.log;

#GZIP COMPRESSION
gzip             on;
gzip_comp_level  2;
gzip_min_length  1000;
gzip_proxied     expired no-cache no-store private auth;
gzip_types       text/plain application/x-javascript text/xml text/css application/xml;

#BUFFERS
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 8 32k;

#TIMEOUTS
client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;

#CACHING
#location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
#       expires 365d;
#       }

location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:2368;
        proxy_redirect off;
     }
}

Restart Nginx and verify that you could access the website.

[root@www~]$ systemctl restart nginx

Making Ghost persitent with Supervisord

Now, until here we have setup Ghost and we used npm to start it. The problem with this method is that as soon as you close your session npm will stop running, using npm is a great way when we want to do some test and development on the platform. But to make your Ghost site persistent you will need to use another method like Forever, Supervisor or by creating an Init script for it. In this example, we will use Supervisor (prepackage with CentOS) to make Ghost persistent.

Installing Supervisor :

[root@www~]$ yum install supervisor -y

Now we are going to set supervisor as a service. In order to do this, we are going to create a file on /etc/rc.d/init.d/ and name it supervisord:

# vim /etc/rc.d/init.d/supervisord 
#!/bin/sh
#
# /etc/rc.d/init.d/supervisord
#
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord

# Source init functions
. /etc/rc.d/init.d/functions

prog="supervisord"

prefix="/usr/"
exec_prefix="${prefix}"
prog_bin="${exec_prefix}/bin/supervisord"
PIDFILE="/var/run/$prog.pid"

start()
{
       echo -n $"Starting $prog: "
       daemon $prog_bin --pidfile $PIDFILE
       [ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog startup"
       echo
}

stop()
{
       echo -n $"Shutting down $prog: "
       [ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"
       echo
}

case "$1" in

 start)
   start
 ;;

 stop)
   stop
 ;;

 status)
       status $prog
 ;;

 restart)
   stop
   start
 ;;

 *)
   echo "Usage: $0 {start|stop|restart|status}"
 ;;

esac

We set the right permissions to the file:

[root@www~]$ chmod +x /etc/rc.d/init.d/supervisord

And now we could enable and start the service:

[root@www~]$ systemctl enable supervisord

[root@www~]$ systemctl start supervisord

The next step will be to create the supervisor startup script for Ghost. To do that we will create a file on /etc/supervisord.d/ and name it ghost.ini:

[root@www~]$ vim /etc/supervisord.d/ghost.ini
[program:ghost]
command = node /var/www/virtual_hosts/www.juliosblog.com/index.js
directory = /var/www/virtual_hosts/www.juliosblog.com
user = root
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/ghost.log
stderr_logfile = /var/log/supervisor/ghost_err.log
environment = NODE_ENV="production"

After this we import, reread, and update the Supervisor config :
[root@www~]$ supervisorctl reread
[root@www~]$ supervisorctl update

And we add and start the service:

[root@www~]$ supervisorctl add ghost; supervisorctl start ghost

Now you could go access and start working on your blog. Enjoy!