Django is a Python-based framework that enables you to quickly and easily create powerful websites. This article demonstrates how to install and configure Django on a Linux shared hosting account that uses cPanel.
After completing the following procedures, you will have a functioning Django site on your account that:
The first step is to create a Python application within cPanel that will host the Django project. To do this, follow these steps:
After you create the Python application in cPanel, you are ready to do the following tasks at the command line:
To do this, follow these steps:
source /home/username/virtualenv/myapp/3.6/bin/activate
To install Django, type the following commands:
cd ~ pip install django
To verify the version of Django that is installed, type the following command:
django-admin --version
To create a Django project, type the following command:
django-admin startproject myapp ~/myapp
To create directories for the static project files, type the following commands:
mkdir -p ~/myapp/templates/static_pages mkdir ~/myapp/static_files mkdir ~/myapp/static_media
Use a text editor to open the ~/myapp/myapp/settings.py file, and then make the following changes:
ALLOWED_HOSTS = ['example.com']
Locate the TEMPLATES block, and then modify it as follows:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR,'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]
Locate the STATIC_URL line, and then add the following lines beneath it:
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static_files') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, "static_media")
Use a text editor to open the ~/myapp/myapp/urls.py file. Delete all of the existing text, and then copy the following text into the file:
from django.contrib import admin from django.urls import path, include from django.conf import settings from django.conf.urls.static import static from django.conf.urls import url from django.views.generic.base import TemplateView urlpatterns = [ path('admin/', admin.site.urls), url(r'^$', TemplateView.as_view(template_name='static_pages/index.html'), name='home'), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Use a text editor to open the ~/myapp/passenger_wsgi.py file, and then make the following changes. Replace username with your own account username:
import myapp.wsgi
SCRIPT_NAME = '/home/username/myapp'
class PassengerPathInfoFix(object):
"""
Sets PATH_INFO from REQUEST_URI because Passenger doesn't provide it.
"""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
from urllib.parse import unquote
environ['SCRIPT_NAME'] = SCRIPT_NAME
request_uri = unquote(environ['REQUEST_URI'])
script_name = unquote(environ.get('SCRIPT_NAME', ''))
offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0
environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
return self.app(environ, start_response)
application = myapp.wsgi.application
application = PassengerPathInfoFix(application)
Type the following command:
python ~/myapp/manage.py migrate
Set up the superuser account:
python ~/myapp/manage.py createsuperuser
Type the following command to collect the static files:
python ~/myapp/manage.py collectstatic
In cPanel, restart the Python application:
Test the Django site:
If the web site does not appear in your browser, try running the passenger_wsgi.py file manually. To do this, type the following command:
python ~/myapp/passenger_wsgi.py
There should not be any text output to the console when you run this file. If there are any errors, check the syntax in the configuration files.
Now that you have a Django-enabled web site up and running, you can start the real work of developing your own applications. The following resources can help: