How to set up and install Flask on a Linux shared hosting account

How to set up and install Flask on a Linux shared hosting account

Flask is a lightweight and flexible web framework for Python, widely used for building web applications and APIs. It’s often described as a “microframework” because it provides the essential components to build web applications, such as routing, request handling, and templating, without enforcing particular project structures or dependencies. 

Key Features of Flask::

Minimalistic and Simple: Flask aims to keep the core simple but extensible. Developers can add extensions as needed. 

Routing: Flask provides a way to map URLs to Python functions, enabling the creation of dynamic web applications. 

Templating: Uses the Jinja2 templating engine to separate HTML and code. 

Flexibility: Flask allows developers to use the tools and libraries they prefer.

Step 1: Create a Python application in cPanel you can follow the below steps.

To begin, you need to create a Python application within your cPanel account to host your Flask project. Follow these steps to get started:

1.Login to your cPanel account.

2.In search bar search the Setup Python App.

3. Then click on CREATE APPLICATION

4.In the Python version dropdown menu, choose the desired Python version. For this example, we’ll select Python 3.7.3.

5.In the Application root field, enter flaskapp.

6.In the Application URL dropdown menu, pick the domain, and then type flaskapp.

7.Leave the fields for Application startup file and Application Entry point empty.

Note: If you leave these fields empty, cPanel will automatically generate a passenger_wsgi.py startup file and a default application object for you.

8.Click on CREATE option,it will create your WEB APPLICATIONS.

9.At the top of the page, locate the section titled “Enter the virtual environment.” To activate the virtual environment, run the provided command. Make sure to copy this command as you will need it for the subsequent steps.

Step 2. To Configured the FLASK project

After creating the Python application in cPanel, you need to complete the following tasks using the command line:

  1. Install Flask.
  2. Configure Passenger to run the Flask application.

Here are the steps:

  1. Access your account via SSH or In cPanel you can search the Terminal option.
  2. Activate the virtual environment using the command you noted in the previous step. For example:
  3. Source /home/supportaccount/virtualenv/flaskapp/3.7/bin/activate && cd /home/supportaccount/flaskapp

 [After activating the virtual environment, the command prompt will    start with (flaskapp:3.7) to indicate that you are working in the flaskapp virtual environment with Python 3.7. All subsequent commands in this article assume that you are within the Python virtual environment. If you log out of your SSH session or deactivate the virtual environment using the deactivate command, ensure you reactivate the virtual environment before proceeding with any further steps]

    4.To install Flask in your activated virtual environment, type the following command:

       pip install flask

This command will use pip, the Python package installer, to download and install        Flask and its dependencies into your virtual environment.

To verify the version of Flask that is installed in your virtual environment, type the following command:

        flask –version

5.To update the passenger_wsgi.py file for a Flask application, you can follow these steps:

  1. Navigate to the File in cPanel (search >>file manager or) text editor: Use a text editor to open the passenger_wsgi.py file. Typically, this file is located in the ~/flaskapp/ directory.

2.Replace File Contents: Replace the existing contents of passenger_wsgi.py with the following configuration for a basic Flask application:

import os

from flask import Flask, request, render_template, redirect, url_for

project_root = os.path.dirname(os.path.realpath('__file__'))
template_path = os.path.join(project_root, 'app/templates')
static_path = os.path.join(project_root, 'app/static')
app = Flask(__name__, template_folder=template_path, static_folder=static_path)

@app.route('/')

def index():
    return 'Hello from flask'

application = app

3.To restart a Python application in cPanel.

-Login to your cPanel account.

Go to the SOFTWARE section on the cPanel home screen.

Click on Setup Python App.

Under WEB APPLICATIONS, find the flaskapp application.

Click on the Restart icon next to it.

4.To test the Flask site after restarting it, follow these steps using your browser:

  1. Open your web browser (e.g., Chrome, Firefox).
  2. Enter the following URL in the address bar: http://www.http://supportaccount.com/flaskapp
    • Replace supportaccount.com with your actual domain name.
    • /flaskapp is the endpoint or route where your Flask application is accessible. Adjust this if your Flask application is deployed at a different route.
  3. Hit Enter or Return.

If everything is set up correctly:

  • You should see the “Hello from flask” page rendered by your Flask application.

The expected outcome is to see a page displaying “Hello from flask”.