gpt4 book ai didi

python - 如何在 Ubuntu 上使用 Python 查找 Apache2 的配置错误?

转载 作者:行者123 更新时间:2023-11-29 21:58:47 24 4
gpt4 key购买 nike

我正在按照 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 测试脚本看起来也很简单。我可以从命令行运行它,不会出现任何问题,然后将文件 chmod 为 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 配置错误。在其他情况下,您将看到所有错误。

关于python - 如何在 Ubuntu 上使用 Python 查找 Apache2 的配置错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32874547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com