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.
-
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, orwsgi.py).
-
-
Export Dependencies:
-
Activate your existing virtual environment and run the following command to generate a
requirements.txtfile:
Bashpip freeze > requirements.txt
-
-
Export Database:
-
MySQL / MariaDB: Export your database as a
.sqlfile using phpMyAdmin ormysqldump. -
PostgreSQL: Export your database dump via phpPgAdmin or
pg_dump. -
SQLite: If your project uses
db.sqlite3, simply keep it inside your compressed.ziparchive.
-
Step 2: Set Up Python Application in New cPanel
-
Log in to your new cPanel dashboard.
-
Under the Software section, click Setup Python App.
-
Click the Create Application button.
-
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.pyfor Django orapp.pyfor Flask). -
Application Entry Point (Optional): Define the WSGI callable object name (usually
applicationorapp).
-
-
Click Create. cPanel will create the application directory and automatically set up a dedicated virtual environment (
virtualenv).
Step 3: Upload and Extract Project Files
-
Go back to the cPanel main dashboard and open File Manager.
-
Navigate to the Application Root directory created in Step 2 (e.g.,
mypythonapp). -
Click Upload, select your project
.zipfile, and upload it. -
Once uploaded, right-click the
.zipfile and select Extract.
Step 4: Install Dependencies in Virtual Environment
-
Return to Setup Python App in cPanel.
-
Copy the command displayed at the top of the page used to enter the virtual environment. It will look similar to:
Bashsource /home/username/virtualenv/mypythonapp/3.10/bin/activate && cd /home/username/mypythonapp -
Open Terminal (or connect via SSH) in cPanel and paste the command.
-
Run the following command inside the active virtual environment to install all required packages:
Bashpip install -r requirements.txt
Step 5: Configure Database and Settings
-
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.
-
-
Import Database:
-
Open phpMyAdmin from cPanel, select your newly created database, go to the Import tab, upload your
.sqlfile, and click Go.
-
-
Update Configuration Settings:
-
In cPanel File Manager, edit your configuration file (e.g.,
settings.pyfor Django or.env/config.pyfor Flask). -
Update database credentials (
DB_NAME,DB_USER,DB_PASSWORD, andHOST='localhost'). -
For Django, update
ALLOWED_HOSTS:
PythonALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com']
-
Step 6: Configure Static Files & WSGI
-
Collect Static Files (Django):
-
Run the following command inside your virtual environment terminal to collect static files into the designated static root:
Bashpython manage.py collectstatic
-
-
Configure
passenger_wsgi.py:
-
Locate the
passenger_wsgi.pyfile inside your application root folder and open it for editing. -
Link your application's WSGI instance.
Example for Django:
Pythonimport os import sys sys.path.insert(0, os.path.dirname(__file__)) from my_project_name.wsgi import application -
Step 7: Restart Application and Verify
-
Go to cPanel Setup Python App.
-
Locate your application and click the Restart button.
-
Open your domain in a web browser to ensure the website is fully functional.
