How to Install Apache2 webserver with PHP,CGI and Perl Support in Ubuntu Server

Sponsored Link
The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for modern operating systems including UNIX and Windows NT. The goal of this project is to provide a secure, efficient and extensible server that provides HTTP services in sync with the current HTTP standards.Apache v2 is the next generation of the omnipresent Apache web server. This version -- a total rewrite -- introduces many new improvements, such as threading, a new API, IPv6 support, request/response filtering, and more.

Install Apache2 in Ubuntu

sudo aptitude install apache2

This will complete the installation.

After installation Type the server’s IP address (or alias if you added the server to your /etc/hosts file) in your browser’s address bar or, if you are browsing on the server itself, type or localhost. If an error occurs, then you will have to edit the apache2.conf file to ensure that Apache can fully resolve the server’s name.If you have any problem then you have to edit the apache2 configuration file using the following command

sudo nano /etc/apache2/apache2.conf

Add the following line somewhere

ServerName localhost


ServerName yourserverip

Save and exit the file

Now you need to  restart Apache server using the following command.

sudo apache2ctl restart

Change default document root in Apache2

The main configuration file located at /etc/apache2/apche2.conf.If you want to change the default document root you need to edit the /etc/apache2/sites-available/default file and look for this line “DocumentRoot /var/www/” here you can change where ever you want to change.For example if you want to change /home/www the above line looks like this “DocumentRoot /home/www/”.

Save and exit the file

Now you need to  restart Apache server using the following command.

sudo apache2ctl restart

Enable PHP support for apache2 webserver

If you want to enable php5 or php4 support to your apache webserver use the following commands to install require packages

For PHP5

sudo aptitiude install php5 libapache2-mod-php5

For PHP4

sudo aptitiude install php4 libapache2-mod-php4

You also make sure the php5 and php4 modules are enabled using the following commands

sudo a2enmod php5

sudo a2enmod php4

After installing php support you need to restart apache webserver using the following command

sudo apache2ctl restart

Test your PHP Support foe apache webserver

To check the status of your PHP installation

sudo nano /var/www/testphp.php

and insert the following line

<?php phpinfo(); ?>

Save and exit the file

Now open web browser at http://yourserveripaddress/testphp.php and check.

Enable CGI and perl support for apache2 server

You need to install the following package

sudo aptitude install libapache2-mod-perl2

Configure a cgi-bin directory

You need to create a cgi-bin directory using the following command

sudo mkdir /home/www/cgi-bin

Configuring Apache to allow CGI program execution is pretty easy. Create a directory to be used for CGI programs and add the following to the site configuration file (again between the <VirtualHost> tags).

ScriptAlias /cgi-bin/ /home/www/cgi-bin/

<Directory /home/www/cgi-bin/>
Options ExecCGI
AddHandler cgi-script cgi pl

The first line creates an alias that points to the directory in which CGI scripts are stored. The final line tells Apache that only files that end with the *.cgi and *.pl extensions should be considered CGI programs and executed.

Test your Perl Program

cd /home/www/cgi-bin

sudo nano

Copy and paste the following section save and exit the file.


#!/usr/bin/perl -w
print "Content-type: text/html\r\n\r\n";
print "Hello there!<br />\nJust testing .<br />\n";

for ($i=0; $i<10; $i++)
print $i."<br />";


make sure you change permissions on it

sudo chmod a+x

Now open your web browser open http://yourserverip/cgi-bin/ should be working.

Sponsored Link

Related posts

46 thoughts on “How to Install Apache2 webserver with PHP,CGI and Perl Support in Ubuntu Server

  1. Hi.

    I see that makin programs for cgi-bin eather is a php script with .php extention og perl with .pl

    Is it possible to make programs without any exstention?

    …. if …. howto?

  2. On Ubuntu 12.04, it wasn’t working for no apparent reason. Everything seemed set up correctly. So I ran:

    aptitude reinstall libapache2-mod-php5 php5-common
    /etc/init.d/apache2 restart

    And now it works.

  3. /etc/apache2/sites-available/default
    gksudo gedit /etc/apache2/sites-available/default
    to edit the file.

  4. hey…. im having trouble with the location of /cgi-bin/
    permission denied.
    and cant run perl file too…. help please?

  5. Hey i was trying the same on myserver IP ,

    when i tried to install
    sudo pt-get install libapache2-mod-perl2

    it show error message :Unable to locate package libapache2-mod-perl2

    please suggest

  6. This is for the Perl CGI PL

    I did everything exactly what the article said, but when i execute it at localhost/cgi-bin/ firefox starts to prompt with a download box. It is not executing it but it is downloading. Why is it downloading, i already restarted my PC, configured my apache2.conf @ /etc/apache2/, Why firefox is downloading it? Please respond Thanks.

  7. I follow your step But error has occur. any one help me .error is -> The requested URL /cgi-bin/ was not found on this server.

  8. after googling…
    you have to execute “sudo a2enmod cgi” to configure apache2 and module cgi
    and “sudo apache2ctl restart”
    so you don’t need to insert “ScriptAlias ……” and “<Directory….."
    only you have to use /usr/lib/cgi-bin/ and save here your perl script.
    at last in your script you have to write ONLY:
    #!/usr/bin/perl -w

    print "Content-type: text/html\r\n\r\n";
    print "Hello there!\nJust testing .\n”;
    for ($i=0; $i<10; $i++)
    print $i."”;

    remember to leave a blank row after #!/usr….
    and it will work!!
    (tested on ubuntu 14.04 for raspberry p2 mod B )

Leave a comment

Your email address will not be published. Required fields are marked *