Node application deployment in Shared Hosting(like GoDaddy)

Aritra Chakraborty
TrappedPanda
Published in
3 min readNov 10, 2018

As you can probably guess, I have a blog now! This is g̶o̶i̶n̶g̶ ̶t̶o̶ ̶b̶e my first post i̶f̶ ̶I̶ ̶c̶a̶n̶ ̶a̶c̶t̶u̶a̶l̶l̶y̶ ̶c̶o̶m̶p̶l̶e̶t̶e̶ ̶i̶t.
Anyway so got a shared hosting from Godaddy, and wanted to do some experiments. Specifically how to install node and deploy node apps and use .htaccess to redirect and forward traffic.

Prerequisite

  • A shared hosting server, the below article will assume it to be linux *Drumroll please*
  • SSH connection to the Host. If it is turned on, great, if it is not then every hosting provider has some sort of easy one click kinda mechanism to enable it. Google it for your webhosting provider. For me, I had an option on my cPanel.

Installing NodeJS

So, First step to actually deploy a node application is to install NodeJS. (yeah, I know, what can I say, I am a genius)

I used NVM (node version manager) to do the dirty work for me. The problem with Shared hosting is you don’t have root access. And node needs root access if you want to apt install it or maybe wget the node file and dpkg -i it, and we know that there is a good chance you need to do a apt install -f after.

NVM has a git repo where the features and uses are greatly demonstrated. I am using the url given in that README. You can check it out yourself: https://github.com/creationix/nvm#install-script
After connecting to the host using SSH , run this:

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

it will (most probably) install the NVM on your host. Now let’s install node.

nvm install stable
nvm alias default stable

Creating an Example Server

Just to demonstrate that we can actually run a server let’s install http-server package and create a directory and a index.html file. Here goes,

npm install -g http-server
mkdir ~/example
cd example
echo "<h1>Hello World</h1>" > index.html

Now let’s run the server.

http-server -p 50080

WHY 50080 port? Good Question, It’s because I practice Safe Sex. To be honest, we cannot choose 80,443 ports because the web service is already listening on those. Inversely we can choose whatever port which is open. But As I said before, to be safe, I chose a port above 50000.
After running the server it will run (Fate series Shiro anyone? Link), but you cannot access this server because, yes right answer, only port 80 and 443 are open from the outside and that port is being used by (most probably) an Apache2 instance. So, we need to forward traffic to this server and return the response.

Yep, we need to make a reverse proxy. To make it we need to edit the .htaccess file.

Changing .htaccess file

Let’s say we want the server to be accessed when we hit http://somedomain.com/myserver
So we make a directory in the public_html folder and create a .htaccess file in it. htaccess files are kind of like local httpd.conf files. Anyway,

cd ~/public_html
mkdir myserver
cd myserver
touch .htaccess

As the .htaccess file is created, we need to forward traffic to the server.

nano ~/public_html/myserver/.htaccess

#write the following rule
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:50080/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:50080/$1 [P,L]

Press Ctrl+O and Enter(Return) to Save and Ctrl+X to exit nano.

the .htaccess file

And Viola, You are actually done.

Go to your favourite Chrome browser(stable, Beta, Dev or canary, Yeah, I am chrome fan) and type in

http://somedomain.com/myserver/index.html

Even though specifying index.html is optional but some shared hosting will request index.html.var instead if you don’t give any file names.

This post is applicable for any kind of node server, just need to forward the traffic using .htaccess files.

Good day and happy hacking!

--

--