Skip to content

Instantly share code, notes, and snippets.

@manu-chroma
Last active March 30, 2016 20:26
Show Gist options
  • Save manu-chroma/4a6f3b6b27aa49683c67b9fb0b23d493 to your computer and use it in GitHub Desktop.
Save manu-chroma/4a6f3b6b27aa49683c67b9fb0b23d493 to your computer and use it in GitHub Desktop.

To setup NGINX server && Securing it with Let's Encrypt

Part 1: Setting up NGINX server

Run this script to setup a working NGINX server proxying requests to the gunicorn server.

NOTES

  1. The script should run with sudo privilages.
  2. Running the script: sudo bash nginx_deploy.sh
  3. There must be a NGINX server config file should already present be present in the directory.
  4. Sample config file used: server_start
  5. The gunicorn server should already be running on 127.0.0.1 before running the script.
    (Make sure that the address of gunicorn server and proxy pass adress is same)

Script and sample config NGINX file

1. Sample config file for the server

Assuming that the gunicorn server is serving at 127.0.0.1:8888

server {
    listen 5676;
    server_name 0.0.0.0;

    location / {
        include proxy_params;
        proxy_pass 127.0.0.1:8888;
    }
}

2. nginx_deploy.sh

Replace <script name> with server_start

#!/usr/bin/env bash

# Making sure that the gunicorn server is already serving at localhost address

# Update and install packages
sudo apt-get update
sudo apt-get install python-pip python-dev nginx -y
echo "Packages installed..."

# cat file2 >> file1
# The >> operator appends the output to the named file or creates the named file if it does not exist.

# Remove any previously setup file of this name (to avoid any error)
sudo rm /etc/nginx/sites-enabled/<script name>
sudo rm /etc/nginx/sites-available/<script name>

# The directory must have the NGINX config file pre-made named server_config
sudo cat server_config >> /etc/nginx/sites-available/<script name>
echo " NGINX congiuration file created succesfully created"

# sudo cat server_config /etc/nginx/sites-available/myproject 
sudo ln -s /etc/nginx/sites-available/<script name> /etc/nginx/sites-enabled
sudo nginx -t #add a if else condition here
echo "Tests succesfull!"

#server reastarted
sudo service nginx restart
echo "Server re-started! Should be serving now at the address in the config file!"
#NOTE: This would only serve the site at http:// (not at https://)

Part 2: Setting up Let's encrypt (To Update)

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