Quick and dirty: set up a local PHP and Ruby development environment on Ubuntu Hardy Heron (8.04)

This is a quick and dirty tutorial how to set up a local development stack on a fresh Ubuntu Hardy Heron (8.04) install.

Here is what we will install:

  • Apache 2.2
  • PHP 5 as apache module
  • Ruby 1.8 as apache module & Rails 2.0.2
  • MySQL 5
  • phpMyAdmin

Software

First of all, let’s install the needed packages (note: I took the package lists from HowToForge’s The Perfect Server – Ubuntu Hardy Heron (Ubuntu 8.04 LTS Server)):

MySQL 5

$ sudo apt-get install mysql-server mysql-client libmysqlclient15-dev

Apache 2

$ sudo apt-get install apache2 apache2-doc apache2-mpm-prefork apache2-utils libexpat1 ssl-cert

PHP 5 as apache module including extensions

$ sudo apt-get install libapache2-mod-php5 php5 php5-common php5-curl php5-dev php5-gd php5-idn php-pear php5-imagick php5-imap php5-json php5-mcrypt php5-memcache php5-mhash php5-ming php5-mysql php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Ruby & Rails

$ sudo apt-get install libapache2-mod-ruby ruby libmysql-ruby rails build-essential irb

phpMyAdmin

$ sudo apt-get install phpmyadmin

Configure Apache

Change Apache’s user

First of all, as it is a local development server, I want apache to run under my username instead of www-data. This is useful as I don’t have to care about file permissions when I work locally. On “real” servers (especially when running more sites), usually it’s better so run server-side scripts under the context of the appropriate user (for example by using SuExec & FastCGI), but for a local server it’s not relevant. So I edit /etc/apache2/envvars and change the apache user to my username:

export APACHE_RUN_USER=maff
export APACHE_RUN_GROUP=maff

Configuring Apache’s virtual hosts

For this how-to, I will create 3 virtual hosts on 127.0.0.1:

  • localhost – virtual host for all scripts which don’t “deserve” getting an own virtual host
  • pma – virtual host for phpMyAdmin
  • railsproject – a test virtual host for a rails application

The directory structure will be the following (I chose this structure because it’s the structure Rails uses. This is useful as it’s not necessary to distinguish between Rails and normal virtual hosts.):

/var/www/<name of the vhost>/public/
directory for vhost’s DocumentRoot
/var/www/<name of the vhost>/log/
directory for log files

To be able to reach my server under different names, I edit my /etc/hosts file:

127.0.0.1    localhost pma railsproject

As I want to have multiple virtual hosts on one IP-address, I configure apache for name-based virtual hosts on port 80 by editing the end of /etc/apache2/apache2.conf:

NameVirtualHost *:80

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

Then it’s time to add our virtual host configuration. First, I remove the default virtual host:

$ sudo a2dissite default
$ sudo rm -f /etc/apache2/sites-available/default

Next, I create the virtual host file for localhost — /etc/apache2/sites-available/localhost. Nothing special here, you might want to add some more configuration, but for me it’s OK.

<VirtualHost *:80>
        ServerName localhost

        DocumentRoot "/var/www/localhost/public/"
        <Directory "/var/www/localhost/public/">
                Options +FollowSymLinks -Indexes
                AllowOverride All
        </Directory>

        ErrorLog /var/www/localhost/log/error.log
        CustomLog /var/www/localhost/log/access.log combined
        LogLevel error
</VirtualHost>

In order to add the other 2 virtual hosts, just copy the configuration file to a new one and replace localhost with its new value. You could do this by hand, but you can save some typing (and potential errors) by using sed:

$ cd /etc/apache2/sites-available
$ sudo cp localhost pma
$ sudo cp localhost railsproject

Now comes the magic ;)

$ sudo sed -i 's/localhost/railsproject/g' railsproject
$ sudo sed -i 's/localhost/pma/g' pma

Enable virtual hosts and required apache modules

To finish apache configuration, enable the virtual hosts we just created:

$ sudo a2ensite localhost
$ sudo a2ensite pma
$ sudo a2ensite railsproject

Additionally, I enable the mod_rewrite apache module:

$ sudo a2enmod rewrite

You could try to restart apache now to check if all configuration is OK, but as our directories are missing it will print some errors. So let’s create our directories first.

Create the directory structure

As the whole stack should run under my user, I change ownage of the /var/www directory. Additionally, I remove all files from /var/www (mine is a fresh install which contains only the apache test files, so make sure you don’t delete anything you might need).

$ sudo rm -rf /var/www/* && sudo chown maff.maff /var/www

Then, create the directory structure for the localhost virtual host:

$ mkdir -p /var/www/localhost/log && mkdir -p /var/www/localhost/public

For a rails vhost, you just have to execute the rails command:

$ rails /var/www/railsproject

The phpMyAdmin virtual host is some kind of special as PMA is already installed on our filesystem and the public directory is a symbolic link:

$ mkdir -p /var/www/pma/log
$ cd /var/www/pma
$ ln -s /usr/share/phpmyadmin public

Test

OK so now it’s time to test our setup. To test your localhost virtual host create a file /var/www/localhost/public/index.php with the following content:

<?php
phpinfo();

Then, restart apache (the warnings concerning the FQDN are okay):

$ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName      [ OK ]

Now we can try our new setup. localhost should give a nice PHPInfo screen with a whole bunch of information on your setup:

railsproject should show the rails welcome page and some errors regarding denied MySQL access when you click on the “About your application’s environment” link (yes, that’s good as the error messages show that rails is working):

Finally, pma should show the phpMyAdmin welcome screen with an error message:

The error message has to do with our change of the apache user. To solve the problem, just adjust ownage of /var/lib/phpmyadmin to your username:

$ sudo chown -R maff.maff /var/lib/phpmyadmin/

And the pma screen should look like this:

Conclusion

With this setup you should have a working development server setup which is customizable and expandable quite easily. Any suggestions and ideas are welcome :)