How to setup Python flask application on shared hosting3 min read
In this tutorial I will show you how to setup a Python Flask application that starts by passenger
Step 1: Create a place to house your files & set correct document root
If you want you can use your main domain. However in this case I will use a subdomain where I will run my application.
First Navigate to Web > Subdomains and enter the subdomain of your preference.
For the next step we’ll want to head on over to the terminal. You may access it via Dev > Terminal or via SSH by using the login details sent to you in the “Get started with your web host” email.
Once there we’ll first create the correct document root.
Make sure to replace python.flask.3li.se with the subdomain that you created
Next we’ll go back to the control panel and update the document root to reflect the change. Navigate to Web > Subdomains again and in the top left corner click Change Mode: List Subdomains
From there change the document root to the /public directory you just created like so
Step 2: install a python version of your choice
From now on we’ll only work in the terminal.
First find out what python version you’d like to use. In my case I chose 3.10 but you can choose any version you like.
Run the following command to install the python version
pyenv install 3.10
It can take a few minutes for it to compile and install python.
Step 3: create all the necessary files.
First navigate one level below the document root of your site.
Next specify the python version you’d like to use
pyenv local 3.10
Step 4: Install flask and create the passenger file
To install flask run the following command
pip3 install flask
Create a Passenger startup file to run Flask as a Python WSGI application:
Edit the file with the editor of your choice and make sure it has the following content:
from flask import Flask application = Flask(__name__) @application.route("/") def hello(): return "Hello World!" if __name__ == "__main__": application.run()
Step 5: Create the .htaccess file to start passenger
The last thing to do is to create the .htaccess file that will start passenger.
First navigate to your document root and create a file called .htaccess
cd /var/www/python.flask.3li.se/public && touch .htaccess
Next edit the file with the editor of your choice and make sure it has the following content:
PassengerAppRoot /var/www/python.flask.3li.se PassengerPython /usr/local/share/python/pyenv/shims/python PassengerEnabled on
Remember to replace python.flask.3li.se with your subdomain.
The last thing to do is delete the template index.html file that was generated.
That’s it. We’re done. You can now visit your site and you should see Hello World!