Headlines

Here is How to Install a Node.js Application

Share tech-trends

Installing a Node.js Application

Pre-Installation Settings

Before you begin, ensure your hosting provider has installed the necessary EasyApache 4 packages for your server.


Required and Recommended Packages

Operating SystemRequired PackagesRecommended Packages
AlmaLinux 8 / Rocky Linux 8Required:
1. ea-ruby27-mod_passenger (disables Apache’s mod_userdir module).
2. ea-apache24-mod_env
Choose one:
ea-nodejs16
ea-nodejs18
ea-nodejs20
ea-nodejs22
ea-ruby27-ruby-devel
AlmaLinux 9 / Rocky Linux 9Required:
1. ea-apache24-mod-passenger
2. ea-apache24-mod_env
Installs the latest Ruby, NodeJS, and Python versions for compatibility.
Choose one:
ea-nodejs16, ea-nodejs18, ea-nodejs20, or ea-nodejs22
CentOS 7Required:
1. ea-ruby27-mod_passenger (disables Apache’s mod_userdir module).
2. ea-apache24-mod_env
3. ea-nodejs16
ea-ruby27-ruby-devel

Note: AlmaLinux 9 and Rocky Linux 9 default to Node.js 16. To use a different version, configure it manually.


Installing the Application

  1. Log in via SSH:
    Log in to the server as a cPanel user.
  2. Create Application Directory:
    Run the following command to create a directory for your application:
   mkdir nodejsapp
  1. Navigate to the Directory:
   cd nodejsapp
  1. Create app.js:
    Use a text editor to create a file named app.js (recommended). Example Configuration for app.js:
   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('Hello World! NodeJS \n');
   });

   server.listen(port, hostname, () => {
     console.log(`Server running at http://${hostname}:${port}/`);
   });

Testing the Application

  1. Start the Application:
    Use the following command, replacing ** with your Node.js version:
   /opt/cpanel/ea-nodejs**/bin/node app.js


Example Output:

   Server running at http://127.0.0.1:3000
  1. Verify the Application:
    In another terminal, run:
   curl http://127.0.0.1:3000


Expected Output:

   Hello World! NodeJS
  1. Stop the Test Application:
    Press CTRL + C or run the following commands:
   ps aux | grep app.js
   kill -9 PIDNUMBER
  1. Export the Node.js Path (Optional):
    Add the following line to your .bashrc file, replacing ## with your Node.js version:
   export PATH=/opt/cpanel/ea-nodejs##/bin/:$PATH

Registering the Application

  1. Open the Application Manager in cPanel:
    Navigate to cPanel » Home » Software » Application Manager.
  2. Register the application and access it via:
   http://example.com/nodejsapp

Restarting the Application

To apply changes:

  1. Create a restart.txt file in the tmp directory:
   touch appDir/tmp/restart.txt
  1. Restart the application after modifying it.

Using a Custom Startup File

If you create a startup file with a different name, update the Apache configuration:

  1. Create the file:
   /etc/apache2/conf.d/userdata/ssl/2_4/user/domain.nodejs.conf
  1. Add the following configuration:
   DocumentRoot /user/example.com/public
   PassengerStartupFile index.js
   PassengerAppType node
   PassengerAppRoot /nodejsapp/example.com
  1. Rebuild and restart Apache:
   /usr/local/cpanel/scripts/rebuildhttpdconf
   /usr/local/cpanel/scripts/restartsrv_httpd

Troubleshooting

  1. Check application logs:
   /home/user/nodejsapp/logs
  1. For port issues, refer to Phusion Passenger’s documentation on reverse port binding.
shadjavist
Author: shadjavist

Humble gentleman who never believes in impossibility

Leave a Reply

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