Cannot run cgi, show plain text only (Ubuntu 13.10 Apache 2.4)

前端 未结 6 1260
盖世英雄少女心
盖世英雄少女心 2021-02-07 10:56

I just install Ubuntu 13.10 and I am trying to install Apache. But when I tried to run a perl file in cgi-bin, the browser showed only plain text.

My default.conf of Apa

相关标签:
6条回答
  • 2021-02-07 11:34

    Make sure apache cgi mode is enable

    sudo a2enmod cgi   //will enable the cgi mode
    sudo a2dismod cgi  //Will disable the cgi mode
    

    Keep your all files under the webroot "cgi-bin" folder

    sudo mkdir /home/www/cgi-bin
    

    Make sure that the file permission to the .cgi file is okay

    sudo chmod 755 yourFile.cgi
    

    Try executing it through terminal

    perl /Path_To_The_File/fileName.cgi
    

    Ensure that your fileName.cgi contains bellow code at top of the file

    #!/usr/bin/perl -w
    
    print "Content-type: text/html\n\n";
    

    If all above step is working well then

    Create virtual host in apache for your perl project

    cp /etc/apache2/sites-available/000-default.conf ./your_project.conf
    sudo nano /etc/apache2/sites-available/your_project.conf
    

    Inside your your_project.conf file, replace these lines

    <VirtualHost *:80>
        ServerAdmin xyz@gmail.com
        DocumentRoot /var/www/html/cgi-bin          //Define where your project is
        ServerName your_project.com                 //URL through which you want to access
        ServerAlias your_project.com
    
    
        ErrorLog ${APACHE_LOG_DIR}/error.log        //Error for the project will store in this file
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    
        ScriptAlias /cgi-bin/ "/var/www/html/cgi-bin/"  //particular directory is set aside for CGI programs
    
        <Directory /var/www/html/cgi-bin/>
        ##TO consider the files as cgi which has .cgi and .pl extension
        Options +ExecCGI
        AddHandler cgi-script .cgi .pl
    
        ##To consider all files as cgi file  (if want to use remove # from last two line add in above 2 line)
        #Options ExecCGI
        #SetHandler cgi-script
        </Directory>
    
    </VirtualHost>
    

    Run these commands

    sudo a2ensite your_project.conf      //Will enable your configuration file
    sudo service apache2 restart         //Restarting apache2
    

    Add your host

    sudo nano /etc/hosts
    //add this line
    127.0.0.1   your_project.com
    

    Now run execute your cgi file

    your_project.com/cgi-bin/fileName.cgi
    
    0 讨论(0)
  • 2021-02-07 11:37

    Try with this command:

    sudo a2enmod cgi
    

    Then restart apache!

    0 讨论(0)
  • 2021-02-07 11:40

    Steps to install Apache on Ubuntu 13.10:

    • sudo apt-get install apache2
    • sudo service apache2 start

    Steps to test Apache on Ubuntu 13.10:

    • Visit http://your-host-here.com/ (or whatever your host is configured as).
    • See the "It works" web page.

    Steps to use CGI programs under Apache on Ubuntu 13.10:

    • None. CGI is configured out of the box.

    Steps to test CGI programs under Apache on Ubuntu 13.10:

    • Edit a file in /usr/lib/cgi-bin/ (e.g. /usr/lib/cgi-bin/test)
    • Add some Perl code (e.g. your code from above)
    • sudo chmod +x /usr/lib/cgi-bin/test
    • Visit http://your-host-here.com/cgi-bin/test

    Note: "your-host-here.com" might well be "localhost", but SO won't let me use that in a URL :-/

    0 讨论(0)
  • 2021-02-07 11:43

    Expanding on the answer from @tops.

    Try sudo a2enmod cgi, if you have already tried following a bunch of tutorials like Apache Tutorial: Dynamic Content with CGI, Ubuntu HTTDP or How to Install Apache2 webserver with PHP, CGI, and Perl Support in Ubuntu Server, and still cannot figure out what is missing from them.

    Then restart apache!

    Which may be done with:

    sudo /etc/init.d/apache2 restart
    sudo apache2ctl restart
    sudo service apache2 restart <- try this first

    This worked for me on Ubuntu 13.10.


    New commands to users coming from RedHad based distributions:

    • Modules
      • a2enmod
      • a2dismod
    • Configurations
      • a2enconf
      • a2disconf
    • Virtual Sites
      • a2ensite
      • a2dissite

    Remember, the main configuration file is /etc/apache2/apache2.conf by default, and individual configuration components for modules and websites are in separate files.


    EDIT: Expanded with details as to why people coming to this page may be having difficulties with enabling Apache CGI on Ubuntu.

    0 讨论(0)
  • 2021-02-07 11:48

    I encountered this problem when trying to setup bugzilla in Ubuntu 14.04 @Andrew's answer was helful and so was @Kevin's links.. so other than enabling cgi, ensure that perl's module for apache2 is installed. You can do this by:

    sudo apt-get install libapache2-mod-perl2
    

    This will automatically enable the module as well as restart apache server. If not do that manually.

    Don't have enough rep to upvote both of you, so thanks.

    0 讨论(0)
  • 2021-02-07 11:49

    I tried to reproduce this issue and this is what i learned: In /etc/apache2/sites-available you either have multiple sites setup or are using the default. Either way there is a .... shows the path to my DocuumentRoot (wherer my .html files live). The second .... shows the path to my .cgi , .pl , .perl files:

    <VirtualHost *:8080>
        ServerName casey.local
        **DocumentRoot /home/casey/work/htdocs/casey.local**
        <Directory />
          Options FollowSymlinks
          AllowOverride All
        </Directory>
    
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 
        **<Directory "/usr/lib/cgi-bin">**
          AllowOverride None
          Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
          Order allow,deny
          Allow from all
        </Directory>
    

    0 讨论(0)
提交回复
热议问题