I'm setting up a Django project on a server, following this guide on DigitalOcean. However, I'm very unfamiliar with Gunicorn and nginx, so I'm running into issues.
The server is running Ubuntu 18.04, Python 3.6.7, Django 2.1.3, virtualenvwrapper 4.8.4, gunicorn 19.9.0.
When running curl --unix-socket /run/gunicorn.sock localhost returns curl: (56) Recv failure: Connection reset by peer
I'm suspecting it is the way I handle the SECRET_KEY of Django.
I'm using virtualenvwrapper to handle my virtual environments and set environment variables in the postactivate and unset them in predeactivate in the following manner:
postactivate:
if [[ -n $SECRET_KEY ]]
then
export SECRET_KEY_BACKUP=$SECRET_KEY
fi
export SECRET_KEY='my-secret-key'
predeactivate:
if [[ -n $SECRET_KEY_BACKUP ]]
then
export SECRET_KEY=$SECRET_KEY_BACKUP
unset SECRET_KEY_BACKUP
else
unset SECRET_KEY
It's clear that the variables are not available unless the virtual env is activated. I retrieve the variables in settings.py with os.environ.get('SECRET_KEY').
How do gunicorn work with virtual environments? Is there a way to have gunicorn activate the environment?
Otherwise, what is best practice when it comes to the SECRET_KEY?
Thank you very much!
Edit: Added variable retrieval method.
Edit2: systemd service-file:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=<name>
Group=www-data
WorkingDirectory=/home/<name>/projects/<projectname>/<appname>
ExecStart=/home/<name>/.virtualenvs/<appname>/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
<appname>.wsgi:application
[Install]
WantedBy=multi-user.target
Related
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?
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
I'm deploying a django project on digital ocean. My project has the following structure:
$/
flo-log/
logistics/
settings.py
wsgi.py
...
manage.py
...
My project gunicorn fille is shown below:
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=flolog
Group=flolog
WorkingDirectory=/home/flolog/flo-log/logistics
ExecStart=/home/flolog/flologenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
logistics.wsgi:application
[Install]
WantedBy=multi-user.target
I created a user with sudo privilege. Whenever I run sudo systemctl status gunicorn I encountered ModuleNotFoundError: no module named logistics.
And from the details of the error, it's due to the logistics.wsgi: application component of gunicorn service file
The error stated that there's no module called logistics however,I have a directory called logistics which contains the wsgi file. Please how do I fix this error? Why is it that they are telling me logisticsbis not a directory. Better still, how do I set myproject.wsgi section of gunicorn service file properly based on my project structure
I would say just look at your project structure. Remove the flo-log first folder and have Logistics and manage.py inside the home directory of your server as well as your virtual env. The project file "Logistics" should be in the same folder as manage.py inside home.
Hope that helps.
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...
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.