Monday, October 17, 2022

Running Python (Flask) server on a shared hosting environment

If you have purchased a domain name through some other domain name registrar and not through your shared hosting provider, first you'll need to ...

1. Point nameservers to your shared hosting provider

The following instructions are for Namecheap: 

  • from the menu on the left side, open the Domain List and
  • click on button MANAGE next to your domain name.


  • Under NAMESERVERS, instead of Namecheap BasicDNS select Custom DNS.


  • Copy-paste name servers that your shared hosting has provided. 
  • Save the modifications by clicking on the green check mark.



2. Create an Addon Domain in shared hosting's cPanel

  • Once you are in cPanel, click on Addon Domains
  • Type in your new domain name and 
  • click on Add Domain button.

3. Setup Python App

  • Now go back to cPanel and click on Setup Python App.


  • Click on Create Application button.


  • Fill out the form by providing:

    • python version, 
    • application root directory (it was created by the system in step 2), 
    • application url (your domain name), 
    • main.py as application startup file and  
    • app as application entry point. 
    • Don't forget to add path to the passenger log file, for example logs/passenger.log
  • Once you do all that, click on CREATE button.

The app is created and your barebones python site is already running. You can check by it browsing to your new domain. If nameserver settings from step 1 still haven't propagated across the internet, you can open the site as a subdomain of your main domain site.

4. Install Flask

Once the python app is created at the top of the scree the following message will be displayed:

Enter to the virtual environment. To enter to virtual environment, run the command: ...

Copy the command, from cPanel open the terminal, paste and run the command there. Next, run: pip install Flask

To verify the installation, run: flask --version

After the installation is done, we'll modify our main.py application startup file. A minimal Flask application would be:

from flask import Flask


app = Flask(__name__)


@app.route
("/")

def hello_world():
    return
"<p>Hello, World!</p>"

Save the changes that you have made and restart the python app. Browse to your domain again and verify that everything is fine. You are done!

Next things you might consider doing:


No comments:

Post a Comment