【问题标题】:How to find config error for Apache2 with Python on Ubuntu?如何在 Ubuntu 上使用 Python 查找 Apache2 的配置错误?
【发布时间】:2015-12-28 17:43:18
【问题描述】:

我正在关注有关在笔记本电脑的 Apache2 服务器上安装 Python 的 Digital Ocean 教程。 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”轰炸,访问日志没有提供更多详细信息。我知道这很简单,而且我办公室里没有其他人有任何专业知识。

提示?

【问题讨论】:

  • 试试/var/log/httpd/error.log
  • 我投票结束这个问题,因为它属于 AskUbuntu

标签: python mysql apache ubuntu python-3.x


【解决方案1】:

试试这个:

#!/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 配置。在其他情况下,您将看到所有错误。

【讨论】:

    【解决方案2】:

    嗯,很奇怪。

    我注意到一个相关的 SO 问题在第二个打印语句之后省略了括号。显然,如果标题后不包含空行,则它与 Apache 抛出错误有关。 (向@robertklep 提供见解。)

    编辑后刷新页面显示“缺少模块”错误。通过 sudo pip install 修复,我又重新开始工作了。谢谢大家!

    【讨论】:

      猜你喜欢
      • 2011-10-13
      • 1970-01-01
      • 2022-07-14
      • 2012-06-19
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多