Run PHP and Nodejs app together in a cPanel account

Gamecock

Registered
Dec 22, 2019
2
0
1
Moscow
cPanel Access Level
Root Administrator
I have installed Nodejs on my cPanel Cloudlinux server.

I created a Nodejs app (Nuxt.js) for my website frotend via "Software > Setup Node.js App" in my cPanel account and linked it to my website root (example.com/).
Now I want to create an API PHP service (Laravel) for my website backend and place it in my "example.com/api" directory which it returns JSON from this address.
But when I visit this url, it returns my Nodejs app page which I created before for website root.

In fact, I want the "publci_html/api" folder to be exclude from rendering via Nodejs server.

Is it possible?
Or should I commit some changes in ".htaccess" file?

Regards
 

cPSamuelM

Technical Analyst Team Lead
Staff member
Nov 20, 2019
196
38
103
USA
cPanel Access Level
Root Administrator
Hello @Gamecock,

Thank you for contacting cPanel!

Did you set up your Node.js application following the instructions on the following page?


If so, you likely used the Application Manager to register the application:


The Application Manager uses Phusion Passenger to reverse proxy requests to your site on port 80 to the port the application is listening on. For this reason, it would likely be simpler to install your Node.js app to a subdirectory or a standalone domain.


With that said, after a bit of researching this particular scenario, I found that it is indeed possible to override the Passenger configuration for subdirectories:


To modify the VirtualHost entry for your domain as described in the StackOverflow thread above, you would need to use an Include file. We have instructions in our documentation for how to accomplish that:


Please let us know if you have any questions.

Best regards
 
  • Like
Reactions: Gamecock

Gamecock

Registered
Dec 22, 2019
2
0
1
Moscow
cPanel Access Level
Root Administrator
To modify the VirtualHost entry for your domain as described in the StackOverflow thread above, you would need to use an Include file. We have instructions in our documentation for how to accomplish that:
I created a subdomain called api.
Assume that our cPanel username is username.
I created includename.conf file in /etc/apache2/conf.d/userdata/std/2_4/username/api.example.com pasted this code there:

Apache config:
<Directory /home/username/public_html/api>
    PassengerEnabled off
    AllowOverride all
</Directory>

and saved changes and restart apache with /usr/local/cpanel/scripts/rebuildhttpdconf and /usr/local/cpanel/scripts/restartsrv_httpd commands. But nothing happened and the api.example.com url still shows the NodeJS pp content which is defined for example.com/

I also edited the codes like this includename.conf file:
Apache config:
    PassengerEnabled off
But again no changes appeared after save and restart.

I also tried to write PassengerEnabled off statement in .htaccess file which is placed in /home/username/public_html/api but no success.

Note that I use Litespeed webserver, not Apache. Does it require different configurations?

Thanks
 

cPSamuelM

Technical Analyst Team Lead
Staff member
Nov 20, 2019
196
38
103
USA
cPanel Access Level
Root Administrator
Hello again @Gamecock,

Thank you for your detailed reply. I found that the same answer from the StackOverflow page I shared with you is actually found in the Passenger documentation as well:


With that said, I think their documentation is a bit outdated. I found that the PassengerEnabled off directive needs to be added in a Location section as opposed to Directory section.

You can find the include file I created on my test server when testing this scenario below. I installed a Node app to /home/$user/public_html/nodejsapp/ and a plain PHP script to /home/$user/public_html/nodejsapp/test/. After creating the Include file below, then rebuilding and restarting Apache, the page at domain.com/nodejsapp/test/ loaded without any issues.

Code:
# cat /etc/apache2/conf.d/userdata/std/2_4/$user/$domain/test.conf
<Location "/nodejsapp/test">
    PassengerEnabled off
    AllowOverride all
</Location>
<Directory "/home/$user/public_html/nodejsapp/test">
    DirectoryIndex index.php
</Directory>
Note, in the Location section I used a relative path. Additionally, I added a DirectoryIndex directive, but this was not necessary.

There are no special steps that need to be taken for compatibility with Litespeed. Please let us know if you have any additional questions.

Best regards
 
  • Like
Reactions: Gamecock