How to Host Flask Applications on Namecheap cPanel
Tags: python, devops, webdev
Published: 2020-12-27 11:35:14

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](https://namecheap.com/) 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.

```python
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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w97fcbp2rdfjhayvqby8.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/aofk66cnllcckphba9ee.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j3m5oipy4j5kta7wdurd.png)

## Generating a Requirements File

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jx1em4sj63ug8qtbavkd.png)

# 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](https://dev-to-uploads.s3.amazonaws.com/i/570fkkk3n61ls1kwc364.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/i/weakbkq8qlrfvpz2sme9.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1loyl795bs6bw2l1jbz2.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/28937tb7a03hve1ls4uv.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/r0zc6g6w7cxwtufb6zkw.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/i/ulj69usd3v7hwzm9atte.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/30h8iik1qcehkqtmhv7f.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e6qrl4xpyeknj2h9fb23.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/i/1m0zu67cothjab9s3vrn.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2qkb7b8k06zmh1eagqs3.png)

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

```python
from app import app as application
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hvvh66orjwskvcd4gj3b.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/i/d0bj7kwpc8aynvp3m38v.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o2lpiqz923rgv285oydw.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/2yetc6wdu3krxwwj8sce.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bjq55t48xqfx4jun5sja.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/i/srm9a5pvdwvuaum63hpv.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/nslpi1wxak0j6179pldr.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i7e3aevl3q4igdzpz1u9.png)

# 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](https://twitter.com/LordGhostX)