Flask

Flask - Part D

revised: 25-Dec-2021

Set Nginx to host the Flask site

Part D1: Establish 'myproject.service' is active

(venv) sammy@imega:/myapp$ sudo systemctl status myproject // to confirm that the service is running. ctl-c to exit.
if not, start the service:
(venv) sammy@imega:/myapp$ sudo systemctl start myproject

Part D2. Configuring Nginx to serve Flask

Assumption: Nginx Webserver is installed and Webmin configured for adjustments on Nginx. If the Nginx module is not installed in Webmin, see the following: iRedMail-Extras-PartA

Tip#01: An experience in a successful setup of a Basic Site (Part B) on Nginx Webserver would be helpful here.
Tip#02 It is strongly recommended to establish first a website certificate -- most conveniently done with Let'sEncrypt. See a step-by-step setup SSL Certificate(s) for Websites

Step01: Configure Nginx to Proxy to uWSGI

Enter Webmin [https://MySeverIP:10000] and go to [Servers]-> [Nginx Webserver]->[YourDomain eg.example.com]. Change the server block script similar to this, but changing the domain (example.com) to your own domain:

server {
    listen         80;
    server_name    example.com www.example.com;
    return         301 https://example.com$request_uri;
    return         301 https://www.example.com$request_uri;
    }
	
server {
  listen         443 ssl;
  listen         [::]:443 ssl;
  server_name example.com www.example.com;
  location / {
   include uwsgi_params;
   uwsgi_pass unix:/home/sammy/myapp/myproject.sock;
  }
  	ssl_certificate         /etc/ssl/certs/iRedMail.crt;
	ssl_certificate_key        /etc/ssl/private/iRedMail.key;
	ssl_prefer_server_ciphers on;
	ssl_ciphers         EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
	
	access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log info;
  
}	

Restart Nginx, open the browser with your domain (eg.https://example.com)

The same msg 'Hello There!' should appear in red. To serve the 3-page website (assuming you have synchonized the files to the server) do the following:

sammy@imega:/myapp$ nano wsgi.py // Change 'project' to the name of your app folder, should it be different in the future
sammy@imega:/myapp$ sudo systemctl restart myproject

Note: Remember restart systemctl service whenever changes to the website pages are made.

Reference: how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04

Notes for the mail-enhanced site:
Two remaining steps are required as follows;
1. Modify the settings in 'project.py' by changing 'example.com' with one's own domain and modifying the 'SECRET_KEY'
2. Create the mailbox: info@yourdomain.com via the 'iredadmin' page.

Tip#03: Via Webmin->Tools->CommandShell, enter the command: systemctl restart myproject
Tip#04: At times when restarting the Nginx webserver, it gives the error: '/var/run/nginx.pid not found'. As in Tip#03, enter the command: touch /var/run/nginx.pid then restart Nginx via Webmin->Servers->Nginx Webserver.
Flask Flask-PartB
Flask-PartC Flask-PartD