jack-zheng
5/16/2018 - 1:48 PM

python, uwsgi, nginx

python, uwsgi, nginx

Level Up Setting Up Website

Level 01, run script base on uWSGI

  • install uwsgi package
pip install uwsgi
  • create test script
file name: 
  foobar.py

file content:
  def application(env, start_response):
      start_response('200 OK', [('Content-Type','text/html')])
      return [b"Hello World"]
  • run command: uwsgi --http: 9090 --wsgi-file foobar.py

output -> 'Hello World' show on localhost:9090

Level 02, install nginx

  • command: sudo apt-get install nginx
  • sudo service nginx start #start nginx server output -> 'welcome to nginx' show on localhost

Level 03, integrate uWSGI with Nginx

  • create .ini file to start service in convenient way
confog.ini
  [uwsgi]
  socket=127.0.0.1:3031
  wsgi-file=foobar.py
  master=true
  processes=4
  threads=2
  stats=127.0.0.1:9191
  • change nginx config
path of ubuntu: 
  /etc/nginx/sites-enabled/default

change location:
  location / {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:3031;
  }
  • restart nginx: sudo service nginx restart
  • run command: uwsgi config.ini output -> 'Hello World' on localhost:8080

Level 04, Integrate With Flask

  • create flask websit file
server.py
  from flask import Flask
  app = Flask(__name__)

  @app.route('/')
  def index():
      return '<h1>Hello World</h1>'
  if __name__ == '__main__':
      app.run(debug=True)
  • update config.ini
after update:
  [uwsgi]
  socket=127.0.0.1:3031
  wsgi-file=server.py
  master=true
  processes=4
  threads=2
  stats=127.0.0.1:9191
  callable=app

output -> 'Hello World' show on localhost page

Source List

Install Nginx In Ubuntu
uWSGI Offical Document
Nginx + uWSGI 中文