[英]Running Node app via PM2 on port 80
I have an express that I want to run on port 80. --> app.listen(80);
我有一个要在端口 80 上运行的
app.listen(80);
> app.listen(80);
I'm using PM2
to manage the app (restarting, stopping, monitoring, etc.) .我正在使用
PM2
来管理应用程序(重新启动、停止、监控等)。 I have a deployment shell script whose last command is PM2 restart index
.我有一个部署 shell 脚本,它的最后一个命令是
PM2 restart index
。 From the console output, I see no errors and PM2
reports that it successfully completed the command.从控制台输出中,我没有看到任何错误,并且
PM2
报告它成功完成了命令。 Yet when I got to my.ec2.ip.address:80
the site is not up.然而,当我到达
my.ec2.ip.address:80
该站点没有启动。 Furthermore, if I run node index.js
in my server project directory, I get a Error: listen EACCES 0.0.0.0:80
.此外,如果我在我的服务器项目目录中运行
node index.js
,我会收到一个Error: listen EACCES 0.0.0.0:80
。 This makes some sense to me as port 80 is below 1024 and therefore a privileged port.这对我来说很有意义,因为端口 80 低于 1024,因此是一个特权端口。
sudo node index.js
will allow the launch to work. sudo node index.js
将允许启动工作。
I'm a newbie to unix, servers, permissions, and deployment, so in addition to the solution, an explanation of the fundamental concepts contributing to my problem would be greatly appreciated.我是 unix、服务器、权限和部署的新手,因此除了解决方案之外,对导致我的问题的基本概念的解释将不胜感激。 For instance.. is it bad to simply run my node app as super-user?
例如.. 简单地以超级用户身份运行我的节点应用程序是不是很糟糕? Is it good practice to run
PM2
(therefore possibly running node as..?) root/super-user?运行
PM2
(因此可能以 .. 身份运行节点?)root/超级用户是一种好习惯吗? The command sudo PM2 restart index
leads to sudo: pm2: command not found
.命令
sudo PM2 restart index
导致sudo: pm2: command not found
。 Why is PM2
not found when running sudo PM2
.. if PM2
is in my path?为什么
PM2
运行时,没有发现sudo PM2
..如果PM2
是在我的道路?
Ultimately though, when using PM2
how can I ensure that my server runs on port 80?最终,当使用
PM2
如何确保我的服务器在端口 80 上运行? not found.未找到。
不要使用端口 80,在其他端口(如 8080)上运行并使用此命令将 80 重定向到该端口
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
It's good to run as little as possible as a priviliged user, as you want to restrict the potential damage in case someone exploits your program.以特权用户身份尽可能少地运行是件好事,因为您希望在有人利用您的程序时限制潜在的损害。 You don't want to run your Node code as root unless you absolutely have to.
除非绝对必要,否则您不想以 root 身份运行 Node 代码。
Therefore, it's better to run your Node program on an unprivileged port (say, port 8000), and instead have a lightweight web server such as Nginx listen on port 80 and simply forward traffic to your Node program.因此,最好在非特权端口(例如端口 8000)上运行您的 Node 程序,而使用轻量级 Web 服务器(例如 Nginx)侦听端口 80 并将流量转发到您的 Node 程序。
If you want to go with Nginx, you can use this configuration to do exactly what I described above, and then just listen with your Node program on port 3000:如果你想使用 Nginx,你可以使用这个配置来完成我上面描述的事情,然后在端口 3000 上监听你的 Node 程序:
server {
listen 80 default;
listen [::]:80 default;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
}
}
Though, you may have solved the issue but for the one who comes here facing the same issue, this worked me :虽然,您可能已经解决了这个问题,但对于来到这里面临同样问题的人来说,这对我有用:
For just troubleshooting, run your app using sudo npm start
.为了排除故障,请使用
sudo npm start
运行您的应用sudo npm start
。 If your app runs normally then you need to bind port 80
with the help of authbind
package.如果您的应用程序运行正常,那么您需要在
authbind
包的帮助下绑定端口80
。 Run these commands :运行这些命令:
sudo apt-get install authbind
sudo touch /etc/authbind/byport/80
sudo chown %user% /etc/authbind/byport/80
sudo chmod 755 /etc/authbind/byport/80
Replace %user%
with the user you run pm2.将
%user%
替换为您运行 pm2 的用户。 Mine was ubuntu
by default.我的默认是
ubuntu
。
Set start
command in your package.json
file to pm2 start <server_file_name>
.将
package.json
文件中的start
命令设置为pm2 start <server_file_name>
。 Run the app using npm start
.使用
npm start
运行应用npm start
。 It should work !它应该工作!
After lot of time spent configuring nginx, finally uninstall it and followed AJ suggestion to configure iptables .花了很多时间配置nginx,终于卸载了,按照AJ的建议配置iptables 。 Thank you AJ
谢谢阿杰
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
But, if anyone know a perfect tutorial to configure nginx, would be a great help.但是,如果有人知道配置 nginx 的完美教程,那将是一个很大的帮助。
I had the same issue, for the ubuntu server.对于 ubuntu 服务器,我遇到了同样的问题。 Fixed with the tutorial below.
用下面的教程修复。
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node
https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps
Also here is another solution from PM2另外这里是 PM2 的另一个解决方案
sudo apt-get install authbind
sudo touch /etc/authbind/byport/80
sudo chown %user% /etc/authbind/byport/80
sudo chmod 755 /etc/authbind/byport/80
https://pm2.keymetrics.io/docs/usage/specifics/#listening-on-port-80-wo-root https://pm2.keymetrics.io/docs/usage/specifics/#listening-on-port-80-wo-root
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.