15

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.

user3026793
  • 151
  • 1
  • 1
  • 3

6 Answers6

45

Try with this command:

sudo a2enmod cgi

Then restart apache!

Javier C.
  • 7,859
  • 5
  • 41
  • 53
tops
  • 451
  • 3
  • 2
  • 5
    If I could give this answer 10 points, I would! The exact same configuration worked under Ubuntu 12.04, but not under Ubuntu 13.10. This command made it work under 13.10. – atmelino Apr 03 '14 at 04:34
  • 1
    @Kevin You should not use edits to drastically change the content of someone else's answer. You may post that as a new answer if you wish. – nobody May 23 '14 at 14:06
  • @tops - you legend! I've just spent the last 2 hours trying to get a reverse proxy pass working from nginx -> apache2, and this was what was stopping me! – Andrew Newby Mar 25 '17 at 10:04
  • Also, In your add *Options +ExecCGI* AddHandler cgi-script .cgi .pl Also seen here: https://stackoverflow.com/a/28808878/1130803 – Meetai.com Feb 23 '20 at 21:09
7

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.

kaykae
  • 721
  • 7
  • 8
6

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.

Community
  • 1
  • 1
Kevin
  • 2,234
  • 2
  • 21
  • 26
  • For anyone not coming here from Ubuntu/Debian: what this means is that the CGI module must be enabled. Look in http.conf and uncomment or add the following: `LoadModule cgi_module modules/mod_cgi.so` – starfry Mar 03 '15 at 16:52
  • @starfry, Yeah, the basic idea is that installation does not necessarily mean activation/enabling in all situations. – Kevin Mar 03 '15 at 17:08
0

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:

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

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • 1
    looks pretty much like he did most of the stuff - how should that reply help? – Henning May 05 '14 at 13:25
  • Well, because often following a list of instructions like that is a good way to work out which step you have omitted or got wrong. – Dave Cross May 06 '14 at 06:32
  • I think in most cases after having gone through a long list of instructions doesn't help, especially when you did most of them already, then you only need to know which bit is missing. Here, you tell a lot of stuff he did already, but say the most important things is not necesary - because you very well need to activate cgo mod explicitly - the following reply is the one that helps! – Henning May 07 '14 at 16:47
  • Then it seems that our approaches to problem-solving differ. There's room for all approaches though :-) I don't agree that I missed out anything important. I came up with this list by starting with a fresh Ubuntu installation and working through the minimum set of steps. If I omitted anything, that's because it's unnecessary. Following these instructions will work. – Dave Cross May 07 '14 at 20:05
  • It's waste of time to explain again. read the simple, short answer that has been upvoted 10 times in this thread to understand, or forget it. – Henning May 11 '14 at 08:34
  • I really don't understand why this bothers you so much. I thought my answer would be useful, so I wrote it. If you don't agree, then that's fine too. Just ignore it and move on to another question. – Dave Cross May 11 '14 at 10:29
  • As simple as that: Your text is partly wrong and partly superfluous according to the question asked, and that way is costing me, and others precious time when looking for a solution to the exact problem the original poster asked, and I strongly suggest, you just remove it. That's the point. – Henning May 11 '14 at 20:17
0

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
bikash.bilz
  • 821
  • 1
  • 13
  • 33
-1

Retracting my pervious answer, from tread of comments I learned that restarting apache was the answer.