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.

git clone example

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.

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.Notes on Choosing Development vs. Production:Development generally has different application hooks in the code of the application, depending on how this environment variable is set. As a general rule, Production is going to have more levels of caching, as well as minimal logging. So, for a Development environment set, you can expect the inverse, with fewer levels of application caching, and more verbose logging and error message output.

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:

create node.js application

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.jsapp.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: 

enter virtual environment command

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.