How can I deploy a Django application on cPanel without SSH/Terminal access?
Django is a high-level Python web framework designed to help developers build robust, scalable web applications quickly and efficiently. It offers a clean, pragmatic design, and comes with powerful features like an integrated admin interface, automatic database management, and built-in security measures against common web vulnerabilities.
Many well-known websites and companies use Django for their platforms, including Instagram, Spotify, Pinterest, Mozilla, and YouTube, showcasing Django’s ability to handle high-traffic, complex applications while maintaining performance and security.
Follow these steps to deploy Django on a cPanel account using the Setup Python App feature without needing SSH/Terminal access.
1. Create a Python Application
- Navigate to cPanel and click on Setup Python App under the Software section.
- Click Create Application.
- Select Python Version: Choose the Python version (e.g., 3.11).
- Application root: Enter
django_app
(or any name you prefer for the root directory). - Application URL: Leave blank to host the app on the root domain, or enter a directory (e.g., django).
- Application startup file & Application entry point: Leave these empty for now.
- Click Create and wait for the app to be created.
2. Create the Requirements File
- Open cPanel → File Manager (For easier tab navigation, search for File Manager in cPanel, then right-click on File Manager and select “Open Link in New Tab.” This allows you to switch between tabs without losing your place)
- Navigate to the django_app folder and create a new file named
requirements.txt
3. Add the Requirements File
- Return to the Setup Python App tab.
- In the Configuration Files section:
- In the Add another file field, type in
requirements.txt
and press enter.
- Click Edit next to
requirements.txt
and enter the following:Django
- Click Save.
4. Install Requirements
- Next to "Configuration Files", click Run Pip Install and select
requirements.txt
5. Create Django Project
- In the Execute Python Script field, enter the following command, replacing
username
with your actual cPanel username,3.11
with the Python version anddjango_app
with your application root. Ensure you enter the . at the end, which specifies the project will be created in the current running directory.
undefined/home/username/virtualenv/django_app/3.11/bin/django-admin startproject myapp .
This will create a manage.py
script and a myapp
folder in the django_app directory. You can verify this in the File Manager.
6. Create a Static Folder
- In the File Manager, create a folder named static inside the public_html directory.
7. Edit Django Settings
- In File Manager, open the file: django_app/myapp/settings.py
- Edit the following line to match your domain name:
ALLOWED_HOSTS = ['exampledomain.co.za']
- Add this line at the bottom, replacing
username
with your cPanel username:
STATIC_ROOT = '/home/username/public_html/static'
- Save the changes.
8. Configure Application Startup
- Return to the Setup Python App tab.
- In the Application startup file field enter
myapp/wsgi.py
9. Set Environment Variables
- Under Environment Variables, click Add Variable:
- Name:
DJANGO_SUPERUSER_PASSWORD
- Value: your desired admin password.
- Click Done,
- Click Save at the top right.
- Scroll up and Restart.
10. Initialise Database and Create Superuser
To initialise your database, collect static files, and create an admin user, run these commands one by one in the Execute Python Script field:
manage.py migrate --noinput
manage.py collectstatic --noinput
manage.py createsuperuser --noinput --username admin --email admin@exampledomain.co.za
The Django installation should now be accessible at https://exampledomain.co.za and display "The install worked successfully! Congratulations!" with the admin interface at https://exampledomain.co.za/admin
If you have any questions or need further assistance, feel free to contact our support team or email helpdesk@elitehost.co.za.
Updated on: 27/10/2024
Thank you!