Building a PHP Development server from scratch

***This post is very out of date. See here for a newer version written in 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

INSTALL OPERATING SYSTEM

If you are following these instructions verbatim then build your machine from a Debian netinst CD. Get networking going and make sure your /etc/apt.d/sources points to a local mirror. FYI I’ve set the server to use DHCP and told my DHCP server to assign it a fixed lease, in this case (192.168.2.110).

Become root:

su -

install some absolute basics:

apt-get install bzip2 ssh vim

INSTALL APACHE

apt-get -y --force-yes apache2 apache2-utils apache2-threaded-dev

I always compile PHP and MySQL from source so I can get whatever is new. As this is a very basic debian install I need a few things before I can start compiling:

apt-get -y --force-yes install gcc g++ make autoconf automake

INSTALL MYSQL

First Install a MySQL dependency:

apt-get install libncurses5-dev

Get the source:

cd && mkdir -p build/src && cd build/src

wget http://downloads.mysql.com/archives/mysql-5.0/mysql-5.0.27.tar.gz

cd ..

tar -zxvf src/mysql-5.0.27.tar.gz

cd mysql-5.0.27/

Configure and build:

CFLAGS="-O3" CXX=gcc CXXFLAGS="-O3 -felide-constructors
-fno-exceptions -fno-rtti" ./configure
--prefix=/usr/local/mysql --enable-assembler
--with-mysqld-ldflags=-all-static

make && make install

Make it start on boot up automatically:

cp support-files/mysql.server /etc/init.d/mysqld
chmod 0755 /etc/init.d/mysqld
update-rc.d mysqld start 75 2 3 4 5 .

Do some simple configuration:

groupadd mysql
useradd -g mysql mysql
cp support-files/my-medium.cnf /etc/my.cnf
cd /usr/local/mysql
chown -R mysql .
chgrp -R mysql .
bin/mysql_install_db
chown -R root .
chown -R mysql var

This server is only for development so I turn off binary logging to save building up massive log files. In /etc/my.conf comment out this line so it look like:
#log-bin=mysql-bin

COMPILE AND INSTALL PHP

First install some PHP dependencies:

apt-get install -y --force-yes flex libssl0.9.7 openssl libcurl3-dev libjpeg62-dev libgd2-xpm-dev libxml2-dev libmysqlclient15-dev

get the source:

cd ~/build/src
wget http://uk.php.net/get/php-5.2.1.tar.bz2/from/this/mirror
cd ..
tar -jxvf src/php-5.2.1.tar.bz2
cd php-5.2.1/

Configure and build:

./configure --with-layout=GNU --with-pear=/usr/share/php --enable-track-vars
--enable-force-cgi-redirect --with-openssl=shared,/usr --with-zlib
--with-exec-dir=/usr/lib/php/libexec --prefix=/usr --with-apxs2=/usr/bin/apxs2
--with-regex=php --with-config-file-path=/etc/php --with-curl=shared,/usr
--with-gd --with-zlib-dir=/usr --with-jpeg-dir=shared,/usr
--with-png-dir=shared,/usr --with-mysql=/usr --with-mysql-sock=/tmp/mysql.sock

make

Normally we’d ‘make install’ now, but this will break as httpd.conf is blank these days with the Apache 2 package. To trick the PHP installer that things are OK we’re going to add a dummy LoadModule line to httpd.conf. (I appreciate that this is a bit of a hack and slightly magic but as it’s a dev server for PHP then I can live with it)

So:

cd /etc/apache2
echo "
#LoadModule foo_module /usr/lib/apache2/modules/foo.so" > /etc/apache2/httpd.conf
(the first line break is important)

Now we can carry on:

make install

..and copy the standard php.ini to the right place:

mkdir /etc/php && cp php.ini-dist /etc/php/php.ini

Then we configure Apache a bit more to make PHP work:

echo "AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps" > /etc/apache2/conf.d/php

Check it’s working:

echo "<?php phpinfo(); ?>" > /var/www/phpinfo.php
/etc/init.d/apache2 restart

On the phpinfo screen (got to in my case by going to http://192.168.2.110/phpinfo.php) I check to see whether php.ini has been picked up, that MySQL is there plus other modules I normally have compiled in. Hmm I notice Curl is not loaded….

A quick check of /usr/lib/php/20060613-zts shows me that curl.so is there so I change extension_dir as follows:

extension_dir = "/usr/lib/php/20060613-zts"

and add the line:

extension=curl.so

Whilst I’m there, make sure we get to see errors and warnings:

error_reporting = E_ALL

Restart Apache and Curl is loaded now.

Ok so we now have a working web server but we need to be able to easily write files to the document root AND we want multiple developer sandboxes.

INSTALL SAMBA

apt-get -y --force-yes install samba

Now configure it by editing /etc/samba/smb.conf, I add:

[homes]
comment = Home Directory
browseable = no
read only = no
writeable = yes
available = yes

/etc/init.d/samba restart

Add a samba user, making the user name & password the same as the system user name (and ideally the same as the host user name) this makes connecting to the shares from the host a piece of cake as one does not need to specify a user name/password then. For the record it is possible to make Samba’s and the system password automatically update when either is changed but I’ve never got it to work and it’s not really a big deal to change a few passwords now and again, so the following suffices:

smbpasswd -a bealers
(enter password)

If you are on Windows to check that it worked open up Windows explorer and enter 192.168.2.110 into the address bar; you should see your home directory. Assuming that you can write to it then you can map a drive and your job is nearly done. The final job is to configure Apache to work from your home directory. I simply chrgp my home dir to www-data (the Apache user) and create a new virtual host container:

chgrp /home/bealers www-data
mkdir ~bealers/www && chown bealers.com www

echo "<VirtualHost *>
ServerName dev.bealers
DocumentRoot /home/bealers/www
<Directory>
Options FollowSymLinks Indexes MultiViews
AllowOverride All
</Directory>
ErrorLog /var/log/apache2/bealers.error.log
LogLevel warn
CustomLog /var/log/apache2/bealers.access.log combined
</VirtualHost>" > /etc/apache2/sites-available/dev.bealers

cd /etc/apache2/sites-enabled/
ln -s ../sites-available/dev.bealers

Assuming that you’ve internal DNS (or a horribly long hosts file) make sure that dev.bealers resolves to 192.168.2.110. Now popping http://dev.bealers into my browser gives me an (empty) directory listing for /home/bealers/www/, which is right.

You can now either develop direct to the dev server or copy and paste the files over when you need to.

In summary: onto a very basic Debian we installed Apache as a Debian package and MySQL/PHP were compiled from source; that gave us our web-server. As we want to easily save our changes and view them immediately without having to faff with FTP or SSH we write directly to the web root of our own sandbox on the server, to facilitate this we use Samba (although there are other options such as NFS).

Enjoy.

This page is updated when something notably changes. Last update April 07 when Etch became stable.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *