---
title: Deploy Django Python App on CloudPanel
description: "In this guide, we will show you how you can deploy your Django Python application on your CloudPanel. #### Create your site. Go to your CloudPanel dashboard and click **Add Site**. ![](https://truehost.com/support/wp-content/uploads/2023/11/image-20-1024x96.png) #### Click **Create a Python Site**. ![](https://truehost.com/support/wp-content/uploads/2023/11/image-21-1024x148.png) ####…"
url: "https://truehost.com/support/knowledge-base/deploy-django-python-app-on-cloudpanel/"
updated: "2024-10-18"
category: Cloudpanel
---

# Deploy Django Python App on CloudPanel

In this guide, we will show you how you can deploy your Django Python application on your CloudPanel.

#### Create your site. Go to your CloudPanel dashboard and click **Add Site**.

![](https://truehost.com/support/wp-content/uploads/2023/11/image-20-1024x96.png)

#### Click **Create a Python Site**.

![](https://truehost.com/support/wp-content/uploads/2023/11/image-21-1024x148.png)

#### Add the domain. Other fields will be filled automatically. Then click **Create**. The Python site will be created.

![](https://truehost.com/support/wp-content/uploads/2023/11/image-22-1024x285.png)

#### Next, we'll install SSL certificate for your site. Click the **SSL/TLS** tab, click Actions then click **New Let's Encrypt Certificate**.

![](https://truehost.com/support/wp-content/uploads/2023/11/image-23-1024x285.png)

#### Upload your files. You can upload through FileZilla or CloudPanel's File Manager. Upload all your python files except** env** or **Venv** folder.

![](https://truehost.com/support/wp-content/uploads/2023/11/image-24-1024x391.png)

#### Let's install Django and create a virtual environment. Go to the Settings page and copy the Root Directory.

![](https://truehost.com/support/wp-content/uploads/2023/11/image-25-1024x56.png)

##### Log in via SSH as root to your VPS. Access the root directory by typing the following command:

```
cd /home/<your-domain>/htdocs/www.<your-domain>.com
```

##### Create a virtual environment:

```
apt install -y python3.10-venv python3-pippython3 -m venv env
```

##### Activate the virtual environment

```
source env/bin/activate
```

##### If your terminal has **env,** then your virtual environment is active.

##### Now install **requirements.txt** file:

```
pip install -r requirements.txt
```

##### Now install **Django** and Gunicorn

```
pip install django gunicorn psycopg2-binary
```

#### Migrate your database

```
python3 manage.py makemigrations
python3 manage.py migrate
```

#### Next, let's collect our static files. Ensure your STATIC_ROOT is set well. Then run the following command:

```
python3 manage.py collectstatic
```

#### Let's allow access to our port through the firewall.

```
ufw allow 8090
```

#### You can test your project by starting up the Django development server with this command:

```
python3 manage.py runserver 0.0.0.0:8090
```

In your web browser, visit your domain name. If your site loads, then the setup was successful.

#### We need to set up Gunicorn to as a more robust way to start or stop our site. First, deactivate from virtual environment and create a Gunicorn socket:

```
deactivate
nano /etc/systemd/system/gunicorn.socket
```

Copy/paste this code there then save the file:

```
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target
```

##### Then create and open a systemd service file for Gunicorn

```
nano /etc/systemd/system/gunicorn.service
```

Copy/paste this code there. Change the bold parts to match your application i.e user to your site user, directory to your root directory and wsgi application to directory where the wsgi file is found (exclude the root directory). Save the file.

```
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=projectkeepwalking
Group=www-data
WorkingDirectory=/home/projectkeepwalking/htdocs/www.projectkeepwalking.xyz
ExecStart=/home/projectkeepwalking/htdocs/www.projectkeepwalking.xyz/env/bin/gunicorn 
          --access-logfile - 
          --workers 3 
          --bind unix:/run/gunicorn.sock 
          django_project.wsgi:application

[Install]
WantedBy=multi-user.target
```

Let's start and enable the Gunicorn socket. When a connection is made to that socket, systemd will automatically start the `gunicorn.service` to handle it:

```
systemctl start gunicorn.socket
systemctl enable gunicorn.socket
```

To check the status of the Gunicorn socket, type the following command:

```
systemctl status gunicorn.socket
```

The status should be similar to this:

```
Output● gunicorn.socket - gunicorn socket     Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)     Active: active (listening) since Mon 2023-11-20 09:34:12 UTC; 34s ago   Triggers: ● gunicorn.service     Listen: /run/gunicorn.sock (Stream)     CGroup: /system.slice/gunicorn.socketNov 20 09:34:12 vm-fa710939 systemd[1]: Listening on gunicorn socket.
```

#### Go to the CloudPanel Site Settings, then click the Vhost tab.

![](https://truehost.com/support/wp-content/uploads/2023/11/image-26-1024x341.png)
Replace the Vhost code with this one:

```
server {
  listen 80;
  listen [::]:80;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  {{ssl_certificate_key}}
  {{ssl_certificate}}
  server_name www.projectkeepwalking.xyz;
  {{root}}

  {{nginx_access_log}}
  {{nginx_error_log}}

  if ($scheme != "https") {
    rewrite ^ https://$host$uri permanent;
  }

  location ~ /.well-known {
    auth_basic off;
    allow all;
  }

 location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/projectkeepwalking/htdocs/www.projectkeepwalking.xyz/blog;
    }

  {{settings}}

  index index.html;

 location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

  #location ~* ^.+.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|woff2|eot|mp4|ogg|ogv|webm|webp|zip|swf)$ {
  #  add_header Access-Control-Allow-Origin "*";
  #  expires max;
  #  access_log on;
  #}

  if (-f $request_filename) {
    break;
  }
}
```

Replace the server_name with your domain name and root with your root directory. Click the **Save** button.

#### If there are no errors, go ahead and restart Nginx. Type this in the terminal:

```
systemctl restart nginx
ufw delete allow 8090
ufw allow 'Nginx Full'
```

Check the status of Nginx:

```
systemctl status nginx
```

##### Done. Now you can refresh your domain your site will be up and running.
