Finally, Flask + uWSGI + Nginx will be able to hit the API.
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>
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;
}
}
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/*;
$ 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.
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