Gunicorn Config File I've Created Doesn't Exist - python

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

Related

Python: "Cannot import flask"

I tried everything but couldn't import app.py. It gives me the following error
set FLASK_APP = "C:\Users\Hp\PycharmProjects\sakshi.py\app.py"
(env) C:\Users\Hp\Documents\flask_app>flask run
Serving Flask app " app.py"
Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
Debug mode: off
Usage: flask run [OPTIONS]
Error: Could not import " app".
Your help would be much appreciated!
Change the sever environment to development env.
export FLASK_APP=yourapp
export FLASK_ENV=development
and then start the server with:
flask run
If you still get error try this way:
set FLASK_ENV=development
flask run
And then run your python file.
At least one problem: spaces in the set command. From the syntax in the microsoft doc:
set [<Variable>=[<String>]]
set [/p] <Variable>=[<PromptString>]
set /a <Variable>=<Expression>
There is no space on either side of the = in the set command.
And, the path name "C:\Users\Hp\PycharmProjects\sakshi.py\app.py" is questionable. It is, at the very least, a bad idea to name a directory sakshi.py.
I could see the problem is with space between the variable and value in set command,
In windows,
set var=value
In *nix family,
export var=value
Your script name is app.py which by default flask recognizes as entry point for the app.
You only have to explicitly set the FLASK_APP variable when you are running from another directory (which you are) or the name of the script is something other than app.py. You can directly run the flask run command from the project base directory itself.

Heroku not reading requirement.txt file

I am trying to deploy my python flask app to heroku but it keeps crashing and complaining about bash: gunicorn: command not found. I have my requirement.txt file in the root folder where my Procfile is also located. My python code is located in src/server/
Procfile contains: web: gunicorn --pythonpath src/server/ route:app --preload
I have gunicorn in my requirements file:
Is there something i'm missing?
gunicorn==19.8.1
Flask==0.12.2
Flask-Cache==0.13.1
Flask-Cors==3.0.2
Flask-MongoAlchemy==0.5.1
flask-mongoengine==0.9.5
Flask-PyMongo==0.5.2
Flask-WTF==0.14.2
gevent==1.2.1
greenlet==0.4.12
pymongo==3.6.1
folder structure;
I faced a similar problem with my app. I just reset all my requirements by sending in an empty requirements.txt file. Then built my app. Then sent the original file. I don't understand why, but it worked.
I am also having a similar problem like this on Heroku. I created an IDENTICAL new project and magically it works. Heroku has some bug related to ignoring requirements.txt
I fixed it by creating a new environment in Heroku. The previous environment seems not to be reading the requirement.txt file, I couldn't figure why.

Supervisor cannot start Gunicorn: ENOENT

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

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.

Heroku: ImportError: No module named site

After some changes in my repo and deploy to heroku I am receiving the following error:
ImportError: No module named site
I not have idea what can cause the problem because I only change some Django templates in the last 2 commits.
Best Regards
Take a look at your Procfile. It should show something like this:
web: gunicorn site:app
Make sure site is the name of your app.
Heroku has an article for this: https://help.heroku.com/BWJ7QYTF/why-am-i-seeing-importerror-no-module-named-site-when-deploying-a-python-app
Updates to the Python buildpack mean that the PYTHONPATH and PYTHONHOME config vars being set on the app may introduce this issue.
Firstly, check if these are present with
heroku config
To fix the issue, you can unset them like so:
heroku config:unset PYTHONHOME -a appname
heroku config:unset PYTHONPATH -a appname
Add the following to your proc file
web: gunicorn site:app
Also check your gitignore and make sure site isnt included

Categories

Resources