I run Python scripts like PHP using mod_cgi
Here is a tutorial by Apache. And the cgi interface to use is here by Python.org
A second good tutorial that I used is here once your up and running.
I would add that there is a simiplier way to configure Apache.
Step 1: The first step is not mentioned in the guides above is to enable CGI processing in apache.
sudo a2enmod cgi
This will automatically enable mod_cgid if your server is configured with a multi-threaded MPM, which was the case for me.
Step 2: Edit your httpd.conf or whatever it is named in /etc/apache2/sites-enabled
in Linux Mint 19.2.
Enable a script for / with an index.py
<VirtualHost *:80>
DocumentRoot /your/www/html
DirectoryIndex index.py
</VirtualHost>
Step 3: Enable other python scripts so they can also run in the same folder or in subdirectories.
<Directory "/your/www/html/*">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
AddHandler cgi-script .py
AddHandler default-handler .jpg .png .gif .css .js .ico
</Directory>
ScriptAlias / /your/www/html/
There are two caveats that I have encountered that must be adhered to to run python scripts successfully.
When running in linux, make sure the line endings of each python file.py are
unix line endings. Otherwise the python script will not run. For example,
Notepad++ has Edit, EOL Conversion, Linux (LF) in its menu,tool bar.
Ensure that the permission of each python file.py has execute permissions.
In Linux Mint 19.2 I right click the file, go to Properties, go to Permissions,
then check the checkbox at Execute: Allow executing program as file. Or just
run the command:
chmod a+x python_script.py