A simple Debian based dev environment.

Views: 88830
Rating: 5/5
Votes: 2

After walking a user through this entire process last night on irc I figure I’m probably better off documenting it somewhere so I don’t have to repeat myself.

This is just a quick walkthrough describing how to setup a decent development environment allowing the easy setup of multiple sites. It already assumes you have a working Debian or Ubuntu OS installed and configured with PHP, MySql &
Apache already running. You will also need to have a working sudo.

While some of this stuff is Debian / Ubuntu specific it wouldn’t be difficult at all to apply this to any other distro.

The very first thing we need to do is create a ‘www’ group. Anyone within this group will be able to create new websites.

sudo groupadd www

Put yourself in this group.

sudo gpasswd –a username www && newgrp www

Now, we need to make sure that the location all our sites are going to be stored within are owned and writable by www.

sudo chown :www /var/www
sudo chmod g+ws /var/www

The s switch for chmod sets the sticky bit and ensures that all directories created within /var/www will also belong to the www group.

We need to make sure www can write to the directories where our vhost configs are kept as well.

sudo chown :www /etc/apache2/sites-*

Next, we are going to create a template vhost configuration. We will use this to generate configurations for each of our sites.

<VirtualHost *:80>
    ServerName {SITE}.local
    DocumentRoot /var/www/sites/{SITE}.local/htdocs

    DirectoryIndex index.php

    <Directory />
	Options FollowSymLinks
	AllowOverride All
    <Directory /var/www/sites/{SITE}.local/htdocs>
	Options Indexes FollowSymLinks MultiViews
	AllowOverride All
	Order allow,deny
	allow from all

    ErrorLog /var/www/sites/{SITE}.local/logs/error.log
    LogLevel warn
    CustomLog /var/www/sites/{SITE}.local/logs/access.log combined


Save that as /etc/apache2/sites-available/example.conf

Now, lets create a simple test site.

mkdir -p /var/www/sites/foo.local/{htdocs,logs}
echo '<?php phpinfo(); ?>' > /var/www/sites/foo.local/htdocs/index.php
cd /etc/apache2/sites-available
sed -e 's/{SITE}/foo/g' example.conf > foo.conf
sudo a2ensite foo.conf
sudo /etc/init.d/apache2 restart
echo ' foo.local' | sudo tee -a /etc/hosts

Theres a few commands here, but its all pretty straight forward:

The first line creates the minimal required directory structure for a new web site.
We then create an index.php file which has a call to phpinfo() in it.
Next we move into the directory where our vhost configs are stored.
We then create a new vhosy config called foo.conf which is a copy of the example.conf from above, we use sed to replace all instances of '{SITE}' with 'foo'
Next, enable this vhost. This adds a symlink within /etc/apache2/sites-enabled
Restart Apache.
Add foo.local to our hosts file. This enables the url http://foo.local to resolve to our local machine and hence, be served by Apache.

That's about it really. You can build quite a bit on this basic idea though. One thing I have implemented in a multi-user system is to make new commands available to the members of the 'www' group. These commands are mostly just wrappers
around the stuff that requires sudo, but it does make the process allot cleaner from an end user point of view. I might go into further details about that in another tutorial given the chance and the interest.

One thing to note. If you need directories within your web site to be writable by Apache you will have to make them belong to the www-data group, and this group will need write permissions. eg;

mkdir -p /var/www/sites/foo.local/uploads
sudo chown :www-data /var/www/sites/foo.local/uploads
sudo chmod g+w /var/www/sites/foo.local/uploads