I get asked from time to time how a person can host a Flask application on web hosting platforms that run on cPanels like Namecheap and if that is even possible. I end up responding with a link to a YouTube video that shows the possibility. Then, I send a couple of screenshots that serve as a guide.
I decided to publish an article that will serve as a step-by-step guide on how to host Flask applications on cPanels like Namecheap as there are presently not enough resources online on this topic.
Getting Started
Creating a Flask Application
We will create a simple Flask application and host it on Namecheap during this tutorial.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
@app.route("/<string:name>/")
def say_hello(name):
return f"Hello {name}!"
if __name__ == "__main__":
app.run()
Save the code above in a file named app.py
like every other Flask application. When you run it, you should get a result similar to the images below.
Generating a Requirements File
Next, generate a requirements.txt
file for the project. Its structure should resemble the image below.
Hosting Flask on cPanel
This section assumes we have created a Namecheap account and purchased a domain along with hosting.
Step 1: Log in to cPanel
Step 2: Create a Python Application
cPanel has tools for operations like SSL management, Databases, File management, Emails, Site metrics, Domain management, etc. Navigate to the SOFTWARE
section and select Setup Python App
.
Next, click the CREATE APPLICATION
button on the right side of the screen.
You will see a screen similar to the one above, select a Python version (preferably the latest). Provide a name for your application in the Application root
form field, and select the domain/subdomain you want the app hosted on in the Application URL
section.
Step 3: Upload the Flask Application to cPanel
Navigate to the FILES
section in cPanel and select File Manager
. It is a tool that allows for the management of files on our server.
Next, zip your local project, upload it to the cPanel file manager, then unzip it on the server.
Step 4: Configuring our WSGI file
Open and edit the passenger_wsgi.py
file that is automatically generated by the cPanel. You should see a starter WSGI config like in the image below.
Next, replace all the content in the passenger_wsgi.py
with the code below and save the file.
from app import app as application
Step 5: Install Project Requirements
Open the Python App
page again and scroll down the page to the Configuration files
section.
In the first form field, enter requirements.txt
. Then, click the Add
button to add the requirements for installation.
After doing this, notice that the Run Pip Install
button is no longer disabled. Click on the button and select the requirements file you just set.
Step 6: Restart the Flask Application
Whenever you make changes to the web application, you have to restart the application to effect the changes. To do this, scroll up on the Python App
page and locate the RESTART
button, then click it.
Congratulations! You have successfully hosted a Flask application on Namecheap cPanel. It is accessible via the domain you linked to the app during its creation.
Conclusion
We saw the possibility of hosting Flask web applications on cPanels like Namecheap and learned how to host a Flask project on cPanel. If you have any questions, don't hesitate to contact me on Twitter: @LordGhostX
Top comments (12)
So easy to follow! Thanks
Awesome π
Thank you very much
Great!!!....kindly do the django versionπ
I'm not too much of a Django user, but sure I will
Thanks for putting this out senseiπ€.
You're welcome chief
hi bro
in the req.txt, I need to put the same flask version as u did or not
Put the one your project uses, it's not compulsory you use the same as in this article.
why the "setup python app" don't show?
How to environment variables to namecheap?
Namecheap provides both a web terminal and SSH you can use to manage your website, you can set environment variables from it