- Your cart is currently empty.
Creating a Basic Django (Python) App in cPanel
Django is a popular framework based on the Python programming language that makes it fast and easy to create powerful websites. We’ve already written about how to create a simple Python application on our blog.
In today’s article, we’ll show you how to install and configure a Django application on a hosting package that you can manage via the cPanel control panel.
Follow the procedure below and you’ll soon have a working Django application on your website that:
- loads a static web page,
- loads the Django admin interface,
- uses a SQLite database.
Table of contents
1. Create a Python application in cPanel
The first step is to create a Python application in the cPanel that will allow you to set up a Django project.
1. Log in to the cPanel control panel (login instructions).
2. In the SOFTWARE section, click on the Setup Python App icon.
3. Click on the blue CREATE APPLICATION button.
4. You will be taken to an interface with a few fields:
- Python version: select the recommended version
3.7.Xor3.8.X. - Application root: specify an umbrella folder for the application, e.g. enter
myapp - Application URL: select the domain where you want to install the application
- Application startup file: leave blank
- Application Entry point: leave blank
- Passenger log file*: if you wish, you can specify the log file of the Python application
If you leave the Application startup file and Application Entry point fields empty, cPanel will automatically create the passenger_wsgi.py startup file and the default application object. If you are not happy with the default settings, please fill in the fields as you wish.
5. In the top right corner, click CREATE and wait a moment for cPanel to create the application.
6. At the top, you will see the command that you will use to access the virtual environment later. Copy it, as you will need it in one of the next steps.
* If you have a hosting package on a Litespeed server, then you can set up the Passanger log file directly by entering the command below in the .htaccess file (located in the folder where the Application URL points):
PassengerAppLogFile "/home/vasadomain/myapp/logs/passenger.log"
IMPORTANT: The root directory of the application in this example is “myapp”; if you want to put the log in a subfolder (e.g. “logs”), you need to create this folder first.
Configure the Django project
After creating the Python application in cPanel, you are ready to edit it via the command line:
- Django framework installation,
- creating and configuring the Django project,
- setting up Passenger to work with the Django project.
1. Connect to your hosting package via SSH (instructions).
2. Activate the virtual environment with the command mentioned in step 6 of the procedure for creating a Python application.
source /home/user/virtualenv/myapp/3.7/bin/activate && cd /home/user/myapp
Note: adjust the user notation in the above code accordingly, including the Python version if necessary!
The command line now starts with (myapp:3.7), which means that you are in the virtual environment myapp with Python version 3.7. All the following commands assume that you are working in this virtual environment. If you terminate the SSH session, make sure you reactivate the virtual environment.
3. Install Django using the commands below:
cd ~
pip install django
To check the version of the installed Django framework, use the following command:
django-admin --version
4. Create a new Django project with the command:
django-admin startproject myapp ./myapp
5. To create directories for the project’s static files, enter the following commands:
mkdir -p ./templates/static_pages
mkdir ./static_files
mkdir ./static_media
6. Using a text editor, open the file ~/myapp/myapp/settings.py and edit the following changes:
- Find the ALLOWED_HOSTS line and add the entry
'vasadomena.si'.
ALLOWED_HOSTS = ['vasadomena.si']
- Find the TEMPLATES code section and update it as shown below:
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',
],
},
},
]
- Find the STATIC_URL line and add the code below it:
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static_media')
7. Using a text editor, open the file ~/myapp/myapp/urls.py and delete the entire contents of the file and replace it with the following:
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)
8. Using a text editor, open the file ~/myapp/passenger_wsgi.py and delete the entire contents of the file and add the following code:
import os
import sys
import django.core.handlers.wsgi
from django.core.wsgi import get_wsgi_application
# Set up paths and environment variables
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
# Set script name for the PATH_INFO fix below
SCRIPT_NAME = os.getcwd()
class PassengerPathInfoFix(object):
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)
# Set the application
application = get_wsgi_application()
application = PassengerPathInfoFix(application)
9. In the ~/myapp/templates/static_pages folder, use a text editor to create the base index.html file. It can be just a simple text file with the text Hello world.
10. Type the following command at the command prompt:
python ~/myapp/manage.py migrate
11. Set up a Superuser account with the following command:
python ~/myapp/manage.py createsuperuser
You will then be prompted to enter three pieces of information on the command line:
- Username: specify the administrator username and press Enter.
- Email address: specify the administrator’s email address and press Enter.
- Password: specify the administrator’s password and press Enter.
12. Enter the following command to collect static files:
python ~/myapp/manage.py collectstatic
Note: When asked if you want to overwrite existing files, type yes and press Enter.
13. In the cPanel control panel, click the RESTART button to restart the Python application.
14. Check the performance of the Django application on your website:
- In your web browser, go to http://www.vasadomena.si/ (replace
vasadomena.siwith your actual domain). You should see the contents of theindex.htmlfile. - In your web browser, go to http://www.vasadomena.si/admin (replace
vasadomena.siwith your actual domain). You should see the page you use to log in to the Django administration. To log in to the administration, enter the details you specified in step 11.
If the website does not appear in your browser, try running the passenger_wsgi.py file manually. To do this, at the command prompt, type:
python ~/myapp/passenger_wsgi.py
When you run this file, there should be no text displayed in the console. If there are errors, check the syntax in the configuration files.
3. Additional information
Now that you have a published website that supports Django, you can start working on developing more advanced Django applications. The following resources will help you do this:
- The official Django documentation: https://docs.djangoproject.com/
- The official Django forum: https://forum.djangoproject.com/
- Information about Django extensions: https://github.com/django-extensions/django-extensions
- The south library is popular for performing database migrations: https://pypi.org/project/South/
- And the fabric library helps to simplify deploying an application to production: https://docs.fabfile.org/





COMMENT THE POST
Your comment has been successfully submitted
The comment will be visible on the page when our moderators approve it.