Django is a python framework used to build web applications. In this guide, we will learn how to deploy Django application on a VPS running on apache web-server on ubuntu-18.04.
Install required Libraries
Access your VPS on ssh
ssh root@yourip
#then key in your password on prompt
sudo apt-get update
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
#this will update the ubuntu os on the vps and install apache2 webserver , pip3- which is the python package manager, and mod_wsgi a module for integrating Django and the apache webserver.
Set Up the Django virtual environment
sudo pip3 install virtualenv
Navigate to the location where you want to set up your project. In our guide, we will set up the project on the home directory
cd /home
#navigate to the home directory
mkdir test_django
#create a folder for your app
cd test_django
#navigate to the root folder of your application
create and activate the virtual environment
virtualenv env
#create a virtual environment with the name env
source env/bin/activate
#activate your virtual environment. This helps in ensuring that only dependencies downloaded for this application will only be applicable to this application. In case you have multiple Django applications, you can run them with different versions of dependencies.
On activating the env, your command line should change
(env) root@username:/home/test_django#
pip3 install django
#install djangodjango-admin startproject django_project .
#start a Django aplication on the current folder To edit the files, we will use nano as our text editor. nano django_project/settings.py #launch the settings.py to make some edits. add the following code, save and close.STATICFILES_DIRS=[ (BASE_DIR/ 'static') ]
#defining the root folder for static files ALLOWED_HOSTS = ['yourdomain','serverIP','www.yourdomain','127.0.0.1'] #adjust the allowed host accordingly. Setting up the virtual Host Navigate to the recommended folder for creating virtual host and create one for your projectnano /etc/apache2/sites-available/djangoproject.conf
#this will create and launch the file djangoproject.conf. Paste the code below.<VirtualHost *:80> ServerAdmin admin@yourdomain ServerName yourdomain ServerAlias www.yourdomain DocumentRoot /home/test_django ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /static /home/test_django/static <Directory /home/test_django/static> Require all granted </Directory> Alias /static /home/test_django/media <Directory /home/test_django/media> Require all granted </Directory> <Directory /home/test_django/django_project> <Files wsgi.py> Require all granted </Files> </Directory> WSGIDaemonProcess django_project python-path=/home/test_django python-home=/home/test_django/env WSGIProcessGroup test_django WSGIScriptAlias / /home/test_django/django_project/wsgi.py </VirtualHost>
Enable the virtual Host file Navigate to the file location and and activate it and reload apache webservercd /etc/apache2/sites-available
sudo a2ensite djangoproject.conf
service apache2 reload
Set up Local file so that your website loads from your domain namenano /etc/hosts
#lauch the hosts file and add the following code127.0.0.1 yourdomain
#save and exit
Resolving Permissions Issues Update some file permissions to avoid file permission related errors. Run the following commands. Sqlite3 related commands only applies if sqlite3 is the default database. Skip the first 2 lines in case you will use any other database. See mysql database confirguration below.sudo chmod 664 /home/test_django/db.sqlite3
sudo chown :www-data /home/test_django/db.sqlite3
sudo chown :www-data /home/test_django
Do a test to ensure all the configurations are Ok. Should there be an error, check and adjust according. Most common errors are path related.sudo apache2ctl configtest
#If the response is OK: everything is working as expected. MYsql Database configuration