https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04
https://www.golinuxcloud.com/flask-gunicorn-nginx/
https://faun.pub/deploy-flask-app-with-nginx-using-gunicorn-7fda4f50066a

https://faun.pub/deploy-flask-app-with-nginx-using-gunicorn-7fda4f50066a

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04

https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04

https://gunicorn.org/#deployment

https://www.fullstackpython.com/nginx.html


NGINX:
Nginx is commonly used as a web server to serve static assets such as images, CSS and JavaScript to web browser clients.

Nginx is also typically configured as a reverse proxy, which passes appropriate incoming HTTP requests to a WSGI server. The WSGI server produces dynamic content by running Python code. When the WSGI server passes its response, which is often in the HTML, JSON or XML format, the reverse proxy then responds to the client with that result.

Nginx works as a proxy server between the gunicorn server and the client(Web browser/REST Client).
 
Typically the client will not know or need to know that a Python web application generated the result. The result could have instead been generated by one or more backend systems written in any programming language, not just Python.

Nginx is an implementation of the web server concept. Learn how these pieces fit together in the deployment chapter or view the table of contents for all topics.

E Award 1,334.00


Gunicorn:  https://docs.gunicorn.org/en/stable/
Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model ported from Ruby’s Unicorn project. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.

Features
Natively supports WSGI, Django, and Paster
Automatic worker process management
Simple Python configuration
Multiple worker configurations
Various server hooks for extensibility
Compatible with Python 3.x >= 3.5

Gunicorn is a WSGI HTTP server. It is best to use Gunicorn behind an HTTP proxy server. We strongly advise you to use nginx.


use gunicorn as a WSGI server to communicate with our flask app, and Nginx as a proxy server between the gunicorn server and the client.

We need gunicorn between flask and nginx because the flask development server although good for debugging is weak and will not stand in production, so we need gunicorn as a wsgi server to communicate with flask.This article assumes that you already have a Linux server.

aafak@aafak-virtual-machine:~$ mkdir nginx_apps
aafak@aafak-virtual-machine:~$ cd nginx_apps/
aafak@aafak-virtual-machine:~/nginx_apps$ sudo apt install python3-venv

aafak@aafak-virtual-machine:~/nginx_apps$ mkdir nginx_app1
aafak@aafak-virtual-machine:~/nginx_apps$ cd nginx_app1/
aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ python3 -m venev nginx_app1_env
/usr/bin/python3: No module named venev
aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ python3 -m vev nginx_app1_env
/usr/bin/python3: No module named vev
aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ python3 -m venv nginx_app1_env
aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$
aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ source nginx_app1_env/bin/activate
(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$
(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ pip install wheel
Collecting wheel
  Using cached wheel-0.37.1-py2.py3-none-any.whl (35 kB)
Installing collected packages: wheel
Successfully installed wheel-0.37.1
(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$
(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ pip install gunicorn flask

(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ vim flask_app.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    app.run(host='0.0.0.0')
(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ sudo ufw allow 5000
Rule added
Rule added (v6)
(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$
(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ python3 flask_app.py
 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses (0.0.0.0)
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://127.0.0.1:5000
 * Running on http://172.17.29.165:5000 (Press CTRL+C to quit)


Browse: http://172.17.29.165:5000/

172.17.29.165 IP of this box(ubuntu VM)

(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ vim wsgi.py

from flask_app import app

if __name__ == "__main__":
    app.run()

(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ gunicorn --bind 0.0.0.0:5000 wsgi:app
[2022-03-29 13:23:55 +0530] [1818651] [INFO] Starting gunicorn 20.1.0
[2022-03-29 13:23:55 +0530] [1818651] [INFO] Listening at: http://0.0.0.0:5000 (1818651)
[2022-03-29 13:23:55 +0530] [1818651] [INFO] Using worker: sync
[2022-03-29 13:23:55 +0530] [1818653] [INFO] Booting worker with pid: 1818653
[2022-03-29 13:24:35 +0530] [1818651] [CRITICAL] WORKER TIMEOUT (pid:1818653)
[2022-03-29 13:24:35 +0530] [1818653] [INFO] Worker exiting (pid: 1818653)
[2022-03-29 13:24:35 +0530] [1818978] [INFO] Booting worker with pid: 1818978



Browse: http://172.17.29.165:5000/


(nginx_app1_env) aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ deactivate
aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$


aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ sudo vim /etc/systemd/system/nginx-app1.service


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

[Service]
User=aafak
Group=www-data
WorkingDirectory=/home/aafak/nginx_apps/nginx_app1
Environment="PATH=/home/aafak/nginx_apps/nginx_app1/nginx_app1_env/bin"
ExecStart=/home/aafak/nginx_apps/nginx_app1/nginx_app1_env/bin/gunicorn --workers 3 --bind unix:nginx-app1.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target



aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ sudo systemctl start nginx-app1
aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ sudo systemctl enable nginx-app1
Created symlink /etc/systemd/system/multi-user.target.wants/nginx-app1.service → /etc/systemd/system/nginx-app1.service.
aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$


aafak@aafak-virtual-machine:~/nginx_apps/nginx_app1$ sudo systemctl status nginx-app1
● nginx-app1.service - Gunicorn instance to serve myproject
     Loaded: loaded (/etc/systemd/system/nginx-app1.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-03-29 13:56:48 IST; 58s ago
   Main PID: 1838254 (gunicorn)
      Tasks: 4 (limit: 11872)
     Memory: 57.4M
     CGroup: /system.slice/nginx-app1.service
             ├─1838254 /home/aafak/nginx_apps/nginx_app1/nginx_app1_env/bin/python3 /home/aafak/nginx_apps/nginx_app1/nginx_ap>
             ├─1838258 /home/aafak/nginx_apps/nginx_app1/nginx_app1_env/bin/python3 /home/aafak/nginx_apps/nginx_app1/nginx_ap>
             ├─1838259 /home/aafak/nginx_apps/nginx_app1/nginx_app1_env/bin/python3 /home/aafak/nginx_apps/nginx_app1/nginx_ap>
             └─1838260 /home/aafak/nginx_apps/nginx_app1/nginx_app1_env/bin/python3 /home/aafak/nginx_apps/nginx_app1/nginx_ap>

Mar 29 13:56:48 aafak-virtual-machine systemd[1]: Started Gunicorn instance to serve myproject.
Mar 29 13:56:49 aafak-virtual-machine gunicorn[1838254]: [2022-03-29 13:56:49 +0530] [1838254] [INFO] Starting gunicorn 20.1.0
Mar 29 13:56:49 aafak-virtual-machine gunicorn[1838254]: [2022-03-29 13:56:49 +0530] [1838254] [INFO] Listening at: unix:nginx>
Mar 29 13:56:49 aafak-virtual-machine gunicorn[1838254]: [2022-03-29 13:56:49 +0530] [1838254] [INFO] Using worker: sync
Mar 29 13:56:49 aafak-virtual-machine gunicorn[1838258]: [2022-03-29 13:56:49 +0530] [1838258] [INFO] Booting worker with pid:>
Mar 29 13:56:49 aafak-virtual-machine gunicorn[1838259]: [2022-03-29 13:56:49 +0530] [1838259] [INFO] Booting worker with pid:>
Mar 29 13:56:49 aafak-virtual-machine gunicorn[1838260]: [2022-03-29 13:56:49 +0530] [1838260] [INFO] Booting worker with pid:>
lines 1-19/19 (END)



https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04
Step 1 – Installing Nginx:
sudo apt update
sudo apt install nginx


aafak@aafak-virtual-machine:~$ sudo ufw app list
Available applications:
  CUPS
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH
aafak@aafak-virtual-machine:~$
This list displays three profiles available for Nginx:

Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)
It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since you haven’t configured SSL for your server yet in this guide, you’ll only need to allow traffic on port 80.

You can enable this by typing the following:
aafak@aafak-virtual-machine:~$ sudo ufw allow 'Nginx HTTP'
Rule added
Rule added (v6)
aafak@aafak-virtual-machine:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
5000                       ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)
5000 (v6)                  ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

aafak@aafak-virtual-machine:~$
Check with the systemd init system to make sure the service is running:

aafak@aafak-virtual-machine:~$ systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-03-29 15:04:16 IST; 2min 55s ago
       Docs: man:nginx(8)
   Main PID: 1879768 (nginx)
      Tasks: 9 (limit: 11872)
     Memory: 8.0M
     CGroup: /system.slice/nginx.service
             ├─1879768 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─1879769 nginx: worker process
             ├─1879770 nginx: worker process
             ├─1879771 nginx: worker process
             ├─1879772 nginx: worker process
             ├─1879773 nginx: worker process
             ├─1879774 nginx: worker process
             ├─1879775 nginx: worker process
             └─1879776 nginx: worker process

Mar 29 15:04:15 aafak-virtual-machine systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 29 15:04:16 aafak-virtual-machine systemd[1]: Started A high performance web server and a reverse proxy server.
aafak@aafak-virtual-machine:~$


aafak@aafak-virtual-machine:~$ ip addr show ens160 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
172.17.29.165
fe80::a8d3:64a2:1c4a:85ec
aafak@aafak-virtual-machine:~$

Browse: http://172.17.29.165/
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.


Step 4 – Managing the Nginx Process
Now that you have your web server up and running, let’s review some basic management commands.

To stop your web server, type the following:

sudo systemctl stop nginx
To start the web server when it is stopped, type the following:

sudo systemctl start nginx
To stop and then start the service again, type the following:

sudo systemctl restart nginx
If you are simply making configuration changes, you can often reload Nginx without dropping connections instead of restarting it. To do this, type the following:

sudo systemctl reload nginx
By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing the following:

sudo systemctl disable nginx
To re-enable the service to start up at boot, you can type the following:

sudo systemctl enable nginx
Nginx should now start automatically when the server boots again.


aafak@aafak-virtual-machine:~$ sudo vim  /etc/nginx/sites-available/nginx_app1
aafak@aafak-virtual-machine:~$ cat /etc/nginx/sites-available/nginx_app1
server {
    listen 80;
    server_name your_domain www.your_domain;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/aafak/nginx_apps/nginx_app1/nginx-app1.sock;
    }
}

Working

server {
    listen 80;
    server_name 172.17.29.165;
    location / {
        include proxy_params;
        proxy_pass http://unix:/home/aafak/nginx_apps/nginx_app1/nginx-app1.sock;
    }
}


aafak@aafak-virtual-machine:~$

aafak@aafak-virtual-machine:~$ sudo ln -s /etc/nginx/sites-available/nginx_app1 /etc/nginx/sites-enabled
aafak@aafak-virtual-machine:~$
aafak@aafak-virtual-machine:~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
aafak@aafak-virtual-machine:~$
aafak@aafak-virtual-machine:~$ sudo systemctl restart nginx
aafak@aafak-virtual-machine:~$ sudo ufw delete allow 5000
Rule deleted
Rule deleted (v6)
aafak@aafak-virtual-machine:~$ sudo ufw allow 'Nginx Full'
Rule added
Rule added (v6)
aafak@aafak-virtual-machine:~$

aafak@aafak-virtual-machine:~$ ls /home/aafak/nginx_apps/nginx_app1/nginx-app1.sock
/home/aafak/nginx_apps/nginx_app1/nginx-app1.sock
aafak@aafak-virtual-machine:~$


aafak@aafak-virtual-machine:~$ sudo journalctl -u nginx
-- Logs begin at Sun 2022-03-13 19:44:52 IST, end at Tue 2022-03-29 15:30:54 IST. --
Mar 29 15:04:15 aafak-virtual-machine systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 29 15:04:16 aafak-virtual-machine systemd[1]: Started A high performance web server and a reverse proxy server.
Mar 29 15:21:29 aafak-virtual-machine systemd[1]: Stopping A high performance web server and a reverse proxy server...
Mar 29 15:21:29 aafak-virtual-machine systemd[1]: nginx.service: Succeeded.
Mar 29 15:21:29 aafak-virtual-machine systemd[1]: Stopped A high performance web server and a reverse proxy server.
Mar 29 15:21:29 aafak-virtual-machine systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 29 15:21:29 aafak-virtual-machine systemd[1]: Started A high performance web server and a reverse proxy server.
aafak@aafak-virtual-machine:~$


aafak@aafak-virtual-machine:~$ sudo journalctl -u nginx_app1
-- Logs begin at Sun 2022-03-13 19:44:52 IST, end at Tue 2022-03-29 15:31:26 IST. --
-- No entries --
aafak@aafak-virtual-machine:~$


aafak@aafak-virtual-machine:~$ tail -f  /var/log/nginx/access.log
172.17.160.160 - - [29/Mar/2022:15:12:06 +0530] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
172.17.160.160 - - [29/Mar/2022:15:12:07 +0530] "GET /favicon.ico HTTP/1.1" 404 197 "http://172.17.29.165/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"






aafak@aafak-virtual-machine:~/nginx_apps/nginx_app3$ sudo service nginx-app3 status > service.log
aafak@aafak-virtual-machine:~/nginx_apps/nginx_app3$ vim service.log
● nginx-app3.service - Gunicorn app3 instance to serve myproject
     Loaded: loaded (/etc/systemd/system/nginx-app3.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-03-30 16:23:00 IST; 15min ago
   Main PID: 2792530 (gunicorn)
      Tasks: 4 (limit: 11872)
     Memory: 57.6M
     CGroup: /system.slice/nginx-app3.service
             ├─2792530 /home/aafak/nginx_apps/nginx_app3/nginx_app3_venv/bin/python3 /home/aafak/nginx_apps/nginx_app3/nginx_app3_venv/bin/gunicorn --workers 3 --bind 0.0.0.0:5003 -m 007 wsgi:app
             ├─2792534 /home/aafak/nginx_apps/nginx_app3/nginx_app3_venv/bin/python3 /home/aafak/nginx_apps/nginx_app3/nginx_app3_venv/bin/gunicorn --workers 3 --bind 0.0.0.0:5003 -m 007 wsgi:app
             ├─2792539 /home/aafak/nginx_apps/nginx_app3/nginx_app3_venv/bin/python3 /home/aafak/nginx_apps/nginx_app3/nginx_app3_venv/bin/gunicorn --workers 3 --bind 0.0.0.0:5003 -m 007 wsgi:app
             └─2793072 /home/aafak/nginx_apps/nginx_app3/nginx_app3_venv/bin/python3 /home/aafak/nginx_apps/nginx_app3/nginx_app3_venv/bin/gunicorn --workers 3 --bind 0.0.0.0:5003 -m 007 wsgi:app

Mar 30 16:23:00 aafak-virtual-machine systemd[1]: Started Gunicorn app3 instance to serve myproject.
Mar 30 16:23:00 aafak-virtual-machine gunicorn[2792530]: [2022-03-30 16:23:00 +0530] [2792530] [INFO] Starting gunicorn 20.1.0
Mar 30 16:23:00 aafak-virtual-machine gunicorn[2792530]: [2022-03-30 16:23:00 +0530] [2792530] [INFO] Listening at: http://0.0.0.0:5003 (2792530)
Mar 30 16:23:00 aafak-virtual-machine gunicorn[2792530]: [2022-03-30 16:23:00 +0530] [2792530] [INFO] Using worker: sync
Mar 30 16:23:00 aafak-virtual-machine gunicorn[2792533]: [2022-03-30 16:23:00 +0530] [2792533] [INFO] Booting worker with pid: 2792533
Mar 30 16:23:00 aafak-virtual-machine gunicorn[2792534]: [2022-03-30 16:23:00 +0530] [2792534] [INFO] Booting worker with pid: 2792534
Mar 30 16:23:00 aafak-virtual-machine gunicorn[2792539]: [2022-03-30 16:23:00 +0530] [2792539] [INFO] Booting worker with pid: 2792539
Mar 30 16:23:54 aafak-virtual-machine gunicorn[2792530]: [2022-03-30 16:23:54 +0530] [2792530] [CRITICAL] WORKER TIMEOUT (pid:2792533)
Mar 30 16:23:54 aafak-virtual-machine gunicorn[2792533]: [2022-03-30 16:23:54 +0530] [2792533] [INFO] Worker exiting (pid: 2792533)
Mar 30 16:23:55 aafak-virtual-machine gunicorn[2793072]: [2022-03-30 16:23:55 +0530] [2793072] [INFO] Booting worker with pid: 2793072









https://www.golinuxcloud.com/flask-gunicorn-nginx/



aafak@aafak-virtual-machine:~$ sudo mkdir -p /var/www/your_domain/html
aafak@aafak-virtual-machine:~$ sudo chown -R $USER:$USER /var/www/your_domain/html
aafak@aafak-virtual-machine:~$ sudo chmod -R 755 /var/www/your_domain
aafak@aafak-virtual-machine:~$ vim  /var/www/your_domain/html/index.html
aafak@aafak-virtual-machine:~$ sudo nano /etc/nginx/sites-available/your_domain
aafak@aafak-virtual-machine:~$ sudo vim /etc/nginx/sites-available/your_domain
aafak@aafak-virtual-machine:~$ sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
aafak@aafak-virtual-machine:~$ sudo vim  /etc/nginx/nginx.conf
aafak@aafak-virtual-machine:~$ sudo nginx -t
nginx: [warn] conflicting server name "www.your_domain" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
aafak@aafak-virtual-machine:~$ sudo systemctl restart nginx
aafak@aafak-virtual-machine:~$


aafak@aafak-virtual-machine:~$ tail -f /var/log/nginx/error.log
