Running PHP and NodeJS Apps together

I want to run a nodejs app and blog in wordpress on the same server, how do i do that?

We can run both the apps by configuring the virtual host files. You can find the settings i used for Apache and Nginx below. Make sure you have both of your apps ready by going over the prerequisites. In my case the wordpress blog is running in PHP while the rest of the site is running on NodeJS.

Apache

Prerequisites

  • Apache is installed

  • Proxy module for Apache is installed

  • PHP is installed

  • NodeJS app is running by itself on port 3000

  • PHP is installed

<Virtualhost>
  ServerName craftypixels.com
  ProxyPreserveHost on 
  ProxyPass /blog ! 
  ProxyPass / http://localhost:3000/ 
  ProxyPassReverse / http://localhost:3000/ 
  alias /blog /var/www/craftypixels/blog

  DocumentRoot /var/www/craftypixels/blog/ 
  Options -Indexes
</Virtualhost>
Detailed Documentation

Nginx

Prerequisites

  • Nginx  1.11.* is installed

  • PHP is installed and socket file is located at /php7.0-fpm.sock

  • PHP app is placed at /var/www/craftypixels/blog/

  • NodeJS app is running by itself on port 3000

server {
    listen 80;
    server_name craftypixels.com;
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

location @blog {
  rewrite ^/blog(.*) /blog/index.php?q=$1;
}
location /blog {
    index index.php;
    try_files $uri $uri/ @blog;
    alias /var/www/craftypixels/blog;

     location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

} }
Detailed Documentation
2017-04-20T06:44:06+00:00

One Comment

  1. Faizan March 7, 2017 at 7:50 pm - Reply

    Helpful guide, something to keep in my notes too.

Leave A Comment