Web Hosting cPanel

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.

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

How to Restore Default Page Settings in cPanel

Restoring default page settings in cPanel is a useful action if you've made changes to the interface or settings that you wish to undo. This guide will provide a step-by-step tutorial on how to reset page settings to their default state in cPanel, ensuring that you can quickly return to the original configuration without any hassle.

1 minute read

How to Update Your Contact Information in cPanel

Keeping your contact information up to date in cPanel is essential for ensuring you receive important notifications and updates related to your hosting account. This guide will walk you through the steps to update your contact information in cPanel, making it easy for you to manage your account settings effectively.

1 minute read