This article describes how to install the Node.js platform on managed VPS hosting accounts (please see the Article Details sidebar to the right for a complete list of supported products). You can use Node.js to host third-party applications or you can run your own applications.
Your account must have the normal shell (not jailshell) enabled. To determine which shell your account is using, log in using SSH, and then type the following command:
echo $SHELL
If your account is using jailshell, please open a support ticket on the Customer Portal at https://my.a2hosting.com and request normal shell access for your account.
After you determine that your account meets the installation prerequisites, you can download and install Node.js and npm (the Node.js package manager). To do this, follow these steps:
cd ~
wget https://nodejs.org/dist/v16.20.2/node-v16.20.2-linux-x64.tar.xz
To extract the Node.js files, type the following command:
tar xvf node-v16.20.2-linux-x64.tar.xz
To rename the extracted folder to the more convenient nodejs name, type the following command:
mv node-v16.20.2-linux-x64 nodejs
To install the node and npm binaries, type the following commands:
mkdir ~/bin cp nodejs/bin/node ~/bin cd ~/bin ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
After you run these commands, Node.js and npm are installed on your account. To verify this, type the following commands:
node --version npm --version
After you install Node.js, you are ready to run Node.js applications. However, the exact steps to do this vary depending on the application configuration.
Many third-party and “production-ready” applications (such as Ghost) use the npm program to start the application, as shown by the following command:
nohup npm start --production &
For this method to work, there must be a valid package.json file for the application. The package.json file contains project metadata that the npm program reads to determine how to start the application, manage its dependencies, and more.
For simple applications, or for any application that does not have a package.json file, you can run the node executable directly and specify the application filename. For example:
nohup node my_app.js &
However, you lose the benefits of using npm to manage the application.
To stop a currently running Node.js application, type the following command:
pkill node
This command immediately stops all running Node.js applications.
Depending on the type of Node.js application you are running, you may want to be able to access it using a web browser. To do this, you need to select an unused port for the Node.js application to listen on, and then define server rewrite rules that redirect visitors to the application. The following steps demonstrate how to do this:
DirectoryIndex disabled 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]
In both RewriteRule lines, replace XXXXX with the port on which your Node.js application listens.
Save the changes to the .htaccess file, and then exit the text editor. Visitors to your web site are redirected to the Node.js application listening on the specified port.
For more information about Node.js, please visit http://nodejs.org.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.