How to Migrate a Python Application from Old Hosting to New cPanel

 
Migrating a Python application (such as Django, Flask, or FastAPI) from an old hosting server or local machine to a new cPanel account involves transferring project files, exporting the database, setting up the Python environment, and configuring the web server gateway interface (WSGI).

Follow this step-by-step guide to complete the migration seamlessly.

Step 1: Backup and Export Your Project (Old Hosting / Local)

Before starting the transfer, you need to collect all necessary files and export your database.

  1. Compress Project Files:

    • Navigate to your project directory on the old server or local environment.
    • Zip the entire project folder containing your core code (e.g., manage.py, app.py, or wsgi.py).
  2. Export Dependencies:

    • Activate your existing virtual environment and run the following command to generate a requirements.txt file:

      Bash
       
      pip freeze > requirements.txt
      
  3. Export Database:

    • MySQL / MariaDB: Export your database as a .sql file using phpMyAdmin or mysqldump.
    • PostgreSQL: Export your database dump via phpPgAdmin or pg_dump.
    • SQLite: If your project uses db.sqlite3, simply keep it inside your compressed .zip archive.

Step 2: Set Up Python Application in New cPanel

  1. Log in to your new cPanel dashboard.
  2. Under the Software section, click Setup Python App.
  3. Click the Create Application button.
  4. Fill in the application configurations:

    • Python Version: Select the matching version required by your application (e.g., 3.9, 3.10, 3.11).
    • Application Root: Enter the directory name where your project files will live (e.g., mypythonapp).
    • Application URL: Select the domain or subdomain where the site should run.
    • Application Startup File (Optional): Specify the entry file (wsgi.py for Django or app.py for Flask).
    • Application Entry Point (Optional): Define the WSGI callable object name (usually application or app).
  5. Click Create. cPanel will create the application directory and automatically set up a dedicated virtual environment (virtualenv).

Step 3: Upload and Extract Project Files

  1. Go back to the cPanel main dashboard and open File Manager.
  2. Navigate to the Application Root directory created in Step 2 (e.g., mypythonapp).
  3. Click Upload, select your project .zip file, and upload it.
  4. Once uploaded, right-click the .zip file and select Extract.

Step 4: Install Dependencies in Virtual Environment

  1. Return to Setup Python App in cPanel.
  2. Copy the command displayed at the top of the page used to enter the virtual environment. It will look similar to:

    Bash
     
    source /home/username/virtualenv/mypythonapp/3.10/bin/activate && cd /home/username/mypythonapp
    
  3. Open Terminal (or connect via SSH) in cPanel and paste the command.
  4. Run the following command inside the active virtual environment to install all required packages:

    Bash
     
    pip install -r requirements.txt
    

Step 5: Configure Database and Settings

  1. Create Database:

    • Go to MySQL® Databases in cPanel.
    • Create a new Database, User, and strong Password.
    • Add the User to the Database and grant ALL PRIVILEGES.
  2. Import Database:

    • Open phpMyAdmin from cPanel, select your newly created database, go to the Import tab, upload your .sql file, and click Go.
  3. Update Configuration Settings:

    • In cPanel File Manager, edit your configuration file (e.g., settings.py for Django or .env / config.py for Flask).
    • Update database credentials (DB_NAME, DB_USER, DB_PASSWORD, and HOST='localhost').
    • For Django, update ALLOWED_HOSTS:

      Python
       
      ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com']
      

Step 6: Configure Static Files & WSGI

  1. Collect Static Files (Django):

    • Run the following command inside your virtual environment terminal to collect static files into the designated static root:

      Bash
       
      python manage.py collectstatic
      
  2. Configure passenger_wsgi.py:

    • Locate the passenger_wsgi.py file inside your application root folder and open it for editing.
    • Link your application's WSGI instance.
    Example for Django:

    Python
     
    import os
    import sys
    
    sys.path.insert(0, os.path.dirname(__file__))
    from my_project_name.wsgi import application
    

Step 7: Restart Application and Verify

  1. Go to cPanel Setup Python App.
  2. Locate your application and click the Restart button.
  3. Open your domain in a web browser to ensure the website is fully functional.
Was this answer helpful? 0 Users Found This Useful (0 Votes)