How to run Python Scripts ( *.py files) on Apache2 (configuring unix apache2 servers)

Preparing (install apache2, python):
Installation Apache2:

sudo apt-get install apache2

Installation Python(choose one):

sudo apt-get install python
sudo apt-get install python2.7
sudo apt-get install python3

First step: enabling CGI processing in your apache (automatically enable mod_cgid).

sudo a2enmod cgi

Second step: Allow run CGI scripts in your directory.
You can try allow by .htaccess in your “python” web folder

Options +ExecCGI
AddHandler cgi-script .py

or you can allow .py files to be executed as scripts in a particular folder you would use config in apache conf file:

    <Directory /srv/www/mysite/public_html/python_folder>
        Options +ExecCGI
        AddHandler cgi-script .py

Finish step:

Restart apache

sudo service apache2 restart

Also, you can use this content for check your python web execution

# -*- coding: utf-8 -*-
print("Content-type: text/html \n")
print("Test *.py execution - is ok!")