Web Technologies/2021-2022/Laboratory 7

Web servers
In the context of Web Technologies, we have described how interaction is done in a client-server architecture. With HTTP we have so far seen examples for clients (browsers, web crawlers, download clients). However, on the other part we have Web Servers which implement how HTTP requests are to be dealt with, and how responses are to be crafted, and add the information that is to be delivered to the client.

We are referring specifically to software servers, and there are some popular (Open Source) choices:


 * Apache2 - https://httpd.apache.org/
 * NGINX - https://www.nginx.com/
 * Lighttpd - https://www.lighttpd.net/
 * Apache Tomcat - http://tomcat.apache.org/

NOTE: Laboratory 1 provides some insights about HTTP.

WSGI
WSGI (Web Server Gateway Interface) is a convention for allowing web servers to proxy requests to web applications.

Using WSGI is required in the context of Flask applications.

Flask-SQLAlchemy
The Flask-SQLAlchemy package aims to make using SQLAlchemy ORM (Object Relational Mapper) in conjunction with Flask in an easier fashion.

As the backing DBMS for these examples, we will use SQLite. Creating the database and populating the database can be easily done also with Flask-SQLAlchemy: The users.html template that displays information about the user:

Model-View-Controller (MVC) Architecture
The MVC Architecture proposes that a web application should be structured around three different actors:


 * Models - Objects that store data (generally tightly coupled and designed to fit the schema of a database)
 * Views - Representation of data
 * Controllers - Middleware that specifies how views interact with models and vice-versa,

Why MVC?

 * Separation of business logic and user interface.
 * Easily mantainable codebase.
 * Reusable components.

Using NGINX and uwsgi, by manual configuration:
Ideally, after the development process of a Flask application, the source code will be put into a production environment which will use a web server and a WSGI for serving users.

The following sources illustrate how to install nginx and configure uWSGI in order to deploy a Flask application (assumed that you run a Debian-based GNU/Linux distribution, or WSL2). Creation of a virtual environment with all the dependencies: Activating the virtual environment: Installing flask, flask-sqlalchemy and uwsgi packages: Afterward, the creation of an entrypoint for WSGI where the Flask application is triggered is required, the file wsgi-flask.py would contain: uWSGI needs a configuration file (flaskapp.ini): Afterwards, nginx needs to be configured to proxy requests to WSGI: The content of this file should include the port nginx will listen to, and point to your uWSGI socket: NOTE: Restarting NGINX is needed at this point is required in order for the configuration to take effect.

Under an activated environment uwsgi needs to run (this is usually configured to happen automatically)

Using Docker with a Dockerfile:
If you would like to use Docker instead of manually deploying your application, there are some prerequisites to be done:


 * 1) You should create an directory named `app` in which you will copy the `main.py` and `db_creator.py` scripts.
 * 2) You should create a Dockerfile.

The image we will be using is the tiangolo/uwsgi-nginx-flask/image. We will copy our application directory into the container's `/app` directory as pointed out in the image's documentation.

We also copy our `requirements.txt` file with all the requirements needed for the application to run (you can generate this file by using the `pip freeze > requirements.txt` command while having the virtual environment active).

Then we run `pip install -r /app/requirements.txt` to install all our dependencies.

The last step is for us to run the script that will generate and populate our database using the python command.

You should have the following directory structure in order for this to work: Then, you want to build the image using docker build: Then finally, create a container using docker run, while exposing port 80: Your application should now be available at localhost:80.

Useful links:

https://flask-sqlalchemy.palletsprojects.com/en/2.x/

https://www.sqlalchemy.org/

https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

https://www.sqlite.org/index.html

https://uwsgi-docs.readthedocs.io/en/latest/