The LAMP (Linux, Apache, MySQL/MariaDB, PHP/Python/Perl) stack is a powerful and flexible environment for hosting web applications. Combined with Flask, a lightweight Python web framework, it is an excellent choice for deploying scalable web applications. This guide will walk you through deploying a Python web application using Flask on a LAMP stack configured on Amazon Linux 2023.

Introduction to Configuring LAMP Stack on Amazon Linux 2023

Amazon Linux 2023 provides a secure, stable, high-performance environment optimized for running on Amazon EC2. Configuring a LAMP stack on this OS involves setting up Apache as the web server, MariaDB as the database management system, and Python as the scripting language. This setup is ideal for running dynamic web applications, particularly those built using Flask.

Before deploying, ensure your Amazon EC2 instance runs Amazon Linux 2023 and has the necessary security groups to allow HTTP, HTTPS, and SSH access.

Overview of the config_AMI_WSGI.sh Script for Flask Deployment

The config_AMI_WSGI.sh script is a powerful tool for automating the deployment of your Flask application on a LAMP stack. This script handles everything from installing the necessary components to configuring the environment for your Flask app.

The script is designed to:

  1. Set Up LAMP: Install Apache, MariaDB, Python, and the necessary PHP components.
  2. Configure MariaDB: Set up MariaDB, including creating databases and users.
  3. Install WSGI: Install and configure the Web Server Gateway Interface (WSGI), which allows your Flask application to communicate with the Apache web server.

Script Functions: Setting Up LAMP, Configuring MariaDB, and Installing WSGI

The script performs several critical functions:

LAMP Installation: The script installs Apache, MariaDB, and Python, along with mod_wsgi, which is necessary for deploying Python applications with Apache.
sudo yum install -y httpd mariadb-server python3 mod_wsgi

MariaDB Configuration: The script configures MariaDB, allowing you to create a user-specific database for your Flask application.
sudo systemctl start mariadb

sudo mysql_secure_installation

WSGI Installation: WSGI is configured as the interface between your Flask application and the Apache server.
sudo yum install -y mod_wsgi

Customizing WSGI Configuration for Flask Applications

You must customize the WSGI configuration to ensure your Flask application runs smoothly. The script generates a .wsgi file with your application’s necessary environment variables and paths. Here’s an example of what the .wsgi file might look like:

import sys

sys.path.insert(0, ‘/var/www/html/your_flask_app’)

from your_flask_app import app as application

This script ensures that Apache can correctly find and run your Flask application.

Sending and Executing the config_AMI_WSGI.sh Script on Your Instance

Once the script is ready, the next step is to send it to your EC2 instance and execute it. You can use SCP (Secure Copy Protocol) to transfer the script:

scp -i your-key.pem config_AMI_WSGI.sh ec2-user@your-ec2-ip:/home/ec2-user/

After transferring the script, SSH into your instance:

ssh -i your-key.pem ec2-user@your-ec2-ip

Change the script’s permissions to make it executable:

chmod +x config_AMI_WSGI.sh

Finally, run the script:

./config_AMI_WSGI.sh

Accessing Your Instance and Running the Deployment Script

After executing the script, you can access your EC2 instance through its public IP address or DNS name. The script will have configured Apache to serve your Flask application, and you should see your web application running.

Monitoring the Deployment Process

Monitoring the deployment process is crucial for ensuring everything works correctly. You can monitor Apache logs to see the status of your Flask application:

sudo tail -f /var/log/httpd/error_log

Additionally, check the status of Apache and MariaDB to ensure they are running smoothly:

sudo systemctl status httpd

sudo systemctl status mariadb

Regular monitoring will help you catch and resolve any issues during deployment.

Conclusion

Deploying a Python web application using Flask on a LAMP stack configured on Amazon Linux 2023 is a powerful and flexible solution for web hosting. By automating the setup with the config_AMI_WSGI.sh script, you can ensure a smooth, repeatable deployment process that saves time and reduces errors.

References

 Install a LAMP server on AL2023

Install a LAMP server on AL1