How to host a Node.js app on shared host
Hi guys, have you ever wanted to host your Node.js application on a shared hosting server and you are having issues with hosting it, you might have even done a lot of google search on how to host it, well with this article we will fix this issue together as will move on in this article.
Most Shared Hosting company normally have PHP preinstalled on your server, the first thing to do is to install NVM on your server. To do this you will need access to login with SSH from your terminal(Command line). To have access to SSH of your server ask your host provider.
ssh -p port username@IpAddress
when it asks for your password, enter the password you normally to login to your Cpanel. With this, you are done with the first step.
Step two: Installing the NVM on your Server.
If you have been able to successfully login through ssh, you can now do and undo. The ssh access let you have access to all lovely Linux commands.
Now run this to add NVM install
Run this first
wget -qO- https://cdn.rawgit.com/creationix/nvm/master/install.sh | bash
then run this
nvm install stable
Step four:
After the installation has been completed, close and reopen the terminal where you are running the ssh, login again the same way you logged in the first time.
run
node -vnpm -vTo confirm that node and npm has been successfully installed.
Now create a .htaccess file inside the public_html directory on your server. To do this,
cd ~/public_htmlnano .htaccess
after that copy the below into in the file
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:XXXXX/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:XXXXX/$1 [P,L]
N:B Replace the XXXXX with the port you set in your app.
I assumed you already have uploaded your application to the public_html on your server.
Step Five:
If you have already uploaded your project to the server already you can run the app the same way you normally do on your local server,
node app.js or node server.js, just run it the way you would do it locally
Step Six:
If you reload the website with your domain name, you should load the page correctly by now, but it you press control C on the terminal, i.e if you kill the server you page will not render again. This is perfectly normal, it mean we need a way to make sure our server continue to run after we exit the terminal, you can just do :
node app.js &
it will keep running even after you exist the terminal , but if you want a more efficient way of doing this, you can install a package that take care of that
npm install -g forever
than start the server.
forever start app.js
And now you have successfully hosted your node.js application on shared host.