[英]cgi script doesn't render properly in browser
我是python網站開發的新手。 因此,我很快就在ubuntu 14.04 OS中完成了用於運行cgi腳本的所有設置。 我將hello.py
保留在/var/www/
但是當我在瀏覽器中運行該文件時,它的外觀卻不符合預期。
這是/etc/apache2/sites-available/
000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .psp
PythonHandler mod_python.psp
PythonDebug On
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
這是啟用的mod:
Hello.py文件:
#!/usr/bin/python
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>'
如@Skywrath所示,您確實需要在任何內容之前打印標題。 除此之外,在/ var / www /的.htaccess文件中為python文件添加CGI處理程序,如下所示:
AddHandler cgi-script .cgi .py
Options +ExecCGI
您需要通過在HTML代碼頂部(甚至在之前)添加以下行來指示此頁面是純HTML文件。 例如:
print 'Content-type: text/html'
print #An empty line is needed between the previous line and your HTML code
print '<html>'
...
print '</html>'
您應該考慮將腳本移入CGI目錄而不是文檔根目錄,並使用ScriptAlias在Apache配置文件中進行指定。
然后,您可能會改為通過localhost / cgi-bin / hello.py訪問該腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.