Skip to content

Instantly share code, notes, and snippets.

@6aditya8
Created August 7, 2018 14:02
Show Gist options
  • Save 6aditya8/90de3ce9692132cfd56133b413cb3ca9 to your computer and use it in GitHub Desktop.
Save 6aditya8/90de3ce9692132cfd56133b413cb3ca9 to your computer and use it in GitHub Desktop.
Hosting Django Application in AWS or GCloud using Nginx and uwsgi[emperor mode]
(Reference:https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-ubuntu-16-04)
Create an instance, make sure you have allowed HTTP and HTTPS ports.
sudo apt-get update
sudo apt-get install python-pip python-dev
sudo -H pip install virtualenv virtualenvwrapper
sudo nano .bashrc
Add the following:
export WORKON_HOME=/home/ubuntu/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bashrc (or) restart terminal
mkvirtualenv demo
workon demo
pip install django
mkdir tutorial
cd tutorial
django-admin.py startproject demo
python demo/manage.py migrate
sudo nano demo/demo settings.py
Add the following:
ALLOWED_HOSTS = ['your_server_domain_or_IP', 'second_domain_or_IP', . . .]
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
python demo/manage.py collectstatic
sudo -H pip install uwsgi
sudo mkdir -p /etc/uwsgi/sites
sudo nano /etc/uwsgi/sites/demo.ini
Add the following:
[uwsgi]
project = demo
uid = ubuntu
base = /home/%(uid)
chdir = %(base)/tutorial/%(project)
home = %(base)/.virtualenvs/%(project)
module = %(project).wsgi:application
master = true
processes = 2
socket = /tmp/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 666
vacuum = true
sudo nano /etc/systemd/system/uwsgi.service
Add the following:
[Unit]
Description=uWSGI Emperor service
[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown ubuntu:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
sudo apt-get install nginx
sudo nano /etc/nginx/sites-available/demo
Add the following:
server {
listen 80;
server_name 0.0.0.0:80;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/tutorial/demo;
}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass unix:/tmp/demo.sock;
}
}
sudo ln -s /etc/nginx/sites-available/demo /etc/nginx/sites-enabled
sudo rm /etc/nginx/sites-enabled/default
sudo systemctl restart nginx
sudo systemctl start uwsgi
sudo ufw allow 'Nginx Full'
sudo systemctl enable nginx
sudo systemctl enable uwsgi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment