How to install Ghost with Nginx on CentOS

In this tutorial i will explain you how to install and setup Ghost with Ngnix running on Centos server.

Ghost is a powerful free blogging script based on Node.js. Its Just a blogging platform.

It is a beautifully designed, completely customizable and absolutely Open Source blogging platform exists only with one purpose, posting.

Prerequisite:

  1. A VPS running CentOS. I am using CentOS 6 64-bit running on a 1024MB.
  2. Putty or Terminal to access your server.
  3. Basic know how about common SSH commands to manage a Linux vps.
  4. 30 minutes of your time with a cup of coffee or tea.

Step 1 – Open up Putty or Terminal and login to your server as root or any user with root / sudo privileges.

Step 2 – In order to setup Ghost on your server, the main requirement is Node.js should be available there. So you can install Node.js following my previous tutorial.

Step 3 – Next, install Ghost on your server following this guide. The steps are very simple but one thing notable for this tutorial. Do not replace port 2368 with 80: As describe in Step 11 point 3. You can leave the port as it is or use common secondary web port like 81, 82, or 8080. In this guide I will stick with port 2368 (default).

Next, in the host part, use 127.0.0.1.

INSTALL NGINX

Step 4 – Now we’ll try to install Nginx on CentOS. Please follow the steps given below:

Step 4.a. Create nginx.repo file in yum.repos.d:

nano /etc/yum.repos.d/nginx.repo

Step 4.b. Now put following lines there:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Once done, save the file and exit the editor:

Step 4.c. Now install nginx using simple yum command:

yum install nginx -y

It will take some time to complete.

CONFIGURING NGINX

Step 5 – Now go to nginx default configuration directory:

cd /etc/nginx/

Step 5.a. Make a backup copy of nginx.conf file (just in case if things get disturbed):

cp nginx.conf nginx.conf.bak

Step 5.b. Open up nginx.conf file using file editor:

nano nginx.conf

Step 5.c. Delete everything in that file and replace it with the text below:

user nginx;
worker_processes 1;
pid /var/run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m 
max_size=3000m inactive=600m;
proxy_temp_path /var/tmp;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

gzip on;
gzip_comp_level 6;
gzip_vary on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript 
text/xml application/xml application/xml+rss text/javascript;

gzip_buffers 16 8k;

upstream ghost_upstream {
server 127.0.0.1:2368;
keepalive 64;
}

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

include /etc/nginx/conf.d/*.conf;
}

Now replace 1 at worker_processes line with the number of CPU your vps is allocated. In this example, I use 1.

Also, replace 2368 with the port you defined in your config.js file at ghost folder (example: 8080, 81, 82, etc).

Once done, save and exit editor.

Step 5.d. Now create the virtual host file for your site’s domain:

nano /etc/nginx/conf.d/domain.conf

Replace domain.conf with your actual domain. Example:

nano /etc/nginx/conf.d/cpanelpleskhost.conf

Step 5.e. Now put following lines of code there:

server {
listen 80;

server_name domain.tld www.domain.tld;

if ($host = 'domain.tld' ) {
rewrite ^/(.*)$ http://www.domain.tld/$1 permanent;
}

# location ~ ^/(ghost/signup/) {
# rewrite ^/(.*)$ http://domain.tld/ permanent;
# }

location ~ ^/(img/|css/|lib/|vendor/|fonts/|robots.txt|humans.txt) {
root /var/www/core/client/assets;
access_log off;
expires max;
}

location ~ ^/(shared/|built/) {
root /var/www/core;
access_log off;
expires max;
}

location ~ ^/(favicon.ico) {
root /var/www/core/shared;
access_log off;
expires max;
}

location ~ ^/(content/images/) {
root /var/www;
access_log off;
expires max;
}

location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_cache one;
proxy_cache_key ghost$request_uri$scheme;
proxy_pass http://ghost_upstream;
}
}

Replace domain.tld with your own domain name and its TLD. In my example it is cpanelplesk.com

Additionally you can delete these lines if you don’t want to use www. These lines force your blog to be accessible via www

if ($host = 'domain.tld' ) {
rewrite ^/(.*)$ http://www.domain.tld/$1 permanent;
}

also change /var/www at this part with actual directory where you put Ghost:

location ~ ^/(content/images/) {
root /var/www;
access_log off;
expires max;
}

In my example it is at /var/www/ghost.

Done? Save it and exit.

Step 5.f. Finally, text your nginx configuration before running it:

nginx -t

Step 6 – Start nginx using this command, make sure you get OK message:

service nginx start

INSTALL FOREVER SERVICE

Step 7 – Now issue this command to install Forever, This service will keep Ghost alive in the background and restart it if it ever stops or crashes:

npm install forever -g

Step 8 – Create a file called “starter.sh” in /var/www directory:

nano /var/www/starter.sh

Step 9 – now put these lines there, save it and exit:

#!/bin/sh

if [ $(ps aux | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
then
export PATH=/usr/local/bin:$PATH
export NODE_ENV=production
NODE_ENV=production forever start --sourceDir /var/www index.js >> 
/var/log/nodelog.txt 2>&1
fi

Step 10 – Now issue this command syntax to make sure that newly created .sh file is executable:

chmod +x /var/www/starter.sh

Step 11 – Next, you may want to fix some permissions a bit:

chown -R nginx:nginx /var/www/

Step 12 – Next, create a new cron, type this command:

crontab -e

Step 13 – Now put this in that file:

@reboot /var/www/starter.sh

Save it and exit. You’ll get a message saying “crontab: installing new crontab“:

Step 14 – Finally, go to /var/www folder:

cd /var/www

Step 15 – then start Ghost using this command:

./starter.sh

That’s it. Now open your browser and try accessing your blog.

That’s all to do, Now you know how its easy to install and setup Ghost with Nginx on a CentOS server. You also like to know how to How To Create A Network Bridge On CentOS 7 / RHEL 7 , Please check that and also Give your opinion below if you experience any issues or to discuss your ideas and experiences.

Leave a Reply

Register to Get FREE Offers

Why Pay for Software when you can GET it for FREE

JOIN US TONIGHT!