How Do I Deploy a Django App on Your VPS or Shared Server?
Deploying a Django application does not have to be complicated. Depending on your hosting environment, you can deploy your Django app on shared hosting using cPanel and Passenger or on a VPS using Gunicorn and Nginx.
Shared hosting is suitable when you want a simpler setup with fewer server-level configurations. A VPS gives you more control over the server and allows you to configure the Django application according to your requirements.
This guide explains how to deploy a Django application on both shared hosting and a VPS, including the required Python environment, dependencies, database configuration, static files, and basic troubleshooting.
Prerequisites
Before deploying your Django application, make sure you have:
- A Django project ready for deployment
- A supported Python version
- Access to cPanel or SSH
- A
requirements.txtfile - A configured database such as SQLite, MySQL, or PostgreSQL
- Basic knowledge of Django files such as
manage.pyandsettings.py
It is recommended to test your Django application locally before uploading it to your hosting server.
Deploy Django on Shared Hosting
Shared hosting provides a simple way to host a Django application without managing the complete server configuration. If your hosting account provides cPanel and CloudLinux Passenger, you can create and manage the Python application directly from cPanel.
Step 1: Log in to cPanel
Log in to your hosting account and open cPanel.
Under the Software section, look for Setup Python App.
This option allows you to create and manage Python applications on supported shared hosting accounts.
Step 2: Create a Python Application
Click Create Application and provide the required information.
Select:
- Python version
- Application root
- Application URL
- Startup file
For example, your application root can be:
/home/username/my_django_app
For a Django application, the startup file is typically:
passenger_wsgi.py
After entering the details, click Create.
Passenger can create the Python environment and manage the application process for you.
Step 3: Upload Your Django Project
Upload your Django project files to the application root using cPanel File Manager, FTP, or SFTP.
For example:
/home/username/my_django_app/
Make sure your Django project files and project directory are placed correctly inside the application directory.
Your project should contain important files such as:
manage.py
requirements.txt
and your Django project directory containing settings.py and wsgi.py.
Step 4: Install Django Dependencies
Open the Terminal option in cPanel if it is available.
Activate the virtual environment created for your Python application:
source /home/username/virtualenv/my_django_app/3.9/bin/activate
Then install the dependencies:
pip install -r requirements.txt
If terminal or SSH access is not available on your hosting plan, contact your hosting provider to check whether it can be enabled.
Step 5: Configure passenger_wsgi.py
Create a file named:
passenger_wsgi.py
inside your application root.
A basic configuration can look like this:
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Replace myproject.settings with the actual location of your Django settings file.
Step 6: Run Migrations and Collect Static Files
Activate your virtual environment and run:
python manage.py migrate
Then collect your static files:
python manage.py collectstatic
The migration command applies the required database changes, while collectstatic prepares your CSS, JavaScript, images, and other static files for deployment.
Step 7: Restart the Application
Return to:
cPanel → Setup Python App
Find your Django application and click Restart.
Your Django application should now be available through the configured domain.
Deploy Django on a VPS
A Django VPS Hosting environment gives you greater control over the server configuration. You can install the required packages and configure Gunicorn as the application server with Nginx as the reverse proxy.
Step 1: Update the Server
Connect to your VPS through SSH and update the system:
sudo apt update && sudo apt upgrade -y
Step 2: Install Required Packages
Install Python, pip, the virtual environment package, and Nginx:
sudo apt install python3 python3-pip python3-venv nginx -y
These packages provide the basic environment required to run a Django application.
Step 3: Upload or Clone Your Django Project
Move to the web directory:
cd /var/www/
Create a directory for your application:
sudo mkdir myproject
cd myproject
You can then upload your project or clone it from your Git repository.
For example:
sudo git clone https://github.com/yourusername/yourproject.git .
Replace the repository URL with your actual Django project repository.
Step 4: Create a Virtual Environment
Create a Python virtual environment:
python3 -m venv venv
Activate it:
source venv/bin/activate
Install your project dependencies:
pip install -r requirements.txt
Using a virtual environment keeps your Django application’s Python packages separate from the system-wide packages.
Step 5: Run Django Migrations
Run the database migrations:
python manage.py migrate
Then collect the static files:
python manage.py collectstatic
Your Django application is now ready to be connected to Gunicorn.
Step 6: Configure Gunicorn
Gunicorn works as the application server for your Django project.
Create a systemd service:
sudo nano /etc/systemd/system/gunicorn.service
Add the following configuration:
[Unit]
Description=gunicorn daemon for Django
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/myproject
ExecStart=/var/www/myproject/venv/bin/gunicorn --workers 3 --bind unix:/var/www/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
Replace myproject with the actual name of your Django project.
Save the file and reload systemd:
sudo systemctl daemon-reload
Enable Gunicorn:
sudo systemctl enable gunicorn
Start the service:
sudo systemctl start gunicorn
You can check its status with:
sudo systemctl status gunicorn
Step 7: Configure Nginx
Nginx receives requests from visitors and passes them to your Django application through Gunicorn.
Create an Nginx configuration file:
sudo nano /etc/nginx/sites-available/myproject
Add:
server {
listen 80;
server_name yourdomain.com;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location /static/ {
root /var/www/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/myproject/myproject.sock;
}
}
Replace yourdomain.com with your actual domain name.
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
Then restart Nginx:
sudo systemctl restart nginx
Your Django application should now be accessible through your domain.
Configure the Django Virtual Environment
A virtual environment is recommended for both shared hosting and VPS deployments. It keeps your application’s dependencies separate from other Python applications on the server.
Use:
python3 -m venv venv
Activate it:
source venv/bin/activate
Install the required packages:
pip install -r requirements.txt
Remember to activate the virtual environment before running Django or pip commands.
Configure the Database
Django can work with different database systems depending on your application requirements and hosting environment.
Common options include:
- SQLite
- MySQL
- PostgreSQL
For example, a basic MySQL configuration in settings.py can look like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname',
'USER': 'dbuser',
'PASSWORD': 'dbpass',
'HOST': 'localhost',
'PORT': '3306',
}
}
For a basic SQLite setup:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
Choose the database according to your application’s requirements and available hosting resources
Configure Static and Media Files
Before putting your Django application into production, make sure static and media files are configured correctly.
Run:
python manage.py collectstatic
You can then define the required paths in settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
Correct static file configuration is important for loading your website’s CSS, JavaScript, images, and other frontend resources.
Common Django Deployment Mistakes
Some common issues during Django deployment include:
- Forgetting to run
collectstatic - Leaving
DEBUG=Truein production - Not adding the domain or server IP to
ALLOWED_HOSTS - Using an incompatible Python version
- Forgetting to activate the virtual environment
- Incorrect Gunicorn configuration
- Incorrect Nginx configuration
- Not restarting Gunicorn or Passenger after making changes
Checking these settings can help resolve many common deployment problems.
Troubleshooting Django Deployment
If your Django application returns a 500 Internal Server Error, check the application and server logs first.
For a Passenger-based setup, check the relevant application log, for example:
tail -n 50 passenger.log
If static files are not loading, review the STATIC_URL and STATIC_ROOT settings and make sure collectstatic has been completed.
For a VPS deployment, you can also check Gunicorn:
sudo systemctl status gunicorn
If necessary, restart it:
sudo systemctl restart gunicorn
You can also restart Nginx:
sudo systemctl restart nginx
Frequently Asked Questions
Can I deploy Django on shared hosting?
Yes. If your hosting account supports Python applications and Passenger, you can deploy Django through cPanel’s Setup Python App interface.
Can I deploy Django on a VPS?
Yes. A VPS provides full server access, allowing you to configure Python, a virtual environment, Gunicorn, and Nginx for your Django application.
Do I need Gunicorn for shared hosting?
Not necessarily. On supported shared hosting, Passenger can manage the Python application process. Gunicorn is commonly used when deploying Django on a VPS.
Can I use Git to deploy my Django application?
Yes. On a VPS, you can clone your Django project directly from a Git repository into your application directory. Shared hosting may also provide Git integration depending on the hosting environment.
Why is my Django website showing a 500 error?
A 500 error can be caused by incorrect Django settings, missing dependencies, database problems, incorrect ALLOWED_HOSTS, or server configuration issues. Check your application and server logs to identify the exact problem.
Can I move my Django application from shared hosting to a VPS?
Yes. You can migrate the Django project, database, dependencies, static files, and media files to a VPS and then configure Gunicorn and Nginx.
Conclusion
Deploying a Django application on shared hosting or a VPS becomes straightforward when the required Python environment and server configuration are set up correctly.
For shared hosting, cPanel and Passenger simplify the deployment process by managing the Python application environment. For a VPS, Gunicorn and Nginx provide greater control over how your Django application runs.
Whether you are hosting a small website or a larger Python application, using a virtual environment, configuring the database correctly, and managing static files properly will help keep your Django application running smoothly.
Learn more LiteSpeed vs. Nginx: A Deep-Dive Performance Benchmark Analysis