How setup gunicorn to read custom settings with systemd - django

I migrated a server I had to Ubuntu 16.04 and systemd replaced upstart. I have three developing environments: Production, Staging and Development. Only the latter one uses a local database, the other two use a remote one.
So, in order to handle the deployment and the service I managed to create the following systemd service file:
[Unit]
Description=Gunicorn service for Project
After=network.target
[Service]
User=projuser
WorkingDirectory=/home/projuser/sites/Project/source
ExecStartPre=/usr/bin/env DJANGO_SETTINGS_MODULE=v3.settings_staging
ExecStart=/home/projuser/sites/Project/env/bin/gunicorn --bind 127.0.0.1:8090 --workers 6 --access-logfile ../access.log --error-logfile ../error.log v3.wsgi:application
[Install]
WantedBy=multi-user.target
The service starts, but it seems to ignore this line: ExecStartPre=/usr/bin/env DJANGO_SETTINGS_MODULE=v3.settings_staging. I can see in the logs the following:
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Mostly because it is using my default settings.py file, which I use for development.
How can I make sure it loads the staging settings.py file?

Well, passing as an argument to gunicorn also works:
--env DJANGO_SETTINGS_MODULE=myproject.settings
https://gunicorn-docs.readthedocs.io/en/latest/run.html#gunicorn-django
What I wanted could be achieved by having the following file in /etc/systemd/system:
[Unit]
Description=Gunicorn service for a Django Project
After=network.target
[Service]
User=a_user
WorkingDirectory=/home/a_user/project/source
Environment="DJANGO_SETTINGS_MODULE=app.settings_staging"
ExecStart=/home/a_user/envs/project_env/bin/gunicorn --bind 127.0.0.1:8090 --workers 6 --access-logfile ../access.log --error-logfile ../error.log v3.wsgi:application
[Install]
WantedBy=multi-user.target
Note the following line: Environment="DJANGO_SETTINGS_MODULE=app.settings_staging".
https://www.linux.com/learn/understanding-and-using-systemd

Related

How to make djangoQ run as a service using systemd

How to make DjangoQ run as a service using systemd?
There is a gap, djangoQ installation and running is like learning to run a Django project.
But now,
you want to push the project which is using DjangoQ to production.
The problem is you are not aware of server and Linux, maybe you are even coming from Windows....
So can somebody tell us, How to run DjangoQ in ubuntu using systemd
I am getting the error that ExecStart path is not absolute...
I came from here:
Stack Question
But that answer is not complete, too much tacit knowledge unshared...
[Unit]
Description= Async Tasks Runner
After=network.target remote-fs.target
[Service]
ExecStart=home/shreejvgassociates/ot_britain_college/env/bin/python manage.py qcluster --pythonpath home/shreejvgassociates/ot_britain_college/OverTimeCollege --settin$
RootDirectory=home/shreejvgassociates/ot_britain_college/OverTimeCollege
User=shreejvgassociates
Group=shreejvgassociates
Restart=always
[Install]
WantedBy=multi-user.target
It works well. Try it.
[Unit]
Description=Django-Q Cluster for site TestSite
After=multi-user.target
[Service]
Type=simple
ExecStart=/home/test/TestSite/venv/bin/python3 /home/test/TestSite/manage.py qcluster
[Install]
WantedBy=multi-user.target

nginx index page coming instead of actual website in route 53 with ec2 instance

I have hosted my django website in aws ec2 instance
with below ip and pointed to below domain
nginx file
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/myproject
ExecStart=/home/ubuntu/myproject/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/ubuntu/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
18.221.162.213
http://govtcarrier.in/
When i am going 18.221.162.213 in my browser it is coming my website
but when i am going http://govtcarrier.in/ it is showing nginx welcome page.
Please check the screenshots below

Amazon AWS- Gunicorn is throwing a lacks both error whenever I start systemctl

I am unable to start the gunicorn3 using systemctl for some reason. Below are my commands written in gunicorn 3:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=TodoApp
Group=www-data
WorkingDirectory=/home/TodoApp/project1-na2165-hw1714-ab7232-rdl394-rk3194
ExecStart=/home/TodoApp/project1-na2165-hw1714-ab7232-rdl394-rk3194/TodoAppEnv/bin/gunicorn3--access-logfile - --workers 3 --bind unix:/home/TodoApp/project1-na2165-hw1714-ab7232-rdl394-rk3194/Todo/Todo.sock Todo.wsgi:application
[install]
WantedBy=multi-user.target
I tried changing the name of the user (which I assume can be anything we want) the working directory and the exec start multiple times. However, I still get the following error:
systemd[1]: gunicorn.service: Service lacks both
EDIT:
Sorry I couldn't see the entire error before. The full error says:
gunicorn.service: Service lacks both ExecStart= and ExecStop= setting.
Refusing.

Django settings secret key environment variable 502 nginx

I have a Django webapp running on an Ubuntu server using nginx and gunicorn. I'm trying to get my settings.py set up properly in regards to using environment variables to hide secret information such as the SECRET_KEY as well as API keys.
I've tried putting export SECRET_KEY='secret_key' in .bashrc as well as .profile, and using SECRET_KEY=os.environ['SECRET_KEY'] in my settings.py file, but this throws a 502 bad gateway error with nginx and its version at the bottom, upon restarting gunicorn. I'm not sure what else to try, as I'm pretty new to setting up servers.
I believe this is the init file for my gunicorn service:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=myuser
Group=www-data
WorkingDirectory=/home/myuser/myproject/mysite
ExecStart=/home/myuser/myproject/mysite/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/myuser/myproject/mysite/mysite.sock mysite.wsgi:application
[Install]
WantedBy=multi-user.target
I found this error in the nginx error log when trying to request the site, where it gives the 502 bad gateway:
*20 connect() to unix:/home/myuser/myproject/mysite/mysite.sock failed (2: No such file or directory)
I solved this issue by putting my environment variables within the gunicorn.service file, located in /etc/systemd/system/, as export works only in the current shell.
Env variables were input in the file as the following format:
[Service]
Environment="SECRET_KEY=secret-key-string"

Running Gunicorn behind chrooted nginx inside virtualenv

I can this setup to work if I start gunicorn manually or if I add gunicorn to my django installed apps. But when I try to start gunicorn with systemd the gunicorn socket and service start fine but they don't serve anything to Nginx; I get a 502 bad gateway.
Nginx is running under the "http" user/group, chroot jail. I used pythonbrew to setup the virtualenvs so gunicorn is installed in my home directory under .pythonbrew. The vitualenv directory is owned by my user and the adm group.
I'm pretty sure there is a permission issue somewhere, because everything works if I start gunicorn but not if systemd starts it. I've tried changing the user and group directives inside the gunicorn.service file, but nothing worked; if root start the server then I get no errors and a 502, if my user starts it I get no errors and 504.
I have checked the Nginx logs and there are no errors, so I'm sure it's a gunicorn issue. Should I have the virtualenv in the app directory? Who should be the owner of the app directory? How can I narrow down the issue?
/usr/lib/systemd/system/gunicorn-app.service
#!/bin/sh
[Unit]
Description=gunicorn-app
[Service]
ExecStart=/home/noel/.pythonbrew/venvs/Python-3.3.0/nlp/bin/gunicorn_django
User=http
Group=http
Restart=always
WorkingDirectory = /home/noel/.pythonbrew/venvs/Python-3.3.0/nlp/bin
[Install]
WantedBy=multi-user.target
/usr/lib/systemd/system/gunicorn-app.socket
[Unit]
Description=gunicorn-app socket
[Socket]
ListenStream=/run/unicorn.sock
ListenStream=0.0.0.0:9000
ListenStream=[::]:8000
[Install]
WantedBy=sockets.target
I realize this is kind of a sprawling question, but I'm sure I can pinpoint the issue with a few pointers. Thanks.
Update
I'm starting to narrow this down. When I run gunicorn manually and then run ps aux|grep gunicorn then I see two processes that are started: master and worker. But when I start gunicorn with systemd there is only one process started. I tried adding Type=forking to my gunicorn.services file, but then I get an error when loading service. I thought that maybe gunicorn wasn't running under the virtualenv or the venv isn't getting activated?
Does anyone know what I'm doing wrong here? Maybe gunicorn isn't running in the venv?
I had a similar problem on OSX with launchd.
The issue was I needed to allow for the process to spawn sub processes.
Try adding Type=forking:
[Unit]
Description=gunicorn-app
[Service]
Type=forking
I know this isn't the best way, but I was able to get it working by adding gunicorn to the list of django INSTALLED_APPS. Then I just created a new systemd service:
[Unit]
Description=hack way to start gunicorn and django
[Service]
User=http
Group=http
ExecStart=/srv/http/www/nlp.com/nlp/bin/python /srv/http/www/nlp.com/nlp/nlp/manage.py run_gunicorn
Restart=always
[Install]
WantedBy=multi-user.target
There must be a better way, but judging by the lack of responses not many people know what that better way is.