在 Apache 中加载 Python 时必须添加执行权限?
Execute rights must be added while loading Python in Apache?
我在 DigitalOcean Droplet 中使用 Ubuntu 16.04 服务器、Python 2.7.12 和 Apache 2.4.18。
我上传了一个 index.py
到网络根目录。脚本内容如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Turn on debug mode.
import cgitb
import platform
cgitb.enable()
# Print necessary headers.
print "Content-Type: text/html\n"
print "<html><body>Python Version: %s</body></html>" % (platform.python_version())
如果我将 index.py
设置为 644(没有执行权限),脚本将 return 500 Internal Server Error。日志说:
[Fri Dec 28 04:05:18.035946 2018] [cgi:error] [pid 29045] [client 202.75.86.173:54912] End of script output before headers: index.py
从另一个回答,建议我通过chmod +x index.py
添加权限:
-rwxr-xr-x 1 root www-data 254 Dec 28 04:05 index.py
添加执行权限后,Python脚本可以运行没问题。
- 我这样做对吗?
- 这会导致安全问题吗?
这是 Apache 站点配置:
<VirtualHost *:80>
ServerName abc.example.com
DocumentRoot /var/www/vhosts/abc.example.com
<Directory /var/www/vhosts/abc.example.com/>
Options -Indexes
Options +ExecCGI
DirectoryIndex index.py
AllowOverride All
Order allow,deny
allow from all
</Directory>
AddHandler cgi-script .py
ErrorLog ${APACHE_LOG_DIR}/abc-apache2.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access-logfile.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =abc.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
更新:
这个问题不是关于我遇到的错误,正如我在问题中提到的,我已经让我的脚本工作了。我只是担心 安全性 。简而言之,不是重复的。
1. 是的,你做对了。
你告诉 Apache 将 .py 文件作为 cgi 脚本执行。
这意味着 apache 将 "execute" 你 index.py 文件如果它被授权。然后你必须给apache权限来执行你的python文件。
在你的 index.py 文件中有一个 shebang 指示 apache 使用哪个解释器来执行这个文件。
2. 在您的情况下,您可以像这样设置良好的安全性:
chown www-data:www-data index.py
chmod 550 index.py
然后root 并且只有root 能够修改或删除该文件。
并且只有 apache 用户和 apache 组能够读取或执行脚本(假设您的 apache user/group 是 www-data)。
我在 DigitalOcean Droplet 中使用 Ubuntu 16.04 服务器、Python 2.7.12 和 Apache 2.4.18。
我上传了一个 index.py
到网络根目录。脚本内容如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Turn on debug mode.
import cgitb
import platform
cgitb.enable()
# Print necessary headers.
print "Content-Type: text/html\n"
print "<html><body>Python Version: %s</body></html>" % (platform.python_version())
如果我将 index.py
设置为 644(没有执行权限),脚本将 return 500 Internal Server Error。日志说:
[Fri Dec 28 04:05:18.035946 2018] [cgi:error] [pid 29045] [client 202.75.86.173:54912] End of script output before headers: index.py
从另一个回答,建议我通过chmod +x index.py
添加权限:
-rwxr-xr-x 1 root www-data 254 Dec 28 04:05 index.py
添加执行权限后,Python脚本可以运行没问题。
- 我这样做对吗?
- 这会导致安全问题吗?
这是 Apache 站点配置:
<VirtualHost *:80>
ServerName abc.example.com
DocumentRoot /var/www/vhosts/abc.example.com
<Directory /var/www/vhosts/abc.example.com/>
Options -Indexes
Options +ExecCGI
DirectoryIndex index.py
AllowOverride All
Order allow,deny
allow from all
</Directory>
AddHandler cgi-script .py
ErrorLog ${APACHE_LOG_DIR}/abc-apache2.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access-logfile.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =abc.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
更新:
这个问题不是关于我遇到的错误,正如我在问题中提到的,我已经让我的脚本工作了。我只是担心 安全性 。简而言之,不是重复的。
1. 是的,你做对了。
你告诉 Apache 将 .py 文件作为 cgi 脚本执行。 这意味着 apache 将 "execute" 你 index.py 文件如果它被授权。然后你必须给apache权限来执行你的python文件。
在你的 index.py 文件中有一个 shebang 指示 apache 使用哪个解释器来执行这个文件。
2. 在您的情况下,您可以像这样设置良好的安全性:
chown www-data:www-data index.py
chmod 550 index.py
然后root 并且只有root 能够修改或删除该文件。 并且只有 apache 用户和 apache 组能够读取或执行脚本(假设您的 apache user/group 是 www-data)。