Run Gunicorn service on DigitalOcean starting a Django project - django

I have the following systemd file at /etc/systemd/system/gunicorn.service
[Unit]
Description=Gunicorn daemon for Django Project
Before=nginx.service
After=network.target
[Service]
WorkingDirectory=/home/serverapp
ExecStart=gunicorn --name=avesoft --bind unix:/home/serverapp/gunicorn.socket --config /etc/gunicorn.d/gunicorn.py WebApp.wsgi:application
Restart=always
SyslogIdentifier=gunicorn
User=root
Group=www-data
[Install]
WantedBy=multi-user.target
When I manually change the directory to /home/serverapp and run gunicorn --name=avesoft --bind unix:/home/serverapp/gunicorn.socket --config /etc/gunicorn.d/gunicorn.py WebApp.wsgi:application everything works fine and Nginx connects to my Django app through Gunicorn.
But by rebooting the server, I get Bad Gateway error that seems Gunicorn has not started working. I don't get what is the reason behind my service file not working.

So I fixed that problem by providing the path for gunicorn :
[Unit]
Description=Gunicorn daemon for Django Project
Before=nginx.service
After=network.target
[Service]
WorkingDirectory=/home/serverapp
ExecStart=/usr/bin/gunicorn --name=avesoft --bind unix:/home/serverapp/gunicorn.socket --config /etc/gunicorn.d/gunicorn.py WebApp.wsgi:application
Restart=always
SyslogIdentifier=gunicorn
User=root
Group=www-data
[Install]
WantedBy=multi-user.target

Related

How to add Gunicorn to Django app running in docker?

I have a django app running with a docker in a Digitalocean droplet.
My question is, where do I add the gunicorn.socket and the gunicorn.service? In the Django Docker app or in the DigitalOcean running the docker?
`gunicorn.socket is:
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
and gunicorn.service is:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myprojectdir
ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
--access-logfile - \
-k uvicorn.workers.UvicornWorker \
--workers 3 \
--bind unix:/run/gunicorn.sock \
myproject.asgi:application
[Install]
WantedBy=multi-user.target

Gunicorn error: unrecognized arguments wsgi:application

I was trying to run Django in Nginx with the centos7 platform and also I am a newbie to it. After some time, I configured Django in Nginx with the help of the gunicorn service, But suddenly gunicorn service stopped with an unrecognized argument error (WSGI).
enter image description here
and my gunicorn service file:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=user
Group=nginx
WorkingDirectory=/home/websitehg/websites/qatarfactory/qf_project
ExecStart=/home/websitehg/websites/qatarfactory/qf_env/bin/gunicorn --workers 3 --reload true --bind unix:/home/websitehg/websites/qatarfactory/qf_project/qf_project.sock qf_project.wsgi:app
[Install]
WantedBy=multi-user.target
I don't know what's wrong with it
I added threads to it, Problem solved for me
--threads=3

Question about EnviromentFile config setting for Gunicorn and Django production, do I need to use it?

I have a Django app in production, using gunicorn for help. I have a question about the setting EnviromentFile, should I refence the venv path for my project? Is it mandatory? The reason why I'm not adding it in my service file, is because I'm not serving any variables through the env.
/etc/systemd/system/gunicorn.service :
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=user
Group=www-data
WorkingDirectory=/home/user/alpha/project
ExecStart=/home/user/alpha/project/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
project.wsgi:application
[Install]
WantedBy=multi-user.target
Do I still need to add, EnviromentFile,to my file above? And if I do, is referencing the activation needed? Like so: EnviromentFile=PATH/TO/venv/bin/activate?

"name".sock file is not being created

I am trying to configure Gunicorn and Ngnix my Ngnix is configured correctly but Gunicorn is not. I am not able to create "name".sock file.
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After= network.target
[Service]
User=dexter
dexter=www-data
WorkingDirectory=/home/dexter/Documents/cpg_ad_tech/cpg_ad_tech ExecStart=/home/dexter/Documents/cpg_ad_tech/cpg_ad_tech_env/bin/gunicorn
--access-logfile - \
--workers 3 \
--bind unix:/home/dexter/Documents/cpg_ad_tech/cpg_ad_tech/cpg_ad_tech.sock \
cpg_ad_tech.wsgi:application
[Install]
WantedBy=multi-user.target
This could be problem with access rights. You can tray for tests with sudo
Here is configure which really works.
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=user
Group=nginx
WorkingDirectory=/var/www/skystock
ExecStart=/var/www/env/env36/bin/gunicorn --workers 3 --bind unix:/var/www/skystock/tmp/sky.sock skystock.wsgi:application
[Install]
WantedBy=multi-user.target
Of course, you should have the rights to access the folder.
sudo chown user:user /var/www/skystock

How to run multiple Django App Gunicorn systemd?

I have two django projects running on different ports in same ec2 instance, One I have configured with gunicorn system as follows by creating gunicorn.service file at /etc/systemd/system/ which is working awesome if I run the command systemctl start gunicorn. Now I want to run another proect, how can I configure it, currently I am running that using gunicorn command /usr/local/bin/gunicorn --graceful-timeout 30 --bind 0.0.0.0:8690 projectFile.wsgi:application --daemon, Can I add same in gunicorn.service itself. How do I configure multiple projects in gunicorn systemd?
gunicron.service file -
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
PIDFile=/tmp/gunicorn.pid
LogFile=/tmp/lgunicorn.log
User=root
Group=www-data
WorkingDirectory=/home/ubuntu/website/UniservedWebsite
ExecStart = /usr/local/bin/gunicorn --workers 2 --graceful-timeout 30 --bind 0.0.0.0:8134 UniservedWebsite.wsgi:application
[Install]
WantedBy=multi-user.target
Just create two .service file, and start them separately...