繁体   English   中英

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

[英]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.

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