Question

Just tell me if you ran the site and that was what you used

I just want to know, if in this community, there are some users who have been able to configure their site for django - gunicorn - nginx following the tutorial steps

Https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

Or Https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

Has managed to run its application and that is also visible in production by the number of ip and / or the domain name “www.xxxxxx.xx” and does not appear BAD 502. only that ??? If it works please report that you had success, to clear if there is something in my procedure or is some bug, suddenly if some I achieved using another system for example as dbain, please report please.

I have my doubts, since practically in all the forums my problem is in the air without RESOlVER.


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Well every time I get more clear this matter, but I still can not run my application.

I commented that the application is at zero only has the minimum parameters in “settings.py” so that I launch “its work” from django.

I did everything carefully, according to the guide that you sent me and although it really helped me a lot I did not deploy the site, I only get the welcome to “nginx” and accessing the domain launches the welcome page of nginx, but accessing by ip Throw me bad 502.

Important thing I noticed, is that I do not see anywhere a “myproject.sock” file or I do not know where it is and checking the error log gives me the following.

Can you see that can be happening please ??

root@max:/home/max/myproject/myproject# sudo systemctl status gunicorn● gunicorn.service - gunicorn daemon Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Wed 2017-04-12 22:09:50 UTC; 12min ago Main PID: 24464 (code=exited, status=1/FAILURE)

Apr 12 22:09:45 max gunicorn[24464]: [2017-04-12 22:09:45 +0000] [24464] [INFO] Starting gunicor Apr 12 22:09:45 max gunicorn[24464]: [2017-04-12 22:09:45 +0000] [24464] [ERROR] Retrying in 1 s Apr 12 22:09:46 max gunicorn[24464]: [2017-04-12 22:09:46 +0000] [24464] [ERROR] Retrying in 1 s Apr 12 22:09:47 max gunicorn[24464]: [2017-04-12 22:09:47 +0000] [24464] [ERROR] Retrying in 1 s Apr 12 22:09:48 max gunicorn[24464]: [2017-04-12 22:09:48 +0000] [24464] [ERROR] Retrying in 1 s Apr 12 22:09:49 max gunicorn[24464]: [2017-04-12 22:09:49 +0000] [24464] [ERROR] Retrying in 1 s Apr 12 22:09:50 max gunicorn[24464]: [2017-04-12 22:09:50 +0000] [24464] [ERROR] Can’t connect t Apr 12 22:09:50 max systemd[1]: gunicorn.service: Main process exited, code=exited, status=1/FAI Apr 12 22:09:50 max systemd[1]: gunicorn.service: Unit entered failed state. Apr 12 22:09:50 max systemd[1]: gunicorn.service: Failed with result ‘exit-code’.

and log error:

2017/04/12 22:16:26 [crit] 24587#24587: *1 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:16:41 [crit] 24587#24587: *1 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:22:58 [crit] 24587#24587: *6 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:23:04 [crit] 24587#24587: *6 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:23:40 [crit] 24587#24587: *6 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:30:29 [crit] 24587#24587: *11 connect() to unix:/home/max/myproject/myproject.soc$ 2017/04/12 22:30:29 [crit] 24587#24587: *13 connect() to unix:/home/max/myproject/myproject.soc$

$t.sock failed (2: No such file or directory) while connecting to upstream, client: 181.163.181$ 2017/04/12 22:16:41 [crit] 24587#24587: *1 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:22:58 [crit] 24587#24587: *6 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:23:04 [crit] 24587#24587: *6 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:23:40 [crit] 24587#24587: *6 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:30:29 [crit] 24587#24587: *11 connect() to unix:/home/max/myproject/myproject.soc$ 2017/04/12 22:30:29 [crit] 24587#24587: *13 connect() to unix:/home/max/myproject/myproject.soc$

$63.181.193, server: 67.207.84.106, request: “GET / HTTP/1.1”, upstream: "http://unix:/home/max$ 2017/04/12 22:16:41 [crit] 24587#24587: *1 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:22:58 [crit] 24587#24587: *6 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:23:04 [crit] 24587#24587: *6 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:23:40 [crit] 24587#24587: *6 connect() to unix:/home/max/myproject/myproject.sock$ 2017/04/12 22:30:29 [crit] 24587#24587: *11 connect() to unix:/home/max/myproject/myproject.soc$ 2017/04/12 22:30:29 [crit] 24587#24587: *13 connect() to unix:/home/max/myproject/myproject.soc$

its so importan for me, that you givme a direction to do

@maxgonpe

For Python 3, my test consisted of the following commands. I used max as the username here since that’s what you wanted to use, so you should be able to simply copy and paste my commands in to terminal (MacOS), PuTTy (Windows), etc.

You can copy and paste multi-line commands directly in as well (I use them to prevent having to enter in single line commands and speed things up a bit).

sudo apt-get update \
&& sudo apt-get -y dist-upgrade \
&& sudo apt-get -y install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx \
&& sudo pip3 install virtualenv \
&& pip3 install --upgrade pip
mkdir -p /home/max/myproject \
&& useradd -d /home/max max
cd /home/max/myproject \
&& virtualenv myprojectenv \
&& source myprojectenv/bin/activate \
&& pip install django gunicorn psycopg2 \
&& django-admin.py startproject myproject .
nano myproject/settings.py

Find ALLOWED_HOSTS = [] and add your Droplet’s Public IPv4 IP there so that it looks like this (change 111.222.333.444 to your IP).

ALLOWED_HOSTS = ['111.222.333.444']

Find:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Replace it with (I used the defaults, you’ll need to change NAME, USER, PASSWORD to match).

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

At the bottom of the file, find STATIC_URL = '/static/' and directly below it add:

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

CTRL + X and save the file. Then run:

./manage.py makemigrations \
&& ./manage.py migrate \
&& ./manage.py createsuperuser

You’ll be asked to create a user. Choose whatever username and password you’d like. That’s what you’ll login to the admin panel with once the project is live.

./manage.py collectstatic

For the time being, skip all the firewall rules in the guide. With Ubuntu, ufw (the firewall) is off by default, so let’s not worry about those rules just yet. Well move on to the rest of the setup.

Let’s make sure the project can be served now.

./manage.py runserver 0.0.0.0:8000

If you can access the project on port 8000, let’s see if Gunicorn works now. Hit CTRL + C to exit the server.

gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

If that works too, so far so good :-). Hit CTRL + C to exit the server and then run deactivate to kill off the virtual env.

Now we need to make sure all the files inside the home directory are owned by max otherwise the systemd file we create will fail to create the socket. So let’s run:

chown -R max:max /home/max/*

Now we’ll setup the systemd file.

sudo nano /etc/systemd/system/gunicorn.service

Within that paste in:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=max
Group=max
WorkingDirectory=/home/max/myproject
ExecStart=/home/max/myproject/myprojectenv/bin/gunicorn --workers 3 -u max -g max --bind unix:/home/max/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

Hit CTRL + X and save the file. Now run:

sudo systemctl start gunicorn && sudo systemctl enable gunicorn

Now we’ll setup the NGINX server block:

sudo nano /etc/nginx/sites-available/myproject

Within that file, paste:

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/max/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/max/myproject/myproject.sock;
    }
}

You need to change server_name server_domain_or_IP; by replacing server_domain_or_IP with the IP of your Droplet. So it should look like:

server_name 111.222.333.444;

Where 111.222.333.444 is your IP. CTRL + X and save the file.

Create the required symlink:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

Test the configuration:

sudo nginx -t

Restart NGINX:

sudo systemctl restart nginx

You’ll need to refer back to the guide on setting up the Postgres Database, though the above should get you up and working with Python 3. I just tested this with each of the commands I just used above.

@jtittle any way , thanks

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.