简体   繁体   中英

How to configure my node.js application on a dedicated server to put it online?

I've installed node.js on my server(it is a virtual windows server). I am also having the domain. I want to run my node.js application on a port 8001 so that when I open, say http://example.com:8001/ it will open my application.

Actually, I am also having a PHP site running on Apache server on port 80(XAMPP). It is perfectly working fine when I open say, http://example.com .

Thanks

In apache, create a new vhost. You have to proxy all requests through apache to your node app as apache is listening to port 80.

<VirtualHost *:80>
    ServerName example.com

    ProxyRequests off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    <Location />
        ProxyPass http://localhost:8001/
        ProxyPassReverse http://localhost:8001/
    </Location>
</VirtualHost>

If you are not on a name-based virtual host environment you can just start your Node.js server and it will be available to the world from the defined port. But if the server IP address runs many services using different domain names and you want your Node.js server to reply only from http://example.com:8001 you can use eg the vhost module on your node server to listen to specific domain only:

var express = require('express'),
    host = require('vhost');

var app = express();
app.get('/', function(req, res) {
  res.send("Hello from vhost");
});

var main = express();
main.use(vhost('example.com', app));

if (!module.parent) {
  main.listen(8001);
}

You can also proxy the requests through a web server like Apache or nginx, but to have the service responding from port 8001 you would need to bind the web server to port 8001 in addition to 80 you are already using, and run your Node.js server on some other port.

Generally people prefer using the standard HTTP port and use a web server to reverse proxy traffic to the Node.js server running on a non-privileged port. As you already have the PHP application on your domain name you would then follow advice by @SamT and run your Node.js application from eg http://mynodeapp.example.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM