[PYTHON] API with Flask + uWSGI + Nginx

Finally, Flask + uWSGI + Nginx will be able to hit the API.

Nginx installation

First, install Nginx and make sure it works.

$ sudo apt install -y nginx
$ sudo systemctl start nginx
$ curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Nginx config file

Since /etc/nginx.nginx.conf says include /etc/nginx/conf.d/*.conf ;, create a configuration file for uWSGI in /etc/nginx/conf.d/.

$ sudo vi /etc/nginx/conf.d/api.conf
server {
    listen       80;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
}

Nginx default site disabled

There is a default site setting in / etc / nginx / sites-enabled / default, which is included in nginx.conf, so comment it out.

$ sudo vi /etc/nginx/nginx.conf
- include /etc/nginx/sites-enabled/*;
+ #include /etc/nginx/sites-enabled/*;

start uWSGI

$ uwsgi --socket=/tmp/uwsgi.sock --wsgi-file=app.py --callable=app --chmod-socket=666
$ curl http://localhost/hello
{'message': 'Hello world!'}

You can now hit the API with Flask + uWSGI + Nginx.

uWSGI settings

It's neat to put the arguments in an ini.

$ vi uwsgi.ini
$ uwsgi uwsgi.ini
[uwsgi]
 
wsgi-file=app.py
callable=app
http=0.0.0.0:8000
socket=/tmp/uwsgi.sock
chmod-socket=666

I wonder if I should make it a service and start it automatically.

Recommended Posts

API with Flask + uWSGI + Nginx
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (installation)
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (implementation)
Persist Flask API server with forever
Until Hello World with Flask + uWSGI + Nginx @ Sakura's VPS (CentOS 6.6)
Create Python + uWSGI + Nginx environment with Docker
[Python] Quickly create an API with Flask
Flask can't be RESTful with azure API Apps
Hello World with nginx + uwsgi + python on EC2
Create Nginx + uWSGI + Python (Django) environment with docker
Try slack OAuth authentication with flask (Slack API V2)
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Using Flask with Nginx + gunicorn configuration [Local environment]
Non-blocking with Python + uWSGI
IP restrictions with Flask
Extrude with Fusion360 API
WebSocket with Python + uWSGI
Hello world with flask
Programming with Python Flask
Output log to console with Flask + Nginx on Docker
Try to make RESTful API with MVC using Flask 1.0.2
Setting to run application in subdirectory with nginx + uwsgi
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
From installing Flask on CentOS to making it a service with Nginx and uWSGI
[With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 2]
Image download with Flickr API
Deploy Flask with ZEIT Now
Use Trello API with python
Touch Flask + run with Heroku
Hello World with Flask + Hamlish
Create an API with Django
[blackbird-nginx] Monitor Nginx with blackbird
SNS Flask (Ajax) made with Flask
Web application development with Flask
I made LINE-bot with Python + Flask + ngrok + LINE Messaging API
Control error wording with nginx
Get information with zabbix api
Web API with Python + Falcon
Play RocketChat with API / Python
View flask coverage with pytest-cov
Support yourself with Twitter API
Call the API with python3.
Web application with Python + Flask ② ③
Use subsonic API with python3
File upload with Flask + jQuery
[With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 1]
Successful update_with_media with twitter API
Web application with Python + Flask ④
Qiita API Oauth with Django
Until API made with Flask + MySQL is converted to Docker
Get ranking with Rakuten API
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Django development environment construction with Docker-compose + Nginx + uWSGI + MariaDB (macOS edition)
(Note) Notes on building TensorFlow + Flask + Nginx environment with Docker Compose
Operate Nutanix with REST API Part 2
SNS Flask (Model) edition made with Flask
Build FastAPI + uvicorn + nginx with docker-compose
[LINE login] Verify state with Flask
[AWS] Create API with API Gateway + Lambda
Get reviews with python googlemap api
[Memo] Links for developing with Flask