在 Linux mint 上部署节点应用程序的最简单方法是什么?



我有一个node应用程序,它在VS Code中运行得很好,我可以从终端窗口使用script命令启动它。

这只是一个实用程序,我想在我的开发机器的后台运行。它监视按键并将我的忙/空闲指示器设置为其他应用程序。如果你好奇的话,它会使用iohook

我如何部署它只是在后台运行(所有的时间,包括启动时)?我打算把它部署为一个web服务器,这样我就不必和linux服务搞混了。

我已经安装了apache和nginx和所有其他web服务器的东西,从我做过的许多教程,但我不知道如何部署到它们。

我试过了:

https://plainenglish.io/blog/deploying-a-localhost-server-with-node-js-and-express-js

,但它只能从vs code或命令行启动,它不是一个真正的"服务器";它可以一直运行,不需要终端窗口,并且在系统启动时启动。

我需要从apache或nginx或类似的东西运行。

https://www.ionos.com/digitalguide/websites/web-development/nodejs-for-a-website-with-apache-on-ubuntu/

我试着:

要从web访问Node.js脚本,使用命令安装Apache模块proxy和proxy_http:

sudo a2enmod proxy
sudo a2enmod proxy_http

安装完成后,重新启动Apache以使更改生效:

sudo service apache2 restart

[Linux Mint]在本地机器上运行web应用程序

的第一个网站
  1. [Terminal] Install node.js:sudo apt-get install nodejs
  2. [Terminal] Install apache:sudo apt-get install apache2
  3. [终端]安装PM:sudo npm install -g pm2
  4. [终端]启动apache:sudo systemctl status apache2
  5. [Browser] test apache:localhost
  6. [终端]到你的web根目录。它可以在任何你喜欢的地方:cd /home/homer-simpson/websites
  7. [Nemo][root]创建app文件夹:/home/homer-simpson/websites/hello-app
  8. [Nemo][root]创建node.js hello world文件:/var/www/html/hello-app/hello.js
  9. [终端]使hello.js可执行:sudo chmod 755 hello.js
  10. [xed][root]在此文件中创建node.js应用程序:参见hello.js清单
  11. [终端]从终端运行作为测试:node hello.js
  12. [浏览器]测试网站:http://localhost:4567/
  13. 关闭节点应用CTRL+C
  14. 在PM中启动应用程序:sudo pm2 start hello.js
  15. [Terminal][root]添加PM到启动脚本:sudo pm2 startup systemd
  16. [Terminal][root] Save PM apps:pm2 save
  17. [终端]启用apache模块:sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart
  18. [Nemo][root]以root身份打开apache config:/etc/apache2/sites-available/000-default.conf
  19. [xed][root]为您的网站添加/替换配置:参见000-default.conf清单
  20. [Terminal] restart apache:sudo systemctl restart apache2
  21. [浏览器]测试网站:http://localhost/hello-app
  22. Hello World !

  23. 后续网站:

  1. [Nemo][root]在你的网站根目录下创建一个新的app文件夹:/home/homer-simpson/websites/another-app
  2. [Nemo][root]将脚本复制到这里并使其可执行
  3. [终端]启动应用程序在PM:sudo pm2 start another-app.js
  4. [Terminal][root] Save PM config:pm2 save
  5. [终端][根]添加新的网站到apache配置下的一个新的Location标签与新的应用程序的端口号(必须是唯一的):sudo xed /etc/apache2/sites-available/000-default.conf
  6. [Terminal] restart apache:sudo systemctl restart apache2

通过局域网查看:

  1. [防火墙]设置为"Home"概要文件。禁止入站,允许出站,启用
  2. [防火墙]添加规则,简单选项卡,端口80,名称"Apache">
  3. [终端]获取主机名:hostname
  4. [Terminal][root]将您的机器名称更改为一些很酷的东西:hostname tazerface
  5. [xed][root]修改/etc/hosts
  6. 中的主机名
  7. [xed][root]修改/etc/hostname
  8. 中的主机名
  9. tazerface重新引导。<=现在这是您的机器名称。天哪,这个名字太酷了。
  10. 确保pm2自动启动,并将您的应用程序列为"在线":pm2 list
  11. [电话][浏览器]测试您的网站:http://tazerface/hello-app
  12. 如果不工作,请确保tazerface没有使用网络中继器提供的wifi。它需要与手机在相同的wifi网络上(但可以在5GHz或2.4GHz变体上)

添加免费ssl证书:

  1. [终端]添加ssl模块:sudo a2enmod ssl
  2. [防火墙]添加一个规则,简单选项卡,端口443,名称"Apache ssl">
  3. [终端]创建自签名免费证书:sudo openssl req -x509 -nodes -days 999999 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
  4. [终端]除通用名外,其余答案均为默认(按回车键),输入tazerface
  5. [终端]测试证书:openssl verify apache-selfsigned.crt
  6. [终端]用ssl .conf列表替换.conf中的内容:sudo xed /etc/apache2/sites-available/000-default.conf
  7. [终端]重启Apache:sudo systemctl restart apache2
  8. [电话][浏览器]测试您的网站:https://tazerface/hello-app

hello.js

var http = require('http');  
//create a server object:  
const port = 4567
http.createServer(function (req, res) {  
res.write('Hello World!'); //write a response to the client  
res.end(); //end the response  
}).listen(port); //the server object listens on port 4567   
// Console will print the message  
console.log(`Server running at ${port}`); 

000-default.conf (no ssl)

<VirtualHost *:80>
ServerName example.com
<Directory /var/www/>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location /hello-app>
ProxyPass http://127.0.0.1:4567
ProxyPassReverse http://127.0.0.1:4567
</Location>
</VirtualHost>

000 - default.conf (ssl)

# The only thing in the firewall that needs to be open is 80/443
<VirtualHost *:80>
Redirect / https://tazerface/
</VirtualHost>
<VirtualHost *:443>
ServerName tazerface
<Directory /var/www/>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location /hello-app>
ProxyPass http://127.0.0.1:4567
ProxyPassReverse http://127.0.0.1:4567
</Location>

</VirtualHost>