3f1c49ca2c9032e5a01a72dcdcc9aa282b5e71b5
By:
maxgonpe

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

April 12, 2017 659 views
Django Ubuntu 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.

18 Answers
maxgonpe April 12, 2017
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

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.

@jtittle

I know you did everything you could to help me, so on that side you have an "A" jee jee. But I will also see the issue of permissions in files and directories. Before closing the thread in this thread. I would like to know what you ask about:


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??


After you answer this, I do not bother you anymore and I close the thread.

And thank you, very grateful for all your help.

  • @maxgonpe

    You're not bothering me, I do this for a living :-).

    Setting up the virtual environment is, last I recall, part of developing with Python. It's common to see it as a requirement. You could very well try without, though I'm not a Python developer, so as far as whether it's a "best practice" or not, I'm not sure.

    The error showing that the connection was refused signals a permissions issue and running the chown command should resolve that.

@jtittle

We almost did it,

I think this is the last one, after so much review and testing, including the permissions actions that you suggested to me. I managed to create the gunicorn.socket
Among the actions I did was, to give permissions, to "max" the group "www-data" and to make a change in the /etc/systemd/system/gunicorn.service

In the directory
WorkingDirectory = / home / max / myproject

by:
WorkingDirectory = / my home / max / myproject / myproject

now : i can see my page "working it" from django when i used de ip
but when use the domain , only "nginx working"

but must importan that i have a "myproject.sock" create.

some things else ? for win

@maxgonpe

In your NGINX server block, i.e:

/etc/nginx/sites-available/myproject

You'll need to change server_name to use your domain instead of an IP. So, this:

server_name 111.222.333.444;

Becomes:

server_name domain.com www.domain.com;

You would then point the A record for domain.com to the IPv4 IP of your Droplet. Once the changes have been made to your NGINX server block, you'd restart NGINX for the changes to take.

systemctl restart nginx

@jtittle

¡¡¡¡ ACCOMPLISHED!!!!

Thanks, we did it (it was for your help that I could do it). Now I can see my site through the domain and that is what I wanted and needed. I ran two more commands according to the tutorial:

Sudo ufw delete allow 8000
Sudo ufw allow 'Nginx Full'

The only thing I do not know if it is correct, is that now when I enter the number of ip, I get "welcome nginx" ??? , But I really do not care, since all this gave me a lot of war,

Thank you jtitle.

  • @maxgonpe

    That'd be due to this file:

    /etc/nginx/sites-available/default
    

    You can delete that server block:

    rm -rf /etc/nginx/sites-available/default
    

    The IP will show whatever is defined as the default_server in the listen directive. So you can open up:

    /etc/nginx/sites-available/myproject
    

    using

    nano /etc/nginx/sites-available/myproject
    

    and replace

    listen 80;
    

    with

    listen 80 default_server;
    

    then restart NGINX systemctl restart nginx and that should fix that.

@jtittle

And solve by adding the ip in allow-host. And it was excellent.

Take care my friend, I say goodbye, here I am, it's time to go to sleep.

Greetings bye

@jtittle

NOW ABOUT DATABASE POSTGRESS

Hello,

I tell you that I already take the trick to the issue of seeing the applications in production environment. And all thanks to you, since otherwise, would not have achieved.

Now I have a little problem, I want to share it if it has already happened to you. This is the postgres database, when I create the base and the user and I want to move from sqlite to postgres, I do not recognize the tables of the applications, there is no way that the "makemigrations" and "migrate" command add the tables Work, the only way to get it in pasandomen to sqlite. But I need a more robust database, since I do not trust much in sqilte.
And if I want to pass me after updating the tables in sqlite for posgres I get the following error:

"CommandError:" migrate "is not a valid port number or address: port pair."

I have already created two databases in postgres with new user and I have also tried to delete the "cache" of the migrations, but there is no way.

Do you have any idea of this topic ??

  • @maxgonpe

    The guide I provided uses the one you posted as a reference, which uses Postgres instead of SQLite. I noted in my guide that you needed to replace:

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

    with:

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

    Which is what allows you to use Postgres instead of SQLite. You will, however, need to refer to the guide posted by the DigitalOcean team on how to create the Postgres database and setup.

    The guide parts that I posted was mainly intended to get you setup and running, though you still need to use the Postgres part of their guide if you want to use Postgres :-).

@jtittle

hellow again, send to you my regals:

I have no problem changing my database from sqlite3 to postgres. The problem is another, it is that when I generate the database in sqlite3 or postgres, the database is created by default with the ten tables of systems, but it does not take me into account, nor add the additional tables that are In the "models.py of my applications.

When I run "migrations" always tells me that there are no changes

When I run "migrate" list and our tables that are in my applications and are those that I am interested to include; However, he tells me not to migrate them and he does not explain why.

So I have tried to delete the files from the "migrations" folder and all the cache deleting the ".pyc" files, but I always get the basic table by default without the tables of my application, suddenly manipulating and manipulating in sqlite3 I have Achieved, but the one that interests me is postgres that is more robust for what I want.

That's why I'm consulting you, if there is any way to reset the database and forget the whole history of migrations and can start from scratch ??? . I have also created new databases with another name, but the same is the basic database.

Well, I know it can be a problem of mine, rather than something I have forgotten in the design of my application, so I ask you to give me or guide me with some test or etc ...

Thank you.....

@jtittle

Why the changes of my development server, are not reflected in the production server (views and urls)?

Hi, I am writing to see if you can help me with this: I happen is that I make changes in the development server and they work me well, but when I want to see them in production, I do not recognize them and I throw the error:

Page not found (404)
Request Method: GET
Request URL: http://www.maxsistemas.cl/casas/mipagina

I have tried all sorts of views, and called in urls, (all of them work in development, but nothing works for me in production) last clone a class definition that makes the same call and uses the same template but only change the name of the Definition to see if it works, and it happens to me the same everything works well in development, but when I want to see it in production, I throw the error mentioned at the beginning (404).

I have also tried, reloading nginx with several commands for me to take the changes, but the negative result even, these are the commands I tried:

Sudo systemctl daemon-reload
Sudo systemctl start gunicorn
Sudo systemctl restart nginx
Sudo systemctl start nginx
Sudo ufw allow 'Nginx Full'

Thank you for the help, I do not know what else to try.

Have another answer? Share your knowledge.