Deploy Django Python App on CloudPanel

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

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

2. Click Create a Python Site.

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

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

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

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

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-pip
python3 -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

7. Migrate your database

python3 manage.py makemigrations
python3 manage.py migrate

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

python3 manage.py collectstatic

9. Let’s allow access to our port through the firewall.

ufw allow 8090

10. 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.

11. 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.socket

Nov 20 09:34:12 vm-fa710939 systemd[1]: Listening on gunicorn socket.

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

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.

13. 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.

Was this article helpful?

Related Articles

Leave A Comment?