Updating Your Live Flask Application on cPanel

This guide provides a structured workflow for deploying updates to an existing Python Flask application hosted via cPanel and Phusion Passenger. If you are starting a fresh deployment see the “Deployment from Scratch” article.

1. Prepare Your Local Environment

Before uploading, ensure your dependency list is accurate. If you have added new libraries during development, regenerate your requirements.txt.

  • Command: Open your terminal in your project root and run: pipreqs . --force
    • This ensures only the packages actually used in your code are listed, preventing version conflicts.

2. Upload Updated Files

You need to replace the old code on the server with your new version.

  • Via File Manager: Navigate to your Application Root folder (e.g., /home/username/myapp) and upload your updated .py files, templates/, and static/ folders.
  • Via FTP/SFTP: Use a client like FileZilla to connect to your server and overwrite the files in the application root directory.
  • Important: Always ensure you overwrite your entry file (usually app.py or main.py).

3. Synchronise Dependencies

If your update includes new libraries (e.g., requests, pandas, or BeautifulSoup), you must install them into the server’s virtual environment.

  1. Upload the new requirements.txt to your Application Root.
  2. In cPanel, open Setup Python App.
  3. Click the Edit (pencil icon) next to your application.
  4. In the Configuration files section, ensure requirements.txt is listed, then click Run Pip Install.

4. Trigger the Application Restart

cPanel/Passenger caches your Python code. Changes will not appear until the application is restarted.

Method A: The GUI (Easiest)

  • In the Setup Python App interface, click the Restart icon (circular arrow) at the top of your application settings.

Method B: The “Touch” Method (Advanced)

  • Using File Manager, create a folder named tmp inside your application root.
  • Inside tmp, create an empty file named restart.txt.
  • Note: If you want to trigger a restart later, simply “edit” and save this file again. Passenger monitors the “last modified” timestamp of this file to trigger a reload.

5. Verify the WSGI Bridge

If your update involved renaming your main file or your Flask instance, you must update the passenger_wsgi.py file.

Ensure the import line matches your new structure:

Python

# Format: from [filename] import [flask_variable] as application
from app import app as application

6. Troubleshooting & Debugging

If your application shows an error or doesn’t reflect changes after a restart:

  • Clear Browser Cache: Perform a “Hard Refresh” (Ctrl + F5 or Cmd + Shift + R) to ensure you aren’t looking at a cached version of the site.
  • Enable Error Display: If you see a generic “Something went wrong” page, enable detailed logs. Open your .htaccess file in public_html and add:
    • ApachePassengerFriendlyErrorPages on
  • Check Port Conflicts: Ensure your code does not contain app.run(). If it does, wrap it in:
    • if __name__ == "__main__": app.run()

Posted in CPanel, Knowledgebase, Web and Email Hosting

Dzinaishe Mpini