请原谅我的不好意思。我在Ubuntu 14.04上通过AngularJS和NodeJS建立了一个简单的网站应用程序。启动我的服务器后,IP地址不会加载并引发连接失败错误。
登录我的SSH服务器后,这些是我从终端到终端的步骤:
ubuntu@ip-172-31-25-241:~$ sudo mkdir src
ubuntu@ip-172-31-25-241:~$ cd src
ubuntu@ip-172-31-25-241:~/src$ sudo git init (after installing git)
ubuntu@ip-172-31-25-241:~/src$ sudo git pull origin master (pulled repo)
ubuntu@ip-172-31-25-241:~/src$ sudo apt-get install npm
ubuntu@ip-172-31-25-241:~/src$ sudo apt-get install nodejs
ubuntu@ip-172-31-25-241:~/src$ cd API
ubuntu@ip-172-31-25-241:~/src/API$ ls
server.js
ubuntu@ip-172-31-25-241:~/src/API$ sudo nodejs server.js
Server Started on http://localhost:8080
Press CTRL + C to stop server
这是@joKeRxbLaCk要求的我的server.js文件中的代码:
// initialize Express
var express = require('express');
// run Express
var app = express();
app.use(express.static(__dirname + './../APP'));
app.get('/', function(req, res) {
res.send('Express is running!');
});
app.listen(80, function() {
console.log('Server Started on http://localhost:8080');
console.log('Press CTRL + C to stop server');
});
当我尝试命令“nodejs server.js”而不是“sudo nodejs server.js”时,我收到此错误:
ubuntu@ip-172-31-25-241:~/src/API$ nodejs server.js
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at EventEmitter.listen (/home/ubuntu/src/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/home/ubuntu/src/API/server.js:15:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
我希望服务器不会抛出错误并为我的网站加载:(请帮助!我知道我错过了一步但无法弄清楚在哪里。
您正尝试在端口80上运行应用程序。在Linux上,端口<1024是特权端口,需要提升权限才能运行。
因此,您的应用程序将以sudo身份运行,因为您使用提升的权限运行。没有sudo,您无权使用端口80。
因此,您需要确保运行您的应用程序的用户具有正确的权限,或者使用在端口80上运行的Apache或nginx等应用程序,并在非特权端口(例如8080)上转发到您的应用程序。
在AWS上,您必须手动打开要使用的任何端口。 这是Open Port on AWS Ec2的指南