This article will explain how to set up Nodejs applications in your cPanel control panel.
Please note: cPanel supports nodejs v16 as well as v10.
(Only one version can exist on a server at any one time - Rolling back is supported.)
Step 1 - Setting up Nodejs applications in cPanel
First set up your site and files. You'll likely be doing this from a pre-existing git repository.
Step 2
Set up your domain. If this doesn't already exist on the server, you'll need to create it (via WHM --> "Create a new account").
Step 3
If the account is already set up, go to the cPanel account for the chosen domain name. If you're using a subdomain or addon domain, you should set it up now.
Step 4
Once you have set up the domain, you can then clone your repo to it. This is done either via cPanel --> File Manager --> Git Version Control. Alternatively, via Git Clone over SSH.
You should clone your repository either into public_html for your domain, or an appropriate subdirectory if it's an addon domain or subdomain.
Step 5
If your files are not in version control, you can skip this setup and upload them via another method, so long as they are on the server, you'll be able to run them.
Step 6
Next, register the application. This step is vital as it tells Passenger (which handles process management and proxying for your application) where it is and to serve it.
Go to cPanel --> Application Manager --> click on Register Application.
Step 7
Most of the fields here should be self-explanatory. Your Deployment Domain is the domain you want your application to run on, and the Application Path is the path to the repo you cloned/set up earlier.
You can also add any environment variables you may need to pass to your application as well.
Step 8
Once you've registered your application, that's it!
Passenger will start it and serve it on the domain you chose.
Please note: If you want to restart the application (e.g. if you've made changes) you should create a '/tmp/restart.txt' file in your application directory, which tells Passenger to perform a graceful restart.
You can explore Passengers in-depth article for further explanation - [Restarting applications - Apache - Passenger Library (phusionpassenger.com)]. Note that restarting Apache also achieves the same thing.
Custom Entry Points
By default, Passenger looks for 'app.js' to start when it runs an application. Some frameworks do not use this filename as the startup file, and so need an additional step to make this work by customising the 'PassengerStartupFile' directive.
To do this (as root), do the following:
- Create a file 'etc/apache2/conf.d/userdata/ssl/2_4/user/domain.nodejs.conf' (replace domain with your domain name as applicable).
- Add the following to include, replacing the variables as applicable:
DocumentRoot /user/example.com/public
# Use server.js as the startup file (entry point file) for
# your Node.js application, instead of the default app.js
PassengerStartupFile index.js
PassengerAppType node
PassengerAppRoot /nodejsapp/example.com
Rebuild and restart Apache with:
/usr/local/cpanel/scripts/rebuildhttpdconf
/usr/local/cpanel/scripts/restartsrv_httpd
For further information, take a look at cPanel's documentation [How to Install a Node.js Application - cPanel Knowledge Base - cPanel Documentation]
Need further assistance?
If you require further assistance please contact the ProStack team and we can help get this resolved for you! Get in touch today!