简体   繁体   中英

How to create nodejs server with php?

I'm trying to execute nodejs with php, do this it's really simple

<? shell_exec("node app.js &"); ?>

My nodejs file creates a server with express.

var express = require("express"),
app         = express();

app.get('/', function(request,response)
{
  response.status(200).send("Welcome");
});

app.listen(82, 'localhost', function(){console.log("Server are running on port 82");});

When the file runs this is the output

Error: listen EACCES 127.0.0.1:82

at Object.exports._errnoException (util.js:873:11)

at exports._exceptionWithHostPort (util.js:896:20)

at Server._listen2 (net.js:1237:19)

at listen (net.js:1286:10)

at net.js:1395:9

at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:64:16)

at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:83:10)

The port is open, this is the output for "service iptables status" command

20 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpts:81:90

I think the error is because apache (apache runs on port 80) don't have permission to create a server

Someone have any idea of how solve this?

You will either need to grant the process that starts the node.js app higher privileges so it can use such a low port number or you can use a higher port number (higher than 1024) that is not so restricted. For example, port 3000 is common.

Some people also work-around this by starting their server on a higher port (like 3000) and then using iptables to forward incoming requests from a low port (like the 82 you were using) to port 3000. This allows your server to run without needing the higher privileges, but it can still "work" on the lower port number.

Here's more discussion of work-arounds: Is there a way for non-root processes to bind to "privileged" ports on Linux? and How can I run a server on Linux on port 80 as a normal user? .

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