Subscribe to PHP Freaks RSS

A simple Debian based dev environment.

by Tony Quilkey on Sep 2, 2010 8:16:18 PM - 88,455 views

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


Jason Lewis Sep 2, 2010 11:13:02 PM

Just like reading our chat log again. Very good, assumes a little knowledge unless people just go willy nilly running commands. But good none the less. I took note of piping the new host to tee, something new. And good mention of writable directories. I'd be keen on seeing more about this too.

You might want to go through yourself or get someone else to go through and correct any spelling, I saw a few things.

richardathome Sep 8, 2010 8:51:46 AM

Many thanks for posting this. The step by step explanation each of the commands is a real help to a Linux n00b like me :-)

guvenck Oct 2, 2010 11:19:56 AM

Nice guide, Tony. What about adding a part for the FTP server? Should save everyone a lot of trouble.

Ross Ylitalo Feb 3, 2011 11:45:45 AM

Very nicely done, thank you!

James somewhere Apr 15, 2011 10:44:05 AM

Thank you for posting this walk thru. Could someone please post some reference material on this.
Is the command line bash, korn, Z shell ?, virtual host ?.
I have been putting all of my energy into learning conditional programing etc and have only touched on the command line and know 0 on virtual host environments.
Thank you.

Tony Quilkey Apr 21, 2011 7:28:49 PM

All commands where issued using the Bash shell (default in most all Linux distributions) though they would work fine in any other shell.

Keep in mind though that this is a Debian/Ubuntu specific tutorial. The a2ensite command + the configuration layout is all relevant to how Debian does things.

tamronlens Jul 29, 2011 10:24:57 PM

It work so good, thanks a lot!

Bazzaah Nov 2, 2011 4:44:19 AM

Would I be able to use a dev environment like this to test php scripts offline or at least without putting them on a website?

Tony Quilkey Nov 2, 2011 6:57:11 AM

Of course you could.

Bazzaah Nov 2, 2011 9:05:52 AM

Great! Will set this up later.

Bazzaah Nov 4, 2011 5:16:50 AM

Thanks for this - works very well.

Add Comment

Login or register to post a comment.