How to set up a Python app (Flask or Django)
Before you start
Python applications run through CloudLinux's app manager and Phusion Passenger.
Compatibility depends on your hosting server:
- cPanel: Python applications are supported.
- DirectAdmin: Contact Webway Support before starting. Most DirectAdmin servers use OpenLiteSpeed, which is not compatible with this app manager. Webway can confirm your server or move your account if necessary.
You will need:
- Your Python application files
- A
requirements.txtfile - Your domain or subdomain
- Database details, if the application uses a database
Python 3.11 is recommended for new applications. Other versions may be available for older applications.
1. Create the Python application
-
Sign in to cPanel.
-
Open Software.
-
Select Setup Python App. On a compatible DirectAdmin server, the same tool is listed as Python App.
-
Select Create Application.
-
Choose Python 3.11, unless your application requires another version.
-
Enter an application root, such as:
apps/my-python-app -
Keep the application root outside
public_html. -
Select the domain or subdomain that should open the application.
-
Create the application.
The app manager creates:
- A Python virtual environment
- A
passenger_wsgi.pyentry-point file - Passenger configuration in the domain's
.htaccessfile
Do not remove the Passenger lines written to public_html/.htaccess.
2. Upload your application
Upload the application files to the application root you selected.
For example:
/home/USERNAME/apps/my-python-app
Replace USERNAME with your hosting username.
Do not upload the application itself to public_html. Only files that must be directly public, such as static assets, should be exposed there.
3. Remove the placeholder page
A new hosting account may contain a placeholder file at:
public_html/index.html
This file can be served instead of your Python application.
Delete it if it is Webway's default placeholder. Do not delete it if it belongs to an existing website.
4. Configure a Flask entry point
Suppose your application is defined in app.py like this:
from flask import Flask
app = Flask(__name__)
Edit passenger_wsgi.py in the application root:
from app import app as application
The name on the left must remain application. Change the module and object names on the right to match your project.
For example, if your Flask instance is called website inside main.py:
from main import website as application
5. Configure a Django entry point
Django projects normally provide a WSGI application in the project's wsgi.py file.
For a project package named myproject, use this in passenger_wsgi.py:
from myproject.wsgi import application
Replace myproject with the package containing your Django settings.py and wsgi.py files.
6. Set Django's allowed hosts
Add your domain names to ALLOWED_HOSTS in Django's settings:
ALLOWED_HOSTS = [
"example.co.za",
"www.example.co.za",
]
Replace these examples with your own domain.
Avoid using ALLOWED_HOSTS = ["*"] on a production website.
7. Install the required packages
Add your requirements.txt file to the application.
You can then install its packages from the app screen or over SSH. See How to install Python packages with pip and requirements.txt for full instructions.
8. Run Django migrations
Activate the virtual environment using the exact command shown on the application screen.
Then run:
cd /home/USERNAME/APPROOT
python manage.py migrate
Replace USERNAME and APPROOT with the values for your application.
Back up an existing production database before running new migrations.
9. Collect Django static files
Configure STATIC_URL and STATIC_ROOT in your Django settings. Then run:
python manage.py collectstatic --noinput
The collected files must be stored in a location that the web server can serve directly.
Set STATIC_ROOT to a folder inside the domain's public_html, for example public_html/static, and STATIC_URL = "/static/", so the web server serves the files directly.
10. Add environment variables
Use Add Variable on the application screen for secrets and configuration such as:
DJANGO_SETTINGS_MODULESECRET_KEY- Database credentials
- API keys
- Production or debug settings
Do not publish secret values inside your source code or public_html.
11. Restart and test the application
- Return to the Python application screen.
- Select Restart.
- Open the application's URL.
- Test its main pages and forms.
- Review the Passenger and domain error logs if it returns an error.
Restart the application after every code or dependency change.
About ASGI applications
Passenger expects a WSGI entry point. Flask and Django can provide one.
ASGI-only applications, such as applications designed specifically for FastAPI and Uvicorn, do not run directly through this WSGI interface. A compatible WSGI adapter may work, but asynchronous features and WebSockets are not supported.
ASGI-only applications aren't supported on shared hosting. Use a VPS for them.
Did this answer it?