Deployment of Django application to ubuntu based server. We'll be using Nginx & WSGI to run the application. PostgresSQL will be used as the database in this example.
You have to download pem file from the console to ssh into the server.
ssh -i key.pem ubuntu@(ip-address)
Clone your code for the application from the repository.
git clone (repo web URL)
Update the packages and installing pip with python3.
sudo apt-get update
sudo apt-get install python3-pip
Now install virtualenv and make a virtual enviornment.
sudo apt install virtualenv
virtualenv -p python3 (venv-name)
In case of Locale Issue:
export LC_ALL=C
Now you need to activate the virtual environment.
source (venv-name)/bin/activate
Install the requirements for the your django application through pip.
cd (app folder)
pip install -r requirements.txt
If in any case you have to install psycopg2, you will get eggs error. You can resolve this by installing following libs:
sudo apt install libpq-dev python3-dev
Create local settings file on the server for local configurations.
cd (Main app folder)
vim local_settings.py
It will be good to refresh our local package index and then install postgresql.
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
Create database.
sudo -i -u postgres
createdb (db-name)
Add database settings to your local settings file like below:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': (db-name),
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'localhost',
'PORT': '',
}
}
Now migrate the database.
python manage.py migrate
If you face any issue for db passwords, you can try this:
sudo -u postgres psql
ALTER USER postgres PASSWORD 'postgres';
Now that we have two Django projects set up and ready to go, we can configure uWSGI
sudo apt-get install python-dev
sudo apt-get install uwsgi uwsgi-plugin-python3 -y
Creating Configuration Files
sudo vim /etc/uwsgi/apps-available/uwsgi.ini
Add following details to this file:
[uwsgi]
virtualenv = (path-to-virtual-envr)
uid = www-data
gid = www-data
chmod-socket = 664
chown-socket = www-data
processes=4
threads=2
master=true
env = DJANGO_SETTINGS_MODULE=(project-name).settings
module = django.core.wsgi:get_wsgi_application()
chdir = (path-to-project)
socket = /tmp/(project-name).sock
logto = /var/log/uwsgi/(project-name).log
vacuum=true
thunder-lock
memory-report
max-requests=10000
no-orphans = true
limit-as=784
reload-on-as=784
reload-on-rss=512
Create Symbolic Link to Uwsgi
cd /etc/uwsgi/apps-enabled/
sudo ln -s /etc/uwsgi/apps-available/uwsgi.ini .
Restart uwsgi service and load new configuration
sudo service uwsgi restart
With uwsgi configured and ready to go, we can now install and configure Nginx as our reverse proxy. This can be downloaded from Ubuntu's default repositories:
sudo apt-get install nginx
You can create a server block configuration file for each of your project
sudo nano /etc/nginx/sites-available/(project-name)
Create server block configuration for the project.
server {
listen 80;
server_name (domain_name);
access_log /var/log/nginx/(domain_name)_access.log;
error_log /var/log/nginx/(domain_name)_error.log;
location / {
uwsgi_pass unix:///tmp/(project-name).sock;
include uwsgi_params;
uwsgi_read_timeout 300;
}
}
Create a symbolic link
sudo ln -s /etc/nginx/sites-available/(project-name) /etc/nginx/sites-enabled
You can test Nginx Configurations
sudo nginx -t
Now restart the Nginx
sudo service nginx restart
You are now good to go.
Don't forget to give Star to the gist. Happy Coding!