Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
121 lines (88 sloc) 1.99 KB
description
This show what commands to use to setup express app with nginx (I use this because I tend to forget the commands)

Express + Nginx

First choose a host (I recommend Digital Ocean)

{% embed url="https://m.do.co/c/6aef79a2cf4a" %}

Update Ubuntu (The OS I will be using)

$ sudo apt update
$ sudo apt upgrade

Create Project

$ mkdir app
$ cd app
$ git init

Install Node.js

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

Install PM2

$ npm install pm2 -g

{% embed url="https://pm2.keymetrics.io/" %}

Create app

$ npm install express
$ nano app.js

{% code-tabs %} {% code-tabs-item title="app.js" %}


const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

{% endcode-tabs-item %} {% endcode-tabs %}

{% hint style="success" %} Use "CTRL + X" to exit nano editor {% endhint %}

$ pm2 start app.js

Open ports

$ sudo ufw enable
$ sudo ufw status
$ sudo ufw allow ssh
$ sudo ufw allow http
$ sudo ufw allow https

Install Nginx

$ sudo apt install nginx
$ sudo nano /etc/nginx/sites-available/default

Add to server block

{% code-tabs %} {% code-tabs-item title="default" %}

server_name beta.andre.gg;

    location / {
        proxy_pass http://localhost:3000;
        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;
    }

{% endcode-tabs-item %} {% endcode-tabs %}

Last steps

$ sudo nginx -t
$ sudo service nginx restart

SSL

{% embed url="https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04" %}

You can’t perform that action at this time.