[英]wordpress on apache2 running python as cgi
所以這就是我想做的:
在我的 Raspi 上,一個 python 程序正在運行。 在 wordpress 站點上,應顯示程序的當前 state,並且某些配置應該可以更改。
這是問題所在:
每當我想執行 python 腳本時,我都會收到 500 錯誤代碼。 如果我只想顯示值或更改它並不重要。 我是 html、cgi 和 apache 的新手,嘗試了很多但現在我不知道如何繼續。 如果有人能指出我正確的方向,我將不勝感激。
這是我的配置:
Apache:
編輯文件 /etc/apache2/apache2.conf:
<Directory /var/www/>
Options +ExecCGI +Indexes +FollowSymLinks
AddHandler cgi-script .cgi .py
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
<Directory "/var/www/cgi-bin">
Options All
</Directory>
我也跑了sudo a2enmod cgi
Web 服務器目錄 (/var/www/) 如下所示:
.
├── cgi-bin
└── html
├── pma
│ └── ...
└── wordpress
└── ...
Wordpress:
在 wordpress 站點上,我 go 進入“文本”模式並具有以下 html 代碼:
Curent Value: <form action="/cgi-bin/apfautostartval.py" method="get"></form>
<form action="/cgi-bin/apfcgi.py" method="post" target="_blank">
<input name="autoTest" type="radio" value="True" /> True (do automatic scan)
<input name="autoTest" type="radio" value="False" /> False (do manual scan)
<input type="submit" value="Submit" /></form>
Python 個文件:
apfautostartval.py 應該只從 config.ini 中獲取值並將其發布:
#!/usr/bin/python3
import configparser
import os
import cgi, cgitb
cgitb.enable()
# Create config parser
config = configparser.ConfigParser()
configFilePath = os.path.join(os.path.sep,"home","pi",..., "config.ini")
config.read(configFilePath)
print("Content-type: text/html")
print()
print("<!DOCTYPE html>")
print("<html>")
print("<body>")
print(str(config['SETTINGS']["autoTest"]))
print("</body>")
print("</html>")
最后 apfcgi.py 應該接收提交的新值並將其寫入 config.ini:
#!/usr/bin/python3
import configparser
import os
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
cgitb.enable()
# Create config parser
config = configparser.ConfigParser()
configFilePath = os.path.join(os.path.sep,"home","pi",..., "config.ini")
print("Content-type: text/html")
print()
print("<!DOCTYPE html>")
print("<html>")
print("<body>")
# Receive autotest command from web site
if form.getvalue("autoTest"):
config.read(configFilePath)
if form.getvalue("autoTest").lower() == "true":
config['SETTINGS']["autoTest"] = "True"
else:
config['SETTINGS']["autoTest"] = "False"
with open(configFilePath, 'w') as configfile:
config.write(configfile)
print("</body>")
print("</html>")
我有同樣的問題。 問題出在編碼上。 默認情況下,ConfigParser.read() 使用encoding=none參數。 我指定了 utf-8,它起作用了。
config = configparser.ConfigParser()
config.read(configFilePath, encoding='utf-8')
好的,我已經找到了解決問題的一部分的方法:
更改值時,請確保已為cgi腳本授予了該文件的權限:
在/etc/apache2/apache2.config中添加:
<Directory "/home/pi/dirToTheConfigFile/">
Options +ExecCGI +Indexes +FollowSymLinks
AddHandler cgi-script .cgi .py
AllowOverride None
Require all granted
</Directory>
並確保允許用戶www-data修改文件。
通常將cgitb.enable()
添加到python腳本中,因此對於html 500錯誤,您將在/var/log/apache2/error.log
文件中獲得詳細的錯誤消息。
為了接收數據,找到了以下解決方案:不幸的是,Python腳本是root擁有的,因此無法執行cound。 我更改了權限。
WordPress中的接收html也已被重寫:
<iframe src="/cgi-bin/apfautostartval.py" width="100" height="29.5" frameborder="0" marginwidth="8" marginheight="0" scrolling="no" align="bottom"></iframe>
現在是iframe,並顯示返回的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.