Posted on

by

in

Python Hosting with cPanel, Deploy and Manage Python App

Using the Setup Python App tool, deploying Python applications is now straightforward and uncomplicated thanks to cPanel’s Python hosting. Basically, this tutorial will cover step-by-step procedure to deploy and manage simple python application using various frameworks such as Django, Flask and FastAPI. While writing this example we used Python Hosting plan provided MediaStroke as it comes with Setup Python App tool. In case, you don’t have you can buy suitable plan from here.

Step By Step Procedure to Deploy and Manage Simple Python Application with Python Hosting

Deploying and managing your Python applications on Python Hosting is possible by following the instructions in this guide.

Here, basically 3 frameworks are provided for python hosting:

  • Flask
  • Django
  • FastAPI

Python Application Deployment

To create a python application following steps are involved:

  1. Provide Login Credentials to cPanel.
  2. Select Setup Python App under Software section of cPanel home screen.
  3. Click on CREATE APPLICATION to begin the application setup on the Setup Python App page.
Create application window with form containing application details in python hosting
  1. On the CREATE APPLICATION form, fill out the following fields:
  • Python version : Choose your preferred version from the drop-down list. Latest version is recommended.
  • Application root: It is for the location of application files in the file system. To form the full path to the application files in the cPanel home directory, the value will be appended to /home/<username>/ e.g. /home/<username>/<applicationroot>.Root names for applications can be flaskproject, djangoproject, fastapiproject, or any other name the user chooses.
  • Application URL: It is to form a web URL of the application.
  • Application startup file: The very first file that the application will process once it has been launched.
  • Application Entry Point: You can define the path along with the filename.
  • Passenger log file: You can define the path along with the filename e.g. /home/<username>/<applicationroot>logs/logs.log.

Click CREATE after finishing the form.

Flask Project initialization in Python Hosting in cPanel

Flask is lightweight microframework in python used for building web applications.

For creating the Flask application implement the following steps:

  1. Create requirements.txt and app.py files
    1. After creating the application go to the tools>> Files>> File Manager.
    2. Go to the application root folder, create requirements.txt and app.py files.
  1. Edit requirements.txt file
    1. Select requirements.txt, click on edit and add following required Flask library.
    2. Click on Save Changes.
Flask
  1. Edit app.py
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"
  1. Edit passenger_wsgi.py
import os
import sys
import importlib.util
import importlib.machinery

sys.path.insert(0, os.path.dirname(__file__))
 
def load_source(modname, filename):
    loader = importlib.machinery.SourceFileLoader(modname, filename)
    spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
    module = importlib.util.module_from_spec(spec)
    loader.exec_module(module)
    return module
 
wsgi = load_source("wsgi", "app.py") 
application = wsgi.app #Note:app should be present in the entry point
  1. Restart the application.
  2. Your application will run.
User defined message in python hosting

Django Project initialization in Python Hosting in cPanel

Django is a Python-based framework that helps developers build web applications.This framework is flexible, safe, and packed with features.

For creating the Django application implement the following steps:

  1. Create a virtual environment for Django
Edit application window for dJangoproject with source command in python hosting
  1. Copy the link which is given on the top of the table.
  2. Go to the tools section.
  3. Then go to advance section, click on terminal.
  4. Paste the copied link on the terminal.
  5. Hit Enter.Then you will be in the virtual environment.
  6. Enter the following command on terminal.
python --version

It will display the version of python.

  1. Django Installation
  • Add the following installation command on terminal:
pip install Django

or

pip3 install Django
Django process installation process on terminal in python hosting
  • To start a project add the following command on the terminal:
django-admin startproject dJangoproject .

The folder named dJangoproject will be created in the application root.

  • In setup add “dJangoproject/wsgi.py” in Application startup file. Then restart the application.
Edit on application startup file and application entry point in python hosting
  • Visit site, check for following screen.
Disallowed host error in python hosting
  • Go to the tools>> Files>> File Manager.
  • In application root folder, go to dJangoproject folder.
  • Edit settings.py file add “ALLOWED_HOSTS = [‘example.com’]”.
Edit window for settings.py allowed hosts in python hosting
  • Reload the setup application.
  • If the given below screen shows up it means Django is installed.
Django installation success in python hosting

FastAPI Project initialization in Python Hosting in cPanel

FastAPI is high-performance web framework in python used for building APIs. It provides asynchronous support and efficient data handling.

For creating the FastAPI application implement the following steps:

  1. Create requirements.txt and app.py files
    1. After creating the application go to the tools>> Files>> File Manager.
    2. Go to the application root folder, create requirements.txt and app.py files.
  2. Edit requirements.txt file
    1. Select requirements.txt, click on edit and add following required fastapi and a2wsgi libraries.
    2. Click on Save Changes.
fastapi
a2wsgi
  1. Edit app.py
from fastapi import FastAPI
app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}
  1. Edit passenger_wsgi.py
from a2wsgi import ASGIMiddleware
from app import app

application = ASGIMiddleware(app)
  1. Restart the application.
  2. Your application will run.
User defined message in python hosting

Conclusion:

This article summing up covering step-by-step procedure to deploy and manage Python applications on server using cPanel with Python Hosting.This article is written considering simple Python applications of each framework as described above.

Leave a Reply

Your email address will not be published. Required fields are marked *