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 System | Required Packages | Recommended Packages |
---|---|---|
AlmaLinux 8 / Rocky Linux 8 | Required: 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 9 | Required: 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 7 | Required: 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
- Log in via SSH:
Log in to the server as a cPanel user. - Create Application Directory:
Run the following command to create a directory for your application:
mkdir nodejsapp
- Navigate to the Directory:
cd nodejsapp
- Create
app.js
:
Use a text editor to create a file namedapp.js
(recommended). Example Configuration forapp.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
- 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
- Verify the Application:
In another terminal, run:
curl http://127.0.0.1:3000
Expected Output:
Hello World! NodeJS
- Stop the Test Application:
PressCTRL + C
or run the following commands:
ps aux | grep app.js
kill -9 PIDNUMBER
- 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
- Open the Application Manager in cPanel:
Navigate to cPanel » Home » Software » Application Manager. - Register the application and access it via:
http://example.com/nodejsapp
Restarting the Application
To apply changes:
- Create a
restart.txt
file in thetmp
directory:
touch appDir/tmp/restart.txt
- 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:
- Create the file:
/etc/apache2/conf.d/userdata/ssl/2_4/user/domain.nodejs.conf
- Add the following configuration:
DocumentRoot /user/example.com/public
PassengerStartupFile index.js
PassengerAppType node
PassengerAppRoot /nodejsapp/example.com
- Rebuild and restart Apache:
/usr/local/cpanel/scripts/rebuildhttpdconf
/usr/local/cpanel/scripts/restartsrv_httpd
Troubleshooting
- Check application logs:
/home/user/nodejsapp/logs
- For port issues, refer to Phusion Passenger’s documentation on reverse port binding.