0

I have been following this tutorial to deploy my django project on Digital Ocean. I am trying to configure gunicorn.

My project structure looks similar to this:

enter image description here

On my settings.py I use DEBUG=False

I create the gunicorn.socket and gunicorn.service.

/etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

/etc/systemd/system/gunicorn.service

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

[Service]
User=someuser
Group=www-data
WorkingDirectory=/home/someuser/myproject
ExecStart=/home/someuser/myproject/myprojectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          Myproject.wsgi:application

[Install]
WantedBy=multi-user.target

I start and enable the Gunicorn socket:

sudo systemctl start gunicorn.socket 
sudo systemctl enable gunicorn.socket

Check the status of the process to find out whether it was able to start:

sudo systemctl status gunicorn.socket

This is what I get and then it returns to the command line.

Failed to dump process list, ignoring: No such file or directory
●  gunicorn.socket - gunicorn socket
   Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
   Active: active (listening) since Sat 2019-05-04 23:12:03 UTC; 13s ago
   Listen: /run/gunicorn.sock (Stream)
   CGroup: /system.slice/gunicorn.socket

May 04 23:12:03 myproject systemd[1]: Listening on gunicorn socket.

Next, I check for the existence of the gunicorn.sock file within the /run directory:

file /run/gunicorn.sock
/run/gunicorn.sock: socket

It seems that a file or directory doesn't exist, but it doesn't provide any more details. The gunicorn.sock seems to exist.

I have decided to go through with the whole tutorial and see what happens:

This is the gunicorn socket's log:

$ sudo journalctl -u gunicorn.socket

-- Logs begin at Fri 2019-05-03 21:38:51 UTC, end at Tue 2019-05-07 20:05:36 UTC. --
May 07 20:04:42 myproject systemd[1]: Listening on gunicorn socket.

Testing Socket Activation

$ sudo systemctl status gunicorn

gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

To test the socket activation mechanism, we can send a connection to the socket through curl by typing:

$ curl --unix-socket /run/gunicorn.sock localhost

running this, going to myip:8000, the tutoral says that I should see the HTML output from my application in the terminal which does not happen.

After a while of nothing happening, I get in the terminal:

<h1>Bad Request (400)</h1>

To verify that the Gunicorn service is running:

$ sudo systemctl status gunicorn

gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-05-07 20:06:15 UTC; 1min 5s ago
 Main PID: 1884 (gunicorn)
    Tasks: 4 (limit: 1152)
   CGroup: /system.slice/gunicorn.service
           1884 /home/someuser/myproject/myprojectenv/bin/python3 /home/someuser/myproject/myprojectenv/bin/gunicorn --access-
           1915 /home/someuser/myproject/myprojectenv/bin/python3 /home/someuser/myproject/myprojectenv/bin/gunicorn --access-
           1916 /home/someuser/myproject/myprojectenv/bin/python3 /home/someuser/myproject/myprojectenv/bin/gunicorn --access-
           1917 /home/someuser/myproject/myprojectenv/bin/python3 /home/someuser/myproject/myprojectenv/bin/gunicorn --access-

May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1904] [INFO] Booting worker with pid: 1904
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1905] [INFO] Booting worker with pid: 1905
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1902)
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1904)
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1905)
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1915] [INFO] Booting worker with pid: 1915
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1916] [INFO] Booting worker with pid: 1916
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1917] [INFO] Booting worker with pid: 1917
May 07 20:07:02 myproject gunicorn[1884]:  - - [07/May/2019:20:07:02 +0000] "GET / HTTP/1.1" 400 26 "-" "curl/7.58.0"
May 07 20:07:10 myproject gunicorn[1884]:  - - [07/May/2019:20:07:10 +0000] "GET / HTTP/1.1" 400 26 "-" "curl/7.58.0"

Checking the gunicorn logs:

$ sudo journalctl -u gunicorn

-- Logs begin at Fri 2019-05-03 21:38:51 UTC, end at Tue 2019-05-07 20:07:58 UTC. --
May 07 20:06:15 myproject systemd[1]: Started gunicorn daemon.
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1884] [INFO] Starting gunicorn 19.9.0
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1884] [INFO] Listening at: unix:/run/gunicorn.sock (1884)
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1884] [INFO] Using worker: sync
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1902] [INFO] Booting worker with pid: 1902
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1904] [INFO] Booting worker with pid: 1904
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1905] [INFO] Booting worker with pid: 1905
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1902)
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1904)
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1905)
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1915] [INFO] Booting worker with pid: 1915
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1916] [INFO] Booting worker with pid: 1916
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1917] [INFO] Booting worker with pid: 1917
May 07 20:07:02 myproject gunicorn[1884]:  - - [07/May/2019:20:07:02 +0000] "GET / HTTP/1.1" 400 26 "-" "curl/7.58.0"
May 07 20:07:10 myproject gunicorn[1884]:  - - [07/May/2019:20:07:10 +0000] "GET / HTTP/1.1" 400 26 "-" "curl/7.58.0"

After that it is the configuration of nginx:

$ sudo nano /etc/nginx/sites-available/Myproject 

server {
    listen 80;
    server_name xxx.xxx.xxx.xxx;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /staticfiles/ {
        root /home/someuser/myproject ;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

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
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If no errors are reported, go ahead and restart Nginx by typing:

$ sudo systemctl restart nginx

Finally, we need to open up our firewall to normal traffic on port 80. Since we no longer need access to the development server, we can remove the rule to open port 8000 as well:

$ sudo ufw delete allow 8000
Rule deleted
Rule deleted (v6)

$ sudo ufw allow 'Nginx Full'
Rule added
Rule added (v6)

You should now be able to go to your server's domain or IP address to view your application.

Going to xxx.xxx.xxx.xxx:8000, I get that this site can't be reached, took too long to respond.

During all this in my settings.py, I have this:

ALLOWED_HOSTS = ["xxx.xxx.xxx.xxx"]  # my droplet's ip

I am familiar with Python but not with servers and deploying so I am at a loss here.

I have successfully followed the similar tutorial for Ubuntu 16.04 a few months ago, but now I keep hitting issues.

Any weird characters seem to appear because I copy paste the output from the PyCharm terminal.

  • Hm, I think something went wrong with your paste, I could not see the error message the first time because of some strange characters. Probably your ssh connection is not using UTF-8 for some reason. I think it's fixed now, and I can see the error. – Michael Hampton May 7 at 3:30
  • A quick trip through Google suggests that nobody else knows what it means either, but that it's entirely harmless. You should continue setting up your service. – Michael Hampton May 7 at 3:40
  • @MichaelHampton Ok, thakns. I will keep going with the tutorial and report any other issues, if that's ok. – GiannisIordanou May 7 at 3:56
  • @MichaelHampton I have updated my question with the commands I run and the output I get. – GiannisIordanou May 7 at 20:42
  • Going to xxx.xxx.xxx.xxx, the site is being served correctly, apart of the static files. I suppose there is an issue with the proper directories. – GiannisIordanou May 7 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.