Get Web Hosting

Web Hosting

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.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Search the Knowledge Base

Share