Ubuntu: Apache/PHP/MySQL in 5 minutes

I use Ubuntu on my laptop and thus need a working environment for web development. This means the usual suspects: Apache, PHP and MySQL with some common extras.

Here's all you need to do to get your Ubuntu system ready for Drupal, Wordpress, etc. I used Ubuntu 10.10 (Maverick), but everything should be about the same in any recent version.

First, install Apache, PHP and MySQL. This will also install a bunch of dependencies: $ sudo apt-get install apache2 php5 php5-cli libapache2-mod-php5 mysql-server php5-mysql Apache and MySQL are started automatically, and the PHP5 module is enabled in Apache's configuration (if it's not, do sudo a2enmod php5). Since the latter happened after Apache was started, you need to restart it:

$ sudo /etc/init.d/apache2 restart

You now have a working environment up and running with the web root in /var/www, but read on for the extras I normally install and how to add a quick virtual host.

Some useful extras

Many programs use ImageMagick or the fork GraphicsMagick for various image operations (resizing, cropping, etc.). GraphicsMagick has better performance. They can be installed alongside each other. GD is also popular, so let's install that too.

# sudo apt-get install graphicsmagick imagemagick php5-gd

Set up a local virtual host

Most of my sites have dedicated domains or subdomains, so I want the same when developing.

In the directory /etc/apache2/sites-enabled, create a new file, myhost.net, with the following: (replace myhost.net and the paths with whatever you want)

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName myhost.net
        DocumentRoot /home/fooninja/projects/myhost.net
        <Directory /home/fooninja/projects/myhost.net/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/myhost.net_error.log
        CustomLog ${APACHE_LOG_DIR}/myhost.net_access.log combined
</VirtualHost>

The ErrorLog and CustomLog paths have been changed in recent versions of Ubuntu. See default for reference. AllowOverride All might not be optimal for security, but this is just a development box.

Next, activate the configuration and reload Apache:

$ sudo a2ensite myhost.net
$ sudo /etc/init.d/apache2 reload

The first line just creates a symlink from /dev/apache2/sites-available/myhost.net to /etc/apache2/sites-enabled/myhost.net, which is the directory Apache is configured to look in.

If the hostname for your virtual host doesn't exist, or if it exists but points to somewhere other than your local computer, edit /etc/hosts and add 127.0.0.1 myhost.net:

echo '127.0.0.1 myhost.net' >> /etc/hosts

PHP configuration

In /etc/php5/apache2/php.ini I usually change at least some values, like display_errors (set to On to see error messages directly, otherwise you'll have to look in the log file), post_max_size and upload_max_filesize.

2010-10-08 · · · ·

blog comments powered by Disqus