Skip to content

Instantly share code, notes, and snippets.

@aaayushsingh
Last active August 7, 2018 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaayushsingh/7ce287e2356e5debc85574844b51d6c4 to your computer and use it in GitHub Desktop.
Save aaayushsingh/7ce287e2356e5debc85574844b51d6c4 to your computer and use it in GitHub Desktop.
Setup a nodejs or any such process on a webserver and make it accessible using nginx.

Setup a nodejs or any such process on a webserver and make it accessible using nginx.


This does not use pm2 module, but uses bg process instead.

1. Install nodejs

for debian and ubuntu based distros, do the following.

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

replace 8 with 10 for node v10 source: https://nodejs.org/en/download/package-manager/

2. Install nginx

sudo apt install ngnix -y

3. Configure nginx

Open the configuration file using

sudo nano /etc/nginx/sites-available/default

and replace everything in this block to the below code

. . .
    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

change your port accordingly

5. start your server

Start your server using the normal commands and append & at the end of the command to make it a background process so that it does not terminate when you close the terminal. eg: npm start &

Alternatively, start the process normally using npm start and press CTRL/COMMAND+Z. Then type bg and press enter to send the process to background. This is the same as appending & to the command.

use pm2 for nodejs

6. Restart nginx

sudo systemctl restart nginx

7. open your ip in a tab and have fun!

I use ng serve --host 0.0.0.0 --disable-host-check to test live without building by configuring a webhook that pulls every push to github. Disable host check flag allows to access the ng serve pages on different host.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment