by

Configure Apache, mySQL, and cgi-bin on an Ubuntu LAMP Server

In a previous post, I shared step-by-step instructions for setting up a Ubuntu LAMP (Linux, Apache, mySQL, PHP) server on your local area network (LAN). I also showed how you could customize the server installation to include all of the niceties of Ubuntu’s Desktop GUI. If this installation was successful, you should be able to view the default site served by Apache (actually an index of the default site) using any machine on your LAN. 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 127.0.0.1 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. Out of good measure you should do this anyway, even if everything seems to be working. Use your favorite text editing program (I use vi here) and open the file.

sudo vi /etc/apache2/apache2.conf

Add the following line somewhere:

ServerName localhost

and restart Apache.

sudo /etc/init.d/apache2 restart

If the default site loads then you are ready to configure Apache to serve your own sites. This configuration process may not be familiar to you if someone else hosts your websites. Here I walk you through the necessary steps. Additionally, I show you how to install phpMyAdmin to administer mySQL databases and how to configure a cgi-bin directory to run CGI programs.

Step 1: Create a new Apache site

To create a new site, first create a directory where you want to keep files associated with this site. I like to keep sites in my home directory so (using my user name of jeff) I create a /home/jeff/public_html directory. The configuration files for all sites available to Apache are stored in the /etc/apache2/sites-available/ directory. Create a configuration file for your new site (here called fiz-ix) by copying the configuration file for the default site.

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/fiz-ix

Open the new configuration file.

sudo vi /etc/apache2/sites-available/fiz-ix

You will notice that there is a lot of stuff between <VirtualHost> tags. You have to edit some of this. Change the DocumentRoot path to point to the newly created site directory.

DocumentRoot /home/jeff/public_html

In a similar fashion, change one of the Directory directives (it looks something like this)

<Directory /var/www/>

to be consistent with your site path.

<Directory /home/jeff/public_html/>

After making these changes, disable the default site using the Apache disable site script.

sudo a2dissite default 

Next, use the Apache enable site script to activate the new site.

sudo a2ensite fiz-ix

Finally, restart Apache.

sudo /etc/init.d/apache2 restart

Create a simple index.html file and save it in /home/jeff/public_html/ to test your new site. Something simple like

<html>
<strong>Fiz-ix is Fun</strong>

Browse (preferable on another machine) to your new site to test the configuration.

Step 2: Install phpmyadmin and create root mySQL user

In order to make mySQL database administration easy, many hosts provide phpMyAdmin. You can easily install this tool on your Ubuntu LAMP server too using the built in package management software. At the command prompt type

sudo apt-get update
sudo apt-get install phpmyadmin

You may have to insert the Ubuntu install CD to complete this installation. During the installation the script will ask which version of Apache to automatically configure. If you installed apache as per this previous post then select apache2 (with space bar) and continue. After the install completes, the phpMyAdmin script should be in the /usr/share/directory and (after restarting Apache; see above) it should resolve in your browser when you type localhost/phpmyadmin (or xxx.xxx.xxx.xxx/phpmyadmin). If it doesn’t, you must add the following alias and include to the bottom of the apache configuration file apache2.conf in /etc/apache2/.

Include /etc/phpmyadmin/apache.conf
Alias /phpmyadmin /usr/share/phpmyadmin

You can log in by using root as your username and the root password configured during the mySQL installation (if you decided not to enter a mySQL password or were not prompted for a password then the password field is blank by default). Once logged on to phpMyAdmin, you can set a new root password by navigating to the privileges page, clicking the icon that looks like a pencil next to each root account (there may be more than one), and entering a password in the appropriate field of the page that loads.

Step 3: Configure a cgi-bin directory

Configuring Apache to allow CGI program execution is pretty easy. Create a directory to be used for CGI programs (here I create /home/jeff/public_html/cgi-bin) and add the following to the site configuration file (again between the <VirtualHost> tags).

ScriptAlias /cgi-bin/ /home/jeff/public_html/cgi-bin/
<Directory /home/jeff/public_html/cgi-bin/>
          Options ExecCGI
          AddHandler cgi-script cgi pl
</Directory>

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.

Congratulations! Your new site is configured. It is worth mentioning that if you are going to be granting computers on the wide area network access to your new Ubuntu LAMP server, you should take some time to learn how to properly secure Apache. Check out these other sites for more information on configuring Apache under Ubuntu. Ubuntu Community, Apache2.2 Documentation

Leave a Reply

Your email address will not be published.