Installing and configuring Flask on a Linux shared hosting account

Flask is a lightweight Python framework for building web applications. This guide shows how to install and configure a Flask application on a Linux shared hosting account using cPanel.

Important
This configuration is tested on shared hosting accounts but is not officially supported. Use it as a starting point for your own projects. Custom troubleshooting is not provided.

Step 1: Create a Python application in cPanel

  1. Log in to cPanel.

  2. In the Software section, click Setup Python App.

  3. Click CREATE APPLICATION.

  4. In the application form:

    • Python version: Select your preferred version (e.g., Python 3.7.3).

    • Application root: Type flaskapp.

    • Application URL: Select the domain and type flaskapp.

    • Leave Application startup file and Application entry point blank (cPanel will generate passenger_wsgi.py automatically).

    • Optionally, specify a Passenger log file.

  5. Click CREATE to set up the application and its virtual environment.

  6. Note the command displayed to enter the virtual environment; you’ll need it in the next step.


Step 2: Configure the Flask project

  1. Log in via SSH.

  2. Activate the virtual environment (replace the path with the command noted in Step 1):

source /home/username/virtualenv/flaskapp/3.7/bin/activate && cd /home/username/flaskapp

The prompt now shows (flaskapp:3.7) to indicate the virtual environment is active.

  1. Install Flask:

pip install flask
  1. Verify the Flask version:

flask --version
  1. Edit the passenger_wsgi.py file (~/flaskapp/passenger_wsgi.py) with the following contents:

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
  1. Restart the Python application in cPanel:

    • Go to Setup Python App under SOFTWARE.

    • Locate the flaskapp application and click the Restart icon.

  2. Test the Flask site in a browser:

http://www.example.com/flaskapp

You should see the page:

Hello from flask

Tip: If the page doesn’t appear, run the passenger_wsgi.py file manually to check for syntax errors:

python ~/flaskapp/passenger_wsgi.py

More information

Ця відповідь Вам допомогла? 0 Користувачі, які знайшли це корисним (0 Голосів)

Powered by WHMCompleteSolution