In my wsgi.py I am conditionally setting DJANGO_SETTINGS_MODULE to two different files(local and production).
On the server I have set "PROD" variable in /etc/profile
if "PROD" in os.environ:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
else:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings_dev")
But, still I am getting an error because right settings file is not being set. So maybe if condition isn't working. See below pic.
My gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/home/myproject/myproject
ExecStart=/usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/myproject/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
You should set PROD environment variable in your Gunicorn configuration this way:
[Service]
environment=PROD=True
User=root
Group=www-data
WorkingDirectory=/home/myproject/myproject
ExecStart=/usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/myproject/myproject/myproject.sock myproject.wsgi:application
// EDIT: I overlooked that you set ENV var in /etc/profile... You can try my solution, otherwise I will delete my answer.
You can set the environment variable in Gunicorn.service file. From there it can be accessed by Gunicorn.
[Service]
...
ExecStart=/virtuall_env_path/bin/gunicorn --workers 3 --bind
unix:/path_to_myproject.sock
myproject.wsgi:application -e my_var=value -e my_var2=value2
...
Related
I use Django-Q for task queue and scheduler. I need to keep running the command: python manage.py qcluster.
How can I do it with Systemd?
I've found this code for .service file but I don't know how to use my Virtualenv for python path:
[Unit]
Description=Async tasks runner
After=network.target remote-fs.target
[Service]
ExecStart=/usr/bin/django-admin qcluster --pythonpath /path/to/project --settings settings
User=apache
Restart=always
[Install]
WantedBy=multi-user.target
Use the django-admin binary installed in your virtualenv's bin directory, or the python binary there to run manage.py within your project's working directory:
ExecStart=/path/to/my-venv/bin/django-admin qcluster --pythonpath /path/to/project --settings settings
or
ExecStart=/path/to/my-venv/bin/python manage.py qcluster --pythonpath /path/to/project --settings settings
RootDirectory=/path/to/project
For those who still have problem with this, Just follow these steps:
create a service, example:
sudo nano /etc/systemd/system/qcluster.service
Edit service as follows:
[Unit]
Description=qcluster runner
After=network.target
[Service]
User=user
WorkingDirectory=/home/user/path_to_project
ExecStart=/home/user/path_to_project_env/bin/python manage.py qcluster
[Install]
WantedBy=multi-user.target
Enable the service:
sudo systemctl enable qcluster.service
Start the service:
sudo systemctl start qcluster.service
I am trying to setup a simple flask application served by Nginx and Gunicorn and have mostly followed this tutorial. When trying to access the webpage, I get a 502 Bad Gateway error.
The nginx log (/var/log/nginx/error.log) says :
[crit] 23472#0: *1 connect() to unix:/home/crawforc3/webpage/webpage.sock failed (13: Permission denied)
I checked and there is a webpage.sock file in my project directory that looks like this:
srwxrwxrwx 1 crawforc3 www-data 0 May 31 17:59 webpage.sock
Here is my /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=crawforc3
Group=www-data
WorkingDirectory=/home/crawforc3/webpage
Environment="PATH=home/crawforc3/miniconda3/envs/HALSWEBPAGE/bin"
ExecStart=/home/crawforc3/miniconda3/envs/HALSWEBPAGE/bin/gunicorn --workers 3 --bind unix:/home/crawforc3/webpage/webpage.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
I discovered that there was a discrepancy with /etc/nginx/nginx.conf
I had the user set as www-data and I should have set it as crawforc3. Making this change and restarting nginx and gunicorn resolved the issue.
I set up a VPS (django + gunicorn + nginx ) and it was working fine showing default django screen, but after I updated my django code and made all migrations I thought that now I need to restart gunicorn to apply changes.
So I did this:
sudo systemctl restart gunicorn
After this I've got Internal Server Error
Everything is set up like here
gunicorn.service :
[Unit]
Description=gunicorn daemon
After=network.targett
[Service]
User=thekotik
Group=www-data
WorkingDirectory=/home/thekotik/glboy
ExecStart=/home/thekotik/glboy/denv/bin/gunicorn --workers 3 --bind unix:/home/thekotik/glboy/closer.sock closer.wsgi:application
[Install]
WantedBy=multi-user.target
<class 'socket.error'>, [Errno 13] Permission denied: file: /usr/lib/python2.7/socket.py line: 228
unix socket files holds the same case unix file permissions; the file ( /home/thekotik/glboy/closer.sock) is currently being used or not owned by gunicorn process user that causes error 13.
I recommend using the TCP option --bind 127.0.0.1:8000.For tcp based changes,
gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.targett
[Service]
User=thekotik
Group=www-data
WorkingDirectory=/home/thekotik/glboy
ExecStart=/home/thekotik/glboy/denv/bin/gunicorn --workers 3 --bind 127.0.0.1:9090 closer.wsgi:application
[Install]
WantedBy=multi-user.target
use above systemd service file for gunicorn & change the proxy option in nginx to
proxy_pass http://127.0.0.1:9090$request_uri;
that will avoid unix socket file based permission problems.
I am deploying a flask app that is running inside virtual env
I have the systemd file as follow:
[Unit]
Description=Gunicorn instance to serve my-page
After=network.target
[Service]
User=jb
Group=www-data
WorkingDirectory=/home/jb/webjosue/my-page
Environment="PATH=/home/jb/webjosue/FlaskENV/bin"
ExecStart=/home/jb/webjosue/FlaskENV/bin/gunicorn --workers 3 --bind unix:my-page.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
In my main.py I have
settings = os.environ['APP_SETTINGS']
and I get this error
raise KeyError(key)
Sep 24 00:10:13 ubuntu-512mb-nyc3-01 gunicorn[23439]: KeyError: 'settings'
I am guessing since I have my environment pointing to the virtualenv (FlaskENV) the other environment variables that are in my .profile are not being recognized.
Any ideas?
As #Joe Doherty said here, you can use Environment directive in Service section to add environment variables, more information. For instance:
[Service]
Environment="PATH=/xx/yy/zz/venv/bin"
Environment="FLASK_ENV=development"
Environment="APP_SETTINGS=config.DevelopmentConfig"
I was using virtualenv so /xx/yy/zz/venv/bin is the path of virtualenv folder.
Somehow Environment="FLASK_ENV=development" with quotes wasn't working
Environment=FLASK_ENV=development
with no quotes was working just fine
I'm trying to setup gunicorn with systemd for my django project but it fails to load another library of my project.
File "/home/ubuntu/venv/lib/python3.4/importlib/__init__.py", line 109, in import_module
gunicorn[6043]: return _bootstrap._gcd_import(name[level:], package, level)
gunicorn[6043]: ImportError: No module named 'templates'
Templates is another part of the project that is in a separate directory. I would get the same error if I try to run the site without /home/ubuntu/templates/ in my $PYTHONPATH, I've added the pythonpath in my systemd unit file but it didn't do anything.
I can successful run gunicorn with this command:
/home/ubuntu/venv/bin/gunicorn --pid /tmp/pid-gunicorn site_gfa.wsgi:application -b 0.0.0.0:8083
But I fails in systemd
Systemd Unit File
[Unit]
Description="Site"
After=network.target
[Service]
PIDFile=/tmp/pid-gunicorn
User=ubuntu
Group=users
Environment=PYTHONPATH='/home/ubuntu/templates/'
WorkingDirectory=/home/ubuntu/gfa-apps/
ExecStart=/home/ubuntu/venv/bin/gunicorn --pid /tmp/pid-gunicorn site_gfa.wsgi:application -b 0.0.0.0:8083
PrivateTmp=true
Type=forking
[Install]
WantedBy=multi-user.target
I'm running CentOS7 with Python 3.4 and gunicorn 19.4.5
Thanks in advance!
Solved it, the Environment var in systemd unit file shouldn't have quotes around it.
[Unit]
Description="Site"
After=network.target
[Service]
PIDFile=/tmp/pid-gunicorn
User=ubuntu
Group=users
Environment=PYTHONPATH=/home/ubuntu/templates/
WorkingDirectory=/home/ubuntu/gfa-apps/
ExecStart=/home/ubuntu/venv/bin/gunicorn --pid /tmp/pid-gunicorn site_gfa.wsgi:application -b 0.0.0.0:8083
PrivateTmp=true
Type=forking
[Install]
WantedBy=multi-user.target