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
- Log in to your cPanel.
- Navigate to File Manager.
- 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.
- 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
- Go back to cPanel Node.js Application Manager.
- In the Application root field, enter the path to your new directory (e.g., /home/username/nodeapp).
- Ensure the Application URL points to the correct domain or subdomain.
- 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.
- 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.
- Navigate to File Manager > public_html and create a .htaccess file (if it doesn’t already exist).
- 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
- 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.