Python Django

Quick guide to installing an up-to date version of Python Django:


# Upgrade your local PIP:
$ python3.9 -m ensurepip --upgrade
$ python3.9 -m pip install --upgrade pip

# Make sure that you have your ~/.local/bin early in your path:
$ cat ~/.bash_profile 
export PATH=$HOME/.local/bin:$PATH
$ source ~/.bash_profile

# Upgrade required dependencies
$ pip3 install setuptools --upgrade
$ pip3 install virtualenv --upgrade

# Create a directory for your Django project:
$ mkdir ~/django ; cd ~/django

# Create and activate a new virtual environment for the Django app:
~/django$ python3.9 -m virtualenv -p python3.9 venv
~/django$ source venv/bin/activate

# Install Django and start a new project:
(venv) ~/django$ pip3 install Django
(venv) ~/django$ python -c "import django;print(django.get_version())"
(venv) ~/django$ django-admin startproject djangoProject

# Change the config to allow access:
(venv) ~/django$ sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ['*']/" djangoProject/djangoProject/settings.py

# Enable mod-passenger for the Apache Vhost:
(venv) ~/django$ cat .htaccess
PassengerEnabled On

# Create a startup file for the mod-passenger application
(venv) ~/django$ cat passenger_wsgi.py
import sys, os
ApplicationDirectory = 'djangoProject'
ApplicationName = 'djangoProject'
VirtualEnvDirectory = 'venv'
VirtualEnv = os.path.join(os.getcwd(), VirtualEnvDirectory, 'bin', 'python')
if sys.executable != VirtualEnv: os.execl(VirtualEnv, VirtualEnv, *sys.argv)
sys.path.insert(0, os.path.join(os.getcwd(), ApplicationDirectory))
sys.path.insert(0, os.path.join(os.getcwd(), ApplicationDirectory, ApplicationName))
sys.path.insert(0, os.path.join(os.getcwd(), VirtualEnvDirectory, 'bin'))
os.chdir(os.path.join(os.getcwd(), ApplicationDirectory))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', ApplicationName + '.settings')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

#Thats it! See below for how to create a subdomain to run this app

If your subdomain is called e.g. django.MYDOMAIN.COM its DocumentRoot must be set to the absolute path:

/var/www/vhosts/MYDOMAIN.com/django/djangoProject


You can check the Python/Django versions matrix on this page:
https://docs.djangoproject.com/en/3.1/faq/install/

  • 25 Users Found This Useful
Was this answer helpful?

Related Articles

Available Python versions

Most servers provide the following Python versions: 2.7 3.4 3.8 3.9 If you need a...