Here's how you can configure both PHP and Node.js apps together using Nginx on the same server:

  1. Install PHP and Node.js on the server if they are not already installed.

  2. Configure your PHP app to listen on a specific port, for example, 127.0.0.1:9000.

  3. Configure your Node.js app to listen on a different port, for example, 127.0.0.1:3000.

  4. Create two separate server blocks in your Nginx configuration file. The first server block will proxy requests to the PHP app, and the second server block will proxy requests to the Node.js app.

  5. Add the following configuration to the first server block to proxy requests to the PHP app:

perl
server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:9000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
  1. Add the following configuration to the second server block to proxy requests to the Node.js app:
perl
server { listen 80; server_name node.example.com; location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
  1. Reload Nginx to apply the new configuration.
sudo service nginx reload

With these steps, Nginx will route incoming requests to the appropriate app based on the server name specified in the URL. Requests to example.com will be routed to the PHP app listening on 127.0.0.1:9000, and requests to node.example.com will be routed to the Node.js app listening on 127.0.0.1:3000.

Have questions or queries?
Get in Touch