The database & web server

  1. Install MySQL by running aptitude install mysql-server mysql-client. During this step, you’ll be asked a number of questions. Pay close attention to them – they’re important!
  2. Install Apache2 by running aptitude install apache2 or aptitude install apache2 apache2-doc if you also want the documentation.
  3. Install PHP by running aptitude install php5 libapache2-mod-php5.
  4. Install PHP/MySQL integration by running aptitude install libapache2-mod-auth-mysql php5-mysql
  5. If you need phpMyAdmin, install it by running aptitude install phpmyadmin. There are also a number of important questions to answer during this process.
  6. Optionally, enable mod_rewrite so you can make use of .htaccess files. Do this by running a2enmod rewrite – trust me, you’ll need it sooner than you think (e.g. if you install CMS packages like WordPress, CodeIgniter, ExpressionEngine, etc).
  7. Restart Apache by running service apache2 restart or, if you’re hardcore, /etc/init.d/apache2 restart – they do the same thing. Ok, I was joking aboutu the hardcore part, but the commands do exactly the same thing.

At this point you should be able to fire up a web browser and, assuming you don’t have a network configuration that blocks the required ports, browse to http:///. If it’s all working, you should see the good old “It works!” message appear.

If you want to test PHP is working, create a file called phpinfo.php (or whatever) in the web server root directory (/var/www/ by default) and putting the following in it:

 <?php phpinfo(); ?> 

You can then browse to http:///phpinfo.php and see a ton of useful info about your PHP configuration.

Multiple sites?

The above basic guide will install Apache2 and prepare it for multiple virtual sites without you having to do anything. If you need more than one site on port 80 of your new server, make sure you do the following first:

  1. Configure DNS or edit /etc/hosts to make sure name resolution works. Don’t forget to also do this on the machines you’ll be browsing from.
  2. For now, assume you’re creating a site accessible by http://test.local. Create a file called test.local in /etc/apache2/sites-available/ and put the following in it: DocumentRoot “/var/www/test.local/” ServerName test.local DirectoryIndex index.php

  3. Create a directory called /var/www/test.local/
  4. Create a file in that new directory called index.php (i.e. /var/www/test.local/index.php)
  5. Add a single line to the new file:

     <?php phpinfo(); ?> 

    • Enable the new site by running a2ensite test.local. You should see something similar to the following:

Enabling site test.local. Run ‘/etc/init.d/apache2 reload’ to activate new configuration!

  1. Do as it says and reload the Apache configuration by running /etc/init.d/apache2 reload (or service apache2 reload, if you prefer).

If your DNS/name resolution is all OK, you should now be able to browse to http://test.local and see all that lovely info about your PHP configuration.

Unless you need it, I also disable the default website at this point by running a2dissite default. If you still need it running for some reason, it’s a good idea, in my opinion, to move it out of /var/www and into, say, /var/www/default. Don’t forget to update /etc/apache2/sites-available/default if you do this.

Question: What’s in /etc/apache2/sites-enabled?
Answer: Nothing you need to mess with manually. Running the a2ensite and a2dissite commands simply create symbolic links from the configuration files in /etc/apache2/sites-available/ to files with the same name in /etc/apache2/sites-enabled/. These configuration files are dynamically loaded when Apache2 starts – if you don’t believe me, take a look at /etc/apache2/apache2.conf and look for the following text:

 # Include the virtual host configurations: Include sites-enabled/ 

See? Easy. Please feel free to ask questions, if you like. 🙂