Skip to content

Instantly share code, notes, and snippets.

@azeemh
Last active June 29, 2017 19:13
Show Gist options
  • Save azeemh/0209603dc831c304fe25952b239fb655 to your computer and use it in GitHub Desktop.
Save azeemh/0209603dc831c304fe25952b239fb655 to your computer and use it in GitHub Desktop.
Simple Deployment of App to Proxy with Nginx & Ubuntu 16.04 (Rails, Sinatra, PHP, etc)

This tutorial can be used for any application that runs a server on localhost:port, in this case localhost:3000. i.e. rails s, any sinatra or nodejs server bound to a port, and even php -S localhost:3000...

INSTALL NGINX

purges any old versions

sudo apt-get purge nginx*

install clean nginx

sudo apt-get install nginx

Once installed successfully, you can verify via

nginx -v

Manually start the Nginx daemon

sudo service nginx start

(You can also stop it via sudo service nginx stop or restart it to propagate changes to a config etc without taking the site down via sudo service nginx restart)

NORMAL CONFIGURATION

Disable default site by removing the symlink

sudo rm /etc/nginx/sites-enabled/default

This will create and open a new virtual host config file (for your app)

sudo vi /etc/nginx/sites-available/name_of_your_app.conf

Insert the following server config block into name_of_your_app.conf (press i to insert, esc to not, arrows to navigate, and shift + ZZ to save -- it should be empty if it's a clean version)

server {
  listen  80;
  server_name yourdomainname.com;
  
  location / {
    proxy_pass http://127.0.0.1:3000;
  }
}

enable your config file by creating symlink in /etc/nginx/sites-enabled

sudo ln -sf /etc/nginx/sites-available/name_of_your_app.conf /etc/nginx/sites-enabled/name_of_your_app.conf

restart nginx with new config

sudo service nginx restart

start your server

cd name_of_your_app
rails s

or for php users,

php -S localhost:3000

ADDITIONAL NOTES

In the server configuration localhost is referred to by the ip address 127.0.0.1 and the port number can be whatever you want.

FOR MULTIPLE PROXY CONFIGURATION

You'll need to run the normal config process, but instead, when you create your config file,

sudo vi /etc/nginx/sites-available/name_of_your_app.conf

you can set multiple proxies with the following configuration:

server {
    listen       80;
    server_name  example1.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

server {
    listen       80;
    server_name  example2.com;

    location / {
        proxy_pass http://127.0.0.1:8181;
    }
}

server {
    listen       80;
    server_name  example3.com;

    location / {
        proxy_pass http://127.0.0.1:8282;
    }
}

Your 3 applications will share the 10k concurrent user max, and your applications will need to have their server settings configured to bind all traffic to port 3000, 8181, 8282, etc.

i.e. if you want to run a rails app on localhost:3000 via the standard rails s, a php application on localhost:8080 via php -S localhost:8080, and an additional php app or ruby or nodejs etc at localhost:8282.

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