Skip to content

Instantly share code, notes, and snippets.

@rehmatworks
Last active October 12, 2019 18:33
Show Gist options
  • Save rehmatworks/ab2ea1c71a792ae39a8b8d23b91fc5d1 to your computer and use it in GitHub Desktop.
Save rehmatworks/ab2ea1c71a792ae39a8b8d23b91fc5d1 to your computer and use it in GitHub Desktop.
Django install instructions

This guide is intended to deploy a Django application on an Ubuntu 18.x server from a Git repo. This is written for a specific client's needs only and this is not intended for public help.

Step 1: Install needed packages

sudo apt-get install nginx mysql-server software-properties-common libmysqlclient-dev virtualenv

Step 2: Create a system user with sudo privileges if you are signed in as root

sudo adduser souqkit
usermod -aG sudo souqkit

Step 3: Switch to new user, CD into user's home directory

sudo su souqkit
cd

Step 4: Create a virtualenv

virtualenv p python3 souqkitvenv

Step 5: Clone the code repo

git clone https://github.com/loaymansour15/souqkitproject.git

Step 6: Switch to new virtualenv and install the packages

source souqkitvenv/bin/activate
cd souqkitproject
pip install -r requirements.txt

If you encounter errors, install individual and required packages only. i.e.:

pip install Django
pip install Gunicorn
pip install mysqlclient

Step 7: Create MySQL database

sudo mysql
create database souqkitdb;
grant all privileges on souqkitdb.* to 'souqkitdb'@'localhost' identified by 'dbpassword';
flush privileges;

And update the new database details in settings.py

Step 8: Create service Go to /etc/systemd/system and create a service, i.e. souqkit.service:

cd /etc/systemd/system
sudo nano souqkit.service

Paste this content and write the file:

[Unit]
Description=api service
After=network.target

[Service]
User=souqkit
Group=souqkit
WorkingDirectory=/home/souqkit/souqkitproject
ExecStart=/home/souqkit/souqkitvenv/bin/gunicorn --timeout 600 --access-logfile - --workers 3 --bind unix:/home/souqkit/souqkitproject/SouqKit.sock SouqKit.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

Write the conf file:

CRRL+O and Enter

Restart service sudo service souqkit restart

Step 9: Create NGINX vhost

First replace /etc/nginx/nginx.conf with this file's content:

worker_processes auto;
user souqkit;
worker_rlimit_nofile 100000;
error_log /var/log/nginx/error.log crit;
events {
    worker_connections 4000;
    use epoll;
    multi_accept on;
}

http {
    open_file_cache max=200000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    access_log off;
    client_max_body_size 1G;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 10240;
    gzip_comp_level 1;
    gzip_vary on;
    gzip_disable msie6;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types
        text/css
        text/javascript
        text/xml
        text/plain
        text/x-component
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/rss+xml
        application/atom+xml
        font/truetype
        font/opentype
        application/vnd.ms-fontobject
        image/svg+xml;

    reset_timedout_connection on;
    client_body_timeout 30;
    send_timeout 10;
    keepalive_timeout 30;
    keepalive_requests 100000;
    include  "/etc/nginx/mime.types";
    include  "/etc/nginx/conf.d/*.conf";
    include "/etc/nginx/vhosts.d/*.conf";
}

Now create vhosts directory:

cd /etc/nginx
sudo mkdir vhosts.d

And create vhost:

cd vhosts.d
nano souqkit.conf

Paste this content:

server {
    listen 80;
    server_name souqkit.com;

    location /favicon.ico {access_log off;log_not_found off;}

    location /static/ {
        root /home/souqkit/souqkitproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/souqkit/souqkitproject/SouqKit.sock;
    }
}

Restart NGINX:

service nginx reload
service nginx restart

This should work and your Django app should go live.

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