Skip to content

Instantly share code, notes, and snippets.

@Tam
Last active October 19, 2015 12:40
Show Gist options
  • Save Tam/d91ab8328d6920ffabbb to your computer and use it in GitHub Desktop.
Save Tam/d91ab8328d6920ffabbb to your computer and use it in GitHub Desktop.
nginx on DigitalOceans NodeJS image

Node / NPM

Use DigitalOceans Ubuntu Node image.

nginx

Install

sudo apt-get update
sudo apt-get install nginx

Start

sudo service nginx start

Start after Reboot

update-rc.d nginx defaults

If you receive the following message, it means nginx is already setup to start after server reboot:

System start/stop links for /etc/init.d/nginx already exist.

Add site

Replace example.com where appropriate.

nano /etc/nginx/conf.d/example.com.conf

Inside example.com.conf add the following:

upstream app_example {
    server 127.0.0.1:{PORT};
    keepalive 8;
}

# Remove WWW
server {
    listen 0.0.0.0:80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 0.0.0.0:80;
    server_name example.com example;
    access_log /var/log/nginx/example.log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_pass http://app_example/;
        proxy_redirect off;
    }
}

Replace {PORT} with the port number the app is running on.

Reboot nginx

sudo /etc/init.d/nginx restart

PM2

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Install

sudo npm install pm2 -g

Start app

Depending on how the app is setup (look in package.json to find out) it is worth running npm install or npm start and performing an initial test run before setting up the following.

cd /path/to/node/app/
pm2 start app.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment