Why Node.js for CloudLinux?
In the last few years, the stability and ease of use of Node.js has lead to heavy adoption in application development. However, deploying and configuring a Node.js application to work with cPanel presents a number of hurdles. CloudLinux’s recently released Node.js Selector is a great solution that includes a graphical interface to make deployment go more smoothly. To use this utility, you will need to have CloudLinux installed along with the LVE Manager plugin. In this configuration, your Node.js application will also benefit from the resource usage monitoring that comes with the CloudLinux LVE Manager.
Step 1 – Installation of Phusion PassengerWe’ll need to enable Phusion Passenger for EasyApache. In Web Host Manager, navigate to EasyApache. From there, in the upper left corner of the screen type in “EasyApache 4”. Select “Customize” in the menu that follows and then “Apache Modules”. In the list of Apache modules searching for “Passenger” will bring up the option to install mod-alt-passenger as shown below:
After selecting mod-alt-passenger, all that needs to be done is to skip to the “Review” step on the left sidebar and click “Provision” on the review menu to apply the changes.
Step 2 – Installation of CloudLinux Node.js SelectorTo start the installation of Node.js Selector for your users, proceed to the LVE Manager page in Web Host Manager. Under the Options tab, click on the Node.js line to expand the section containing the install menu for the various Node.js versions:
Only the Node.js versions installed here will be available for each cPanel user. So for each Node.js version, first click “Install” to put in place the needed packages. Then, select the “Enable” switch for each version of Node to make it available. Once each version you would like to use is installed and enabled, be sure to also enable the overall toggle switch where it says “Node.js” to make the system available for your users.
Post-installation, with everything in place, the install menu should look like this:
Step 3 – Web Host Manager (WHM) Configuration:
At this point, you’ll have the LVE Manager configured, as well as Node.js setup for your cPanel users. However, the default cPanel memory limit is sometimes insufficient to install Node.js packages using NPM. It is recommended to increase this memory limit to avoid issues during deployment. To make the change in WHM, proceed to the “Tweak Settings” page, and search for “Max cPanel process memory”. Increase this from the default of 768MB to at least 1024MB to avoid issues with the NPM installer. This setting does not affect the memory allocated to a Node.js application, but it does allow for the initial installation of Node.js packages to make use of additional memory needed for dependency resolution. Once the change is made, the Tweak Settings page will show the updated limit in the System section:
Step 4 – Version Control with Git
Before we can configure your Node.js application, the first step is getting that application on your server. There are a few options. You can use FTP to upload the copy of your application to your server, or you can use cPanel’s native Git support to clone the repository onto your server and deploy from there. Either method meets the requirement to get the application onto the server. In this example, we’ll be using cPanel’s native Git support to clone the application onto your server and then deploy it.
Within the cPanel user account that you’d like to host the application on, under the “Files” tab, select the “Git Version Control” option. This will bring you to a page where you can create a Git repository. In this example, we’ll be using the chat example repository to deploy a simple chat application.
To keep your application data secure be sure not to clone or upload into the public_html folder since this makes the code itself potentially accessible from the web. CloudLinux strives to keep your data safe, and during deployment, it’s not a requirement that your application data be publicly accessible.
Here’s an example of the Git page, before the clone is completed.
Once you click on “Create” this will bring you back to the repository page showing the full path of the application being deployed. This page is important, so save that path for later.
Step 5 – Application Configuration
With the application files in place on the server, we are ready to configure Node.js to launch that application from the web. From cPanel on the same account under the “Software” tab, select “Setup Node.js App”. On this setup screen, you’ll select “Create Application” to bring up the options for choosing the Node.js version as well as whether to use a Development or Production environment.
Example: In production, you may only see an error page, but in development, you may see a full stack trace on an application error, letting you know exactly where the error took place inside the code.
Here’s an example of an application that’s ready to be saved:
Node.js applications, in general, depend on several environment variables. These can be added by using the “Add Variable” button near the bottom right of the page. Your Node.js production or development variable is already set separately in the top section using the “Application Mode”, so it is not necessary to set it again in the Environment variables section.
This same menu is where you can select the version of Node.js that you’d like to use as well as the application’s startup file. This might be index.js, app.js or any number of variations; it depends on the application.
Once you’ve saved your application, you’ll need to resolve its package dependencies. The NPM package manager does this automatically based on the package.json file packaged with the application, so all that you’ll need to do here is click on the ‘NPM Install” button. (This step will require your domain to resolve to an IP address, or the installation script will have an error) . NPM will read the contents of the package.json file and install the needed packages into a virtual environment specific to the application.
If you are familiar with the command line, you can follow the instructions at the top of the page to gain access to the “npm” and “node” commands to make additional changes manually, allowing for a great deal of customization:
Step 6 – Application Deployment
By this point, you’ll have your application configured and Node.js modules installed, so you’re ready to launch the application! CloudLinux makes this effortless: on the same page where you configured your application, click “Run JS Script”. This will execute the application startup file that you defined earlier during setup. As Node.js applications have several options, this can bring up another menu with different options to select depending on the application that you’re running. In general, you’ll want to select the “Start” option next. Finally, you can select the “Open” option to visit your page, and see your application!
CloudLinux’s Node.js Selector has many advantages over manual deployment. It allows for several applications to be run under the same user, accessed under the “Setup Node.js App” page in your cPanel account. Phusion passenger is used for application handoffs, so you won’t need to set up any .htaccess redirects to tell the web server to forward to the applications port. With Node.js Selector this is all handled for you.
Node.js Selector and CloudLinux do not support Litespeed’s Web server, nor NGINX at this time. As always, if you’d like to have us configure and install this software on your Liquid Web server, please open a ticket or a chat with our Heroic support team!