os-init/debian-11/apps/nodejs-20/nodejs-20.sh

44 lines
1.1 KiB
Bash
Raw Permalink Normal View History

2023-11-19 22:04:42 +00:00
#!/bin/bash
mkdir /srv/node
curl -sL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
npm i -g nodemon
cat > /etc/systemd/system/nodejs.service << EOF
[Service]
ExecStart=/usr/bin/node app.js
WorkingDirectory=/srv/node
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=node
#User=websites
#Group=websites
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
EOF
cat > /srv/node/app.js << "EOF"
var http = require('http')
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'})
res.write('Hello World!')
res.end()
}).listen(80)
EOF
systemctl daemon-reload
systemctl enable nodejs.service
systemctl start nodejs.service
if [ -f "/etc/iptables/rules.v4" ]; then
sed -i '/^-A INPUT -i lo -j ACCEPT.*/a -A INPUT -p tcp -m tcp --dport 80 -m state --state NEW -j ACCEPT' /etc/iptables/rules.v4
sed -i '/^-A INPUT -i lo -j ACCEPT.*/a -A INPUT -p tcp -m tcp --dport 443 -m state --state NEW -j ACCEPT' /etc/iptables/rules.v4
sed -i '/^-A INPUT -i lo -j ACCEPT.*/a # NodeJS' /etc/iptables/rules.v4
iptables-restore /etc/iptables/rules.v4
fi