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