DEV Community

Cover image for How to Host Flask Applications on Namecheap cPanel
LordGhostX
LordGhostX

Posted on

How to Host Flask Applications on Namecheap cPanel

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()
Enter fullscreen mode Exit fullscreen mode

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.

Alt Text
Alt Text
Alt Text

Generating a Requirements File

Next, generate a requirements.txt file for the project. Its structure should resemble the image below.

Alt Text

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

Alt Text

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.

Alt Text
Alt Text

Next, click the CREATE APPLICATION button on the right side of the screen.

Alt Text

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.

Alt Text

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.

Alt Text

Next, zip your local project, upload it to the cPanel file manager, then unzip it on the server.

Alt Text
Alt Text

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.

Alt Text
Alt Text

Next, replace all the content in the passenger_wsgi.py with the code below and save the file.

from app import app as application
Enter fullscreen mode Exit fullscreen mode

Alt Text

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.

Alt Text
Alt Text

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.

Alt Text
Alt Text

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.

Alt Text

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.

Alt Text
Alt Text

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)

Collapse
 
olanetsoft profile image
Idris Olubisi💡

Awesome 👌

Collapse
 
lordghostx profile image
LordGhostX

Thank you very much

Collapse
 
shosenwales profile image
Shuaib Oseni

Great!!!....kindly do the django version😊

Collapse
 
lordghostx profile image
LordGhostX

I'm not too much of a Django user, but sure I will

Collapse
 
bkoiki950 profile image
Babatunde Koiki

Thanks for putting this out sensei🤗.

Collapse
 
lordghostx profile image
LordGhostX

You're welcome chief

Collapse
 
anallely753 profile image
Anallely Mtz

So easy to follow! Thanks

Collapse
 
yurrey profile image
abdelwahed

hi bro
in the req.txt, I need to put the same flask version as u did or not

Collapse
 
lordghostx profile image
LordGhostX

Put the one your project uses, it's not compulsory you use the same as in this article.

Collapse
 
yousifsaif profile image
yousif

why the "setup python app" don't show?

Collapse
 
xarmzon profile image
Adelola Kayode Samson

How to environment variables to namecheap?

Collapse
 
lordghostx profile image
LordGhostX

Namecheap provides both a web terminal and SSH you can use to manage your website, you can set environment variables from it