Supervisor cannot start Gunicorn: ENOENT - python

I have deployed Django using gunicorn and nginx. The django project is located in a virtual environment. Everything is working perfectly when I run -
gunicorn mydjangoproject.wsgi -c gunicorn_config.py
I am running the above command inside my Django project folder containing manage.py with the virtual environment active.
However now i want to close the server terminal and want gunicorn to run automatically. For this I am using Supervisor. I have installed supervisor using apt-get and created a gunicorn.conf file in supervisor's conf.d.
But when I run supervisorctl start gunicorn I am getting a fatal error-
gunicorn: ERROR (abnormal termination)
So checked the log file and it says-
supervisor:couldn't exec root/ervirtualenvpy2/bin/gunicorn: ENOENT
child process was not spawned
My configuration file for supervisor's gunicorn.conf looks like this-
[program:gunicorn]
command = root/ervirtualenvpy2/bin/gunicorn myproject.wsgi -c root/path/to/the/gunicorn_conf.py/file
directory = root/ervirtualenvpy2/path/to/myproject/
user=root
autorestart=true

Going by what you said and your config everything seems right except that you have specified relative path rather than absolute path:
see gunicorn docs
Instead it should be:
[program:gunicorn]
command = /root/ervirtualenvpy2/bin/gunicorn myproject.wsgi -c /root/path/to/the/gunicorn_conf.py/file
directory = /root/ervirtualenvpy2/path/to/myproject
user=root
autorestart=true

Also Check your gunicorn file path in env/bin/gunicorn file
In my case, I changed my env directory to another place, so please be sure with that
Wrong path: #!/home/ubuntu/nikhil_project/env/bin/python
Correct path: #!/home/ubuntu/env/bin/python

Related

Gunicorn Config File I've Created Doesn't Exist

I am using Django and Gunicorn to create a blog and am wanting to run my config file that I have created.
This is the path to my config file (the conf folder is at the same level as manage.py):
/var/www/website.co.uk/blog/DjangoBlog/Articles/conf/gunicorn_config.py
I am in this path: /var/www/website.co.uk/blog/DjangoBlog/Articles and run this command:
gunicorn -c gunicorn_config.py Articles.wsgi
However, it returns the error:
Error: 'gunicorn_config.py' doesn't exist
Is Gunicorn looking in the wrong place for my config file?
Any help would be massively appreciated. I have not been able to solve this for a while.
Kind regards
you dont seem to be using the correct directory for the gunicorn config file, try this
gunicorn -c ./conf/gunicorn_config.py Articles.wsgi

Gunicorn: Failed At step EXEC spawning No such file or directory

I am trying to get my gunicorn setup. Following is my config file for the systemmd config file:
# Unit section is used to specify metadata and dependencies
[Unit]
Description=DEMO
# only start once the network target has been reached
After=network.target
# Service section specify the user and group that we want the process to run under
[Service]
# www-data group assigned to facilitate easy communication with nginx
Group=www-data
# path to working directory
WorkingDirectory=/srv/demo/git
# path to virtual environment
Environment="PATH=/srv/demo/git/venv/bin"
ExecStart=/srv/demo/git/venv/bin/gunicorn --workers=5 --bind unix:git.sock --log-level=debug wsgi:app --timeout 3600
#--workers=5 --threads=2 --worker-class=gthread
[Install]
WantedBy=multi-user.target
~
~
~
And my virtual environment folder does contain gunicorn:
But I am still unable to run this, the systemmd file says that it was unable to find gunicorn. No such file or directory:
i have uninstall gunicorn from outside my virtual env. But when i do a which gunicorn it shows me the path to be /usr/bin/gunicorn. When i put this path, it dosnt throw the same error but simply says that the apt_pkg and gunicorn module wasnt found but not a directory error. Is this a path issue? If so how can i fix it?
I hope following steps will be helpful to solve this problem....
Step 1: cd ~/myprojectdir
Step 2: source myprojectenv/bin/activate
Step 3: which gunicorn
Step 4: Paste that into the path section of the ‘ExecStart’ value exchange ‘/etc/systemd/system/gunicorn.service’ by which guicorn '..../path"
Step 5: sudo systemctl daemon-reload
Step 6: sudo systemctl restart gunicorn
Step 7: sudo systemctl status gunicorn

Nginx Django and Gunicorn. Gunicorn sock file is missing?

I have an ansible provisioned VM based on this one https://github.com/jcalazan/ansible-django-stack but for some reason trying to start Gunicorn gives the following error:
Can't connect to /path/to/my/gunicorn.sock
and in nginx log file:
connect() to unix:/path/to/my/gunicorn.sock failed (2: No such file or directory) while connecting to upstream
And actually the socket file is missing in the specified directory. I have checked the permissions of the directory and they are fine.
Here is my gunicorn_start script:
NAME="{{ application_name }}"
DJANGODIR={{ application_path }}
SOCKFILE={{ virtualenv_path }}/run/gunicorn.sock
USER={{ gunicorn_user }}
GROUP={{ gunicorn_group }}
NUM_WORKERS={{ gunicorn_num_workers }}
# Set this to 0 for unlimited requests. During development, you might want to
# set this to 1 to automatically restart the process on each request (i.e. your
# code will be reloaded on every request).
MAX_REQUESTS={{ gunicorn_max_requests }}
echo "Starting $NAME as `whoami`"
# Activate the virtual environment.
cd $DJANGODIR
. ../../bin/activate
# Set additional environment variables.
. ../../bin/postactivate
# Create the run directory if it doesn't exist.
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Programs meant to be run under supervisor should not daemonize themselves
# (do not use --daemon).
exec gunicorn \
--name $NAME \
--workers $NUM_WORKERS \
--max-requests $MAX_REQUESTS \
--user $USER --group $GROUP \
--log-level debug \
--bind unix:$SOCKFILE \
{{ application_name }}.wsgi
Can anyone suggest what else could cause the missing socket file?
Thanks
Well, since I don't have enough rep to comment, I'll mention here that there is not a lot of specificity suggested by the missing socket, but I can tell you a bit about how I started in your shoes and got things to work.
The long and short of it is that gunicorn has encountered a problem when run by upstart and either never got up and running or shut down. Here are some steps that may help you get more info to track down your issue:
In my case, when this happened, gunicorn never got around to doing any error logging, so I had to look elsewhere. Try ps auxf | grep gunicorn to see if you have any workers going. I didn't.
Looking in the syslog for complaints from upstart, grep init: /var/log/syslog, showed me that my gunicorn service had been stopped because it was respawning too fast, though I doubt that'll be your problem since you don't have a respawn in your conf. Regardless, you might find something there.
After seeing gunicorn was failing to run or log errors, I decided to try running it from the command line. Go to the directory where your manage.py lives and run the expanded version of your upstart command against your gunicorn instance. Something like (Replace all of the vars with the appropriate litterals instead of the garbage I use.):
/path/to/your/virtualenv/bin/gunicorn --name myapp --workers 4 --max-requests 10 --user appuser --group webusers --log-level debug --error-logfile /somewhere/I/can/find/error.log --bind unix:/tmp/myapp.socket myapp.wsgi
If you're lucky, you may get a python traceback or find something in your gunicorn error log after running the command manually. Some things that can go wrong:
django errors (maybe problems loading your settings module?). Make sure your wsgi.py is referencing the appropriate settings module on the server.
whitespace issues in your upstart script. I had a tab hiding among spaces that munged things up.
user/permission issues. Finally, I was able to run gunicorn as root on the command line but not as a non-root user via the upstart config.
Hope that helps. It's been a couple of long days tracking this stuff down.
I encountered the same problem after following Michal Karzynski's great guide 'Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL'.
And this is how I solved it.
I had this variable in the bash script used to start gunicorn via Supervisor (myapp/bin/gunicorn_start):
SOCKFILE={{ myapp absolute path }}/run/gunicorn.sock
Which, when you run the bash script for the first time, creates a 'run' folder and a sock file using root privileges. So I sudo deleted the run folder, and then recreated it without sudo privileges and voila! Now if you rerun Gunicorn or Supervisor you won't have the annoying missing sock file error message anymore!
TL;DR
Sudo delete run folder.
Recreate it without sudo privileges.
Run Gunicorn again.
????
Profit
The error could also arise when you haven't pip installed a requirement. In my case, looking at the gunicorn error logs, I found that there was a missing module. Usually happens when you forget to pip install new requirements.
Well, I worked on this issue for more than a week and finally was able to figure it out.
Please follow links from digital ocean , but they did not pinpoint important issues one which includes
no live upstreams while connecting to upstream
*4 connect() to unix:/myproject.sock failed (13: Permission denied) while connecting to upstream
gunicorn OSError: [Errno 1] Operation not permitted
*1 connect() to unix:/tmp/myproject.sock failed (2: No such file or directory)
etc.
These issues are basically permission issue for connection between Nginx and Gunicorn.
To make things simple, I recommend to give same nginx permission to every file/project/python program you create.
To solve all the issue follow this approach:
First thing is :
Log in to the system as a root user
Create /home/nginx directory.
After doing this, follow as per the website until Create an Upstart Script.
Run chown -R nginx:nginx /home/nginx
For upstart script, do the following change in the last line :
exec gunicorn --workers 3 --bind unix:myproject.sock -u nginx -g nginx wsgi
DONT ADD -m permission as it messes up the socket. From the documentation of Gunicorn, when -m is default, python will figure out the best permission
Start the upstart script
Now just go to /etc/nginx/nginx.conf file.
Go to the server module and append:
location / {
include proxy_params;
proxy_pass http<>:<>//unix:/home/nginx/myproject.sock;
}
REMOVE <>
Do not follow the digitalocean aricle from here on
Now restart nginx server and you are good to go.
I had the same problem and found out that I had set the DJANGO_SETTINGS_MODULE to production settings in the the gunicorn script and the wsgi settings were using dev.
I pointed the DJANGO_SETTINGS_MODULE to dev and everything worked.

Django Deploy using Heroku - [Errno 2] No such file or directory

I get this error ([Errno 2] No such file or directory) after I push the repo to heroku master. Here are my logs.
2012-04-17T18:24:53+00:00 app[web.1]: python: can't open file '/test/project/manage.py': [Errno 2] No such file or directory
2012-04-17T18:24:54+00:00 heroku[web.1]: Process exited with status 2
2012-04-17T18:24:54+00:00 heroku[web.1]: State changed from starting to crashed
2012-04-17T18:24:54+00:00 heroku[web.1]: State changed from crashed to created
2012-04-17T18:24:54+00:00 heroku[web.1]: State changed from created to starting
2012-04-17T18:24:57+00:00 heroku[web.1]: Starting process with command python /test/project/manage.py runserver 0.0.0.0:4473 --noreload 2012-04-17T18:24:57+00:00 app[web.1]: python: can't open file '/test/project/manage.py': [Errno 2] No such file or directory
My Procfile looks like the following:
web: python /test/project/manage.py runserver 0.0.0.0:$PORT --noreload
I don't know why it can't open the file. It opens fine when I am using my development server. Any ideas? Thanks for reading.
Your current setup in your Procfile references an absolute path '/test/project/manage.py' that doesn't exist on Heroku. The '/test/ is the root of the instance you're running in and is incorrect. You should first change this to be the relative path, this is likely something like:
web: python project/manage.py runserver 0.0.0.0:$PORT --noreload
If this does not work you can explore the location of the project by running:
heroku run bash
This should place you in '/app' from here you can see what the path to start your project is.
Since your initial push likely failed to start the process you'll likely need to scale a web process. You can then do this with:
heroku scale web=1
you can attach an ls to heroku to find out the actual structure of the file system.
> heroku run ls /
Running ls / attached to terminal... up, run.1
app dev home lib64 mnt sbin usr
bin etc lib lost+found proc tmp var
It might be the case that they wrap your app inside an app directory
Try using os.path to join the elements of the path., btw have you try it to change the ProcFile to read from worker? python hellodjango/manage.py
Edit later:
Try to run this three commands in order to make heroku master:
pip install -r ./requirements.txt
foreman start
heroku create mempy-demo --stac=cedar
git push heroku master
Now test a simple 'Hello World':
$ curl mempy-demo.herokuapp.com
You could do something like this to change the environment variables (e.g add it in your wsgi.py file if that's where the error originates):
os.environ["DJANGO_SETTINGS_MODULE"] = "myblog.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myblog.settings")

gunicorn_django error "cannot find myproject.settings in your PYTHONPATH"

I can run gunicorn_django on the root dir of my project, but when i set all thing in supervisor I got this error message "Error: Can't find 'myproject.settings' in your PYTHONPATH."
my supervisor setting:
command=/usr/local/django/myproject/gunicorn_django -c /usr/local/django/myproject/gunicorn.conf.py
directory=/usr/local/django/myproject
user=www-data
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/supervisord.log
redirect_stderr=true
gunicorn_django configure file gunicorn.conf.py
bind = "127.0.0.1:9004"
logfile ="/var/www/vhosts/myproject.net/log/gunicorn.log"
workers = 3
UPDATE: problem solved, it was because that www-data didn't have the read permission to settings.py and other files needed
Make sure gunicorn is in your INSTALLED_APPS and change command to this:
command=/path/to/python /path/to/manage.py run_gunicorn -c /path/to/gunicorn.conf.py
Also, check for missing Python dependencies. I've had Django crash in a similar fashion when there were missing Python libs.

Categories

Resources