Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put nodejs and apache in the same port 80

I have to put nodejs in port 80, but apache is already using it. How can I put both (nodejs and apache) on the same port 80? I need it because in my university all the ports are blocked except for PORT 80. (This is a realtime application with nodejs and socket.io (websockets) and in the other side a php application). Thanks a lot

like image 651
user1203334 Avatar asked Jun 23 '12 19:06

user1203334


People also ask

Can I run NodeJS on port 80?

If you want to run NodeJS on different port, change 80 to your required port number (e.g 8080). Also the function defined in createServer() will be executed whenever someone sends a request to your NodeJS server.

Can you use node js with Apache?

Set up a Node. js app for a website with Apache on Ubuntu 16.04. Node. js is a JavaScript runtime environment which lets you easily build server-side applications.


2 Answers

I do this via node.js proxy..

Install http-proxy with npm or official page

Example:

var http = require('http'), httpProxy = require('http-proxy'), proxyServer = httpProxy.createServer ({     hostnameOnly: true,     router: {         'domain.com':       '127.0.0.1:81',         'domain.co.uk':     '127.0.0.1:82',         '127.0.0.1':        '127.0.0.1:83'     } });  proxyServer.listen(80); 

This creates a node process listening to port 80, and forwarding requests for domains which go to :81,82,83 etc. I recommend running this with forever and adding an entry to init.d so your proxy is up in case system shuts down.

like image 163
Matej Avatar answered Oct 10 '22 20:10

Matej


You can also use Apache 2's mod_proxy and mod_proxy_http, which might be more reliable or perform better depending on your system.

Here's an example:

Firstly run below command to proxy to allow

sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_balancer sudo a2enmod proxy_balancer sudo a2enmod lbmethod_byrequests  # Use Apache for requests to http://example.com/ # but use Node.js for requests to http://example.com/node/ <VirtualHost *:80>     ServerName example.com     DocumentRoot /var/www/example/     <Location /node>         ProxyPass http://127.0.0.1:8124/         ProxyPassReverse http://127.0.0.1:8124/     </Location> </VirtualHost> 

And of course you can modify the directives to your needs, such as using a different port for your virtual host (e.g., 443), different port for Node.js, or set up the proxy under a different block, such as for a subdomain (e.g., node.example.com).

like image 32
Synexis Avatar answered Oct 10 '22 21:10

Synexis