配置Python时启动apache2服务器出错

1 投票
1 回答
2272 浏览
提问于 2025-04-16 16:55

你好,我正在尝试为Python配置apache2服务器,我用这个命令安装了apache2:

sudo apt-get install apache2
cd /etc/apache2/mods-enabled/
sudo ln -s ../mods-available/mod_python.load mod_python.load
cd /etc/apache2/sites-available/
sudo gedit default

然后我修改了默认的配置文件为:

<Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride AuthConfig
                Order allow,deny
                allow from all

                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On

                # Uncomment this directive is you want to see apache2's
                # default start page (in /apache2-default) when you go to /
                #RedirectMatch ^/$ /apache2-default/
    </Directory>

接着我使用了这个命令:

sudo /etc/init.d/apache2 restart

结果出现了一个错误:

* Starting web server apache2                                                  apache2: Syntax error on line 204 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/mods-enabled/mod_python.load: LoadModule takes two arguments, a module name and the name of a shared object file to load it from

当我用cat命令查看mod_python.load里面的链接时,我得到了 LoadModule python_module / usr/lib/apache2/modules/mod_python.so

请帮帮我。

继续……现在我遇到了一个新错误。

先生,我在一个叫cgi-bin的文件夹里放了一个名为hello.py的文件,里面有以下代码:

#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

这个文件放在 /var/www 目录下。

当我通过apache2运行这个文件时,使用的链接是:

http://localhost/cgi-bin/hello.py

但是我收到了这个错误:

The requested URL /cgi-bin/hello.py was not found on this server.
Apache/2.2.14(Ubuntu)Server at localhost Port 80

1 个回答

3

看起来在路径的开头有一个斜杠(/)和后面的模块路径之间有个空格。你确认有这个空格吗?如果有的话,去掉这个空格再试试...

撰写回答