Skip to content

Instantly share code, notes, and snippets.

@obrassard
Last active October 3, 2019 19:35
Show Gist options
  • Save obrassard/851812680132a597462c69ef43e4e9db to your computer and use it in GitHub Desktop.
Save obrassard/851812680132a597462c69ef43e4e9db to your computer and use it in GitHub Desktop.
Host a Node.js app alongside apache sites

Host a Node.js app alongside apache sites

Walkthrough to setup apache vhost proxy towards a Node.js app + HTTPS on Ubuntu.

Requirements

  • Apache 2
  • Certbot
  • Node.js
  • A new domain / subdomain

Steps

1. Enable proxy module on Apache2

sudo a2enmod proxy_http
sudo systemctl restart apache2

2. Create a new vhost

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

3. Setup SSL Certificate

sudo certbot --apache -d app.domain.com

# Test autorenew
sudo certbot renew --dry-run

4. Start the node app

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 &"

Tips to stop the node process :

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment