How to Deploy a Python Django Application on Hostinger VPS with CloudPanel (Example: deplow)
Prerequisites
Before we start, ensure you have the following:
- A Hostinger VPS with CloudPanel installed.
- Python 3.x installed on the server.
- A domain configured (e.g., py.uplesk.com).
- A working Django application (we’ll use deplow as an example).
- SSH access to your VPS server.
Step 1: Setting Up CloudPanel
CloudPanel is an easy-to-use server management panel for VPS hosting that can handle multiple applications, including Python-based ones like Django.
Once your CloudPanel is installed on your Hostinger VPS, follow these steps:
- Log into CloudPanel: Open your browser and access CloudPanel using your server’s IP address (or domain) and port :8443. It will look something like:
http://your-server-ip:8443
. - Create a New Application: In the CloudPanel dashboard, click on “Create Application”. Select Python as the application type. Set a domain for your app (e.g., py.uplesk.com) and assign a document root. This is where your Django project will reside.
- Configure the Application: CloudPanel will automatically set up the environment for running your Python app. But, you may still need to perform additional steps, such as installing dependencies.
Step 2: Install Dependencies on the Server
Now that CloudPanel has created the Python environment, you need to install the necessary dependencies:
- SSH into Your VPS: Log in to your VPS via SSH:
ssh root@your-server-ip
- Install Virtualenv (if not already installed):
sudo apt install python3-venv
- Create a Virtual Environment for Django:
cd /home/cloudpanel/your-app-folder # navigate to your application directory
python3 -m venv venv
source venv/bin/activate
- Install Django and Gunicorn:
pip install django gunicorn
Step 3: Set Up Your Django Application
Now, either clone your Django project or create a new one:
django-admin startproject deplow
Navigate to your Django project directory:
cd deplow
In settings.py
, ensure the ALLOWED_HOSTS
include your domain:
ALLOWED_HOSTS = ['127.0.0.1', 'py.uplesk.com']
Step 4: Configure Gunicorn to Serve Django
Test Gunicorn with the following command to ensure it works:
gunicorn --bind 127.0.0.1:8000 deplow.wsgi
Once confirmed that Gunicorn is running, you can run it in the background using:
nohup gunicorn --bind 127.0.0.1:8000 deplow.wsgi &
Step 5: Configure Nginx as a Reverse Proxy with CloudPanel
With CloudPanel, Nginx is automatically configured as the reverse proxy for your application. However, you may need to ensure that it’s configured to forward requests from the public domain to the local Gunicorn server.
- Log into CloudPanel and go to your application.
- Configure Nginx: CloudPanel will have a default Nginx configuration for your application, but you may need to confirm that it includes the following in the proxy settings:
server { listen 80; server_name py.uplesk.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Step 6: Restart Nginx and Apply Changes
- Restart Nginx to apply the changes:
sudo systemctl restart nginx
- Ensure that Nginx is correctly forwarding traffic to Gunicorn by accessing py.uplesk.com in a browser.
Step 7: Set Up Domain Name and DNS
Make sure the DNS settings of py.uplesk.com point to your VPS IP address. You can configure DNS through your domain registrar.
- Log in to your domain registrar’s control panel.
- Set the A record for py.uplesk.com to point to the IP address of your VPS.
Step 8: Monitor Gunicorn with Systemd (Optional)
You may want Gunicorn to run as a service and restart automatically if the server reboots. To do this, create a systemd service file for Gunicorn:
- Create the Gunicorn service file:
sudo nano /etc/systemd/system/gunicorn.service
- Add the following configuration to the file:
[Unit] Description=Gunicorn instance to serve deplow After=network.target [Service] User=root Group=www-data WorkingDirectory=/home/cloudpanel/your-app-folder/deplow ExecStart=/home/cloudpanel/your-app-folder/venv/bin/gunicorn --workers 3 --bind unix:/home/cloudpanel/your-app-folder/gunicorn.sock deplow.wsgi:application [Install] WantedBy=multi-user.target
- Enable and start the service:
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
Step 9: Final Test
After completing all steps, visit http://py.uplesk.com in your browser. If everything is set up correctly, your Django application deplow should be up and running.
Conclusion
In this blog post, we’ve covered the process of deploying a Python Django application on Hostinger VPS with CloudPanel, using deplow as an example. With CloudPanel’s user-friendly interface, the deployment process is simplified, and you can ensure your Django application runs efficiently. By following the steps above, your app will be accessible via your domain, py.uplesk.com, and ready for production!