How to run multiple Django App Gunicorn systemd? - django

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

Related

systemctl enable gunicorn fails with error "/etc/systemd/system/gunicorn.service:8: Missing '='."

I am deploying Django application working on web server using gunicorn locally on WSL.
I need to enable needed systemd files. When I run command
systemctl enable gunicorn
I get error
"/etc/systemd/system/gunicorn.service:8: Missing '='. Failed to enable
unit, file gunicorn.service: Invalid argument.
gunicorn.service looking like that:
[Service]
User=root
Group=www-data
WorkingDirectory=<base_app_path>
Environment="PATH=<base_app_path>/env/bin"
EnvironmentFile=<base_app_path>/.env
ExecStart=<base_app_path>/env/bin/gunicorn \
--workers 4 \
--bind 0.0.0.0:8080 \
meeting.wsgi:application
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
What can be the problem here?

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?

Run Gunicorn service on DigitalOcean starting a Django project

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

How to properly setup systemctl service with gunicorn?

I have a Django project that I run with gunicorn.
My manage.py file is in /home/Projects/myproject/myproject.
My virtualenv is in /home/myproject.
In /lib/systemd/system I created a myproject.service file:
[Unit]
Description=My Project
After=network.target
[Service]
User=my_user
Group=my_group
WorkingDirectory=/home/Projects/myproject/myproject
ExecStart= ???
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
I want to know how to properly configure my service file so that it can run my gunicorn start server command on server restart, failure, etc.