A Python-based framework, Django enables you to create powerful websites in a quick and easy way. In this article, you will learn to install and configure Django using cPanel.
After you complete the procedure of installation and configuration of Django, your site will be able to:
- Load a static homepage for the domain.
- Load the Django administration interface.
- Use a SQLite database.
Create A Python Application In cPanel
At first you will need to create a Python application within cPanel to host the Django project. To do this, follow these steps:
1. Log in to cPanel.
2. Go to the SOFTWARE section of the cPanel home screen and click on Setup Python App.
3. Under Setup new application, select 3.6 in the Python version list box.
4. Type myapp in the App Directory text box.
5. Choose the domain you want to use in the App Domain/URI list box and then leave the URI text box empty.
6. Click on Setup. With this, cPanel creates the application and sets up the Python environment.
7. Under Existing applications, copy the command from the Command for entering the virtual environment. This information will be needed in the following procedure.
Configure The Django Project
After creating the Python application in cPanel, you will need 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.
For this, follow the below steps:
1. Log in to your account using SSH.
2. Use the command you noted in the above step to activate the virtual environment. For example:
source /home/username/virtualenv/myapp/3.6/bin/activate
3. Type the below command to install Django:
cd ~ pip install django==2.1.8
You can verify the version of Django installed, with the following command:
django-admin --version
4. Type the below command for creating a Django project:
django-admin startproject myapp ~/myapp
5. In order to create directories for the static project files, use the below commands:
mkdir -p ~/myapp/templates/static_pages mkdir ~/myapp/static_files mkdir ~/myapp/static_media
6. Open the ~/myapp/myapp/settings.py file by using the text editor, and then make the following changes:
- Find the ALLOWED_HOSTS line and then modify it as below. Replace example.com with your own domain name:
ALLOWED_HOSTS = ['example.com']
- Find the TEMPLATES block, and then modify it as 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', ], }, }, ]
- Locate the STATIC_URL line, and then add the below 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")
7. Open the ~/myapp/myapp/urls.py file using the text editor. Delete the existing text and copy the below 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)
8. Open the ~/myapp/passenger_wsgi.py file and do 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)
9. For creating a basic index.html file in the ~/myapp/templates/static_pages directory. The file can be as simple as a text file that says Hello world.
10. Type the following command:
python ~/myapp/manage.py migrate
11. Create and set up the superuser account:
- For this, type the below command:
python ~/myapp/manage.py createsuperuser
- Type the administrator username at the Username prompt and then press Enter.
- Type the administrator e-mail address at the Email address prompt and then press Enter.
- Type the administrator password at the Password prompt and then press Enter.
12. To collect the static files, type the below commands:
python ~/myapp/manage.py collectstatic
13. Restart the Python application in cPanel:
- Log in to cPanel.
- Click Setup Python App in the SOFTWARE section of the cPanel home screen.
- Locate the correct application under the Existing applications and then click Restart.
14. Test the Django site:
- Go to http://www.example.com, where example.com represents your domain name. The index.html file should load.
- Go to http://www.example.com/admin, where example.com represents your domain name. The Django administration login page should be displayed. Use the superuser credentials that you created earlier to log in.
If there is a problem for the website to appear in your browser, run the passenger_wsgi.py file manually. For this, type the below command:
python ~/myapp/passenger_wsgi.py
When you run this file, you should get any text output to the console. In case there are any errors, check the syntax in the configuration files.
That’s all! Now, you can easily install and configure Django from your cPanel.