I am using CentOs 7 and alread installed Apache2. I try to run a cgi script with Python 3. I have a file named index.py with the following code:
#!/usr/bin/env python3.7
import platform
import pymysql
import cgitb
cgitb.enable()
print ("Content-type: text/html\r\n\r\n")
print ("<html>\n<body>")
print ("<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\$
connection = pymysql.connect(host='127.0.0.1', user='user', password='password', db='database', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
sql = "SELECT * FROM Marken"
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
print(row['ID'], row['markenName'], '</br>')
finally:
connection.close()
print ("Python Script Test Page", platform.python_version())
print ("</div>\n</body>\n</html>")
which gives a 500 internal server error.
I added following lines to apache httpd.conf:
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-script .py
</Directory>
and made the file acessable throut
sudo chmod 755 var/www/html/cgi/index.py
So if I comment out all the pymysql code and test it in browser it prints out the expected Python Test Page text and the python platform version 3.7.4. I can not even import pymysql as it gives an server error. With the mysql.connector I had the same problem. If I run the script as a standalone script with the pymysql code it works alright in shell.
I configure my system as a non root user with sudo access. Is this the problem? and are there maybe any enviroment variables to be set?
What I'm doing wrong?
<VirtualHost *:80>
<Directory "/var/www/html/cgi">
Options +ExecCGI
DirectoryIndex index.py
</Directory>
AddHandler cgi-script .py
DocumentRoot /var/www/html/cgi
</VirtualHost>
in apache configuration section you missed to define directory index for python file. also define DocumentRoot so python index file should able point to correct path. based on documentation you need to rid out handler from directory.