Hosting NodeJS and MySQL Application using cPanel

In this blog, we will learn how to host a Node.js and MySQL application using cPanel. cPanel provides an easy-to-use interface that allows you to deploy and manage your Node.js applications seamlessly.

Creating NodeJS Application
Login to cPanel using provided credentials. After logging in, search for “Setup Node.js App”.

Setup Nodejs.App

Choose the required Node.js version and fill in other details as shown below. Your application operates from the “Application root” folder.

Your Node.js application is ready. To check if it’s working, click the “OPEN” link.

Database Setup
Before diving into the application, we need to create the database and ensure that the database user has the appropriate privileges.

Scroll down to create a database user.

After creating a user, go back and scroll down to a section where it says “Add User to Database”

Select the privileges that you want to grant to “nodejs_user” user and click the “Make changes” button.

We need to create tables next. For that search phpMyAdmin.

To execute queries, click on “nodejs_db” and then select “SQL”.

Paste and click the “Go” button to run the query.

CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`position` varchar(255) NOT NULL,
`is_deleted` tinyint(3) UNSIGNED DEFAULT 0,
`created_at` datetime NOT NULL,
`updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`)
);

Adding code to Application using File Manager
Note: It’s assumed that you already have your application ready.

Folder Structure ( home/username i.e. /home/nodejsapp )

Make sure that you are working on the same directory as mentioned as application root.

 

Uploading code to root directory
Click “Upload” button

Drag your compressed code file here.

Go to file manager and click “Extract” to extract your code

After file extraction, the folder structure looks like the figure below.

Before dependency installation, we need to add database details to the database config file.

Dependency Installation

Scroll down until you find “Run NPM Install” and then click it.

Restart the application.

To access the application, scroll down until you locate the “Application URL” and click on the provided “OPEN” link.

Congratulations!!! You successfully deployed your NodeJS application on cPanel.

Drop Your Comments Below