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

那年仲夏 提交于 2021-02-06 10:06:58

问题


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 Apache is below:

    AddHandler cgi-script .cgi .pl
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +Indexes +ExecCGI +MultiViews +SymLinksIfOwnerMatch
            #Order allow,deny
            Require all granted
            Allow from all
    </Directory>

This is my perl cgi file:

    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    print "<html>\n";
    print "<title> PERL CGI</title>\n";
    print "<body>";
    print "hello perl-cgi!!!!!!!";
    print "</body>";
    print "</html>\n";

I have turned on the Handler in mime.conf The file is in /usr/lib/cgi-bin, and I run it as http not file:///. I have also installed mod_perl. I am new to Apache. I have searched for several hours, try endless Apache configurations, read Apache document but still cannot solve it. Is it because the "+ExecCGI" code has some problem? I saw another person also meet similar problem after updating Ubuntu 12.04 to 13.10. Maybe Ubuntu is the problem? Please help. The Apache configuration and Ubuntu permission almost drive me mad.


回答1:


Try with this command:

sudo a2enmod cgi

Then restart apache!




回答2:


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.




回答3:


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.




回答4:


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



回答5:


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 :-/




回答6:


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>



来源:https://stackoverflow.com/questions/20172518/cannot-run-cgi-show-plain-text-only-ubuntu-13-10-apache-2-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!