浏览器尝试下载.pl文件而不是执行它 - apache2,perl cgi脚本,ubuntu

时间:2016-07-27 08:10:26

标签: linux apache perl ubuntu cgi

我的ubuntu版本是15.04。 我按照配置服务器的步骤进行了操作:

  1. sudo apt-get install apache2
  2. sudo apt-get install libapache2-mod-perl2
  3. sudo apt-get install perl-debug
  4. sudo apt-get install libapache2-mod-perl2-dev
  5. sudo apt-get install libapache2-request-perl libdatetime-perl
  6. mkdir /var/www/cgi-bin
  7. nano perltest.pl

    #!/usr/bin/perl -w
    print "Content-type: test/html\r\n\r\n";
    print "Hello there!<br/>\nJust Testing. <br/>\n";
    
    for ($i=0; $i<10;$i++)
    {
    print $i."<br/>";
    }
    
  8. chmod a+x perltest.pl

  9. 修改/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/
    
    # 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
    
    
    < Files ~ "\.(pl|cgi)$">
    SetHandler perl-script
    PerlResponseHandler ModPerl::PerlRun
    Options +ExecCGI
    PerlSendHeader On
    < /Files>
    
    
    ScriptAlias /cgi-bin/ /var/www/cgi-bin/
    
    < Directory /var/www/cgi-bin/>
    AllowOverride None
    Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
    AddHandler cgi-script cgi pl
    Order allow,deny
    allow from all
     </Directory>
    
    
    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
    
  10. /etc/apache2/sites-available/default-ssl.conf档案为here(第5-11行)

  11. /etc/apache2/sites-enabled/000-default.conf文件为here(第12-37行)
  12. 向纳米/etc/apache2/apache2.conf文件添加行here(行 - (54-78))
  13. 在浏览器中,点击http://127.0.0.1/cgi-bin/perltest.pl
  14. 但是,浏览器会下载文件。 我无法理解这些问题。请帮忙。

1 个答案:

答案 0 :(得分:0)

 print "Content-type: test/html\r\n\r\n";
                        ^
                      Here is your problem

应为print "Content-type: text/html\r\n\r\n";

转到root并通过以下命令重新启动计算机中的apache服务器

/etc/init.d/apache2 restart