Skip to content

Instantly share code, notes, and snippets.

@arielnoname
Last active May 9, 2022 08:28
Show Gist options
  • Save arielnoname/7ed4e6a08a3e405d8dd1ca1ae74d242b to your computer and use it in GitHub Desktop.
Save arielnoname/7ed4e6a08a3e405d8dd1ca1ae74d242b to your computer and use it in GitHub Desktop.
Install Django + Nginx + MySQL + Supervisor in Ubuntu. Python 2

Install nginx, MySQL, supervisor and dependencies:

$ sudo apt-get install nginx mysql-server supervisor python-pip python-dev libmysqlclient-dev

Install pyenv: https://github.com/pyenv/pyenv
Install virtualenvwrapper: https://virtualenvwrapper.readthedocs.io/en/latest/

Configure MySQL server:
$ sudo mysql_secure_installation

Create database and user, grant privileges on that DB to the user. Make DB UTF-8 Use root password used in last command:

$ mysql -uroot -hlocalhost -p
mysql> CREATE DATABASE databasename;
mysql> CREATE USER databaseuser;
mysql> GRANT ALL ON databasename.* TO 'databaseuser'@'localhost' IDENTIFIED BY 'NEWPASSWORDHERE';
mysql> ALTER DATABASE databasename CHARACTER SET 'utf8';
mysql> exit;

Create virtualenv and activate it:

$ mkvirtualenv app_venv
$ workon app_virtualenv  

Install gunicorn and Django:
(virtualenv)$ pip install django MySQL-python gunicorn

Setup app files (make sure of user permissions here depending of where you want to put the app):

(virtualenv)$ sudo mkdir -p /var/sites/app_name
(virtualenv)$ sudo chown -R user:user /var/sites/app_name
(virtualenv)$ cd /var/sites/app_name
(virtualenv)$ django-admin startproject myproject .

Config nginx server

/etc/nginx/nginx.conf

worker_processes 1;
user www-data;
pid /var/www/run/nginx.pid;

events {
    worker_connections 1024; # increase if you have lots of clients
    accept_mutex off; # set to 'on' if nginx worker_processes > 1
    # 'use epoll;' to enable for Linux 2.6+
    # 'use kqueue;' to enable for FreeBSD, OSX
}

http {
    include mime.types;
    default_type application/octet-stream;  # fallback in case we can't determine a type
    sendfile on;

    access_log /var/www/log/nginx_access.log;
    error_log /var/www/log/nginx_error.log;

    server {
        # if no Host match, close the connection to prevent host spoofing
        listen 80 default_server;
        return 444;
    }

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Add site to nginx $ vi /etc/nginx/sites-available/mysite

/etc/nginx/sites-available/mysite

upstream rosenberg_sge {
  server unix:/var/www/sockets/rosenberg_sge.sock fail_timeout=0;
}

server {
    server_name 190.13.66.79;

    client_max_body_size 4G;

    access_log /var/www/logs/sge_arosenberg-nginx-access.log;
    error_log /var/www/logs/sge_arosenberg-nginx-error.log;

    location /static/ {
        alias   /var/www/sites/sge.clinicarosenberg.com/static/;
    }

    location /media/ {
        alias   /var/www/sites/sge.clinicarosenberg.com/media/;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://rosenberg_sge;
            break;
        }
    }
}

supervisor:

[program:sge_arosenberg] directory=/var/www/sites/sge_arosenberg/ command=/var/www/.virtualenvs/sge_arosenberg/bin/gunicorn --workers 3 --bind unix:/var/www/sockets/sge_arosenberg.sock core.wsgi:application autostart=true autorestart=true stderr_logfile=/var/www/sites/tmp/supervisor.gunicorn.out.log stdout_logfile=/var/www/sites/tmp/supervisor.gunicorn.err.log user=sge_arosenberg group=webapps environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8

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