简体   繁体   English

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

[英]How to find config error for Apache2 with Python on Ubuntu?

I'm following a Digital Ocean tutorial for installing Python on my laptop's Apache2 server. 我正在阅读Digital Ocean教程,以在便携式计算机的Apache2服务器上安装Python。 Ubuntu 14.04 LTS, Python 3.4.3, MySQL 14.14. Ubuntu 14.04 LTS,Python 3.4.3,MySQL 14.14。

Updating the Apache config seems straightforward: 更新Apache配置似乎很简单:

<VirtualHost *:80>
    <Directory /var/www/test>
        Options +ExecCGI
        DirectoryIndex index.py
    </Directory>
    AddHandler cgi-script .py
    ...
    DocumentRoot /var/www/test
    ...

I can restart Apache w/o issues. 我可以不带问题重新启动Apache。

The Python test script at /var/www/test/index.py also seems straightforward. /var/www/test/index.py上的Python测试脚本似乎也很简单。 I can run it from the command line w/o problems, and chmod'd the file to 755 afterwards. 我可以从没有问题的命令行运行它,然后将文件更改为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 is consistently bombing out with a "500", and the access log doesn't provide further detail. Apache始终使用“ 500”进行轰炸,并且访问日志未提供更多详细信息。 I know this is something simple and there's nobody else in my office with any expertise. 我知道这很简单,而且我的办公室里没有其他人拥有任何专业知识。

Hints? 提示?

Try this one: 试试这个:

#!/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")

Open in browser and then check /var/log/errors_python.log file. 在浏览器中打开,然后检查/var/log/errors_python.log文件。 If file empty then looks like wrong apache2 configuration. 如果文件为空,则看起来是错误的apache2配置。 In other scenario you will see all errors. 在其他情况下,您将看到所有错误。

Well, that was weird. 好吧, 很奇怪。

I noticed that a related SO question omitted the parens after the 2nd print statement. 我注意到一个相关的SO问题在第二个打印语句后省略了括号。 Apparently it's related to Apache throwing an error if a blank line is not included after the header. 显然,这与Apache引发错误有关,如果标头后面没有空白行,则会引发错误。 (Props to @robertklep for the insight.) (建议使用@robertklep以获得洞察力。)

Refreshing the page after the edit revealed a 'missing module' error. 编辑后刷新页面,显示“缺少模块”错误。 Fixed via a sudo pip install, and I'm back in business. 通过sudo pip安装修复,我又回来了。 Thanks all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM