Yes, this is possible on even the cheapest shared hosting tier. @nebulr instructions are correct. Here is a slightly updated and expanded version for noobs like me.
(1) Enable SSH on your shared hosting account:
• Log into your GoDaddy hosting and turn on SSH Access (on the Dashboard, it's in "Settings" on the bottom right). Take note of the cPanel login username and change the password if you don't remember it. Note that you also may need to create keys in CPanel, under "Security" and "SSH Access".
(2) Install the nodejs program itself:
• Download the Node.js binaries from https://nodejs.org/en/download/ Specifically you'll want the Linux x64 version (direct link https://nodejs.org/dist/v10.15.0/node-v10.15.0-linux-x64.tar.xz)
• Unpack this .tar file on your computer and look inside for the bin
folder (on a Mac you may need a program such as The Unarchiver to unpack it). The bin
folder will have a file called "node" that's about 40Mb. This "node" file is the only thing we're going to use in this package.
• Using the CPanel File Manager or FTP program, create a folder on the server called "bin" in /home/yourUserName/
and give it permissions of 755. Note this is NOT inside public_html
.
• Upload the "node" file to /home/yourusername/bin/
(3) Create a simple nodejs script:
• Open a text editor (like Sublime) and create a new file called "app.js" (or whatever):
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('NodeJS server running on Shared Hosting\n');
});
server.listen(port, hostname, () => {
console.log('Server running at http://${hostname}:${port}/');
});
Note that this is just the basic server app from https://nodejs.org/en/docs/guides/getting-started-guide/
• Open your CPanel File Manager or FTP program, and upload the app.js file to /home/yourusername/public_html/
(4) Modify the .htaccess file:
• Use your FTP program to add these lines to the .htaccess file:
RewriteEngine on
RewriteRule (.*) http://localhost:3000/$1 [P,L]
Note that the .htaccess file is probably blank by default. Also note that you can use nano, vim, or emacs in SSH to edit the .htaccess file if you're brave or 1337.
(5) Start the node server:
• SSH into the godaddy server by opening Putty (Windows) or Terminal (Mac) and at the command line type: ssh [email protected]
(where username is your hosting account's cPanel login)
The server should respond with [email protected]'s password:
which is where you enter the cPanel login password.
Note: If it's your first time SSH'ing to the server, you'll get a message: The authenticity of host 'X.X.X.X' can't be established. RSA key fingerprint is XXXX. Are you sure you want to continue connecting (yes/no)?
Type yes and continue on.
• Navigate to /home/yourUserName/public_html/
by typing cd public_html
. Start the node server script by typing: node app.js &
In a couple seconds you should see the message: Server running at http://127.0.0.1:3000/
(6) Check it out:
• Open a web browser and type your website's URL. You should get a white page with the text NodeJS server running on Shared Hosting
or whatever message you put in line 9 of app.js above. Note that you can't use the IP address on a shared hosting account, you need to use the domain name.