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.
Step 1: Create a Python application in cPanel
The first step is to create a Python application within cPanel that will host the Flask project. To do this, follow these steps:
Log in to cPanel.
If you do not know how to log in to your cPanel account, please see this article.
In the SOFTWARE section of the cPanel home screen, click Setup Python App:
cPanel – Software – Setup Python App icon
Click CREATE APPLICATION:
- cPanel – Python Selector – Create Application button
- The application form appears:
- cPanel – Python – Create application form
- In the Python version list box, select the Python version you want to use. In this example, we use Python 3.7.3.
- In the Application root text box, type flaskapp.
- In the Application URL list box, select the domain, and then type flaskapp.
- Leave the Application startup file text box and Application Entry point text box blank.
- In the Passenger log file text box, you can optionally specify a log file for the application.
- In the top right corner of the page, click CREATE:
cPanel creates the application and sets up the Python environment.
- At the top of the page, next to Enter to the virtual environment. To enter to the virtual environment, run the command, and copy the command. You will need this information in the following procedure.
Step 2: Configure the Flask project
After you create the Python application in cPanel, you are ready to do the following tasks at the command line:
Configure Passenger to work with the Flask application.
To do this, follow these steps:
- Log in to your account using SSH.
- Activate the virtual environment, using the command you noted in step 10 above. For example:
Copysource /home/username/virtualenv/flaskapp/3.7/bin/activate && cd /home/username/flaskapp
- To install Flask, type the following command:
Copypip install flask
To verify the version of Flask that is installed, type the following command:
- 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:
Log in to cPanel.
In the SOFTWARE section of the cPanel home screen, click Setup Python App.
Under WEB APPLICATIONS, locate the flaskapp application, and then click the Restart icon.
- 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 the flask page.