Running Ghost with cPanel on Shared Host via Rails

I have been looking to migrate my WordPress-driven blog over to Ghost but I don’t want to use a VPS since that would require me to configure and maintain every detail. Shared hosting is great in that it is essentially a managed instance – always updated, backed up, and available (depending on the quality of the host, of course). Most importantly, it’s someone else’s problem if there’s downtime.

This post is to document my steps to run Ghost or any other nodejs applications on a cPanel shared web host. While ssh is not required, it makes debugging and testing a little easier. At the time of writing, cPanel does not support nodejs apps natively (https://features.cpanel.net/topic/nodejs-hosting). However, they do support Rails 2 apps, so we can leverage Rails to bootstrap our nodejs app. Note, we cannot just run nodejs applications via command line because port(s) need to be mapped on the account such that yourdomain.com:12345 will be redirected correctly.

For the sake of completeness, I also looked at an alternative method (https://github.com/niutech/node.php) that uses PHP to proxy requests to a node app. That sounds incredibly dirty, though my method is also hacky but arguably less so. Anyway, here it goes.

Outline of steps:
1. Create 2 Rails Applications in cPanel
– one will be used to bootstrap
– second will not be used; we use the assigned port in Ghost or nodejs app
2. Modify Rails app #1 for bootstrapping
3. Install nodejs and npm
4. Upload and configure Ghost
5. Run it and setup redirects as needed
6. …
7. Profit!

1. Create 2 Rails Applications in cPanel
– one will be used to bootstrap
– second will not be used; we use the assigned port in Ghost or nodejs app
ghost_rails1
2. Modify Rails app #1 for bootstrapping

In the bootstrap Rails app ./config/boot.rb, append to the bottom:

1
2
3
4
5
6
7
8
pid = Process.fork
if pid.nil? then
  sleep 5
  exec '(cd ~/node_apps/ghost && npm install)'
  exec '/home/YOURUSERNAME/bin/node /home/YOURUSERNAME/node_apps/ghost/index.js'
else
  Process.detach(pid)
end

note: you may want to immediately exit from the Rails app
note2: screenshot application names maybe different from the commands shown here

3. Install nodejs and npm

1
2
3
4
5
6
7
8
9
10
wget http://nodejs.org/dist/v0.12.3/node-v0.12.3-linux-x64.tar.gz
tar xvf node-v0.12.3-linux-x64.tar.gz
mkdir -p ~/local/bin
cp node-v0.12.3-linux-x64/bin/node ~/local/bin/
rm -rf node-v0.12.3*
echo prefix = ~/local >> ~/.npmrc
wget https://www.npmjs.org/install.sh
chmod o+x install.sh
./install.sh
rm -rf install.sh

note: npm from the node package seems to be statically linked to /usr/local which you won’t have access to, so we install it directly with the prefix option

4. Upload and configure Ghost
– start the bootstrap Rails app from cPanel
ghost_rails4-1

ghost_rails4-2
5. Run it and setup redirects as needed

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *