Flask is a lightweight and flexible web framework for Python that makes it easy to develop web applications. This tutorial will guide you through the process of how to install Flask on Rocky Linux. If you are new to Rocky Linux, you can check out our installation guide to get started.
Table of Contents:
- Prerequisites
- Install Python
- Create a Virtual Environment
- Install Flask
- Create a Simple Flask Application
- Run the Flask Application
- Conclusion
How to Install Flask on Rocky Linux
Prerequisites
Before you begin, ensure that you have:
- A Rocky Linux system with sudo privileges
- A stable internet connection
- Basic knowledge of Linux commands
Install Python on Rocky Linux
Flask requires Python, so the first step is to install Python on your Rocky Linux system. Rocky Linux 8 comes with Python 3.6 pre-installed, but you can install the latest version from the official repositories by running:
sudo dnf install python3 -y
To verify the installation, check the Python version by typing:
python3 --version
If Python is installed correctly, you should see the version number in the output.
Create a Virtual Environment on Rocky Linux
It’s a good practice to create a virtual environment for your Flask projects. This isolates the dependencies of each project and prevents potential conflicts. To create a virtual environment, first, install the python3-venv
package:
sudo dnf install python3-venv -y
Next, create a new directory for your Flask project and navigate to it:
mkdir my_flask_app
cd my_flask_app
Now, create a virtual environment named venv
:
python3 -m venv venv
Activate the virtual environment by running:
source venv/bin/activate
You should see (venv)
at the beginning of your prompt, indicating that the virtual environment is active.
Install Flask on Rocky Linux
With the virtual environment active, you can now install Flask using pip
. pip
is a package installer for Python, and it comes bundled with the python3-venv
package. Install Flask by running:
pip install Flask
This command will install Flask and its dependencies in the virtual environment.
Create a Simple Flask Application on Rocky Linux
Now that Flask is installed, let’s create a simple Flask application. In your project directory, create a new file named app.py
:
touch app.py
Open the file with your favorite text editor, such as Vim or Nano, and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, Flask!'
if __name__ == '__main__':
app.run()
This code creates a simple Flask application with a single route that returns “Hello, Flask!” when accessed.
Run the Flask Application on Rocky Linux
To run the Flask application, execute the following command in your project directory:
flask run
You should see output similar to:
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Create a Simple Flask Application
- First, create a new directory for your Flask project and navigate to it:bash
mkdir my_flask_app
cd my_flask_app
Create a new file named app.py
using a text editor, such as nano or vim:
nano app.py
Add the following code to app.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
Save and exit the file.
Run your Flask application using the following command:
flask run
You should see output similar to the one from before. Open a web browser and visit http://127.0.0.1:5000/
. You should see the message “Hello, World!” displayed.
Configure Flask for Production
For a production environment, you’ll want to use a more robust web server like Gunicorn instead of the built-in development server. First, let’s install Gunicorn:
pip install gunicorn
Now, you can run your Flask application using Gunicorn:
gunicorn app:app
You should see output similar to:
[2023-04-28 12:34:56 +0000] [12345] [INFO] Starting gunicorn 20.1.0
[2023-04-28 12:34:56 +0000] [12345] [INFO] Listening at: http://127.0.0.1:8000 (12345)
[2023-04-28 12:34:56 +0000] [12345] [INFO] Using worker: sync
[2023-04-28 12:34:56 +0000] [12346] [INFO] Booting worker with pid: 12346
Your Flask application is now running on Gunicorn, listening on port 8000. Visit http://127.0.0.1:8000/
in your web browser to view your application.
Set Up a Reverse Proxy with Nginx on Rocky Linux
To serve your Flask application on a public IP address and domain name, you’ll want to set up a reverse proxy. We recommend using Nginx, a popular and powerful web server.
- First, install Nginx on your Rocky Linux system:
sudo dnf install -y nginx
Start and enable the Nginx service:
sudo systemctl start nginx
sudo systemctl enable nginx
Create a new Nginx configuration file for your Flask application:
sudo nano /etc/nginx/conf.d/my_flask_app.conf
Add the following configuration to the file, replacing your_domain.com
with your actual domain name:
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
- Save and exit the file.
- Test your Nginx configuration to ensure there are no errors:
sudo nginx -t
If everything is configured correctly, you should see the message “nginx: configuration file /etc/nginx/nginx.conf test is successful”.
Reload the Nginx service to apply your new configuration:
sudo systemctl reload nginx
Your Flask application is now accessible via your domain name (e.g., http://your_domain.com
). Ensure that your domain’s DNS records are pointing to your server’s IP address.
Configure SSL with Let’s Encrypt
To secure your Flask application with HTTPS, you can use a free SSL certificate from Let’s Encrypt. Follow these steps to set up SSL:
- Install the
certbot
package and the Nginx plugin:
sudo dnf install -y certbot python3-certbot-nginx
Run Certbot to automatically obtain and configure an SSL certificate for your domain:
sudo certbot --nginx -d your_domain.com
Follow the on-screen prompts to complete the SSL certificate setup.
Test the automatic certificate renewal process to ensure that your SSL certificate will be renewed automatically before it expires:
sudo certbot renew --dry-run
Your Flask application is now served securely over HTTPS. You can access it via https://your_domain.com
.
Conclusion
In this tutorial, you learned how to set up a Flask application on Rocky Linux 8, run it with Gunicorn, set up a reverse proxy using Nginx, and secure it with an SSL certificate from Let’s Encrypt. You now have a solid foundation for building and deploying your Flask applications on a production server. Keep in mind that you should also consider implementing additional security measures, monitoring, and performance optimizations as your application grows. Learn How to Install Apache Tomcat on Rocky Linux and How to Install Bacula Backup Server on Rocky Linux