How To Install Flask On CentOS 8

How to install Falsk on CentOS 8

In this article we will show you how to install Flask on CentOS 8 operating system. In this guide, we will walk you through the steps to install and use Flask on a CentOS system. And we will run it on production environment.

Introduction

Flask is a popular Python web framework that provides a simple and easy-to-use interface for building web applications. It is designed to be lightweight and modular, making it easy to add new features and functionality as needed.

Here are some of the key features and characteristics of Flask:

  • Micro framework: Flask is a “micro” framework, which means that it provides only the basic functionality needed to build web applications. This makes it lightweight and easy to use, but also means that it may not be suitable for very complex or large-scale applications.
  • Routing: Flask provides a simple routing system that maps URLs to specific Python functions, making it easy to create dynamic web pages.
    Templating: Flask includes a built-in templating engine that allows developers to create dynamic HTML pages with placeholders for dynamic content.
  • Flask extensions: Flask has a wide range of third-party extensions that can be used to add additional functionality to the framework, such as support for database integration, user authentication, and more.
  • RESTful support: Flask includes built-in support for creating RESTful APIs, making it easy to build web services that can be used by other applications.
  • Jinja2 template engine: Flask uses the Jinja2 template engine, which is a powerful and flexible templating language that allows developers to create dynamic HTML pages with ease.

Flask Installation On CentOS 8

Prerequisites

Before we proceed to the Flask installation process, there are several prerequisites that must be met, namely :

  • A server running one of the following operating systems: CentOS 8.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A root user access or normal user with administrative privileges.

The Flask installation will be consiste of several steps, as mentioned below :

1. Update CentOS 8 System
2. Creating Virtual Environment
3. Installing Flask
4. Verifying Flask
5. Create a Simple Flask Application
6. Run the Flask Application
7. Configure Firewall (Optional)
8 .Deploy Flask in Production

 

Step 1 : Update CentOS 8 System

The first is to update our CentOS 8 system, this task is to make sure if we are accessing the latest packages versions. This task will cut down the installation time.  To update our packages software we will use command line :

$ sudo dnf update -y

Step 2: Creating Virtual Environment

In this step, we will create Python3 virtual environment where Flask will be lived in. For this purpose we will submit the following command line :

# python3 --version
# mkdir projekgue
# cd projekgue/
# python3 -m venv venv
# . venv/bin/activate

The output will be as shown below :

[root@otodiginet ~]# python3 --version
Python 3.6.8
[root@otodiginet ~]# mkdir projekgue
[root@otodiginet ~]# cd projekgue/
[root@otodiginet projekgue]# python3 -m venv venv
[root@otodiginet projekgue]# . venv/bin/activate

Step 3: Installing Flask

Within the activated environment, we will use the following command to install Flask :

(venv) [root@otodiginet projekgue]# pip install Flask

Output :

(venv) [root@otodiginet projekgue]# pip install Flask
Collecting Flask
Downloading https://files.pythonhosted.org/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl (94kB)
100% |████████████████████████████████| 102kB 1.2MB/s 
Collecting Jinja2>=2.10.1 (from Flask)
Downloading https://files.pythonhosted.org/packages/7e/c2/1eece8c95ddbc9b1aeb64f5783a9e07a286de42191b7204d67b7496ddf35/Jinja2-2.11.3-py2.py3-none-any.whl (125kB)
100% |████████████████████████████████| 133kB 1.4MB/s 
Collecting Werkzeug>=0.15 (from Flask)
Downloading https://files.pythonhosted.org/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298kB)
100% |████████████████████████████████| 307kB 625kB/s 
Collecting itsdangerous>=0.24 (from Flask)
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting click>=5.1 (from Flask)
Downloading https://files.pythonhosted.org/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl (82kB)
100% |████████████████████████████████| 92kB 1.1MB/s 
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->Flask)
Downloading https://files.pythonhosted.org/packages/b2/5f/23e0023be6bb885d00ffbefad2942bc51a620328ee910f64abe5a8d18dd1/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: MarkupSafe, Jinja2, Werkzeug, itsdangerous, click, Flask
Successfully installed Flask-1.1.2 Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 itsdangerous-1.1.0
You are using pip version 9.0.3, however version 21.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

3.1. Upgrading pip

At this point, we have successfully installed Flask, according to the recommendations above, we will upgrade pip to version 21 using the command line.

(venv) [root@otodiginet projekgue]# pip install --upgrade pip

Output :

(venv) [root@otodiginet projekgue]# pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/fe/ef/60d7ba03b5c442309ef42e7d69959f73aacccd0d86008362a681c4698e83/pip-21.0.1-py3-none-any.whl (1.5MB)
100% |████████████████████████████████| 1.5MB 320kB/s 
Installing collected packages: pip
Found existing installation: pip 9.0.3
Uninstalling pip-9.0.3:
Successfully uninstalled pip-9.0.3
Successfully installed pip-21.0.1

Step 4: Verifty Flask Installation

At this step, we will verify the installation with the following command which will print the Flask version:

(venv) [root@otodiginet projekgue]# python -m flask --version

Output :

(venv) [root@otodiginet projekgue]# python -m flask --version
Python 3.6.8
Flask 1.1.2
Werkzeug 1.0.1
Install Flask on CentOS 8

 

Step 5: Create a Simple Flask Application

In this step, we will create a simpla Flask application which is will be running on CentOS 8. Create a new file called as app_bckinfo.py in your project directory:

$ vi app_bckinfo.py

Add the following code to create a basic Flask application:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
return "Hello, Flask on CentOS!"

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)

Save and exit the file.

Step 6: Run the Flask Application

Run your Flask application:

$ python app_bckinfo.py

You should see output indicating that the Flask development server is running:

* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

Open a web browser and navigate to http://<your-server-ip>:5000/ to see the application in action.

Step 7: Configure Firewall (Optional)

If we are running CentOS with a firewall enabled, you need to allow traffic on port 5000:

$ sudo firewall-cmd --zone=public --add-port=5000/tcp --permanent
$ sudo firewall-cmd --reload

 

Step 8: Deploy Flask in Production

For production use, avoid running Flask’s built-in server. Instead, use a production-grade WSGI server like Gunicorn and a reverse proxy like Nginx:

Install Gunicorn:

$ pip install gunicorn

Run your application with Gunicorn:

$ gunicorn -w 4 -b 0.0.0.0:5000 app:app

Set up Nginx as a reverse proxy to forward requests to Gunicorn (optional).

Conclusion

In this tutorial we have learned how to install Flask on CentOS 8. I hope this tutorial will be helpful.  We can now start building and deploying web applications using this powerful framework. Remember to always use a production-grade server for deployment and keep your system updated for security and stability.

 

(Visited 449 times, 1 visits today)

You may also like