Friday, March 4, 2022

Run Python with Apache on Ubuntu

sudo apt-get install libapache2-mod-wsgi-py3 

sudo apt-get install python3 libexpat1 -y 

sudo apt-get install apache2 apache2-utils ssl-cert libapache2-mod-wsgi -y 

a2enmod cgid

sites-available 

Non-SSL

<VirtualHost *:80>

        ServerName xxxx

        ServerAlias xxxx

        <Directory /var/www/xxxx>

                Options +ExecCGI

                AddHandler cgi-script .cgi .py

                Allow from all

                DirectoryIndex index.py

        </Directory>

         ProxyRequests Off

         ProxyPreserveHost On

         ProxyVia Full

        <Proxy *>

         Require all granted

        </Proxy>

        <Location />

          ProxyPass http://xxxx:port/

          ProxyPassReverse http://xxx:port/

        </Location>

        DocumentRoot /var/www/xxxx

</VirtualHost>


SSL

<VirtualHost *:443>

        ServerName xxx

        ServerAlias xxx

        SSLEngine on

        SSLCertificateFile /etc/ssl/certs/xxxx

        SSLCertificateKeyFile /etc/ssl/private/xxxx

        SSLCertificateChainFile /etc/ssl/xxxx

        <Directory /var/www/xxxx>

                Options +ExecCGI

                AddHandler cgi-script .cgi .py

                #Order allow,deny

                Allow from all

                AllowOverride All

                #Options +ExecCGI

                DirectoryIndex index.py

                Require all granted

        </Directory>

         ProxyRequests Off

         ProxyPreserveHost On

         ProxyVia Full

        <Proxy *>

         Require all granted

        </Proxy>

        <Location />

          ProxyPass http://xxxx:port/

          ProxyPassReverse http://xxx:port/

        </Location>

        DocumentRoot /var/www/xxxx

        ErrorLog ${APACHE_LOG_DIR}/error.log

        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

systemctl restart apache2

Home page of py added the first line:

#!/usr/bin/python

If python app is version 3 so need to map python to python3

Check Apache log
sudo tail -f /var/log/apache2/error.log

Check python version
python --version
Note: Python app must be up service


No comments:

Post a Comment