Giter Club home page Giter Club logo

config-ubuntu-nginx-uwsgi-flask-socketio-gevent's Introduction

๐Ÿ”ง Config for Ubuntu 18.04 + NGINX+ uWSGI + Flask + SocketIO + Gevent

I had a hard time finding out on how to get SocketIO to work through NGINX and uWSGI. That's why I have put all needed config files here for future reference. Hopefully, it might help you too.

Notes

The server is setup using the following guides. You don't have to follow them, they're just here in case you are starting from scratch.

Some mistakes I have made in the process:

  • It's important that your project files are not owned by the root user. Otherwise, NGINX will complain later on because it can't access files that need root permissions. So make sure you have another user and let him own the project (chown)
  • Take care of the python version you use. On Ubuntu 18.04, you have to explicitly use the python3.6 command if you need python 3.6
  • To install your dependencies, only use the pip command when you are in your virtual environment. This pip will install everything in your local environment

Python dependencies

  • flask
  • flask-socketio
  • uwsgi
  • gevent

Gevent is the easiest way to let SocketIO work with the built-in websocket support of uWSGI. Below are the relevant parts of pip freeze in my case.

Flask==1.0.2
Flask-SocketIO==3.1.1
gevent==1.3.7
uWSGI==2.0.17.1
(...)

uWSGI config

wsgi.py

from app import app, socketio

if __name__ == '__main__':
    socketio.run(app)

wsgi.ini

[uwsgi]
module = wsgi:app

master = true
gevent = 500
buffer-size=32768 # optionally
http-websockets = true

socket = myproject.sock
chmod-socket = 660
vacuum = true

die-on-term = true

The argument gevent is the number of async cores to spawn (Gevent docs). When you raise this number, you can get an error saying something like invalid request block size: 21327 (max 4096)...skip. To fix this, add buffer-size=32768 to the config (source)

NGINX config

/etc/nginx/sites-available/myproject

server {
    listen 80;
    server_name example.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/path/to/project/myproject.sock;
    }

    location /socket.io/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        include uwsgi_params;
        uwsgi_pass unix:/path/to/project/myproject.sock;
    }
}

Both location sections use uwsgi_pass directly to the unix socket. It's important to add include uwsgi_params in both sections.

systemd service (optional)

Making a systemd service is optional. You can start uWSGI the way you like.

/etc/systemd/system/myproject.service

[Unit]
Description=uWSGI instance to serve myproject
After=network.target

[Service]
User=myuser
Group=www-data
WorkingDirectory=/path/to/project
Environment="PATH=/path/to/project/venv/bin"
ExecStart=/path/to/project/venv/bin/uwsgi --ini wsgi.ini

[Install]
WantedBy=multi-user.target

config-ubuntu-nginx-uwsgi-flask-socketio-gevent's People

Contributors

jellevhb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.