In this tutorial, you will learn to quickly install and set up a Django application with the cPanel graphical interface and from the command line.
Prerequisites
You will need a general knowledge of programming concepts to go through this tutorial, and Python is recommended.
You’ll also require a Python Hosting plan. At ChemiCloud, we support the following python versions: 2.7.18, 3.3.7, 3.4.9, 3.5.9, 3.6.15, 3.7.12, 3.8.6, and 3.9.10.
How to Set up a Django Application
Step 1: Create a Python application in cPanel
The first step is to create a Python application within cPanel that will host the Django project. To do this, follow these steps:
- Log in to cPanel.
- In the SOFTWARE section of the cPanel home screen, click Setup Python App:
- Click CREATE APPLICATION:
The application form appears:
- In the Python version list box, select the Python version you’d like to use for your app. In our example, we’ll use Python version 3.8.6.
- In the Application root text box, type myapp.
- In the Application URL list box, select the domain. Leave the rest of the URL blank.
- Leave the Application startup file text box and Application Entry point text box blank.
- Click CREATE
cPanel creates the application and sets up the Python environment in the top right corner of the page. - At the top of the page, next to Enter to the virtual environment. To enter the virtual environment, run the command and copy the command. You will need this information in the following procedure.
Step 2: Configure the Django project
After you create the Python application in cPanel, you are ready to do the following tasks at the command line:
- Install Django.
- Create and configure the Django project.
- Configure Passenger to work with the Django project.
To do this, follow these steps:
- Log in to your account using SSH Client. Or use your cPanel’s Terminal feature.
- Activate the virtual environment using the command you noted in step 9 above ^. For example:
source /home/username/virtualenv/myapp/3.8/bin/activate && cd /home/username/myapp
- To install Django, type the following commands:
cd ~; pip install django==2.1.8
- 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:
- Locate the ALLOWED_HOSTS line, and then modify it as follows. Replace example.com with your domain name:
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', ], }, }, ]
Once you’ve finished editing the settings.py file, please Save the changes.
- 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")
- Locate the ALLOWED_HOSTS line, and then modify it as follows. Replace example.com with your domain name:
- 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. Delete all of the existing text, and then copy the following text into the file:
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): """ 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) # Set the application application = get_wsgi_application() application = PassengerPathInfoFix(application)
- Use a text editor to create a basic index.html file in the ~/myapp/templates/static_pages directory. The file can be as simple as a text that says Hello world!
- Next, we’ll use the migrate command, which is responsible for applying and unapplying migrations.
Type the following command:
python ~/myapp/manage.py migrate
- Next, we’ll set up the superuser account. This is useful if you need to create an initial superuser account or if you need to generate superuser accounts for your site(s) programmatically.)
- Type the following command:
python ~/myapp/manage.py createsuperuser
- At the Username prompt, type the administrator username and then press Enter.
- Type the administrator’s e-mail address at the Email address prompt and then press Enter.
- At the Password prompt, type the administrator password and then press Enter.
- Type the following command:
- Type the following command to collect the static files:
python ~/myapp/manage.py collectstatic
- In cPanel, restart the Python application:
- Test the Django site:
- Use your browser to http://www.example.com, where example.com represents your domain name. The index.html file should load.
- Use your browser to http://www.example.com/admin, where example.com represents your domain name. You should see the Django administration login page. To log in, use the superuser credentials that you created earlier.
If the website 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.
More Information
That’s it; now you know how to install and set up a Django Application.
Now that you have a Django app up and running, you can start the real work of developing your applications. The following resources can help:
- To view the official Django documentation, please visit http://docs.djangoproject.com.
- For information about Django extensions, please visit https://github.com/django-extensions/django-extensions.
How can I host a asgi django application?