aA

REST API (RESTful) running on an Enterprise class system

REST API (RESTful) running on an Enterprise class system

In this article we will show you how to easily implement a web application based on flask microstructure using Apache HTTP Server. Flask is a simple web framework written in Python. It is often called a microframework for its lightness and simplicity. However, it can also be used for larger projects and even for commercial projects. Apache is an easy-to-use, widely utilized HTTP server.

Server distributions such as EuroLinux, CentOS or Red Hat® Enterprise Linux® are ideal systems for this type of application.

Required components:

  • Python3
  • Virtualenv
  • Apache HTTP Server / httpd
  • mod_wsgi
  • Flask.

Installation of components

Python3:

sudo yum install python3

Virtualenv:

sudo pip3 install virtualenv

Apache HTTP Server / httpd

sudo yum install httpd
sudo systemctl start httpd
sudo systemctl status httpd

mod_wsgi:

sudo yum install python3-mod_wsgi

Flask will be installed after creating the virtual environment with the virtualenv command.

Creating an application

The application we create will be equipped with the simplest authentication mechanism and will display the user's status. If we are not logged in, it will display Guest, and after logging in, the name of the active user. However, we will not deal with the issue of frontend here. It will be a simple stateless API that responds to Hello world!.

We start developing the application by creating a directory in the /var/www/ folder. We immediately create a virtual environment in it. We can do this in the directory next to or directly in the directory where we intend to place the application.

cd /var/www/
sudo mkdir /var/www/hello_world
sudo virtualenv hello_world

Then we activate the virtual environment and use pip to install flask.

cd hello_world
. bin/activate
sudo pip3 install flask

Now we create 3 files inside the directory:

  • run.py – responsible for starting the application manually in order to test its operation
#!/usr/bin/env python3

import os
from app import app

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port, debug=True)
  • app/__init__.py – defines the package of files that make up the application
#!/usr/bin/env python
from .web import app
  • app/web.py – contains all the logic of our application. Decorator @app.route defines the URI, return returns the response that the user will see.
#!/usr/bin/env python

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello world!"

Now we can test the operation of our application:

FLASK_APP=run.app; flask run

 * 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 http://127.0.0.1:5000/ (Press CTRL+C to quit)

After entering the address received in the terminal into the browser, we should be greeted with the words Hello world!.

Preparing Apache server

The last step will be to prepare the configuration of wsgi and Apache HTTP server.

The wsgi.py file indicates the place where the Python modules are installed in the previously prepared virtual environment, imports sys and site packages and our application.

#!/usr/bin/env python

import sys
import site

site.addsitedir('/var/www/hello_world/lib/python3.6/site-packages')

sys.path.insert(0, '/var/www/hello_world)

from app import app as application

Then we configure the Apache server. We add the following configuration to the file that can be found in /etc/httpd/conf/httpd.conf for Enterprise-class operating systems such as EuroLinux or CentOS:

/etc/httpd/conf/httpd.conf

<VirtualHost *:80>

     ServerName localhost

     WSGIDaemonProcess hitme user=apache group=apache threads=2

     WSGIScriptAlias / /var/www/hello_world/wsgi.py

     <Directory /var/www/hello_world>
         Require all granted
     </Directory>

</VirtualHost>

We restart the Apache HTTP server:

sudo systemctl restart httpd

Now you just need to check that after typing localhost in the browser, you are greeted with the message Hello world!.

Summary

In this way, a simple website can be used to display data on the server and it is easy to add new functionalities to it. In the following articles, we will show you how to implement various methods of user authentication and authorization, as well as a simple database.

Leave a Reply

Your email address will not be published. Required fields are marked *