Set up a LAMP Web Server on Ubuntu

Update (06-May-08): Updated for Ubuntu 8.04.

This is a quick guide or refresher for setting up a basic LAMP sever on Ubuntu/Xubuntu 7.10/8.04.

1. Basic Installation

What Packages Required Configuration / Remarks
GNU/Linux Refer to my GNU/Linux installation notes for installing Ubuntu
Apache apache2 Settings: /etc/apache2/
Loggings: /var/log/apache2/
MySQL mysql-server libapache2-mod-auth-mysql php5-mysql Settings: /etc/mysql/
Loggings: /var/log/mysql/
PHP php5 Settings: /etc/php5/
Errors: Displayed by default; For production server, set ini_set('display_errors', false); ini_set('error_log', 'log_filename');
PhpMyAdmin phpmyadmin Access via http://localhost/phpmyadmin/
MySQL Administrator mysql-admin Access via $ mysql-admin

2. Post-Installation Setup

2.1. Add a New Site

Copy the settings for the default site:

$ sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mysite
$ sudo vi /etc/apache2/sites-available/mysite

Modify the file mysite:

  • Change two occurences of '/var/www' to '/path_to_mysite'.
  • In the '<Directory /path_to_mysite/>' section, you may want to use 'AllowOverride All' instead to allow the directives specified in .htaccess (default filename).

By default the apache user is 'www-data' and the group is 'www-data'. Make sure that the directory is accessible by apache2. Optionally, change 'ErrorLog' and 'CustomLog' for the site if you want separate logs.

To disable the default site and use the new site:

$ sudo a2dissite default
$ sudo a2ensite mysite
$ sudo /etc/init.d/apache2 reload

If you get the error 'apache2: Could not reliably determine the server's fully qualified domain name, using x.x.x.x for ServerName' here, create a file /etc/apache2/conf.d/servername with a line 'ServerName localhost' and reload the configuration. Access the new site via http://localhost/.

You can also create mappings from URLs to filesystem locations with the Alias directive (provided by the mod_alias module). For example, create a file under /etc/apache2/sites-available/ with content similar to this:

Alias /url_dir "/filesystem_dir"
<Directory "/filesystem_dir">
  Options Indexes FollowSymLinks
  DirectoryIndex index.php
  AllowOverride None
  Order allow,deny
  Allow from all
</Directory>

2.2. Apache Modules

Enable and disable apache modules with a2enmod and a2dismod.

2.3. Reset MySQL Root Password

In case you forget your MySQL root password, first check what package mysql-server depends on:

$ dpkg-query -s mysql-server | grep Depend
Depends: mysql-server-x.y

Then, reconfigure that package and you will be prompted to enter the new root password:

$ sudo dpkg-reconfigure mysql-server-x.y

3. References