Uploaded new Flask code, socket file disappeared, how do I bring it back?

January 29, 2017 4.7k views
Nginx Python Ubuntu 16.04

I start this off with the fact that I am a python developer and I know very little about server management so this is all a learning curve for me.

I created a new Ubuntu 16.04 droplet and I followed this tutorial - https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-16-04. All was well and it worked.

I used Filezilla to move my flask application from my localhost to the droplet. I overwrote the old app.py with my app.py. wsgi.py was unchanged nothing else should have changed.

I ran sudo systemctl restart myproject and went to view the server from my browser and I received a 502 error.

Looking at the error log I have this:

2017/01/29 23:21:37 [crit] 18456#18456: *1 connect() to unix:/home/user/myproj/proj.sock failed (2: No such file or directory) while connecting to upstream, client: xx.xx.xx.xx, server: xx.xx.xx.xx, request: “GET / HTTP/1.1”, upstream: “http://unix:/home/user/myproj/proj.sock:/”, host: “xx.xx.xx.xx”

Which suggests there’s no socket file. I ran ls -al in /home/user/myproj and I can verify it’s not there. I didn’t delete it so I am not sure what happened to it and how to make it return.

I’ve verified the nginx.conf in /sites-enabled for this project and in the systemctl .service and all the info matches up.

I tried restarting/reloading my project.service, nginx and even a reboot of the server and no luck.

How do I properly return this socket file?

3 Answers

@rbourne

http://unix:/home/user/myproj/proj.sock:/

At first glance, the above is the key indicator. Sockets aren’t accessed over http locally, and that line seems to state that it’s attempting to do so – when it does, it fails.

Socket connects should look like:

unix:/home/user/myproj/proj.sock

So you’d need to find the line where the http:// is being defined and remove that.

  • Thanks for the feedback I do not believe that is.

    That comes right out of the Digital Ocean tutorial (linked in post). As part of /etc/nginx/sites-available/myproject under server { listen {} } it has proxy_pass http://unix:/home/user/myproj/proj.sock;

    That’s what I had before and it worked. That’s what I have on a similar droplet with another flask application that works.

    But to be sure I did replace it to just proxy_pass unix:/home/sammy/myproject/myproject.sock; and that throws an error. I cannot restart nginx as I receive the error nginx: [emerg] invalid URL prefix

    • @rbourne

      Ok, what I did was spin up a Droplet and ran through the entire guide twice to make sure I was able to duplicate what I did.

      The one fail point is with permissions on the /home/sammy directory. I had a general idea that this would be where I’d land, but I wanted to make sure since I am not a Python dev by trade (I normally work with PHP and Bash), thus my working knowledge of the setup other than NGINX is a bit limited.

      What you should need to do is run:

      chown -R sammy:www-data /home/sammy
      

      and then restart myproject and nginx

      sudo systemctl restart myproject \
      && sudo systemctl restart nginx
      

I found the solution.

My uploaded app.py was not able to be compiled on the server due to having not (yet) installed the requirements. I used pip install from my requirements.txt, verified I could run python app.py and when it was good to go I restarted myproject.service and the socket file was created. I could then view the app on the internet.

I was following the same tutorial when myproject.sock disappeared. I am using supervisor so in myproject.ini, i set socket=127.0.0.1:8080 an then
cd /etc/nginx/sites-available, edit the myproject file. In location,

location / {
include uwsgiparams;
uwsgi
pass 127.0.0.1:8080;
}

Then you need to restart nginx and it should work fine

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!