What you need to do is the following:
In your requirements.txt file, make sure you have the following packages:
daphne==4.1.2
channels==4.1.0
channels-redis==3.4.1
Then you will do a standard deployment as per the tutorial above
Once your Django site is up and running, it's time to switch to running on ASGI. Make sure you can run your site locally using ASGI and Daphne. Below is how my ASGI is configured:
`import django
import os
settings_module = 'app.settings.development' if 'DEVELOPMENT' in os.environ else 'app.settings.production'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_module)
django.setup()
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from django.core.asgi import get_asgi_application
import podcast.routing
application = ProtocolTypeRouter(
{
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
podcast.routing.websocket_urlpatterns
)
),
}
)`
- Now, stop the web app in aaPanel in Python Manager,
- Next, you need to create a service in systemd. I called mine gunicorn.service (but you can call it anything you want). - Make sure you have signed into your VM using putty or something similar. You will also need root access.
To do this run the following commands
sudo nano /etc/systemd/system/gunicorn.service
Paste the following code into the service
`# /etc/systemd/system/daphne.service
[Unit]
Description=Daphne daemon for ASGI application
After=network.target
[Service]
User=www
Group=www-data
WorkingDirectory=/www/wwwroot/YourWebApp
ExecStart=/www/wwwroot/YourWebApp/845a0f0f7d207b4b8ff321b28924c442_venv/bin/daphne -b 0.0.0.0 -p 8099 app.asgi:application
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
`
- Make sure to change the name of 'YourWebApp' to your actual folder name where your django code lives
- Make sure to use the correct venv (mine is called 845a0f0f7d207b4b8ff321b28924c442_venv, yours will be different) folder name that is created when you deployed your django app
- And lastly the line of code that says 'app.asgi:application', make sure to use the correct folder path to your asgi file
Next run (make sure you have change directory to /etc/systemd/system)
sudo systemctl daemon-reload
sudo systemctl start gunicorn.service
sudo systemctl enable gunicorn.service
sudo systemctl status gunicorn.service
you should see the following in the terminal
gunicorn.service - Daphne daemon for ASGI application
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; preset: enabled)
Active: active (running) since Wed 2024-10-16 16:23:25 UTC; 1 day 14h ago
Main PID: 2317748 (daphne)
Tasks: 8 (limit: 9448)
Memory: 79.2M (peak: 79.7M)
CPU: 14min 26.964s
CGroup: /system.slice/gunicorn.service
└─2317748 /www/wwwroot/YourWebApp/845a0f0f7d207b4b8ff321b28924c442_venv/bin/python3.11 /www/wwwroot/ListenIn.club/845a0f0f7d207b4b8ff321b28924c442_venv/b>