How to create a Node.js web server with cPanel
Tags: webdev, beginners, tutorial, cpanel
Published: 2024-06-26 03:39:53

Welcome to this article, where I guide you on creating your own Node.js Express web server with [cPanel](https://cpanel.net/). This guide will apply for most domain registrars which offer hosting services with cPanel, such as [Namecheap](https://www.namecheap.com/hosting/shared/) or [Hostgator](https://www.hostgator.com/web-hosting), but for this tutorial, we will be using [Spaceship](https://www.spaceship.com/hosting/shared/) Shared Hosting.

> Spaceship is a domain registrar owned and operated by Namecheap which offers competitive domain pricing for most top TLDs. They offer a variety of different services for websites, such as website hosting and emails.

Without further ado, let's get started!

Before we continue, make sure you have a domain that is already added to your cPanel. If you already have this setup, go ahead and navigate to **Software** tab. Click the **Setup Node.js App** to start the process.

![Software tab in cPanel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k3zy71oy4669oxfnyg1z.png)

Once you're on the Node.js page, click **Create Application** and fill out the following details:
- **Node.js Version**: Select a desired Node.js version for your web server. It's highly recommended that you select a higher Node.js version to ensure that Node.js is supported by npm and isn't outdated.
- **Application Mode**: Select the option applicable in your case. If you are unsure of which option to select, select the <u>Production</u> option.
- **Application Root**: This is the folder name that will contain all of the files for your Node.js App. Make sure the name isn’t the same as your domain, because all of the files will be served (yes, your code could be leaked!).
- **Application URL**: In the dropdown, select the domain that will be used for your Node.js App.
- **Application Startup File**: Enter the name of the main Javascript file, which will run whenever you start the Node.js App. For this tutorial, we will use <u>index.js</u>.
(if you'd like to add any environment variables, you can add them now)


![Node.js Configuration Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6oigyjp428ibqeri8ebn.png)

Once you fill out the details above, click the **Create** button to finish the process (be patient!).

After your Node.js App has been created, go back to your cPanel and locate the **Files** tab. Then, click the **File Manager** option to launch the file manager.

![Files tab in cPanel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywnxzl8l9bk1b7q717bl.png)

Once you're in the file manager, click the name of the folder that we created earlier for our Node.js App. When you open the folder, you may already see an <u>index.js</u> file located inside. Click the file, and then click **Edit** from the top bar to open the file.

In the index.js file, let's create some code for our Express server. Remove the old file contents, and enter the code below:

```js
const express = require('express');
const http = require('http');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, world!');
});


const server = http.createServer(app);
server.listen();
```
Click the **Save Changes** button on the top-right of the screen, and go back to the file manager tab.

Next, we'll need to create a <u>package.json</u> file which will contain our dependencies. This is crucial for our Node.js App to run properly, and will allow us to install npm later on.

Click the **+ File** option located on the left side of the top bar, and name the file <u>package.json</u>. Then, click **Create New File** to confirm your changes.

Open up the <u>package.json</u> file in the editor as we did previously, and insert the following JSON code:
```json
{
  "name": "example-express-server",
  "version": "1.0.0",
  "description": "A simple Node.js Express web server.",
  "main": "index.js",
  "dependencies": {
    "express": "*"
  }
}
```
Click the **Save Changes** button to save the contents, and go back to the Node.js dashboard in cPanel.

Once you're back in the Node.js App dashboard, click the pencil icon (**Edit your application**) to open up your app. Scroll down and click the **Run NPM Install** button to install the dependencies in our <u>package.json</u> file.
![Configuration file tab in cPanel Node.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6uo3084ytw2oazpuq4yg.png)

> In rare cases, you may receive an error like this:
> `An error occured during installation of modules. The operation was performed, but check availability of application has failed. Web application responds, but its return code "None" or content type before operation "text/html" doesn't equal to contet type after operation "text/html; charset=utf-8".`
> If you receive this error, don't worry! The process has still been completed successfully, and you may continue with the guide.

Once the process has finished, click the **Restart** button to start your Node.js App with the new updates. When your Node.js App restarts, open a new tab and visit your domain.

![Privacy error on Chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f3gg48si7oho60pmlj1v.png)

Oh no! When visiting your website, your browser may warn you that the connection is unsafe. In order to solve this issue, we'll need to generate valid SSL certificates! Don't worry, it isn't as complicated as it sounds.

Visit the cPanel dashboard, and navigate to the **Security** tab. Click the **SSL/TLS Status** option, and then locate your domain in the list provided.

![Security tab in cPanel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mwadjmzo834k154cc4ae.png)

Once you find your domain, click the **+ Include During AutoSSL** option. Make sure to do this on the `www` subdomain of your site as well. Lastly, click the **Run AutoSSL** option to create an SSL certificate with Let's Encrypt. This process may take a few minutes, so be patient!

![AutoSSL in cPanel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xn9zm3rb566pyi2e9mkp.png)

Once the SSL certificate process is completed, your cPanel page will refresh automatically. You will then see a green lock next to your domain name, with the label **AutoSSL Domain Validated**.

Lastly, go back to your website and refresh the page. The page will automatically redirect to HTTPS, and you will now see that your website is secure.


Congratulations! In this guide, you learned how to set up your own Node.js web server with Express using cPanel, and you set up SSL certificates to secure your website. If you found this guide useful, or if you have any questions, let me know in the comments below. Bye for now! 👋