Flask

Flask - Part B

revised: 05-Sep-2023

Prerequisites: a. Basic Server (Ubuntu22.04 64bit) with Webmin, FTP, Firewall and iRedmail

Note: Adjust the Firewall, it's to be re-configured to allow port 5000 for testing purposes, if the port has not already been opened.

Flask on a Ubuntu Server

Part B. Create the Python Environment on the Server

Note: By default, Python 3 (3.10)is now initially installed and this is the version used.

Logon the server, via PuTTY, and enter the following commands:

sammy@imega:$ sudo apt update
sammy@imega:$ sudo apt upgrade
sammy@imega:$ sudo apt install build-essential python3-pip python3-dev
sammy@imega:$ mkdir -p ~/myapp
sammy@imega:$ cd myapp
sammy@imega:~/myapp$ python3 -m venv venv
sammy@imega:~/myapp$ . venv/bin/activate  // command begins with a "."
(venv) sammy@imega:~/myapp$ pip install uwsgi flask
(venv) sammy@imega:~/myapp$ pip install flask-wtf flask-mail email_validator // (Optional) where there is also a mailserver.

Part B2. Testing Flask and Setting WSGI as a service
-- In preparation for the webserver.

Step01:Test Flask

(venv) sammy@imega:~/myapp$ nano myproject.py   //see ***
(venv) sammy@imega:~/myapp$ python3 myproject.py

*** Copy & paste this text within file: myproject.py

Enter in the browser, one's domain at port:5000 (eg. http://[MySeverIP]:5000), the result should 'Hello there!' in green letters. Type in the PuTTY console: 'ctl'+c to exit.

Step02:Test wsgi

Logon the server, via PuTTY, and enter the following commands:

(venv) sammy@imega:~/myapp$ nano myproject.py   // change the word 'green' to 'blue
(venv) sammy@imega:~/myapp$ nano wsgi.py  //see **

** Copy & paste this text within file: wsgi.py

(venv) sammy@imega:~/myapp$ uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app

Enter in the browser, one's domain at port:5000 (eg. http://myexample.com:5000), the result should 'Hello there!' in blue letters. Type in the PuTTY console: 'ctl'+c to exit wsgi.

Step03: WSGI serving Myproject.py

Logon the server, via PuTTY, and enter the following commands:

(venv) sammy@imega:~/myapp$ nano myproject.ini    //see below **
(venv) sammy@imega:~/myapp$ sudo nano /etc/systemd/system/myproject.service   //see below ***
(venv) sammy@imega:~/myapp$ nano myproject.py   // change 'blue' to 'red'
(venv) sammy@imega:~/myapp$ sudo systemctl start myproject
(venv) sammy@imega:~/myapp$ sudo systemctl status myproject  // 'ctl'+c to exit

The output should show the service as active. Moreover, a file called 'myproject.sock' should appear in the 'myapp' directory.
Should the service be functioning well, then established it by enabling it permanently into services with the following command:

(venv) sammy@imega:~/myapp$ sudo systemctl enable myproject

** Copy & paste this text within file: myproject.ini

*** Copy & paste this text within file: myproject.service

Warning: There are in the Debian environment, slight differences with regard to that of the Win-PC. Take note of these:
- File names are case-sensitive. Example: myphoto.jpg and myphoto.JPG are seen as different files. If the image is not appearing in your website, check this issue.
- Permissions and Ownerships are concepts deeply implemented in the Linux environment. To ensure correct permissions and ownership for the above-example:
sammy@imega:~/myapp$ cd ~
sammy@imega:~$ chmod -R 775 myapp/   //all files executetable
sammy@imega:~$ chown -R sammy:www-data myapp/   // all files under one ownership.
Flask Flask-PartB
Flask-PartC Flask-PartD