Darren Beale Web developer, retailer & photographer

Building a PHP development server from scratch

B

php-logoThe other day my dev server completely died, this was rather inconvenient to say the least. 24 hrs later and my new (excellent, very quiet, powerful but only consuming 150w) HP 54L Proliant Microserver was sitting on my desk ready for building.

I’ve done a server build so often that I can do it with my eyes closed , but usually miss something, so as I was in a hurry I referred to my previous post of the same name. As I did so the fact that it’s 7 years out of date became apparent. Compiling from source? Nah. Where’s Git? Not using sudo?

So, here I am re-writing from scratch for 2013…

Following these instructions will get you a Debian basedĀ PHP/MySQLĀ development server set-up with the ability to have multiple developer sandboxes that are accessible from a remote machine on the local network, using Samba.

Time needed < 1hr

If you’re following these instructions verbatim then grab & install the newest netinst of Debian, it turns out that Wheezy was released only a few days ago, so that’s what I went with.

This machine is given a fixed DHCP lease on the LAN (192.168.1.4 in my case) and has the hostname of devian on the local LAN’s DNS server. I selected ssh server from the set of install profiles during the install so as soon as the OS isĀ installedĀ I can do the rest headless from my main machine.

ssh in, become root

su -

Add dotdeb to /etc/apt/sources.list

deb http://packages.dotdeb.org wheezy all
deb-src http://packages.dotdeb.org wheezy all

Add the GnuPG key to stop avoid error messages when apt-getting:

wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | sudo apt-key add -

Update.

apt-get update && apt-get upgrade

Install everything.

apt-get install vim bzip2 samba curl pwgen php5 php5-cli php5-mysql mysql-server apache2 git sudo ntp

Note ntp to keep the clock in sync & pwgen as I use all the time, the rest should be obvious, you can of course add your own to this.

Edit /etc/vim/vimrc and uncomment this if you want pretty (readable on a PuTTY terminal) colours when using vim.

syntax on
set background=dark

Edit /root/.bashrc, add this so vim is default editor when doing things like visudo and crotab -e as root

export EDITOR=vim

Re-login.

su -

Edit sudoers.

visudo

As it’s my local server I add this line for my username so I don’t have to supply a password each time, obviously this depends upon your security policies. For your other developers sharing the machine you might limit it to only certain commands..

bealers ALL=(ALL) NOPASSWD:ALL

Become your user.

su - bealers

Make your document root,

cd && mkdir www

Edit /home/you/.bashrc to do the vim thing again & also to add any bash aliases, here’s mine:

export EDITOR=vim
alias ls='ls --color=auto -Fhla --group-directories-first'
alias halt='sudo shutdown -h now'
alias reboot='sudo reboot'
alias ..='cd ..'
alias ...=&#0
39;cd ../..'
alias apres="sudo /etc/init.d/apache2 restart"
alias phplog="sudo tail -f /var/log/php.log"

Create an ssh key for gitlab/password-less remote shell access etc.

ssh-keygen -t rsa

Optionally, symlink php.ini so it’s the same for Apache and the CLI.

sudo cd /etc/php5/cli
sudo mv php.ini php.ini.cli && sudo ln -s ../apache2/php.ini

Edit php.ini with your own changes, for now we’ll just enable php logging.

error_log = /var/log/php.log

Create the log.

sudo touch /var/log/php.log && sudo chown www-data.www-data /var/log/php.log

Restart apache.

sudo /etc/init.d/apache2 restart

Condigure samba.

sudo vi /etc/samba/smb.conf

Edit it so the following is changed:

workgroup = SIFTWARE # this isn't so important these days

I comment out all default shares and create one new one, for multiple users you could make the username a variable. note the group permissions are pretty lax, You could easily tighten this up but you do need to ensure that files are automatically created from Windows so that the www-data (group) can read them. This works for me.

[docroot]
comment = This user's Apache docroot
browseable = Yes
read only = No
create mask = 0775
directory mask = 0775
path = /home/bealers/www

Restart.

sudo /etc/init.d/samba restart

Add user(s) to Samba. To keep things really simple your dev server samba username should match the username for your Windows desktop, ditto passwords. This should mean you don’t have to supply credentials when connecting to your share.

sudo smbpasswd -a bealers

On your desktop check out that you can see the share, if you don’t have a DNS server I’m assuming that you’ve put the dev server hostname into C:WindowsSystem32Driversetchosts

Windows+E then Alt+D then paste devian.siftware.local

Map the docroot folder as a network drive. You might need to ‘connect using different credentials’ if the Samba username or password are different to your Windows logged in user.

Create a file and folder via explorer then ls /home/you/www to make sure the permissions are OK.

Edit the default website in Apache (or create a new vhost for each user to point to their docroot) my /etc/apache2/sites-available/default looks like:

ServerAdmin webmaster@localhost

DocumentRoot /home/bealers/www
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Enable mod_rewrite.

sudo cd /etc/apache2/mods-enabled/
sudo ln -s ../mods-available/rewrite.load

Restart apache.

sudo /etc/init.d/samba restart

Create phpinfo.

cd ~/www && echo "" >> info.php

From your desktop’s web browser access http://devian.siftware.local, or, if you’ve set up a default vhost per developer then it’ll be http://dev.user (or whatever your naming convention is).

You should be able to see info.php, clicking on it should give you the php info screen.

You now can create vhosts for all your projects pointing them to the relevant folders within your docroot (or your various users’ docroots).

Within Windows you should be able to open/write/create these files within your IDE via the mapped drive.

There you go, I can get on with some work again now.

By bealers
Darren Beale Web developer, retailer & photographer