Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server is required.
In this guide, we will demonstrate how to install and configure some components on Ubuntu 14.04 to support and serve Django applications. We will be setting up a PostgreSQL database instead of using the default SQLite database. We will configure the Gunicorn application server to interface with our applications. We will then set up Nginx to reverse proxy to Gunicorn, giving us access to its security and performance features to serve our apps.
In order to complete this guide, you should have a fresh Ubuntu 14.04 server instance with a non-root user with sudo
privileges configured. You can learn how to set this up by running through our initial server setup guide.
We will be installing Django within a virtual environment. Installing Django into an environment specific to your project will allow your projects and their requirements to be handled separately.
Once we have our database and application up and running, we will install and configure the Gunicorn application server. This will serve as an interface to our application, translating client requests in HTTP to Python calls that our application can process. We will then set up Nginx in front of Gunicorn to take advantage of its high performance connection handling mechanisms and its easy-to-implement security features.
Let’s get started.
To begin the process, we’ll download and install all of the items we need from the Ubuntu repositories. We will use the Python package manager pip
to install additional components a bit later.
First, update the local package index and then download and install the packages:
sudo apt-get update
sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib nginx
This will install pip
, the Python development files needed to build Gunicorn later, the Postgres database system and the libraries needed to interact with it, and the Nginx web server.
We’re going to jump right in and create a database and database user for our Django application.
To work with Postgres in its default configuration, it is best to change to the postgres
system user temporarily. Do that now by typing:
sudo su - postgres
When operating as the postgres
user, you can log right into a PostgreSQL interactive session with no further authentication by typing:
psql
You will be given a PostgreSQL prompt where we can set up our requirements.
First, create a database for your project:
CREATE DATABASE myproject;
Every command must end with a semi-colon, so check that your command ends with one if you are experiencing issues.
Next, create a database user for our project. Make sure to select a secure password:
CREATE USER myprojectuser WITH PASSWORD 'password';
Now, we can give our new user access to administer our new database:
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;
When you are finished, exit out of the PostgreSQL prompt by typing:
\q
Now, exit out of the postgres
user’s shell session to get back to your normal user’s shell session by typing:
exit
Now that we have our database ready, we can begin getting the rest of our project requirements ready. We will be installing our Python requirements within a virtual environment for easier management.
To do this, we first need access to the virtualenv
command. We can install this with pip
:
sudo pip install virtualenv
With virtualenv
installed, we can start forming our project. Create a directory where you wish to keep your project and move into the directory afterwards:
mkdir ~/myproject
cd ~/myproject
Within the project directory, create a Python virtual environment by typing:
virtualenv myprojectenv
This will create a directory called myprojectenv
within your myproject
directory. Inside, it will install a local version of Python and a local version of pip
. We can use this to install and configure an isolated Python environment for our project.
Before we install our project’s Python requirements, we need to activate the virtual environment. You can do that by typing:
source myprojectenv/bin/activate
Your prompt should change to indicate that you are now operating within a Python virtual environment. It will look something like this: (myprojectenv)user@host:~/myproject$
.
With your virtual environment active, install Django, Gunicorn, and the psycopg2
PostgreSQL adaptor with the local instance of pip
:
pip install django gunicorn psycopg2
With our Python components installed, we can create the actual Django project files.
Since we already have a project directory, we will tell Django to install the files here. It will create a second level directory with the actual code, which is normal, and place a management script in this directory. The key to this is the dot at the end that tells Django to create the files in the current directory:
django-admin.py startproject myproject .
The first thing we should do with our newly created project files is adjust the settings. Open the settings file in your text editor:
nano myproject/settings.py
Start by finding the section that configures database access. It will start with DATABASES
. The configuration in the file is for a SQLite database. We already created a PostgreSQL database for our project, so we need to adjust the settings.
Change the settings with your PostgreSQL database information. We tell Django to use the psycopg2
adaptor we installed with pip
. We need to give the database name, the database username, the database username’s password, and then specify that the database is located on the local computer. You can leave the PORT
setting as an empty string:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'myprojectuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
Next, move down to the bottom of the file and add a setting indicating where the static files should be placed. This is necessary so that Nginx can handle requests for these items. The following line tells Django to place them in a directory called static
in the base project directory:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
Save and close the file when you are finished.
Now, we can migrate the initial database schema to our PostgreSQL database using the management script:
cd ~/myproject
./manage.py makemigrations
./manage.py migrate
Create an administrative user for the project by typing:
./manage.py createsuperuser
You will have to select a username, provide an email address, and choose and confirm a password.
We can collect all of the static content into the directory location we configured by typing:
./manage.py collectstatic
You will have to confirm the operation. The static files will then be placed in a directory called static
within your project directory.
Finally, you can test our your project by starting up the Django development server with this command:
./manage.py runserver 0.0.0.0:8000
In your web browser, visit your server’s domain name or IP address followed by :8000
:
http://server_domain_or_IP:8000
You should see the default Django index page:
If you append /admin
to the end of the URL in the address bar, you will be prompted for the administrative username and password you created with the createsuperuser
command:
After authenticating, you can access the default Django admin interface:
When you are finished exploring, hit CTRL-C in the terminal window to shut down the development server.
The last thing we want to do before leaving our virtual environment is test Gunicorn to make sure that it can serve the application. We can do this easily by typing:
cd ~/myproject
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
This will start Gunicorn on the same interface that the Django development server was running on. You can go back and test the app again. Note that the admin interface will not have any of the styling applied since Gunicorn does not know about the static content responsible for this.
We passed Gunicorn a module by specifying the relative directory path to Django’s wsgi.py
file, which is the entry point to our application, using Python’s module syntax. Inside of this file, a function called application
is defined, which is used to communicate with the application. To learn more about the WSGI specification, click here.
When you are finished testing, hit CTRL-C in the terminal window to stop Gunicorn.
We’re now finished configuring our Django application. We can back out of our virtual environment by typing:
deactivate
We have tested that Gunicorn can interact with our Django application, but we should implement a more robust way of starting and stopping the application server. To accomplish this, we’ll make an Upstart script.
Create and open an Upstart file for Gunicorn with sudo
privileges in your text editor:
sudo nano /etc/init/gunicorn.conf
We’ll start with a simple description string to state what our service file is for. We’ll then move on to defining the system runlevels where this service should be automatically started. The normal runlevels to run services are 2, 3, 4, and 5. We’ll run our service when the system is in any of those. We’ll tell it to stop when its in any other runlevel (such as when the system is rebooting, shutting down, or in single-user mode):
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
Next, we’ll tell Upstart to automatically restart the service if it fails. We also want to specify the user and group to run under. We’ll use our normal user since all of our files are owned by that user. We’ll let the www-data
group which Nginx belongs to be the group owners. We also need to change to our project’s directory so that the Gunicorn commands execute correctly:
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid user
setgid www-data
chdir /home/user/myproject
Now, we just need to give the command that will start the Gunicorn process. We need to give the path to the Gunicorn executable, which is stored within our virtual environment. We will tell it to use a Unix socket instead of a network port to communicate with Nginx, since both services will be running on this server. This is more secure and faster. You can add any other configuration for Gunicorn here as well. For instance, we’ll specify that we want 3 worker processses:
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid user
setgid www-data
chdir /home/user/myproject
exec myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/user/myproject/myproject.sock myproject.wsgi:application
When you are finished, save and close the file.
Start the Gunicorn service by typing:
sudo service gunicorn start
Now that Gunicorn is set up, we need to configure Nginx to pass traffic to the process.
Start by creating and opening a new server block in Nginx’s sites-available
directory:
sudo nano /etc/nginx/sites-available/myproject
Inside, open up a new server block. We will start by specifying that this block should listen on the normal port 80 and that it should respond to our server’s domain name or IP address:
server {
listen 80;
server_name server_domain_or_IP;
}
Next, we will tell Nginx to ignore any problems with finding a favicon. We will also tell it where to find the static assets that we collected in our ~/myproject/static
directory. All of these files have a standard URI prefix of “/static”, so we can create a location block to match those requests:
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/myproject;
}
}
Finally, we’ll create a location / {}
block to match all other requests. Inside of this location, we’ll include the standard proxy_params
file included with the Nginx installation and then we will pass the traffic to the socket that our Gunicorn process created:
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}
Save and close the file when you are finished. Now, we can enable the file by linking it to the sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
Test your Nginx configuration for syntax errors by typing:
sudo nginx -t
If no errors are reported, go ahead and restart Nginx by typing:
sudo service nginx restart
You should now be able to go to your server’s domain or IP address to view your application.
In this guide, we’ve set up a Django project in its own virtual environment. We’ve configured Gunicorn to translate client requests so that Django can handle them. Afterwards, we set up Nginx to act as a reverse proxy to handle client connections and serve the correct project depending on the client request.
Django makes creating projects and applications simple by providing many of the common pieces, allowing you to focus on the unique elements. By leveraging the general tool chain described in this article, you can easily serve the applications you create from a single server.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Worked all the way up to restarting nginx, but now all I’m getting is a 502 Bad Gateway error. Anyone else seeing the same?
Is the Gunicorn process running correctly? Try to check the logs at
/var/log/upstart/myproject.log
. Also, check that there is amyproject.sock
file in your project directory, that it’s owned by your user account, and that the group ownership of the file is held by thewww-data
group. You can check this with:This comment has been deleted
Hi Justin,
Thanks for the quick reply. It looks like this is indeed the issue. I found a gunicorn.log in the /var/log/upstart path. Lots of “Can’t connect to” messages.
After some poking around, I don’t see a socket anywhere. Was I supposed to have created this?
Thanks, -Matt
@brightfarm: No, the Gunicorn process itself was supposed to have created this socket file within your project file. Make sure that the
setuid
option in your/etc/init/gunicorn.conf
is set to the username you were using to create the project files. Also, make sure that thechdir
points to the parent project folder in your home directory and that the--bind
option within theexec
statement starts withunix:
and points to a socket file within your project file (this should be created automatically by Gunicorn when the service is started).If you are still having issues after checking all of those items, post links to your Upstart log, your
/etc/init/gunicorn.conf
file, etc. and we’ll see if we can figure out what’s going on.I am having the same issue as @brightfarm, but I also did not even have a
/var/log/upstart
directory for a log to be in. I saw that my machine didn’t have upstart installed, so I installed it withsudo apt-get install upstart
. I restarted nginx afterwards, but I’m still seeing a 502 error, and although there is now a/var/log/upstart
directory, there’s no log in it.I checked the things you’ve mentioned immediately above in the first paragraph. The username matches (although I created my directories on a different machine (under an account with the same username) and then
rsync
’d the project over.Thanks,
Nathan
@abprobe88bcd7d6: If you did not have Upstart installed on your machine, you are not running this guide on Ubuntu 14.04. Ubuntu 14.04 uses Upstart as its init system, so it should be available. If you are running on Ubuntu 16.04, you will have the systemd init system instead. You should follow this guide instead if that is the case.
Oh - of course! Thank you.
@jellingwood Ok. That appears to have worked. I had the wrong user. So I fixed that and restarted everything. Now, in the gunicorn log file, I see messages like, “Booting worker with pid:” which says to me that part is working.
I’m still seeing the “Welcome to nginx!” page - is that what I should be seeing? I’m going back over those sections of the tutorial to make sure I didn’t set something up wrong here, too. I’m expecting to see the Django page, like we did when we ran ‘runserver’.
Update: I got it fixed. I also had to remove the ‘default’ files for sites-available/sites-enabled in nginx for it to recognize the ones I created.
Thanks for this great tutorial and all your help fixing my mistakes!
@brightfarm: Excellent! Good work on your troubleshooting. I’m glad you got it working!
I guess you missed the dot at the end when you run
django-admin.py startproject myproject .
. I guess that’s why myproject.sock is not created after you runsudo service gunicorn start
. Hope that can solve your problem.I got the exact 502 bad gateway error as you did after sticking on the step. Then I realized I missed this dot at the beginning. Remove your repository. And redo the work. It shall goes well.
When I start “sudo nginx -t” nginx report a problem:
All the parameters I filled followed by your tutorial.
What can be wrong?
I fix my problem. Just add
from this tutorial
I am also getting a 502 bad gateway after setting up the gunicorn service. There is a sock file project01.sock in my project directory. It’s inside the file with the settings.py and .wsgi files.
here is the ownership .sock file: mochi@projects:~/myproject/project01/project01$ ls -lh project01.sock srwxrwxrwx 1 mochi www-data 0 Apr 1 14:40 project01.sock
here is the log: Traceback (most recent call last): File "/home/mochi/myproject/myprojectenv/local/lib/python2.7/site-packages/gu$ worker.init_process() File "/home/mochi/myproject/myprojectenv/local/lib/python2.7/site-packages/gu$ self.wsgi = self.app.wsgi() File "/home/mochi/myproject/myprojectenv/local/lib/python2.7/site-packages/gu$ self.callable = self.load() File "/home/mochi/myproject/myprojectenv/local/lib/python2.7/site-packages/gu$ return self.load_wsgiapp() File "/home/mochi/myproject/myprojectenv/local/lib/python2.7/site-packages/gu$ return util.import_app(self.app_uri) File "/home/mochi/myproject/myprojectenv/local/lib/python2.7/site-packages/gu$ import(module) ImportError: No module named project01.wsgi
why am I getting this error? Inside my settings.py shows the wsgi should be named correctly: WSGI_APPLICATION = ‘project01.wsgi.application’
@mochi: It seems like there are a few issues if you were attempting to follow the guide exactly. You seem to have three project directories instead of just two (which can happen if you don’t add the final dot in the
django-admin.py startproject
command) and your socket file is being created in the nested directory instead of the parent.I don’t think your
settings.py
file is being read yet because I think that you pointed to thewsgi.py
file incorrectly in thegunicorn.conf
file. Pay close attention to these lines:The
chdir
should point to your project root directory. This should be the location where you can access both your virtual environment folder and your project directory. This is the directory from whichgunicorn
will be run.When you are in the project root directory, you’ll have to make sure the
myproject.wsgi:application
portion of the command correctly points to thewsgi.py
file. Themyproject.wsgi
part means that it will look for a file atmyproject/wsgi.py
. If you have another directory in between that is also calledmyproject
, you need to add that in like this:myproject.myproject.wsgi:application
. I think that you have an issue with the directory paths specified by the combination of thechdir
option and the Python module you passed togunicorn
.What if you are using virtualenvwrappper? And your virtual environment folder and project directory are in separate places?
/var/log/upstart/gunicorn.log
returns:my tree:
my site-enabled file:
@jellingwood thank you for the quick reply but I still can’t seem to get rid of the 502. The gunicorn log still gives the same problem that it can’t find project01.wsgi
My directory is:
/home/mochi/myproject/project01/project01
myproject
has myprojectenv + project01 + project01.sockmyproject/project01
has manage.py + project01 + staticmyproject/project01/project01
has all the settings and .wsgi filesHere’s an image of these directories and privileges: https://www.dropbox.com/s/63tg8a0vft7w8vh/Screenshot 2015-04-02 14.26.13.png?dl=0
I changed the chdir to /home/mochi/myproject my exec is
exec myprojectenv/bin/gunicorn --worker 3 --bind unix:home/mochi/myproject/project01.sock project01.project01.wsgi:application
my proxy_pass is
http://unix:/home/mochi/myproject/project01.sock
@mochi: You are having problems because you have an extra directory level. This was caused by not adding the dot at the end of your
django-admin.py startproject myproject .
command. Gunicorn is trying to find your files based on some assumptions about your directory structure, but is running into issues because things are not where it expects them to be. Move all of the files out of your firstproject01
directory into yourmyproject
directory. Your directory structure should look like this:Try to move your files to mirror this and see if that fixes things.
@jellingwood I finally got it, thanks! Now that the directories are fixed up gunicorn runs correctly and I get the welcome to django screen
@mochi: Awesome! I’m glad that you got it working!
After installing postgres when trying to access with ‘psql’ command I get the following error:
psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?
Does any one has the same problem? How can I solve it?
It looks like Postgres isn’t running, try starting it:
Thanks kamaln7, I tried what you proposed and got: ‘* No PostgreSQL clusters exist; see “man pg_createcluster”’.
I checked and postgres is installed correctly. What can I do? I’m still stuck.
Thanks.
Hi, so I just began getting a 502 error today. If it’s any coincidence I installed VNC yesterday following this link: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-ubuntu-14-04.
My log through Nginx says: connect() to unix:/home/elepolt/fanstats-website/fanstats/fanstats.sock failed (13: Permission denied) while connecting to upstream, client: 104.129.200.56, server: fanstatsapp.com, request: “GET / HTTP/1.1”, upstream: “http://unix:/home/elepolt/fanstats-website/fanstats/fanstats.sock:/”, host: “fanstatsapp.com”
ls -lh for sock gives: srwxrwxrwx 1 elepolt www-data 0 Apr 2 08:44 /home/elepolt/fanstats-website/fanstats/fanstats.sock
My gunicorn.conf file looks correct. And I’ve restarted all the services.
Any other thoughts?
same problem, any help?
This comment has been deleted
Sorry man. I ended up destroying my droplet and starting over.
same problem. I just copied and pasted everything. this is my 4th iteration.
i figured it out. if you go any levels lower than from you home dir, the permissions get wacky. http://stackoverflow.com/questions/25774999/nginx-stat-failed-13-permission-denied
VERY IMPORTANT: after finishing all the steps in this tutorial ensure that you
cd /etc/nginx/sites-available
andsudo rm -r default
for nginx to pick up your new site, that is myproject. Thanks for this wonderful tutorial.why isn’t this in the tutorial???
Thanks for the great tutorial! I’m a newbie developer and this helped greatly. I’m having one issue, though. I was using gmail to send out Django password resets and running in this configuration has broken this feature for me. Here’s my Django error:
TypeError at /password_reset/
‘NoneType’ object is not iterable
Request Method: POST Request URL: http://10.1.0.12/password_reset/ Django Version: 1.8 Exception Type: TypeError Exception Value:
‘NoneType’ object is not iterable
Exception Location: /home/martin/.virtualenvs/dcc/local/lib/python2.7/site-packages/django/core/mail/message.py in sanitize_address, line 106 Python Executable: /home/martin/.virtualenvs/dcc/bin/python
The Nginx log is showing this:
2015/05/12 10:10:56 [error] 29422#0: *7 connect() to unix:/home/martin/dcc/dcc.sock failed (111: Connection refused) while connecting to upstream, client: 10.1.0.169, server: 10.1.0.12, request: “GET /password_reset/ HTTP/1.1”, upstream: “http://unix:/home/martin/dcc/dcc.sock:/password_reset/”, host: “10.1.0.12”, referrer: “http://10.1.0.12/login/”
Thanks in advance for any help! Also, let me know if my question isn’t appropriate to post in this comments section.
Just to answer my own question, I figured out that my ubuntu environment variables weren’t being seen so I had to put them in /etc/default/nginx. You learn something new everyday!
Hey dude, I’m massively struggling with ubuntu’s environment variables myself. I tried setting them in /etc/default/nginx (via both root and another admin user I creaed), but the variables just never get read. Anything more you can share about how you fixed your problem? I’ve almost run out of options.
With these configurations as default and not tuning postgresql or gunicorn, how many concurrent users would my django app be able to handle?
Interesting question. Did you find any optimization tips worth sharing to the community?
This comment has been deleted
All right to:
show the following error
Failed to start gunicorn.service: Unit gunicorn.service failed to load: No such file or directory.
resolved, version of ubuntu
If using Ubuntu 15: the resolution is to install upstart, which was replaced by Systemd
Try not to change to Upstart, they went to Systemd for a reason ;)
Instead, a good solution is to create a Systemd service in /etc/systemd/system
Then write the following (should work the samstarte as the upstart file)
The commands to enable/start/stop should be straightforward
Hope that helps!
HI, I followed this tutorial but I have a problem. After configuring nginx, the only thing that I can see is a “Welcome to nginx!” page. I think I should see the django index page, right? I set configuration like this. //////////////////////////////////////////////// server { listen 80; server_name 0.0.0.0; #(My IP adress)
} ///////////////////////////////////////////////// Weird thing is that if I type ‘/admin’ next to my ip address, I can see my admin page. I also deleted default file from /etc/nginx/sites-available and restarted nginx. I tried all of this tutorial several times, still I don’t know what is the problem. Please help me.
I was having the same problem, I had Django config on a separate sites-available file and tried to activate it linking to
sites-enabled
, however I ended up with the “Welcome to nginx” over and over again (I deleted “default” website too). A partial solution for me was to move config to/etc/nginx/nginx.conf
and removing fromsites/enabled
. I hope this helps you, maybe someone can guide us better to configure it properly.I’m having a similar issue. When I go to the ip address without the port I get the nginx page.
When I visit the ip address with the port appended I get ‘webpage not available’ error.
Anyone has any advice?
While I’m sure it’s not much help to the OP now, but if anyone else has this problem, adding “default_server” to the listen line in the sites-available file worked for me. So it reads:
listen 80 default_server;
Thank you for this guide! I have setup my website and it work very well. But I trying to figure out where I can find Django logs? I have troubles with send email messages from website with Postfix. The Postfix work as expected and I can send email via console utility “mail”. But from the Django application I can’t send email. To find problem - I need to find logs file of my Django web application. Please help me. Thanks.
When I typed “./manage.py collectstatic”, it returned an error.
I solved the problem by add the code
The refer URL https://devcenter.heroku.com/articles/django-assets
Great answer, thanks, helped a lot.
Hi Justin, I’m facing the 502 Bad Gateway error, I read through similar postings, applied some tips you made mention but to no avail.
When i launch the application using gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application, it does work, less not having the inage files to render.
Once the combination of using a Gunicorn Upstart file and Nginx, came into the mix, I have been getting the aforementioned error.
Gunicorn Upstart File content below. I was able to successfully start and stop the service:
Nginx Config details below:
Directory Structure
When i tested nginx, it passed without any error, not sure what to do, to get this running. Your help will be greatly appreciated
@nubianmonk: I’ll try to help you out, but it’s a bit difficult given that you’re not exactly following the guide (you are running your gunicorn service as root, which is ill-advised, and you are using a home directory as the top-level project directory).
I would start out by checking your Upstart logs for errors. A 502 error means that Nginx is not able to successfully pass the request to the backend service (gunicorn), which could mean that the location of the backend service is incorrect in the Nginx config, the permissions and ownership of the project directory are inaccessible to the web server user, or that the backend service is down.
My guess is that the backend service is down because I think your Upstart configuration might be a bit confused. Specifically, this line:
Since you have a
chdir
command specifying a move to the/home/myproject
directory prior to that, according to your directory structure, you have an extramyproject
specified in the dotted path. I would try changing:to
See if that helps. If you are still having issues, posting a link to your Upstart logs would be helpful.
(As an aside, I highly recommend running the gunicorn service as a non-root user. Running as root can lead to very serious security issues).
Hi Justin,
Thank you very much for offering to help me with most appreciated.
Per the extra myproject specified in the dotted path, is based on your prescription to user @mochi, posting of April 2nd 2015, which i followed, as i had tried an array of other things that did not work.
Below is the rendering of the Upstart logs
(myprojectenv)root@akintest:/home/myproject# gunicorn --log-file=- myproject.wsgi:application [2015-07-13 13:36:09 +0000] [1656] [INFO] Starting gunicorn 19.3.0 [2015-07-13 13:36:09 +0000] [1656] [INFO] Listening at: http://127.0.0.1:8000 (1656) [2015-07-13 13:36:09 +0000] [1656] [INFO] Using worker: sync [2015-07-13 13:36:09 +0000] [1661] [INFO] Booting worker with pid: 1661
@nubianmonk: The logs you posted are from running
gunicorn
from the command line. Startinggunicorn
this way will not read the configuration values in your/etc/init/gunicorn.conf
file, which will result in the process starting with mostly default parameters. You can verify this by looking at theListening at: ...
line, which indicates thatgunicorn
is running on port8000
on localhost instead of using the socket file that the configuration specifies.You should start your process with:
The above command will use Upstart to start the
gunicorn
process, allowing it to read your configuration values. You can then check the logs that the Upstart init system writes. You will need to check the logs at/var/log/upstart/gunicorn.log
.Hi Justin,
Following your directives, the following is the content of the log file:
(myprojectenv)root@akintest:/home/myproject# sudo service gunicorn start gunicorn start/running, process 2038 (myprojectenv)root@akintest:/home/myproject# cat /var/log/upstart/gunicorn.log [2015-07-13 14:41:01 +0000] [2038] [INFO] Starting gunicorn 19.3.0 [2015-07-13 14:41:01 +0000] [2038] [INFO] Listening at: unix:/home/myproject/myproject.sock (2038) [2015-07-13 14:41:01 +0000] [2038] [INFO] Using worker: sync [2015-07-13 14:41:01 +0000] [2044] [INFO] Booting worker with pid: 2044 [2015-07-13 14:41:02 +0000] [2045] [INFO] Booting worker with pid: 2045 [2015-07-13 14:41:02 +0000] [2046] [INFO] Booting worker with pid: 2046 (myprojectenv)root@akintest:/home/myproject#
Justin, decided to extract the nginx error file contents as well below: root@akintest:~# tail -f /var/log/nginx/error.log 2015/07/13 08:07:35 [error] 2820#0: *194 connect() failed (111: Connection refused) while connecting to upstream, client: 116.113.110.90, server: 178.62.56.180, request: “GET /pma/scripts/setup.php HT TP/1.1”, upstream: “http://127.0.0.1:8001/pma/scripts/setup.php”, host: “178.62.56.180” 2015/07/13 08:07:38 [error] 2820#0: *196 connect() failed (111: Connection refused) while connecting to upstream, client: 116.113.110.90, server: 178.62.56.180, request: “GET /myadmin/scripts/setup.ph p HTTP/1.1”, upstream: “http://127.0.0.1:8001/myadmin/scripts/setup.php”, host: “178.62.56.180” 2015/07/13 13:36:18 [error] 2820#0: *198 connect() failed (111: Connection refused) while connecting to upstream, client: 125.25.235.66, server: 178.62.56.180, request: “GET /Ringing.at.your.dorbell! HTTP/1.0”, upstream: “http://127.0.0.1:8001/Ringing.at.your.dorbell!”, referrer: “http://google.com/search?q=2+guys+1+horse” 2015/07/13 13:36:18 [error] 2820#0: *200 connect() failed (111: Connection refused) while connecting to upstream, client: 125.25.235.66, server: 178.62.56.180, request: “GET / HTTP/1.0”, upstream: “ht tp://127.0.0.1:8001/” 2015/07/13 13:36:19 [error] 2820#0: *202 connect() failed (111: Connection refused) while connecting to upstream, client: 125.25.235.66, server: 178.62.56.180, request: “GET / HTTP/1.1”, upstream: “ht tp://127.0.0.1:8001/”, host: “127.0.0.1”, referrer: “http://google.com/search?q=2+guys+1+horse” 2015/07/13 13:36:20 [error] 2820#0: *204 connect() failed (111: Connection refused) while connecting to upstream, client: 125.25.235.66, server: 178.62.56.180, request: “GET /Diagnostics.asp HTTP/1.0” , upstream: “http://127.0.0.1:8001/Diagnostics.asp” 2015/07/13 13:36:20 [error] 2820#0: *206 connect() failed (111: Connection refused) while connecting to upstream, client: 125.25.235.66, server: 178.62.56.180, request: “GET / HTTP/1.0”, upstream: “ht tp://127.0.0.1:8001/” 2015/07/13 13:36:21 [error] 2820#0: *208 connect() failed (111: Connection refused) while connecting to upstream, client: 125.25.235.66, server: 178.62.56.180, request: “GET / HTTP/1.0”, upstream: “ht tp://127.0.0.1:8001/” 2015/07/13 13:36:22 [error] 2820#0: *210 connect() failed (111: Connection refused) while connecting to upstream, client: 125.25.235.66, server: 178.62.56.180, request: “GET / HTTP/1.0”, upstream: “ht tp://127.0.0.1:8001/” 2015/07/13 14:21:51 [error] 2820#0: *212 connect() failed (111: Connection refused) while connecting to upstream, client: 185.35.62.11, server: 178.62.56.180, request: “HEAD / HTTP/1.1”, upstream: “ht tp://127.0.0.1:8001/”, host: “178.62.56.180”
Hi Justin, hope all is well with you. Just a gentle reminder, that i posted the logs as directed by yourself about 4 days ago, not sure if it might the case that, you are not getting notifications per the postingss. Your help will surely be appreciated, thanks.
Hi Justin, I humbly ask for your help.
@nubianmonk: I apologize for the delay in responding. I haven’t had much time to check comments recently.
It looks like your Nginx configuration is trying to pass connections to port
8001
on your server. If you followed this guide, and started this on a fresh server, Nginx should be listening to port80
and passing all requests to a socket that is created by your Gunicorn file.These lines, in your Nginx configuration, should be directing all of your traffic to your Unix socket:
Instead, for some reason, they are directing traffic to port
8001
. I’m not sure if you followed other guides in addition to this one, but it looks like Nginx isn’t passing traffic to the correct location, which is why you’re getting 502 errors. Check to see if you have other Nginx server block files within/etc/nginx/sites-enabled
that point to port8001
or whether you have a directive that passes to port8001
in your/etc/nginx/nginx.conf
file.If you continue to have problems after this, it might be better to ask a question in the community so that more people will see it. They may be able to provide better help than I have.
Hi Justin,
Thanks for reaching back, most appreciated. I finally found a work around yesterday and what i did was to replace the following block: server { listen 80; server_name 0.0.0.0;
with the following : server { listen 127.0.0.1:8001; server_name 0.0.0.0; # My server IP Address
Once i did this, it worked by just accessing the application via the server IP address.
@nubianmonk: Awesome! I’m glad you got it sorted out.
HI,
I am trying to migrate my site from amazon.ec2 but I am getting this strange behavior.
I was following this article on “Complete Initial Project Setup” you say ./manage.py runserver 0.0.0.0:8000 The local django server is running ok. In your web browser, visit your server’s domain name or IP address followed by :8000: http://my_ip:8000 but its not working.
I’ve tried ./manage.py runserver my_ip:8000 without success.
So I did ./manage.py runserver 127.0.0.1:8000 Tried to use local browser lynx 127.0.0.1:8000 without success to.
Can anyone help me?
I check the doc but one problem here how do you try to run gunicorn from outside the virtualenv I mean if you try to go to the virtualenv/bin folder and run gunicorn is not gonna run you have to use source virtualenvdir/bin/activate first and the question how can I enter to the virtualenv using the service.conf ?
@clust3rsekt0r: Hello. Sourcing the
virtualenvdir/bin/activate
directory basically sets up some information about the environment. You can still access all of these files directly by referring to their path. Trying to rungunicorn
might not work without sourcing theactivate
script, but if you’re in the/home/user/myproject
directory, and call the executable located atmyprojectenv/bin/gunicorn
(which is exactly what the Upstart script does), will work. This is similar to how you can call a command that is not in your executionPATH
by specifying the absolute path to the command.My project did not serve correctly until I removed the ‘http://’ from the line described in the nginx config file as 'proxy_pass http://unix:/home/user/myproject/myproject.sock;'. I believe ‘proxy_pass unix:/home/user/myproject/myproject.sock;’ is the correct configuration.
Maybe because you were using SSL (https)?
I am following the tutorial and getting this error when I run gunicorn. what am I missing?
Failed to start gunicorn.service: Unit gunicorn.service failed to load: No such file or directory.
Hi i am following the guide and get the error 502 in the gunicorn.log file but and tried to what is posted above and I can not fix it
you can help
does this error
my tree of the project is
guicorn.conf
*/etc/nginx/sites-available/miguel *
Thanks for the tutorial, got me most of the way there.
Unfortunately nginx is not serving my static files when settings.DEBUG=False. Gunicorn is running and templates are loading etc., but no static files.
There are no errors in the ngnix error log, and I am stumped. If I set Django’s DEBUG = True the static files are served. If I stop ngnix then the static files are not served, even if DEBUG = True. I have ran collectstatic etc. and made sure all paths etc. are correct. Stumped though.
Did others make sure their DEBUG was Flase? if so did you have any similar problem to mine?
Ouch, painful. I was using this tutorial to port another project over to gunicorn and nginx. Issue was with compressor. Good tutorial, bad me :)
When I run, “sudo service gunicorn start”
I always end up with a, " start: Job failed to start" error. Any advice on this? I’m running Gunicorn as Root.
don’t need to run it as root and also may have a path wrong in your in the gunicorn conf file.
Yes, I follow that much. And my paths are correct too. Gunicorn now starts, but fails soon after starting a single process.
Can I use this guide to provide few Django app on one server?
Hi Justin, I don’t know why the landing pages of ip and domain name are different. one is “it works” from django and the other one is from nginx. but i figured out a solution to make them the same by adding ip to
server_name
in /etc/nginx/sites-available/myproject. could you tell me the reason?Hi Justin,
Thanks for reaching back, most appreciated. I finally found a work around yesterday and what i did was to replace the following block: server { listen 80; server_name 0.0.0.0;
with the following : server { listen 127.0.0.1:8001; server_name 0.0.0.0; # My server IP Address
Once i did this, it worked by just accessing the application via the server IP address.
@nubianmonk: Awesome! I’m glad you got it sorted out.
HI,
I am trying to migrate my site from amazon.ec2 but I am getting this strange behavior.
I was following this article on “Complete Initial Project Setup” you say ./manage.py runserver 0.0.0.0:8000 The local django server is running ok. In your web browser, visit your server’s domain name or IP address followed by :8000: http://my_ip:8000 but its not working.
I’ve tried ./manage.py runserver my_ip:8000 without success.
So I did ./manage.py runserver 127.0.0.1:8000 Tried to use local browser lynx 127.0.0.1:8000 without success to.
Can anyone help me?
I check the doc but one problem here how do you try to run gunicorn from outside the virtualenv I mean if you try to go to the virtualenv/bin folder and run gunicorn is not gonna run you have to use source virtualenvdir/bin/activate first and the question how can I enter to the virtualenv using the service.conf ?
@clust3rsekt0r: Hello. Sourcing the
virtualenvdir/bin/activate
directory basically sets up some information about the environment. You can still access all of these files directly by referring to their path. Trying to rungunicorn
might not work without sourcing theactivate
script, but if you’re in the/home/user/myproject
directory, and call the executable located atmyprojectenv/bin/gunicorn
(which is exactly what the Upstart script does), will work. This is similar to how you can call a command that is not in your executionPATH
by specifying the absolute path to the command.My project did not serve correctly until I removed the ‘http://’ from the line described in the nginx config file as 'proxy_pass http://unix:/home/user/myproject/myproject.sock;'. I believe ‘proxy_pass unix:/home/user/myproject/myproject.sock;’ is the correct configuration.
Maybe because you were using SSL (https)?
I am following the tutorial and getting this error when I run gunicorn. what am I missing?
Failed to start gunicorn.service: Unit gunicorn.service failed to load: No such file or directory.
Hi i am following the guide and get the error 502 in the gunicorn.log file but and tried to what is posted above and I can not fix it
you can help
does this error
my tree of the project is
guicorn.conf
*/etc/nginx/sites-available/miguel *
Thanks for the tutorial, got me most of the way there.
Unfortunately nginx is not serving my static files when settings.DEBUG=False. Gunicorn is running and templates are loading etc., but no static files.
There are no errors in the ngnix error log, and I am stumped. If I set Django’s DEBUG = True the static files are served. If I stop ngnix then the static files are not served, even if DEBUG = True. I have ran collectstatic etc. and made sure all paths etc. are correct. Stumped though.
Did others make sure their DEBUG was Flase? if so did you have any similar problem to mine?
Ouch, painful. I was using this tutorial to port another project over to gunicorn and nginx. Issue was with compressor. Good tutorial, bad me :)
When I run, “sudo service gunicorn start”
I always end up with a, " start: Job failed to start" error. Any advice on this? I’m running Gunicorn as Root.
don’t need to run it as root and also may have a path wrong in your in the gunicorn conf file.
Can I use this guide to provide few Django app on one server?
Hi Justin, I don’t know why the landing pages of ip and domain name are different. one is “it works” from django and the other one is from nginx. but i figured out a solution to make them the same by adding ip to
server_name
in /etc/nginx/sites-available/myproject. could you tell me the reason?This comment has been deleted
Can’t get this to work for me… I followed the example closely, only remain the paths and the IP as requested. A project_name.sock file was created for me in my project directory, but I can’t access the project from the browser. It still shows the default Nginx page.
NB: I am not using virtualenv
Hi! I’m very new to Linux (currently day 4), Django (day 2). I was trying to use your instructions on a Raspberry Pi 2 running Raspbian Jessie Lite (not Ubuntu) and I got stuck on the “Create a Gunicorn Upstart File” section. The [sudo service gunicorn start] command returns a [Failed to start gunicorn.service: Unit gunicorn.service failed to load: No such file or directory.]. I altered the [/etc/init/gunicorn.conf] file to reflect that I am using the [pi] user account but did not alter the ‘myproject’ folder or location as my project is also named ‘myproject’ and located in [/home/pi/myproject]. Any suggestions on what needs to be done or where I should look? Thanks!
As of Jessie, Raspbian’s init system is systemd. Prior to this release, it was sysvinit. Upstart is found mostly on Ubuntu systems (and, Google tells me, Google’s ChromeOS). The Upstart section would probably need to be translated to a systemd unit file. As handful of quick pointers:
I wrote a breakdown of init systems on single-board computers for Adafruit a while back. The description of systemd uses a Beaglebone Black for its examples, but might be helpful.
Finally, and maybe most usefully, the Gunicorn manual looks to have an example systemd configuration.
I am getting the following error when installing guinicorn…
It finishes installing. When I try to run my initial test to see if it is serving my app, nothing shows up. I suspect it is a gunicorn install problem…
Can anyone help me out with this?
nvm figured it out.
I got it running (after a lot of chmoding, removing the default file and doing fancy ngnix stuff to server mutliple static directories) However, any changes aren’t reflected right away… is something being cached?
Hi,
When I execute manually by shell: (without my virtualenv activated) leandro@ubuntu-maxigenios:~/python_projects$ env_django/bin/gunicorn --workers 3 --bind unix:/home/leandro/python_projects/python_projects.sock webapps.wsgi:application [2016-03-02 08:09:09 +0000] [27330] [INFO] Starting gunicorn 19.4.5 [2016-03-02 08:09:09 +0000] [27330] [INFO] Listening at: unix:/home/leandro/python_projects/python_projects.sock (27330) [2016-03-02 08:09:09 +0000] [27330] [INFO] Using worker: sync [2016-03-02 08:09:09 +0000] [27335] [INFO] Booting worker with pid: 27335 [2016-03-02 08:09:09 +0000] [27337] [INFO] Booting worker with pid: 27337 [2016-03-02 08:09:09 +0000] [27339] [INFO] Booting worker with pid: 27339
and go to url in the browser: http://162.243.9.91/ all run OK. But, I think that exists a problem, a wrong in my configurations because not run OK automatically.
In logs of nginx:
2016/03/02 08:08:42 [error] 26700#0: *9 connect() to unix:/home/leandro/python_projects/python_projects.sock failed (111: Connection refused) while connecting to upstream, client: 187.5.221.66, server: 162.243.9.91, request: “GET / HTTP/1.1”, upstream: “http://unix:/home/leandro/python_projects/python_projects.sock:/”, host: “162.243.9.91”
Any idea ?
I know this is an older thread, but it would be incredibly helpful to see what the full directory structure/file tree is in the walkthrough scenario. As a newbie at this, I have found it incredibly hard to find a definitive source on the ideal setup for these projects and apps. It seems a lot of the problems and comments here reflect that.
Should these directions be expected to work on Ubuntu 15.10? I have followed them several times, over and over, exactly right, and I get a 502 bad gateway every time.
This comment has been deleted
As a first time deployer, it was news to me that the Upstart functionality that this tutorial relies on is completely gone in Ubuntu 15. If you’re having issues with this and you’re in 15, that’s why.
Does this apply to Django 1.9? I’m getting the error “No module named django.core.wsgi” when I try to bind gunicorn.
getting a site cannot be reached error message once i get to the In your web browser, visit your server’s domain name or IP address followed by :8000:
http://server_domain_or_IP:8000 page running the ubuntu 14.04 droplet
Hi Justin,
Being explicit on the file directory tree you are using can be very, very helpful. I mean you are using ‘myproject’ in so many occasions.
I got this command to work in this directory. /home/user/projectdir/virtualenv/
Now I’m stuck at “Create a Gunicorn Upstart File”. executing:
I got this:
If you can help please
I’m also having 502 errors, and can’t figure out what’s going wrong. My
/etc/init/gunicorn.conf
looks like this:If I
sudo service gunicorn start
, the socket file never appears. If I try to visit it in the browser, nginx error logs say:There are no logs in
/var/log/gunicorn
nor an upstart error log file.If I run the following commands, logged in as
admin
, the socket is created and I can visit in my browser:so, basically replicating the same steps in the bash script. All files in the
myproject
folder are owned by the useradmin
and thewww-data
group.Any ideas?
after I made all instructions I’ve got 502 Bad Gateway error
Quick note about the running the in-django server for the first time:
If you ran into a
DISALLOWED HOST
problem, you might want to change yourDEBUG = True
setting insettings.py
toDEBUG = False
, and see what happens.If you still get a
404 Not Found
error, add the ip of your server to theALLOWED_HOSTS
setting insettings.py
, and change back intoDEBUG = True
.Also, if you’re on AWS, it’s a good idea to create a new custom TCP rule in the security group of your instance, that allows 0.0.0.0\0 to enter via port 8000.
I had the same 502 error from nginx. The first fix was to install gunicorn inside my virtualenv.
Then gunicorn raised another error:
The fix for me was to install gevent.
pip install gevent
Where have you created the myproject.sock file ? What’s in there ?
one should add ipaddress or localhost in ALLOWED_HOSTS = [‘ip_address’, ‘localhost’] in settings.py file
I m getting 502 gateway error. I am running in ubuntu 14.04, but i dont seem to have upstart pre installed. so I had to install it. But even after installing it, I was not able to find /upstart/blog.log (blog is my project). Is there any other way to do it ?
this is my gunicorn log. but i dont have myproject.log in upstart
Internal Server Error: / Traceback (most recent call last): File “/root/blog/blogenv/local/lib/python2.7/site-packages/django/core/handlers/exception.py”, line 41, in inner File “/root/blog/blogenv/local/lib/python2.7/site-packages/django/utils/deprecation.py”, line 138, in call File “/root/blog/blogenv/local/lib/python2.7/site-packages/django/contrib/sessions/middleware.py”, line 20, in process_request File “/root/blog/blogenv/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py”, line 18, in init File “/root/blog/blogenv/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py”, line 51, in init File “/root/blog/blogenv/local/lib/python2.7/site-packages/django/utils/module_loading.py”, line 20, in import_string File “/usr/lib/python2.7/importlib/init.py”, line 37, in import_module import(name) ImportError: No module named serializers Internal Server Error: /
Thanks
@jellingwood
What’s wrong with my Gunicorn upscript? It tells me importerror: No module named ‘ipac’
my project dir: /var/www/ipaccorporation.com/ipac-corporation/ipac/ipac/wsgi.py