Question

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

Posted April 12, 2017 7.9k views
DjangoUbuntu 16.04

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.

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.

×
Submit an Answer
19 answers

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

To start with, let’s update, upgrade, and install the packages we need to install to get things started.

apt-get update \
&& apt-get -y dist-upgrade \
&& apt-get -y install python-pip python-dev libpq-dev postgresql postgresql-contrib nginx

Now, what the tutorial doesn’t mention is that you need to create a new user, create a home directory for said user, and work from there. The reason for this is because NGINX and related services do not have access to ~/, which is where the tutorial is telling you to create a directory (i.e. without cd‘ing in to the users directory).

That’s an oversight, and one that causes the tutorial to fail (but there’s another issue too).

So to start:

mkdir -p /home/sammy/myproject
useradd -d /home/sammy sammy
cd /home/sammy/myproject

From within /home/sammy/myproject is where you want to run the commands to get things setup.

Additionally, there’s an issue with the systemctl service script. It doesn’t properly use the defined User and Group as you would expect. What I had to do was change:

ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/sammy/myproject/myproject.sock myproject.wsgi:application

to

ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 -u sammy -g www-data --bind unix:/home/sammy/myproject/myproject.sock myproject.wsgi:application

In the above the only change/addition I’m making is:

-u sammy -g www-data

So basically I’m passing the user and group to ExecStart. I’m not removing the other lines, just making this one change.

If you change this on an existing service, you’ll need to run:

systemctl daemon-reload

Then:

systemctl restart gunicorn

That should fix the common NGINX error that some are seeing.

Of course, change sammy to whatever user you decide to create and use.

  • Thanks, thanks, thanks, jtittle, for answering my question with a good explanation of what happens.

     I will do all my project under the instructions that you repeat me and I will inform you that such was me. For now I will put into practice what I pointed out.

    Thank you.

  • Before starting, I just want to know if my projects are python3, this is not relevant when doing apt-get -y install python-pip python-dev libpq-dev postgresql postgresql-contrib nginx. I can do that later, or it is now time to make the change and do the installation once for “python3” or there is some situation so that after this does not work.
    Anyway I will try as you indicate me first with the normal python

    • @maxgonpe

      The only real changes from my post and their guide is setting up the user directories and changing the systemctl service script. The rest of the guide should be followed as written.

      It only seems that specifics were left out, though the above should work whether you use Python 2 or 3.

@jtittle
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

The myproject.sock file is created when you use systemctl start gunicorn and destroyed when you run systemctl stop gunicorn or in cases where the service crashes. If gunicorn fails to start, this file will not be created.

Using the example/dummy content in the tutorial, if all goes well, it would exist here:

/home/sammy/myproject/myproject.sock

You may need to add sammy to the www-data group using:

usermod -aG www-data sammy

and also set directory permissions on the home directory as well, if you’ve not already. We can do that recursively using:

chown -R sammy:sammy /home/sammy/*

@jtittle
i maked the change with my directory “sammy” to “max” but the same .

in case that is well i send the status again:

root@max:/home/max/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 23:43:11 UTC; 1min 41s ago
Process: 25059 ExecStart=/home/max/myproject/myprojectenv/bin/gunicorn –workers 3 -u max -g w
Main PID: 25059 (code=exited, status=1/FAILURE)

Apr 12 23:43:11 max gunicorn[25059]: File “/home/max/myproject/myprojectenv/lib/python3.5/site
Apr 12 23:43:11 max gunicorn[25059]: time.sleep(0.1)
Apr 12 23:43:11 max gunicorn[25059]: File ”/home/max/myproject/myprojectenv/lib/python3.5/site
Apr 12 23:43:11 max gunicorn[25059]: self.reapworkers()
Apr 12 23:43:11 max gunicorn[25059]: File “/home/max/myproject/myprojectenv/lib/python3.5/site
Apr 12 23:43:11 max gunicorn[25059]: raise HaltServer(reason, self.WORKER
BOOT_ERROR)
Apr 12 23:43:11 max gunicorn[25059]: gunicorn.errors.HaltServer: <HaltServer ‘Worker failed to b
Apr 12 23:43:11 max systemd[1]: gunicorn.service: Main process exited, code=exited, status=1/FAI
Apr 12 23:43:11 max systemd[1]: gunicorn.service: Unit entered failed state.
Apr 12 23:43:11 max systemd[1]: gunicorn.service: Failed with result 'exit-code’.

and the log:

2017/04/12 23:44:09 [crit] 25121#25121: *3 connect() to unix:/home/max/myproject/myproject.sock$
server: 67.207.84.106, request: "GET / HTTP/1.1”, upstream: “http://unix:/home/ma$
ome/max/myproject/myproject.sock:/”, host: “67.207.84.106”

im working of my part that i hope yoy helpme again,

  • @maxgonpe

    If you would, please paste the output of:

    cat /etc/systemd/system/gunicorn.service
    

    To a code block as a reply.

    • sure

      [Unit]
      Description=gunicorn daemon
      After=network.target
      [Service]
      User=max
      Group=www-data
      WorkingDirectory=/home/max/myproject
      ExecStart=/home/max/myproject/myprojectenv/bin/gunicorn –workers 3 -u max -g www-data –bind unix:/home/max/myproject/myproject.sock myproject.wsgi:application
      [Install]
      WantedBy=multi-user.target

      last status of gunicorn service
      root@max:~# 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 Thu 2017-04-13 00:13:59 UTC; 30min ago
      Process: 25391 ExecStart=/home/max/myproject/myprojectenv/bin/gunicorn –workers 3 -u max -g
      Main PID: 25391 (code=exited, status=3)

      Apr 13 00:13:59 max gunicorn[25391]: return util.importapp(self.appuri)
      Apr 13 00:13:59 max gunicorn[25391]: File “/home/max/myproject/myprojectenv/lib/python3.5/si
      Apr 13 00:13:59 max gunicorn[25391]: import(module)
      Apr 13 00:13:59 max gunicorn[25391]: ImportError: No module named ‘myproject.wsgi’
      Apr 13 00:13:59 max gunicorn[25391]: [2017-04-13 00:13:59 +0000] [25413] [INFO] Worker exiting
      Apr 13 00:13:59 max gunicorn[25391]: [2017-04-13 00:13:59 +0000] [25391] [INFO] Shutting down:
      Apr 13 00:13:59 max gunicorn[25391]: [2017-04-13 00:13:59 +0000] [25391] [INFO] Reason: Worker
      Apr 13 00:13:59 max systemd[1]: gunicorn.service: Main process exited, code=exited, status=3/N
      Apr 13 00:13:59 max systemd[1]: gunicorn.service: Unit entered failed state.
      Apr 13 00:13:59 max systemd[1]: gunicorn.service: Failed with result 'exit-code’.

      • @maxgonpe

        Definitely odd as short of what I posted, I followed the tutorial verbatim.

        When you run:

        ./manage.py runserver 0.0.0.0:8000
        

        or (after systemctl stop gunicorn)

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

        Can you access the demo app via IP on port 8000?

        You’ll need to be in your project directory when you run those.

@jtittle

this is correct,

I can enter perfectly both ways

When I’m inside after running those commands, I can see
My site perfectly

Http: // myip: 8000 is good “its working from django”
And
Http: // my
domain: 800 is good “its working forn django”

But when I remove “: 8000”

Http: // myip is bad 502
And
Http: // my
domain is “wlcome to nginx”

Now in case this information serves when I run the command inside of my virtual environment on, the result of:

gunicorn –bind 0.0.0.0:8000 myproject.wsgi:application

(myprojectenv) root@max:/home/max/myproject/myproject# gunicorn –bind 0.0.0.0:8000

(myprojectenv) root@max:/home/max/myproject/myproject# gunicorn –bind 0.0.0.0:8000 myproject.wsgi:application

[2017-04-13 13:48:47 +0000] [3223] [INFO] Starting gunicorn 19.7.1
[2017-04-13 13:48:47 +0000] [3223] [INFO] Listening at: http://0.0.0.0:8000 (3223)
[2017-04-13 13:48:47 +0000] [3223] [INFO] Using worker: sync
[2017-04-13 13:48:47 +0000] [3226] [INFO] Booting worker with pid: 3226

But if the virtual environment was dashed and I threw the mismi command from outside

root@max:/home/max/myproject/myproject# gunicorn –bind 0.0.0.0:8000 myproject.wsgi:application [2017-04-13 13:53:53 +0000] [3268] [INFO] Starting gunicorn 19.6.0
[2017-04-13 13:53:53 +0000] [3268] [ERROR] Connection in use: (‘0.0.0.0’, 8000)
[2017-04-13 13:53:53 +0000] [3268] [ERROR] Retrying in 1 second.
[2017-04-13 13:53:54 +0000] [3268] [ERROR] Connection in use: ('0.0.0.0’, 8000)
[2017-04-13 13:53:54 +0000] [3268] [ERROR] Retrying in 1 second.
[2017-04-13 13:53:55 +0000] [3268] [ERROR] Connection in use: ('0.0.0.0’, 8000)
[2017-04-13 13:53:55 +0000] [3268] [ERROR] Retrying in 1 second.
[2017-04-13 13:53:56 +0000] [3268] [ERROR] Connection in use: ('0.0.0.0’, 8000)
[2017-04-13 13:53:56 +0000] [3268] [ERROR] Retrying in 1 second.
[2017-04-13 13:53:57 +0000] [3268] [ERROR] Connection in use: ('0.0.0.0’, 8000)
[2017-04-13 13:53:57 +0000] [3268] [ERROR] Retrying in 1 second.
[2017-04-13 13:53:58 +0000] [3268] [ERROR] Can’t connect to ('0.0.0.0’, 8000)

I do not know, if this is correct, but I mention it to you, so you can help me with this. And once again I thank you for your time, effort and dedication to show solidarity in this matter. I for my part I am documenting to understand all the concepts well, but I am frustrated that something so simple

@jtittle
I’m curious, suppose you are not working with a virtual environment, but directly everything as it is and has the server for all software, configurations and users?
Then how could be the correct way to address the same of this instruction, but without virtual environment:

ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn –workers 3 -u sammy -g www-data –bind unix:/home/sammy/myproject/myproject.sock myproject.wsgi:application

exits for no virtual enviroment? How would the syntax be?

Come to this situation testing possibilities for it to work, and thinking that maybe it is the virtual environment that complicates something this.

It will be possible??

  • @maxgonpe

    I’m running another test using Python 3 on my end to see if there’s something different that isn’t working compared to the test I ran on Python 2. I should have an update here shortly :-).

@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

I followed step by step very carefully all the tutorial and check up to the smallest detail, everything works well, as I go through the configuration, but in the end, only shows me the welcome to Nginx. what will happen?
Can you tell me that I use to run the ubuntu xx.xx configuration ??

In case something indicates this information, what are you going to see?

root@max:/home/max/myproject/myproject# sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@max:/home/max/myproject/myproject# sudo systemctl restart nginx
root@max:/home/max/myproject/myproject# sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2017-04-13 19:58:07 UTC; 5min ago
Process: 7519 ExecStop=/sbin/start-stop-daemon –quiet –stop –retry QUIT/5 –pidfile /run/ng
Process: 7534 ExecStart=/usr/sbin/nginx -g daemon on; masterprocess on; (code=exited, status=
Process: 7524 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master
process on; (code=exited
Main PID: 7541 (nginx)
Tasks: 2 (limit: 4915)
Memory: 2.5M
CPU: 29ms
CGroup: /system.slice/nginx.service
├─7541 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─7544 nginx: worker process

Apr 13 19:58:07 max systemd[1]: Starting A high performance web server and a reverse proxy serve
Apr 13 19:58:07 max systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Inva
Apr 13 19:58:07 max systemd[1]: Started A high performance web server and a reverse proxy server
ESCOC

and error.log

2017/04/13 19:58:33 [error] 7544#7544: *1 connect() to unix:/home/max/myproject/myproject.sock
failed (111: Connection refused) while connecting to upstream, client: 181.163.145.226,
server: 67.207.84.106, request: “GET / HTTP/1.1”, upstream: “http://unix:/home/max/myproject/myproject.sock:/”, host: “67.207.84.106”

  • @maxgonpe

    If you’re not able to create the socket, the issue is most likely permissions based, which is why you need to run:

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

    Once you’ve ran all the commands to generate your files. If proper permissions are not setup (which is why we run that command), you won’t be able to create the socket.

    You would then restart Gunicorn - systemctl restart gunicorn

    Short of the Postgres commands, the above is everything I used to create a working install on Ubuntu 16.04.

Previous 1 2 Next