0

I have a node.js site running on port 8080 of my Linux server. I also have, on the same sarver, a PHP site (bugzilla) running on port 80. So my current configuration is:

I want to have both sites running on port 80 like this:

Is that possible?

  • 1
    You can't have both applications listening on the same port. But I think what you're probably looking for is a 'reverse proxy'. – Panki Jun 5 at 7:30
1

First of all, disable both services on your machine. then change their ports into something unusual and restart them, for example:

  • PHP application on 6821

  • NodeJS application on 6822

Please remember to open them on 127.0.0.1 ! you shouldn't open them on your network interface.


Install nginx as the most used Reverse Proxy. Depends on your distribution you can install it with one of these commands:

For Debian based distributions:

$ sudo apt install nginx

For Arch based distributions:

$ sudo pacman -S nginx

Completely depends on your distro again, this package could have its own files hierarchical. so I will give you just nginx.conf as the main config file. Just remember to copy original config file with any name you prefer, something like nginx.conf.origin

$ sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.origin

Now, open /etc/nginx/nginx.conf, remove existing configs and copy these configurations inside it:

user nginx;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    keepalive_timeout  15;

    server {
        listen       80 default_server;
        server_name  example.com; 

        location /mynodejs {
            proxy_pass http://127.0.0.1:6822;
        }

        location /bugzilla {
            proxy_pass http://127.0.0.1:6821;
        }
    }
}

Notice: Please consider that I wrote this config file with my mind, and because of that you probably will face with an error when you want to run nginx. If so, please write a comment here, so I will test it.

Now, Just restart your nginx! You will get what you want.

1

The php app can stay where it is unmodified.

When using apache as the webserver, the proxy modules need to be enabled. On ubuntu for example

a2enmod proxy
a2enmod proxy_http

And modify your virtualhost with the ProxyPass and ProxyPassReverse lines.

<VirtualHost *:80>
        ServerName www.example.com
        DocumentRoot "/var/www/www.example.com/htdocs"
        #nodejs app
        ProxyPass /mynodejs http://127.0.0.1:8080/
        ProxyPassReverse /mynodejs http://127.0.0.1:8080/
<Directory "/var/www/www.example.com/htdocs"> 
        Options -Indexes +FollowSymLinks
        AllowOverride All 
        Order allow,deny 
        Allow from all 
</Directory>
        ErrorLog "|/usr/bin/cronolog /var/www/www.example.com/logs/%Y-%m-%d/error_log" 
        CustomLog "|/usr/bin/cronolog /var/www/www.example.com/logs/%Y-%m-%d/access_log" combined 
</VirtualHost>

Restart apache /etc/init.d/apache2 restart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.