Hosting a Flask Website via Apache Server IN SNA Labs

Hosting a Flask Website via Apache Server [WSGI]: A Step-by-Step Guide

Welcome, students! If you're eager to learn how to host a Flask website using an Apache server with WSGI, you're in the right place. This step-by-step guide will walk you through the process, making it easy for you to deploy your Flask application and showcase your projects to the world.

Introduction

Flask is a web framework for Python that allows you to build and deploy web applications quickly. Hosting your Flask app on an Apache server using WSGI (Web Server Gateway Interface) is a common choice for production deployments. It provides a stable and scalable environment for your applications.

Benefits of Hosting Your Application in SNA Labs

  • Blazing Fast Internet Connection
  • Robust Denial-of-Service (DoS) Protection
  • User-Friendly Setup and Understanding
  • 24/7 Availability
  • Free Services for Enhanced Functionality mongodb,memcache, etc ...
  • Real-Time Learning Experience

Obtaining a Domain for Your Website

  1. Visit https://labs.selfmade.ninja/labs.

  2. Navigate to Connectivity -> My Domains -> Add New Domains.

  3. Enter your preferred domain name in the "Choose Domain" input form.

    Domain Input

  4. Return to https://labs.selfmade.ninja/labs.

  5. Select Essential Lab and initiate a Redeploy.

  6. From the "Choose Domains" dropdown, pick your new domain.

    Choose Domain

  7. Confirm the redeployment by clicking Confirm Redeploy.

Now, your website has its own domain. 🌐 It's ready to shine on the internet stage! 🚀

2. Install Python and Flask

Execute these commands in your terminal:

sudo apt install python3.10-venv
sudo apt-get install libapache2-mod-wsgi-py3
sudo a2enmod wsgi

3. Create a Virtual Environment

To create and activate a virtual environment for your Flask application, run the following commands:

python -m venv flaskEnev
source flaskEnev/bin/activate
pip install flask

Alt text for the image

4. Create a Flask App

You can use your own project or follow these steps to create a simple "Hello World" application:

  1. Create a new directory and navigate into it:

    mkdir helloworld
    cd helloworld
  2. Create a new Python file app.py:

    nano app.py
  3. Add the following code to app.py:

from flask import Flask

application = app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello, World!'

if __name__ == '__main__':
app.run(debug=False)

5 Configure Apache to Serve Your Flask App

1. Navigate to Apache is sites-available directory and create a new virtual host configuration file:
cd /etc/apache2/sites-available
nano testingSite.conf
2. Replace the placeholders in the configuration file with your specific details:
<VirtualHost *:80>
    ServerName [Your domain name]

    WSGIDaemonProcess [project_name] user=[your username] group=[your username] threads=5 python-home=[env dir ex=/home/john/flaskEnev]
    WSGIScriptAlias / [application folder ex=/home/john/helloworld/app.py]

    <Directory [project_folder]>
        WSGIProcessGroup [project_name]
        WSGIApplicationGroup [project_name]
        Order deny,allow
        Allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/[project_name]-error.log
    CustomLog ${APACHE_LOG_DIR}/[project_name]-access.log combined
</VirtualHost>

Alt text for the image

3. Test and enable your new site configuration:
sudo apachectl configtest
sudo a2ensite testingSite.conf
sudo systemctl reload apache2

Alt text for the image

Now, your Flask application should be accessible via your domain.

Alt text for the image

Congratulations on successfully hosting your Flask website via Apache Server using WSGI! If you have followed the steps correctly, your Flask app should now be live.

Feel free to explore and expand your Flask application. If you have any questions or run into issues, don't hesitate to seek help or refer to the documentation. Happy coding!

For more tutorials and tech insights, visit http://blog.selfmade.ninja/

Happy Coding!
Author :‎ ‎ ‎ [Jawahar.s]
GITHUB :‎ ‎ ‎ https://github.com/jawa77

Leave a Comment

Realted Blog >>

python-copy
Hosting MQTT Service in SNA Labs
Introduction Welcome to the guide on setting up and using MQTT in SNA Labs. MQTT (Message Queuing Telemetry...
windows-logo-blog
How to Connect to SNA Labs with WireGuard (Windows)
Prerequisites SNA Labs account (If you don’t have an account, create one by clicking here) Connect...
ubuntu-logo-svg
How to connect to SNA labs with Wireguard (Ubuntu)
Prerequisites SNA Gitlab account (Create one by clicking here) Connect to SNA Labs Adding SSH Keys Login...
1 2
Scroll to Top