My linux world » srv-nodejs

srv-nodejs


Prerequiste

I assume that you have a Centos installation.

Installation

You can copy/paste this script and use it to configure automatically your server.

  1. #!/bin/bash
  2.  
  3. echo "install prerequistes"
  4. dnf -y install nodejs
  5.  
  6. echo "create working directory"
  7. mkdir -p /var/www/nodejs
  8.  
  9. echo "create nodejs server"
  10. cat > /var/www/nodejs/server.js << "EOF"
  11. var http = require('http');
  12.  
  13.  
  14. var server = http.createServer(function(req, res) {
  15.  
  16. res.writeHead(200);
  17.  
  18. res.end('Hello World !!');
  19.  
  20. });
  21.  
  22. server.listen(4587);
  23.  
  24. EOF
  25.  
  26.  
  27. echo "create service"
  28. cat > /etc/systemd/system/myNodeJsService.service << "EOF"
  29. [Unit]
  30. Description=my node.js service
  31.  
  32. [Install]
  33. WantedBy=multi-user.target
  34.  
  35. [Service]
  36. ExecStart=/usr/bin/node /var/www/nodejs/server.js
  37. Restart=on-success
  38. StandardOutput=syslog
  39. StandardError=syslog
  40. WorkingDirectory=/var/www/nodejs
  41. Type=simple
  42. User=root
  43. Group=root
  44. KillMode=process
  45. PrivateTmp=true
  46.  
  47. EOF
  48.  
  49.  
  50. systemctl enable myNodeJsService.service
  51. systemctl start myNodeJsService.service
  52.  

That’s all 🙂


Copyright © 2023 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.

Fork me on GitHub