问题
I'm hoping someone might be able to explain to me why this is happening?
I am running httpd on Centos 7 with Python 2.7
I have a python module in /home/user/Path/to/module.py
. Lets say, for example, it prints "Hello World!"
print("Hello World!")
I then try to import it into the file /var/www/cgi-bin/index.py
like so:
#!/usr/bin/python
import sys
sys.path.append('/home/user/Path/to/')
print('Content-type: text/html\n\n')
print('<!DOCTYPE html>')
print('<html lang="en">')
print('<head>')
print('</head>')
print('<body>')
print('<h1>')
import module
print('</h1>')
print('</body>')
print('</html>')
If I were to run this from the terminal I get the following:
Content-type: text/html
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>
Hello World!
</h1>
</body>
</html>
However, when I curl this URL curl http://127.0.0.1/cgi-bin/index.py
I get the following message:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at
root@localhost to inform them of the time this error occurred,
and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>
Httpd error logs show this error?
[Sat Aug 19 10:38:16.107635 2017] [cgi:error] [pid 4061] [client 127.0.0.1:39608] AH01215: Traceback (most recent call last):
[Sat Aug 19 10:38:16.107684 2017] [cgi:error] [pid 4061] [client 127.0.0.1:39608] AH01215: File "/var/www/cgi-bin/index.py", line 7, in <module>
[Sat Aug 19 10:38:16.107691 2017] [cgi:error] [pid 4061] [client 127.0.0.1:39608] AH01215: import module
[Sat Aug 19 10:38:16.107702 2017] [cgi:error] [pid 4061] [client 127.0.0.1:39608] AH01215: ImportError: No module named module
[Sat Aug 19 10:38:16.109117 2017] [cgi:error] [pid 4061] [client 127.0.0.1:39608] End of script output before headers: index.py
Contents of /etc/httpd/conf/httpd.conf
:
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
Any suggestions would be greatly appreciated!
Update:
Attempted to get around this by removing this line:
sys.path.append('/home/user/Path/to/')
And instead creating a symlink to the module in /var/www/cgi-bin/
which appears to work but my module accesses the folder /home/user/Path/to/folder
which is throwing an access denied error in httpd logs.
Update:
Got this working using Django instead. It is, however, still a mystery to me why this wont work using Apache?
来源:https://stackoverflow.com/questions/45770266/python-importing-modules-into-cgi-script