Reputation: 51
I have a node app, it runs great in VS Code, and I can launch it using the script command from the terminal window.
It's just a utility I'd like running in the background on my dev machine. It watches for keystrokes and sets my busy / idle indicator to other apps. It uses iohook
if you're curious, to do this.
How can I deploy it to just be running in the background (all the time, including at startup)? I'm going to deploy it as a web server so I don't have to mess with linux services.
I already have apache and nginx and all that other web server stuff installed from the numerous tutorials I've done, but I don't know how to deploy to any of them.
I tried this:
https://plainenglish.io/blog/deploying-a-localhost-server-with-node-js-and-express-js
but that only enables launching from vs code or command line, it's not a real "server" that runs all the time, doesn't require a terminal window and starts on system startup.
I need this running from apache or nginx or something like that.
I tried:
To access the Node.js script from the web, install the Apache modules proxy and proxy_http with the commands:
sudo a2enmod proxy
sudo a2enmod proxy_http
Once the installation is complete, restart Apache for the changes to take effect:
sudo service apache2 restart
Upvotes: 1
Views: 1697
Reputation: 51
sudo apt-get install nodejs
sudo apt-get install apache2
sudo npm install -g pm2
sudo systemctl status apache2
localhost
cd /home/homer-simpson/websites
/home/homer-simpson/websites/hello-app
/var/www/html/hello-app/hello.js
sudo chmod 755 hello.js
hello.js
listingnode hello.js
http://localhost:4567/
sudo pm2 start hello.js
sudo pm2 startup systemd
pm2 save
sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart
/etc/apache2/sites-available/000-default.conf
000-default.conf
listingsudo systemctl restart apache2
http://localhost/hello-app
Hello World!
/home/homer-simpson/websites/another-app
sudo pm2 start another-app.js
pm2 save
Location
tag with the port number of the new app (must be unique): sudo xed /etc/apache2/sites-available/000-default.conf
sudo systemctl restart apache2
hostname
hostname tazerface
/etc/hosts
/etc/hostname
pm2 list
http://tazerface/hello-app
sudo a2enmod ssl
sudo openssl req -x509 -nodes -days 999999 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
tazerface
openssl verify apache-selfsigned.crt
sudo xed /etc/apache2/sites-available/000-default.conf
sudo systemctl restart apache2
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>
Upvotes: 2