Skip to main content

How to Deploy a Python Application

Quick Intro

Python is a versatile programming language that powers everything from simple scripts to complex web applications. With Python, you can build robust and scalable applications, including web frameworks like Django and Flask.

Important To Know Before You Begin

Each domain on WebHostMost has a default index.html file located in the document root directory: File Manager > domains/yourdomain.com/public_html. This file is automatically created when the domain is set up and must be manually deleted before deploying a Python application. The creation of a Python app does not remove this file, and it can interfere with your app if not deleted.

warning

Important: Deleting the Python app via the Web Control Panel does not remove the files from the File Manager. You must manually delete these files if they are no longer needed. Additionally, you do not need to configure any ports for your Python application; the server automatically handles port assignments.

Setting Up Your Python Environment

  1. Access the Python App Selector:
    • Navigate to Web Control Panel > Website Management > Python App in your Web Control Panel.

Deploy Python App

  1. Create a New Python Application:

    • Click on Create Application.
    • Fill in the following details:
      • Domain: Select the domain or subdomain where you want to deploy the app.
      • Application Mode: Choose between Development or Production mode.
      • Document Root: Set the directory where your application files are located (e.g., domains/yourdomain.com/public_html).
      • Application Startup File: Typically app.py or wsgi.py for web frameworks.
      • Python Version: Choose the version of Python that your project requires.
    • Click Create to set up the environment.
  2. Virtual Environment Setup:

Virtual Environment

  • After creating the Python application, a link to the virtual environment is provided at the top of the Python App page in the Web Control Panel. This virtual environment must be activated before you can install packages or run commands via the terminal.

Uploading Your Project Files

  1. Upload Files:

    • You can upload your project files using the Drag and Drop feature in the File Manager, SCP via Terminal, or Git. Ensure all necessary files are uploaded to the document root directory you specified earlier.
  2. Install Dependencies:

    • Access your server via SSH or use the Terminal in the Web Control Panel.
    • Activate the virtual environment by running the command provided in the Python App section of the Web Control Panel.
    • Navigate to your application’s directory:
      cd domains/yourdomain.com/public_html
    • Once the virtual environment is activated, you can install your project’s dependencies:
      pip install -r requirements.txt
tip

Pro Tip: Make sure your requirements.txt file correctly lists all dependencies needed to run your application.

Configuring the Application

  1. Environment Variables:

    • Set any required environment variables in your application. You can do this directly in the Web Control Panel under Python App > Application Settings or by using a .env file in your project directory.
  2. Application Port:

    • Python web applications typically use ports like 5000 for Flask or 8000 for Django. However, on a shared hosting environment, the system assigns an internal port 80. The Python selector will manage this for you, so you don't need to worry about configuring the port.
  3. Restarting the Application:

    • After configuring your app, restart it from the Web Control Panel under Python App > Manage Applications > Restart.
warning

Important: Ensure your application listens to 127.0.0.1 instead of 0.0.0.0. This prevents unauthorized external access to your app.

Testing Your Application

  1. Access Your Application:

    • Open your browser and navigate to the domain or subdomain where your Python app is deployed. Ensure everything works as expected.
  2. Checking Logs:

    • You can view your application logs directly from the Web Control Panel under Python App > View Logs. This is useful for troubleshooting any issues that arise.

Best Practices

  • Use Version Control: Ensure your Python project is under version control (e.g., Git) for easy deployment and rollback.
  • Optimize for Production: Use a production-ready web server like gunicorn for deploying your app, and set environment variables to handle configuration.
  • Security: Regularly update your dependencies, avoid using outdated Python versions, and follow best practices for securing your app.

Troubleshooting Common Issues

  • Application Not Starting: Check the startup file and ensure it’s correctly defined in the Web Control Panel. Also, check for errors in your code or missing dependencies.
  • Port Issues: Verify that the application is configured to use the port assigned by the Python selector.
  • Slow Performance: Ensure you are in Production mode and that your server resources are sufficient for the application's load.

Conclusion

Deploying a Python project on WebHostMost is seamless and efficient using the tools provided in the Web Control Panel. By following the steps outlined above, you can have your Python application up and running in no time. For further assistance, don’t hesitate to contact our support team.


Deploy your Python apps with confidence using WebHostMost's powerful and flexible Web Control Panel.