Walkthrough to setup apache vhost proxy towards a Node.js app + HTTPS on Ubuntu.
- Apache 2
- Certbot
- Node.js
- A new domain / subdomain
sudo a2enmod proxy_http
sudo systemctl restart apache2
Create a new configuration file :
cd /etc/apache2/sites-available/
vi app.domain.com.conf
Vhost configuration :
<VirtualHost *:80>
ServerAdmin admins@domain.ca
ServerName app.domain.com # DNS record
ProxyPass / http://localhost:8000/ # With the node app's port
</VirtualHost>
Check configurations and enable site :
sudo a2ensite app.domain.com.conf
sudo apache2ctl configtest
sudo systemctl restart apache2
sudo certbot --apache -d app.domain.com
# Test autorenew
sudo certbot renew --dry-run
Create a directory for all nodes apps
mkdir /var/www/node_apps
Copy or clone the app and install packages
cd /var/www/node_apps
git clone git@github.com/user/app.git
cd app/
npm install
Make sure that the port used by the node server is the one used in the vhost
ProxyPass
config
Start the app as a background process using &
node index.js &
or asign it a process name :
bash -c "exec -a <processName> node server.js &"
# bash -c "exec -a app.domain.com node server.js &"
We need to find the PID with :
ps aux | grep node
# root 15327 0.0 3.6 929704 36800 ? Sl 15:08 0:00 node server.js
We can then kill
the process with his PID :
sudo kill <PID>
# sudo kill 15327
However, if you started the process by giving it a name, you can kill this way :
pkill -f <processName>
# pkill -f app.domain.com