How to Deploy a Django Application to cPanel
How to Deploy a Django Application to cPanel
Deploying Your Django Project to cPanel: Step-by-Step Guide
Deploying a Django application on cPanel requires several steps, from preparing your project files to configuring your cPanel environment. Here's a detailed, step-by-step guide to help you through the process.
1. Creating the requirements.txt
File
First, ensure that all the required packages for your project are captured in a requirements.txt
file. To generate this file, run the following command in your terminal:
pip freeze > requirements.txt
This command saves a list of all the installed Python packages into the requirements.txt
file, which will be used for installing dependencies on cPanel.
2. Preparing and Exporting Static Files
Next, you'll need to prepare your project for deployment. This includes collecting your static files.
Run the following command to gather all your static files into the directory specified in your settings.py
file (typically STATIC_ROOT
):
python manage.py collectstatic
Or, if you're using python3
, run:
python3 manage.py collectstatic
This will move all your static files into the appropriate folder (such as staticfiles
), which you can later serve in production.
Before proceeding, set the DEBUG
option in your settings.py
file to False
, which is essential for production.
Zipping Your Project
Once your static files are collected, zip your project files. Make sure to include the requirements.txt
, your Django app files, and static files, but exclude unnecessary large media files to keep the zip size manageable.
3. Uploading the Project to cPanel
- Log in to your cPanel account.
- Navigate to the File Manager.
- In File Manager, create a new folder (e.g.,
project
). - Upload your zipped Django project into this folder.
- Once uploaded, right-click the zip file and select Extract to unzip it.
Your project files should now be in the project
folder.
4. Setting Up Your Python Application in cPanel
-
In cPanel, go to the Setup Python App section (this may be under "Software").
-
Click Create Application and fill out the required fields:
- Python Version: Select Python 3.10 or later.
- Project Root: Enter the folder name where your project is located (e.g.,
project
). - Domain: Choose the domain or subdomain where your project will be hosted.
- Application Entry Point: Leave this as the default
passenger_wsgi.py
file. - Log File: Specify a path for your log file (e.g.,
/project/applog.log
).
-
Click Create to set up your application.
5. Installing Project Dependencies
- Before installing your project dependencies, stop the application to avoid conflicts during installation.
- Scroll down in the Python application setup page, find the configuration files section, and add the
requirements.txt
file. - Click Run pip install and select the
requirements.txt
file to install all the dependencies from the file.
6. Configuring the Database and Running Migrations
If you're using a production database (such as MySQL or PostgreSQL), follow these steps:
- Create a new database in cPanel (you can use MySQL or PostgreSQL). Add a user and grant full permissions to this user.
- Update your
DATABASES
configuration insettings.py
to reflect the new database details.
Next, run the migrations to set up the database tables. In the Setup Python App section, find the input box labeled Run Python Script and type:
manage.py migrate
This will apply all the migrations. If there are any issues, the output will appear below the input box.
7. Updating the Passenger WSGI File
The passenger_wsgi.py
file is the main entry point for your application. You need to modify this file to point to your Django project's wsgi.py
.
- In File Manager, navigate to your project folder.
- Right-click on the
passenger_wsgi.py
file and select Edit. - Replace its contents with:
from myproject.wsgi import application
Make sure to replace myproject
with the actual name of your Django project.
Save the file.
8. Finalizing the Deployment
- Since
DEBUG
is set toFalse
in production, you need to ensure your static files are properly served. Move the static files to the public directory of your domain or subdomain (e.g.,/public_html/staticfiles
). - In Setup Python App, restart the application by selecting it and clicking Start.
9. Final Checks
Your Django app should now be up and running! Here are some final checks and tips:
- Make sure that your database and static files are properly configured.
- If everything is working correctly, you'll see your application live at your domain.
- Check the log file (
applog.log
) for any issues during startup or while running the application.
Congratulations! You've successfully deployed your Django app to cPanel.