---
# System prepended metadata

title: Deploy the flask app on nginx
tags: [flask, python]

---

# Deploy the flask app on nginx
```
pip install flask uwsgin
sudo apt-get install nginx
```
/var/www/myapp/myapp.ini
```
[uwsgi]
module = main:app  # This is assuming your Flask app is in `main.py`
master = true
processes = 3
socket = 127.0.0.1:5000  # Bind uWSGI to a local socket
chmod-socket = 660
vacuum = true
die-on-term = true
```
/var/www/myapp/main.py
```
import flask
...flask app

if __name__ == "__main__":
    app = app.run()
```
Set Up Nginx to Serve Your Flask App
`sudo vim /etc/nginx/sites-available/flask_app`
```
server {
    listen 8123;
    server_name _; # or your.domain.com

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:5000;
    }
}
```
`sudo ln -s /etc/nginx/sites-available/flask_app /etc/nginx/sites-enabled`
## Start nginx
test configuration
```
sudo nginx -t
```
restart nginx deamon
```
sudo systemctl restart nginx
```
run uwsgi
```
uwsgi --ini app.ini
```
Test by curl
```
curl -X 'GET'   'http://xxx.xxx.xxx.xxx:1234/v1/place/batteryswap?latitud
e=25.01845349194727&longitude=121.47080666954706&radius=0.5&items=20'   -H 'accept: application/json'   -H 'Authorization: ke
y: xxxxxxxxx'
[
    {
        "name": "\u65b0\u5317\u5e02\u677f\u6a4b\u5340\u842c\u677f\u8def9\u865f(\u8fd1\u842c\u677f\u8def/\u6c11\u751f\u8def\u4e8c\u6bb5\u53e3)",
        "address": "\u65b0\u5317\u5e02\u677f\u6a4b\u5340\u842c\u677f\u8def9\u865f(\u8fd1\u842c\u677f\u8def/\u6c11\u751f\u8def\u4e8c\u6bb5\u53e3)",
        "city": "\u65b0\u5317\u5e02",
        "district": "\u677f\u6a4b\u5340",
        "latitude": 25.020968,
        "longitude": 121.470388,
        "phone": null,
        "distance": 282.7650519556063
    }
]
```
