Configuring Ghost with Debian and VestaCP on a sub-directory
When we run into walls, we sometimes spend hours trying to get back on the right path. No application of this is truer than in coding or system administration, where undemanding tasks suddenly turn into long, extended periods of debugging where you're trying to not only figure out what went wrong, but how to fix it too.
I mocked Mariusz for not being able to configure his Ghost installation correctly in an under an hour -- he ran into a wall where he wasn't able to resolve Ghost through a sub-domain. He likely messed up the Nginx/Apache configuration somewhere, but karma came out in full force for me. I spent, in total, about 6 hours trying to figure out why Ghost wasn't working. I set up everything correctly -- as far as I knew -- and I couldn't understand why I wasn't able to access Ghost directly through my IP and port, as well as through the http://steven.re/blog sub-directory.
Eventually, as you can guess, I figured it out. In trying to debug this I went through everything from rewriting the Nginx configuration file, through to reinstalling node.js and Ghost, as well as rewriting, checking and double-checking my Ghost configuration file. Eventually I checked iptables and -- to my amusement -- rather than set iptables to accept the packets for the Ghost port, I had configured it to drop them.
Needless to say I feel pretty stupid, and moreso since I was picking on Mariusz for running into a much larger wall.
My apologies for picking on you, Mario.
Anyway, this is the new blog, and hopefully it will be actively updated. I'm going to explain how to properly configure Ghost for those using VestaCP and Debian on a sub-directory. It may work for people using CentOS or Ubuntu, but I can only confirm that it works on Debian.
The first thing we're gonna do is install Node. The Ghost documentation mentions it wants 0.10.x, but if you've already installed 0.12.x or newer you can go ahead and use that. If it doesn't work, you can downgrade to 0.10.x. If you'd like to check which version of Node you have installed, type node -v into a terminal. In order to download and install 0.10.x we're gonna run the following command in a terminal with superuser privileges:
curl -sL https://deb.nodesource.com/setup_0.10 | bash -
apt-get install -y nodejs
Since we're using VestaCP and we're installing it onto a sub-directory, we don't need to create a new Nginx server block. The Nginx configuration for VestaCP is located at /home/admin/conf/web/nginx.conf, so we're now going to type nano /home/admin/conf/web/nginx.conf into our terminal. Go to the domain you'd like to install Ghost on, and under the location / { block we're going to add our own as follows:
location ^~ /blog/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:2368;
proxy_redirect off;
}
If you want your sub-directory to be something other than /blog/, then now is the time to change it. It also assumes you're using the default port of 2368, which you can change to whatever you want to use, if you want to change it. Make sure you don't set it to port 80 -- it's already set to port 80, and what we're doing is forwarding all port 80 requests for http://domain/blog/ to port 2368.
- Now you're going to want to change directory (using the command
cd) to your the file directory of your website. On a default VestaCP installation, this is located at/home/admin/web/{domain}/public_html/, where{domain}is the domain of your website. If you haven't already created the /blog/ subdirectory, you can create it by typingmkdir bloginto the terminal. We're going to change directory into that folder, so typecd bloginto the terminal.
Now download, unzip and install the latest version of Ghost
curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip
unzip ghost.zip
npm install --production
- We're now going to need to edit Ghost's configuration file, so type
nano config.jsinto the terminal. Edit line 13 with the location of your blog, in my case it ishttp://steven.re/blog/(make sure you include thehttp://). Edit line 25 to be the IP address of your machine, and line 27 to be the port you configured innginx.confabove. - If you try to access the site from
http://{your_ip_address}:{port}you'll see that it's not working. This is because we need to set iptables to accept all of the packets for that specific port. For the sake of ease, we're going to do this through the VestaCP panel, so log into that and click on the Firewall tab. Then press the green 'plus' button and setActiontoAccept,Portto whatever port you've specified earlier,IP addressto0.0.0.0/0, and leave a comment withGHOSTso that you know what this firewall rule is for.
In order to ensure that Ghost keeps running, we're going to be using PM2. In order to install it and get it running, enter the following commands into your terminal:
npm install pm2 -g
NODE_ENV=production pm2 start index.js --name "Ghost"
- And that's it! You should now be able to access your Ghost installation from your domain without any hassles.
I've written the above purely from memory so I'm not sure if I'm missing anything. If I did leave something out, please let me know and I'll update the post. Thanks for reading!