Here is a step-by-step example of how to create a Python app on an Apache virtual hosting and call this app from a php-site.
This tutorial is made for DirectAdmin panel, however, for CPanel it will be pretty the same.
1. Creating a virtual environment
1.1) Run Setup Python App in the section Extra Features of your DirectAdmin. If it's not there ask your hoster if they provide Python support.
1.2) When you choose to Create Application you will see the form with the properties and paths of your app.
- Choose Python version
- Fill Application root — the address on the server where you'll put your .py file
- Application URL — the link on which your app will be available
1.3) After you click Create, the plugin creates a folder with virtualenv, the folders you've chosen for App root and App URL, and a passenger_wsgi.py file (what is Passenger WSGI).
2. Setting up files for Python
After the launching of Web App via the hosting plugin, there are two new files created in the application-root folder: .htaccess and passenger_wsgi.py.
File .htaccess for a Python app
In .htaccess, we have our defined paths: PassengerAppRoot, PassengerBaseURI, PassengerPython:
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN PassengerAppRoot "/my-account-path/domains/risksir.com/public_html/myapp" PassengerBaseURI "/myapp" PassengerPython "/my-account-path/virtualenv/domains/risksir.com/public_html/myapp/3.8/bin/python3.8" # DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END # DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION BEGIN <IfModule Litespeed> </IfModule> # DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION END
File passenger_wsgi.py for a Python app
Now, we need to edit the passenger_wsgi.py so that it loads in the app code and gets from it an object that the Passenger runner can work with.
# Commenting this out, as far as we don't need it for our simple app: ''' import os import sys sys.path.insert(0, os.path.dirname(__file__)) def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) message = 'It works!\n' version = 'Python %s\n' % sys.version.split() response = '\n'.join([message, version]) return [response.encode()] ''' # This is only we need now for our testapp.py: from testapp import application
3. Creating Python app file
Ok, we prepared the environment, so let's create our testapp.py we'll use on a website. Let it be just a hello-world code, which will also receive a text from URL arguments and print it.
Installing Flask and other libraries on virtualenv
We want to write an app with Flask, hence we have to install it first. Using SSH, enter the virtual environment we created in previous steps.
The command should be like this:
source /my-account-path/virtualenv/domains/risksir.com/public_html/myapp/3.8/bin/activate && cd /my-account-path/domains/risksir.com/public_html/myapp
Then install Flask (and other stuff you need for your code):
pip install flask
Creating .py file with our program code
Create or upload our hello-world testapp.py file:
from flask import Flask, request app = Flask(__name__) application = app # our hosting requires application in passenger_wsgi @app.route('/', methods=['GET', 'POST']) # define the relative URL for the app and methods we will use def hello(): default_arg1 = 'none' gotten_arg1 = request.args.get('arg1', default_arg1) # if arg1 is empty get default_arg1 return "Hello world, this is the text you sent: " + gotten_arg1 if __name__ == "__main__": # code that protects users from accidentally invoking the script when they didn't intend to app.run()
We deployed our app now — if you put your Application URL in a browser address bar, you'll see the default message we set:
And with an argument arg1:
4. Calling a Python app from php code
You can call your Python app in php from any page by using file_get_contents().
<?php $txt = "Great!+Python+is+connected!"; $info = file_get_contents('http://risksir.com/myapp?arg1='.$txt); echo $info; ?>
and you'll get in response the message from the app:
Finally it works, now we can add our Python scripts on any php-based website like Joomla, Wordpress, or any other CMS.