How To Deploy An App Node.js in cPanel

As JavaScript became one of the most popular programming languages to build web applications, many of our customers have come to love the language.

But to build scalable browser and server applications, they often want to use Node.js.

 

This rockstar open-source cross-platform environment allows for rapid development of applications, which leads to high productivity, and therefore quicker deployment.

Red Pro Host offers elastic web hosting which is often described by our customers as “Reliable hosting company“.

Our Node.js Selector allows you as a JavaScript developer to have web space and resources needed for your application while still minimizing application management headaches.

How to Install a Node.js Application Using Node.js Selector

Node.js Selector is a CloudLinux component that allows each user to easily create Node.js applications, choose Node.js version and other parameters for applications based on their needs.

With a friendly UI, it unites all of the features in one place and makes it very convenient to manage.

At the time of writing this, Node.js Selector supports Node.js versions 6.x, 8.x, 9.x, 10.x, 11.x and later.

The Node.js Selector has many advantages over manual deployment.

It allows for several different 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, all these handled for you.

To start, go to cPanel → Software Section → Select Node.js Version.

The Web Applications page will be displayed and you will see several columns in the list.

App URI: application URI including the domain.

  • App Root Directory: application root directory relative to the user’s home.
  • Mode: can be production or development.
  • Status: started/stopped — displays if an application is running or not and version of the application.
  • Actions: allows to start, restart, stop, edit, and remove a particular application.

Create A cPanel Account

Before you can access and use cPanel for your Node.js deployments, you must have a cPanel account.

To create a cPanel account, visit https://www.redprohost.com/panel/cart.php, and select a hosting package.

Our system will take you through the process which takes less than 5 minutes for you to get your cPanel logins.

How To Create A Node.js Application

Before you can configure your Node.js application, you will need to get that application to your cPanel server.

You can simply use SFTP to upload the copy of your application to your server.

Or you can also use cPanel’s native Git support to clone the repository onto your server and deploy from there.

The following example used cPanel’s native Git support to clone the application to the server and then deploy it.

Clone The Application

  • Login to cPanel.
  • On the Files pane, you see cPanel’s Terminal interface (cPanel – Home – Advanced – Terminal).
  • Click on it to see where you can create a Git repository.
  • Now go back to Github and clone with HTTPS using the web URL. 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. Our system strives to keep your data safe, and during deployment, it’s not a requirement that your application data be publicly accessible.

Cloning To A cPanel Server From A Private Github Repo

Private repos require SSH access, you must perform additional steps in order to clone a privately-hosted remote repository.

You can use cPanel’s Terminal interface (cPanel – Home – Advanced – Terminal) to access the command line from within the cPanel interface.

Generate An SSH Key

If you have not already configured one, run the following command to generate an SSH key:

ssh-keygen -t rsa -b 4096 -C "username@example.com"

In this example, “username” represents the cPanel account username and “example.com” represents the domain name.

After you run this command, the system will prompt you to enter a passphrase.

Do not enter a passphrase.

Press Enter to continue.

Verify That The Key Is Available

To confirm that the key exists and is in the correct location, run the following command:

cat ~/.ssh/id_rsa.pub

Register our SSH Key With The Private Repository Host

To register an SSH key with GitHub, perform the following steps:

  • Log in to your GitHub account.
  • Navigate to your private repository.
  • In the top right corner of the page, click Settings. A new page will appear.
  • In the left side menu, click Deploy keys. A new page will appear.
  • In the top right corner of the page, click Add deploy key. A new page will appear.
  • Enter your SSH key data:
  • In the Title text box, enter a display name for the key.
  • In the Key text box, paste the entire SSH key.

If you want to push code from your cPanel account to your GitHub account, select the “Allow write access” checkbox.

If you do not select this checkbox, you can only deploy changes from your GitHub repository to the cPanel-hosted repository.

  • Click Add key.

Do note that some repository hosts do not allow you to configure write access for your access keys.

For information about how to register your SSH key with another private repository host (Bitbucket, GitLab, etc), consult that host’s website or documentation.

Test Out The SSH Key

To test your SSH key, run the following command.

ssh -T git@example.com

where “example.com” represents the private repository’s host – e.g ssh -T git@github.com.
 

Clone The Repo To cPanel

To clone the repository, run the following command on the cPanel account, where “git clone git@example.com:$name/private-repo.git” represents the private repository’s clone URL:

git clone git@example.com:$name/private-repo.git

If you see “Error: The WebSocket handshake failed at …” when you access cPanel’s Terminal interface (cPanel – Home – Advanced – Terminal), recheck your connection.

If you are using VPN, disconnect and use your normal internet connection.

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 do save that path for later use.

To recap:

  • Select a Node.js version.
  • Select either “Development” or “Production” for the application mode.
  • Select the application root. This has to be a physical address to your application on a server that corresponds with its URI.
  • Select the Application URL. This is an HTTPS link to your application.
  • Fill the form to point to the Application startup file. You can also add additional Environment variables by clicking on the “Add Variable”.
  • Click Create.

… a more detailed explanation.
 
 

Node.js Application Configuration

With the application files in place on the server, you 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.

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.

A typical example would be where in production, you may only see an error page.

In development, you may see a full stack trace on an application error which allows you to figure out exactly where the error took place inside the code.

Since Node.js applications, in general, depend on several environment variables, you can add these using the “Add Variable” button near the bottom right of the page.

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

At this point, you will require your domain to resolve to an IP address, or the installation script will have an error.

If you are using a sub-domain, make sure that this has been created and that you have added all relevant records to your DNS.

You can confirm this by simply running:

$ dig sub-domain.com

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.
 

 
Node.js Application Deployment

By this point, you’ll have your application configured and Node.js modules installed, so you’re ready to launch the application.

Our cPanel systems make this very easy.

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 different 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!

How To Start A Node.js Application

To start a stopped application do the following:

Click Start icon in the Actions column in a stopped application row.

When the action is completed, a Start icon changes to Stop icon.

How To Stop A Node.js Application

To stop a started application do the following:

Click Stop icon in the Actions column in a started application row.

When the action is completed, a Stop icon changes to Start icon.

 
How To Start A Node.js Application

To restart the application do the following:

Click Restart icon in the Actions column in a started application row.

The current row will be blocked and will be unblocked when the process is completed.

 
How To Remove A Node.js Application

To remove the application do the following:

click the “Bin” icon in the Actions column in a particular application row.
in the confirmation, pop-up click Agree to start removing or Cancel to close the pop-up.

When the action is completed, an application will be removed from the Web Applications table and a confirmation pop-up displayed.
 
 

How To Edit Your Node.js Application

To edit application do the following:

Click Pencil icon in the Actions column in a particular application row.

An application tab will be open.

At the moment, you can:

restart application – click Restart button.

  • stop Node.js — click Stop Node.js button.
  • run JavaScript script — click Run JS Script button to run a command specified in the Scripts section of the package.json file. Specify the name of the script to run plus any parameters then click Ok.
  • remove application — click Delete button and confirm the action in a pop-up.
  • change Node.js version — choose Node.js version from a drop-down.
  • change Application mode — choose application mode from a drop-down. Available modes are Production and Development. 
    specify Application root — specify in a field a physical address to the application on a server that corresponds with its URI.
  • specify Application URL — specify in a field an HTTP/HTTPS link to the application.
  • specify Application startup file — specify as NAME.js file.
  • run npm install command — click Run npm install button to install the package(s) described in the package.json file.
  • add Environment variables — click Add Variable and specify a name and a value.

 
 
How To Debug Errors On Your Node.js Application

Directives such as PassengerFriendlyErrorPages and PassengerAppEnv are available for use from a .htaccess file.

This allows cPanel users to debug a Node.js application during development.

For example, if you add one of the following lines to the .htaccess file on the application page and there is an error, you will see the error listed:

PassengerAppEnv development

or

PassengerFriendlyErrorPages on
  • nodejs
  • 6 Users Found This Useful
Was this answer helpful?