Web Hosting

Resolving Directory "public_html" not allowed in cPanel for Node.js Applications

Share this!
Resolving Directory "public_html" not allowed in cPanel for Node.js Applications

When creating a new Node.js application in cPanel/WHM on CloudLinux or AlmaLinux 9, you might encounter an error message: Directory "public_html" not allowed. This error occurs when you attempt to set the Application root to the public_html directory, which is not allowed for Node.js applications.

Why the Error Occurs

The public_html directory is typically reserved for static website files, such as HTML, CSS, and JavaScript. In cPanel/WHM, certain restrictions are applied to this directory, preventing it from being used as the root for a Node.js application. This restriction helps ensure that the architecture remains secure and isolated between static and dynamic content.

How to Fix It

To resolve this issue, you will need to create a separate directory for your Node.js application outside of the public_html folder. Follow the steps below to properly set up your Node.js app.

Step 1: Create a New Directory for Your Node.js Application

  1. Log in to your cPanel.
  2. Navigate to File Manager.
  3. In your home directory (e.g., /home/username/), create a new folder where you will place your Node.js app. You can name it something like nodeapp or app.
  4. Move all your Node.js project files to this new directory (e.g., /home/username/nodeapp).

Step 2: Configure the Node.js Application in cPanel

  1. Go back to cPanel Node.js Application Manager.
  2. In the Application root field, enter the path to your new directory (e.g., /home/username/nodeapp).
  3. Ensure the Application URL points to the correct domain or subdomain.
  4. For the Application startup file, make sure to provide the correct entry point file, such as app.js or index.js, depending on your setup.
  5. Click Create to complete the setup.

Step 3: Set Up an Optional Reverse Proxy

If you want to serve your Node.js application under the root domain (e.g., whatmas.linkysoft.com), you can set up a reverse proxy using Apache.

  1. Navigate to File Manager > public_html and create a .htaccess file (if it doesn’t already exist).
  2. Add the following lines to configure a reverse proxy for your domain:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www\.)?whatmas\.linkysoft\.com$ [NC]
    RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]  # Replace 3000 with your Node.js app port
        
  3. Save the file and your Node.js app will now be served through Apache while running in the background on the specified port.

Conclusion

The public_html directory is restricted for certain types of applications, such as Node.js, to ensure proper separation between static and dynamic content. By creating a separate directory and configuring your Node.js application outside public_html, you can resolve the issue. Additionally, setting up a reverse proxy allows you to serve your Node.js app through your desired domain.

Read more excellent posts from this exact same topic.

Web Hosting for E-Commerce

E-commerce is one of the fastest-growing industries globally, with more consumers shopping online than ever before. As online businesses multiply, the importance of reliable and effective web hosting for e-commerce becomes increasingly critical. In this article, we’ll explore essential aspects of e-commerce hosting, offering solutions and recommendations to help you choose the best hosting service for your online store. We'll also delve into innovative hosting strategies, best practices, and

1 minute read

Forcing HTTPS Using the .htaccess File

Forcing HTTPS using the .htaccess file is a common method to ensure that all traffic to your website is encrypted. This guide will cover several methods to achieve this. The .htaccess file is an Apache configuration file that allows you to make changes to your web server's configuration on a per-directory basis.

1 minute read

Types of Domains and How to Choose the Right One for Your Website

In the current digital landscape, selecting the right domain name is crucial for the success of your online presence. Understanding the different types of domains and how to choose the appropriate one for your website is essential. This comprehensive guide will explore various aspects of website domains, focusing on the key phrase 'domain names,' to help you make an informed decision for your online project. We’ll delve into the history, types, significance, and strategies for choosing the

1 minute read