Flask is a Python-based framework that enables you to quickly and easily create web applications. This article demonstrates how to install Flask and configure it on a Linux shared hosting account that uses cPanel.
After completing the following procedures, you will have a functioning Flask application on your account that displays a simple web page.
The first step is to create a Python application within cPanel that will host the Flask project. To do this, follow these steps:
If you are using the Paper Lantern theme, in the SOFTWARE section of the cPanel home page, click Setup Python App:
Click CREATE APPLICATION:
The application form appears:
Leave the Application startup file text box and Application Entry point text box blank.
In the top right corner of the page, click CREATE:
cPanel creates the application and sets up the Python environment.
After you create the Python application in cPanel, you are ready to do the following tasks at the command line:
To do this, follow these steps:
source /home/username/virtualenv/flaskapp/3.7/bin/activate && cd /home/username/flaskapp
To install Flask, type the following command:
pip install flask
To verify the version of Flask that is installed, type the following command:
flask --version
Use a text editor to open the ~/flaskapp/passenger_wsgi.py file. Replace the file contents with the following changes:
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
In cPanel, restart the Python application:
To test the Flask site, use your browser to go to http://www.example.com/flaskapp, where example.com represents your domain name. You should see the Hello from flask page.
If the web page does not appear in your browser, try running the passenger_wsgi.py file manually. To do this, type the following command:
python ~/flaskapp/passenger_wsgi.py
There should not be any text output to the console when you run this file. If there are any errors, check the syntax in the file.
For more information about Flask, please visit https://flask.palletsprojects.com.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.