Plesk does not natively allow Python apps to be ran, without some tinkering. Follow along as we show you how to deploy your Python apps on Plesk!
Note: Although we will not be proxying requests through Apache, we need to install the (deprecated) Apache Python mods in order to enable Python support on the domain within Plesk.
Step 1 – Install mod_python
Debian/Ubuntu:apt install libapache2-mod-python
RHEL/CentOS/Rocky/Alma:wget http://repo.iotti.biz/CentOS/7/noarch/lux-release-7-1.noarch.rpm
rpm -Uvh lux-release*rpm
yum install mod_python -y
Step 2 – Restart Apache
Debian/Ubuntu:service apache2 restart
RHEL/CentOS/Rocky/Alma:service httpd restart
Step 3 – Enable Python
Update Plesk components by clicking the Refresh button located in Tools & Settings > Server Components.
Check the Python support option in Service Plans > Hosting Plans > ServicePlanName > Hosting Settings.
Scroll down and click the Update & Sync button.
Check the Python support option in Domains > myapp.example.space > Hosting Settings.
Scroll down and click the OK button to save the changes.
Step 4 – Install Phusion Passenger
Log in to Plesk.
Go to Tools & Settings > Updates & Upgrades (or Updates) > Add/Remove Components > Web Hosting.
Install Phusion Passenger service.
Step 5 – Verify Python3 & pip installation
Python3 comes with most distros by default. Check that it and pip are available:
python3 -m pip
If no version is output, then install:
Debian/Ubuntu:sudo apt install python3 python3-pip
RHEL/CentOS/Rocky/Alma:sudo dnf install python3 python3-pip
Step 6 – Update your app’s code
Phusion Passenger is designed to interface with Python web applications via WSGI. This just means you have to add the line application = app
into your code. For example:
# All of your existing code up here ...
# Add this line to expose the WSGI application
application = app
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Step 7 – Upload your code
Upload your app.py and requirements.txt in your domain/subdomain’s httpdocs folder using Plesk or FTP.
The ownership should belong to the user and group that your web server runs under. Typically, for a Plesk setup, this is your Plesk user for the domain’s subscription and psaserv. However, in some configurations, it might be psacln. If you upload or edit code as the root user, your app will not work until you fix privileges:
sudo chown -R myapp:psaserv /var/www/vhosts/example.space/myapp.example.space
sudo find /var/www/vhosts/example.space/myapp.example.space -type d -exec chmod 755 {} \;
sudo find /var/www/vhosts/example.space/myapp.example.space -type f -exec chmod 644 {} \;
Step 8 – Install Python dependencies
cd /var/www/vhosts/example.space/myapp.example.space
python3 -m pip install -r requirements.txt
Step 9 – Disable Apache proxying
In Plesk go to Websites & Domains > myapp.example.space > Apache & nginx Settings.
In the nginx settings section, clear the Proxy mode checkbox.
Click Apply.
Step 10 – Add additional directives to nginx
In the Additional nginx directives section (on the same page as the previous step), insert the following commands, ensuring you replace the values with those relevant to your project:
passenger_enabled on;
passenger_app_type wsgi;
passenger_python /usr/bin/python3;
passenger_startup_file app.py;
passenger_app_root /var/www/vhosts/example.space/myapp.example.space;
Click on the OK button.
Step 11 – Restart the app
passenger-config restart-app /var/www/vhosts/example.space/myapp.example.space
If there’s more than one Python app running, you may have to call it by instance ID:
passenger-config restart-app /var/www/vhosts/example.space/myapp.example.space --instance Q4TzBI2F
Troubleshooting
If your Python app has errors, check the Passenger error log:
tail -f /var/log/passenger/passenger.log