Django Deployment Guide with aaPanel, Nginx, and Python Manager
I get so bored when I have to search for more tutorials and solve more errors when deploying Django projects. So, I decided to write this tutorial to walk through the steps of how to deploy a Django project on your VPS server using aaPanel, Nginx, and Python Manager.
Environment Specifications
- Operating System: Ubuntu 20.04 (VPS)
- Control Panel: aaPanel
- Python Manager: 2.0.0
- Django Version: 3.2.25
- Process Manager: Gunicorn
- Database: MySQL 5.7
- Web Server: Nginx 1.24.0
Prerequisites
- aaPanel installed on your server
- Python Manager 2.0.0 installed via aaPanel
- MySQL 5.7 installed and configured
Deployment Steps
1. Project Setup
For this example, we'll use a sample project:
https://github.com/ferdyhape/Django_Product_Management.git
Clone the project to your server directory. For example, if your project directory is /www/wwwroot/django:
git clone https://github.com/ferdyhape/Django_Product_Management.git
Example of the cloning process:
After cloning:
2. Python Project Configuration
-
Access the aaPanel dashboard and navigate to Python Manager
- If Python Manager isn't visible, enable it via App Store → Display on Dashboard
- If Python Manager isn't visible, enable it via App Store → Display on Dashboard
-
Click "Add Project" and configure your settings:
3. Django Settings Configuration
- Open your project's
settings.pyfile and update the following:
Configure allowed hosts:
ALLOWED_HOSTS = ['yourdomain.com', 'youripaddress']
4. Database Setup
-
In aaPanel, go to Database → MySQL tab and click "Add DB"
-
Fill in the database details as needed
-
Update your
settings.pywith the database configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_db_name',
'USER': 'your_db_user',
'PASSWORD': 'your_db_password',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': {
"sql_mode": "STRICT_TRANS_TABLES",
}
}
}
- Update timezone settings:
TIME_ZONE = "Asia/Jakarta" # Change to your timezone
# USE_I18N = True
# USE_TZ = True
- Configure static and media files:
STATIC_URL = "/assets/"
STATICFILES_DIRS = [os.path.join(BASE_DIR, "assets")]
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
MEDIA_ROOT = os.path.join(BASE_DIR, "file_upload") # file_upload is the folder name
MEDIA_URL = "/media/" # media is the url name
5. Dependencies Installation
Navigate to your project's virtual environment directory (you'll see a unique folder name):
Open terminal in that directory:
Install requirements:
unique_folder_name/bin/pip3 install -r requirements.txt
Example of installation:
Note: You can use the tab key to autocomplete the folder name.
6. Database Migration
Run migrations:
unique_folder_name/bin/python3 manage.py migrate
Example of migration:
7. Domain Mapping
-
In Python Manager, click "Mapping" for your project:
-
Configure project settings including user permissions:
8. Nginx Configuration
-
Go to Website section in aaPanel and click "Config" on your project
-
Add static files location to your Nginx configuration:
server {
listen 80;
server_name yourdomain.com;
...
# Add this location for the static files
location /assets/ {
autoindex on;
alias /www/wwwroot/django/Django_Product_Management/assets/;
}
...
}
9. File Upload Permissions
Set proper permissions for media uploads:
chown -R www-data:www-data file_upload
Verification
Your Django application should now be accessible through your configured domain or IP address.
Troubleshooting
- Check aaPanel's error logs if the application doesn't start
- Verify Nginx configuration syntax
- Ensure all file permissions are correctly set
- Confirm database connectivity
Additional Notes
- Regularly backup your database and media files
- Keep your Django installation updated
- Monitor server resources through aaPanel
- Configure SSL certificate for production deployment