[英]How to find config error for Apache2 with Python on Ubuntu?
我正在閱讀Digital Ocean教程,以在便攜式計算機的Apache2服務器上安裝Python。 Ubuntu 14.04 LTS,Python 3.4.3,MySQL 14.14。
更新Apache配置似乎很簡單:
<VirtualHost *:80>
<Directory /var/www/test>
Options +ExecCGI
DirectoryIndex index.py
</Directory>
AddHandler cgi-script .py
...
DocumentRoot /var/www/test
...
我可以不帶問題重新啟動Apache。
/var/www/test/index.py上的Python測試腳本似乎也很簡單。 我可以從沒有問題的命令行運行它,然后將文件更改為755。
#!/usr/bin/python
# Turn on debug mode.
import cgitb
cgitb.enable()
# Print necessary headers.
print("Content-Type: text/html")
print()
# Connect to the database.
import pymysql
conn = pymysql.connect(
db='example',
user='root',
passwd='your_root_mysql_password',
host='localhost')
c = conn.cursor()
# Insert some example data.
c.execute("INSERT INTO numbers VALUES (1, 'One!')")
c.execute("INSERT INTO numbers VALUES (2, 'Two!')")
c.execute("INSERT INTO numbers VALUES (3, 'Three!')")
conn.commit()
# Print the contents of the database.
c.execute("SELECT * FROM numbers")
print([(r[0], r[1]) for r in c.fetchall()])
Apache始終使用“ 500”進行轟炸,並且訪問日志未提供更多詳細信息。 我知道這很簡單,而且我的辦公室里沒有其他人擁有任何專業知識。
提示?
試試這個:
#!/usr/bin/python
import os, logging
def customLogger(name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(os.path.join('/var/log/', name + '.log'), 'a')
logger.addHandler(handler)
return logger
error_logger = customLogger('errors_python')
# Turn on debug mode.
import cgitb
cgitb.enable()
try:
# Print necessary headers.
print("Content-Type: text/html")
print()
# Connect to the database.
import pymysql
conn = pymysql.connect(
db='example',
user='root',
passwd='your_root_mysql_password',
host='localhost')
c = conn.cursor()
# Insert some example data.
c.execute("INSERT INTO numbers VALUES (1, 'One!')")
c.execute("INSERT INTO numbers VALUES (2, 'Two!')")
c.execute("INSERT INTO numbers VALUES (3, 'Three!')")
conn.commit()
# Print the contents of the database.
c.execute("SELECT * FROM numbers")
print([(r[0], r[1]) for r in c.fetchall()])
except Exception as e:
error_logger.exception("Error")
在瀏覽器中打開,然后檢查/var/log/errors_python.log
文件。 如果文件為空,則看起來是錯誤的apache2配置。 在其他情況下,您將看到所有錯誤。
好吧, 那很奇怪。
我注意到一個相關的SO問題在第二個打印語句后省略了括號。 顯然,這與Apache引發錯誤有關,如果標頭后面沒有空白行,則會引發錯誤。 (建議使用@robertklep以獲得洞察力。)
編輯后刷新頁面,顯示“缺少模塊”錯誤。 通過sudo pip安裝修復,我又回來了。 謝謝大家!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.