Setting up Linux, Apache,
MySQL and PHP (LAMP)

Russell Bateman
28 August 2010
last update:

Introduction

Setting up a Linux server to run the Apache web server, MySQL database and PHP in support of web pages with access to data and other dynamic state (in other words, no longer merely static web pages) is very simple today on Ubuntu.

I chose to install Ubuntu Lucid Lynx desktop because, though I wanted an Apache server and the other components, I also still wanted to use my box to do development work.

As a prerequisite to reading this document, I expect you to be generally aware of Linux practices (only at the user level) including how to work menus, a terminal window, that your user's initial working directory is at /home/username, etc.

Table of Contents

Introduction
Linux
Installing [ ]AMP
Launch browser
Install vim
Set up to use ~/public_html
Adding additional domains
Test

Linux

Install Linux. I'm sorry, I don't show the steps to installing Ubuntu Lucid Lynx here. I assume you've already done that. If you're using a different version of Ubuntu, or even a different Linux, some or all of this may be helpful.

If you'd like to do all of this on a more trial basis before rolling it out on physical hardware, consider installing Sun VirtualBox, use that to install a copy of Lucid, then modify away.

Note: I installed GNOME as my desktop.

Installing [ ]AMP

Launch the Synaptic Package Manager by pulling down the System -> Administration -> Synaptic Package Manager menu. You'll be searching for and installing a few packages.

  1. Click Search (don't use the Quick Search feature).
  2. Type "PHP5".
  3. Double-click and add each of the following in turn:

PHP

MySQL

Search for "MySQL", locate and double-click the following package:

Apache web server

(Everything should be there already.)


Last, you should add

Download and install packages

Click Apply and wait for the packages to be installed. During the installation of MySQL, you'll need to supply the MySQL server's root password. Dismiss Synaptic Package Manager.

Launch browser

At this point, you're already serving up pages. If you're on Ubuntu (what this page is about), the page you're serving up if you type

http://localhost/

...is on the path /var/www/index.html. This is useful. Let's say you want to publish some documents internally and you're sitting behind a firewall. Your IP address is 192.168.109. You can put those on the path /var/www/ and your colleagues will be able to see them by entering

http://192.168.1.109/

They'll see your index page.

Updating your web fodder

Of course, everything at that path belongs to root root. There are many solutions, but the easiest in my mind is to change the group ownership of www to admin (your user is in the admin group, right?) and the permissions on that directory too:

russ@tuonela:~> sudo bash Password: root@tuonela:/home/russ> cd /var root@tuonela:/var> ll total 10 drwxr-xr-x 2 root root 680 2011-01-13 08:39 backups drwxr-xr-x 19 root root 528 2011-01-13 09:17 cache drwxr-xr-x 2 root root 3704 2010-10-07 10:13 games drwxr-xr-x 65 root root 1776 2011-01-13 09:17 lib drwxrwsr-x 2 root staff 48 2010-10-07 03:15 local drwxrwxrwt 3 root root 60 2011-01-13 09:18 lock drwxr-xr-x 17 root root 3120 2011-01-13 09:17 log drwxrwsr-x 2 root mail 48 2010-10-07 09:56 mail drwxr-xr-x 2 root root 48 2010-10-07 09:56 opt drwxr-xr-x 17 root root 700 2011-01-13 09:18 run drwxr-xr-x 7 root root 200 2010-10-07 10:05 spool drwxrwxrwt 2 root root 48 2011-01-10 11:15 tmp drwxr-xr-x 5 root root 896 2011-01-13 09:50 www root@tuonela:/var> chgrp admin www ; ll -d www drwxr-xr-x 5 root admin 896 2011-01-13 09:50 www root@tuonela:/var> chmod g+w ; ll -d www drwxrxxr-x 5 root admin 896 2011-01-13 09:50 www

Now you can edit HTML files and copy them there for publication to your heart's contentment.

As part of a larger solution, however, you might wish to eschew this approach for a different one based on a subdirect, public_html, local to your user. Read about that below.

Install vim

Install vim as your text editor by:

russ@taliesin:~> aptitude install vim-gnome

This will be useful for changing configuration files and other work over the next steps.

Set up to use ~/public_html

Normally, web content on Ubuntu is set up to be rooted at the path /var/www. You'll find an index.html at that location which you can see if you open a browser and type http://localhost as the address.

A more convenient method of web content placement is to put everything under a subdirectory of each user. This makes things easy for supporting diverse domains via the users to whom they belong, e.g.: acme.com's content is set up on the path /home/acme/public_html. Here, my username is russ.

Get root and change your current working directory to /etc/apache2/mods-available. Issue the command to enable the Apache2 module for user directories:

russ@taliesin:~> sudo bash root@taliesin:/home/russ> cd /etc/apache2/mods-available root@taliesin:/etc/apache2/mods-available> a2enmod userdir

While in this same subdirectory, edit php5.conf to fix a setting that removes the possibility of keeping web content under user home directories. Comment out the following lines:

<IfModule mod_userdir> <Directory /home/*/public_html> php_admin_value engine Off </Directory> </IfModule>

Adding additional domains—a parenthesis

If you're hosting a domain, for example, acme.com, on this hardware, you'll need to verify the presence of one setting and add a <VirtualHost> paragraph in support of that domain. First, ensure that /etc/apache2/ports.conf contains the following line:

NameVirtualHost *:80

(This assumes your Apache HTTP server is going to accept traffic over port 80.)

Next, add the following paragraph to a file whose name may be anything in /etc/apache2/sites-available. In our case, I suggest that you name the file acme or acme_com.

<VirtualHost *:80> ServerName www.acme.com ServerAlias acme.com *.acme.com DocumentRoot /home/acme/public_html/ </VirtualHost>

Then, go create a link in /etc/apache2/sites-enabled to that file and bounce Apache.

root@taliesin:/etc/apache2/sites-enabled> ln -s ../sites-available/acme root@taliesin:/etc/apache2/sites-enabled> /etc/init.d/apache2 restart * Restarting web server apache2 ... waiting [ OK ]

At this point, assuming acme.com's DNS settings are correct, you should be able to type http://www.acme.com in a browser to see the site page.

Here are some links on this topic:

Test installation

In your home directory, under public_html, add the following in a new file index.php:

<?php phpinfo();

Thereafter, typing the following address into the browser...

http://localhost/~russ/

...will get you the PHP information page.

You can also create a little PHP server that echoes back whatever it receives:

<?php

    print_r($_GET);

    echo "<pre>\n";
    $html = "<doc>\n";
    foreach( $_GET as $var => $value )
    {
        $html .= "  <$var>$value</$var>\n";
    }
    $html .= "</doc>";
    echo htmlspecialchars( $html );
    echo "</pre>\n";

Type this into the browser address line:

http://localhost/~russ/server.php?user=russ&password=moron

...and the resulting page will display something like:

Array ( [user] => russ [password] => moron ) <doc> <user>russ</user> <password>moron</password> </doc>