Create a local LAMP development environment - part one

LAMP stands for Linux, Apache, MySQL and PHP (or Perl or Python) which is a collection of open source software packages commonly used to build web sites. Most shared web hosting companies provide such an environment because of its popularity. Before your web sit goes live, you need to create a local development environment so you can test freely without worrying breaking anything. Another benefit is that you have complete control of your local environment and can install whatever tools you want to facilitate development. This article describes the steps to create a basic development environment. Subsequent articles provide information about additional setups.

First you need to have Linux installed. Since there are many quality Linux distributions, you can choose a popular one such as Fedora Core, openSUSE, Ubuntu and etc. Modern Linux distributions are quite easy to install and we don't need to elaborate the process in this article. We only highlight one thing: You need to choose the installation type as for development so that development libraries are installed as well. Otherwise later on you may have to come back and install those missing libraries.

Second we suggest you install your own versions of MySQL, Apache, Perl and PHP from source. The reason is that you can control what features will be included and how files should be laid out. Another reason is that usually the versions that come with the Linux distributions are behind the latest ones from their official web sites. By installing from the source you have the freedom to choose whatever versions that suit your need better.

We also suggest that you install all these software as a non-root user. Remember that in a shared web hosting environment the Apache server runs on your behalf as an ordinary user as well. We suggest that all the software be installed in a common directory different from the system directory so in case anything goes wrong you won't mess up the working ones that come with the Linux distribution. You can also back up these files easily and copy them to another similarly configured machine.

Enough for the theory. Now let's move on to install the key software we need for development. Perl, MySQL and Apache can be installed in any order. PHP should be installed after MySQL and Apache are installed. If you plan to use mod_perl and Mason for your web site development which is highly recommended, you need to install them after Apache and Perl are installed.

Since our goal is to get a development setup as quickly as possible, we omit some optimization flags that may be used for the time being.

We assume that you want to install all the above software to the /opt/dev directory. Also for your convenience, you may want to make yourself owner of this directory by running:

sudo chown $USER /opt/dev

Then you don't need any root privilege from now on to install these software packages.

Perl installation

First get the latest Perl tar ball from CPAN or perl.org. Unpack it to a build directory, e.g., $HOME/build. Don't put it to the /tmp directory because files there may go away after reboot. Because you may need to come back to your build directory later to do some tweaks or extra builds, you'd better keep them for a longer while to save you some build time.

Next you just run

sh Configure -Dprefix=/opt/dev/perl -DDEBUGGING -des

This turns on debugging on Perl just in case you want to hack on Perl itself. :) It also provides default answers to many questions that probably you don't need to care much about on initial installation.

To summarize, following are the commands to get Perl configured, compiled and installed.

mkdir /opt/dev/perl
cd $HOME/build
tar xfvz perl-5.10.0.tar.gz
cd perl-5.10.0
sh Configure -Dprefix=/opt/dev/perl -DDEBUGGING -des
gmake
# testing is optional
gmake test
gmake install

You also need to add Perl executables' path to your PATH variable in your .bashrc or .bash_profile file.

export PATH=$PATH:/opt/dev/perl/bin

Once you have a base Perl installation, you can also install those useful Perl packages for web site development. They can be life saving. :)

MySQL installation

MySQL is a popular open-source database management server (DBMS). We suggest you install MySQL 5.x because MySQL 4.x is not officially supported any more. First you can download MySQL from its official web site. Unpack it to the same build directory you created earlier. We would suggest MySQL also be installed in a separate directory, such as /opt/dev/mysql. Then you can just run the following commands to install MySQL:

mkdir /opt/dev/mysql
cd $HOME/build/mysql-5.x.xx
./configure --prefix=/opt/dev/mysql
gmake
gmake install

However, this is not done yet. You also need to create the following directories:

mkdir /opt/dev/mysql/etc
mkdir /opt/dev/mysql/var

The first directory stores MySQL configuration files and the second one stores MySQL runtime data including your databases and tables. Since you install MySQL in a customized place, you need to add the following to your .bash_profile or .bashrc file.

export PATH=$PATH:/opt/dev/mysql/bin

Now you need to copy one of the files mysql-5.x.xx/support-files/my-*.cnf to /opt/dev/mysql/etc and rename it as my.cnf. You can leave the default settings unchanged. However, if you have already had another MySQL server running, then you need to modify my.cnf to point to another port, say 4417 (3306+1111). You should change the port settings for both the mysql section (for the client) and the mysqld section (for the server).

Next you need to initialize various MySQL databases and tables by running

mysql_install_db --defaults-file=/var/tmp/local/mysql/etc/my.cnf --basedir=/var/tmp/local/mysql --datadir=/var/tmp/local/mysql/var

Then you can start MySQL by running:

mysqld_safe --defaults-file=/var/tmp/local/mysql/etc/my.cnf --basedir=/var/tmp/local/mysql --datadir=/var/tmp/local/mysql/var

For your convenience, you may consider adding the following bash function to your $HOME/.bashrc file

function start_local_mysql()
{
  mysqld_safe --defaults-file=/var/tmp/local/mysql/etc/my.cnf --basedir=/var/tmp/local/mysql --datadir=/var/tmp/local/mysql/var
}

Similarly, you can also add the following function in your $HOME/.bashrc file to shut down the MySQL server

function stop_local_mysql()
{
  mysqladmin shutdown -u root -p -S /tmp/mysql.sock
}

So later on you just need to type start_local_mysql or stop_local_mysql without the need to remember all these options.

For the security of your MySQL server, you also need to set the initial password for the root user. For example you can run the following command

mysqladmin -u root password 'mysql123'

For your own convenience, you may add the following to your .bashrc file

alias sqlroot="mysql -u root -pmysql123"

so you can use the MySQL command-line client easily.

Apache installation

Apache is one of the most popular Web servers. We suggest that you install the Apache 2.2.x version if you have the choice. However, if your web hosting company offers Apache 1.3.x installation only, then you should install that version. Usually unless you develop modules for Apache the difference between the two should not matter much to you. Our following discussions apply to Apache 2.2.x installation.

You can download the tar ball from Apache official web site. Then you can unpack it to your build directory and then run the following commands

tar xfvz httpd-2.2.x.tar.gz
cd httpd-2.2.x
./configure --prefix=/opt/dev/apache --enable-so
gmake
gmake install

One of the key configuration setting is --enable-so so later you can build additional Apache modules without the need to recompile Apache.

Once you have Apache installed, you can open the file /opt/dev/apache/conf/httpd.conf and change the default listening port to the one that is greater than 1024, e.g., 8888, so you can start Apache as a non-root user. The configuration directive looks like this

Listen 8888

We will discuss Apache configurations in more details in a separate article. Now you can just manage the Apache process with the apachectl command.

First you need to add the following line to your .bash_profile or .bashrc file.

export PATH=$PATH:/opt/dev/apache/bin

Then you can run apachectl start to start Apache. You can verify Apache is running by visiting the local homepage which should return the message saying that it works.

In case you need to install additional Apache modules, you can first check with httpd -l to see which modules are compiled into Apache. Then you can go to Apache distribution's modules directory and locate those modules you are interested in. For example, if you are interested in the mod_info module, then you can find the corresponding file mod_info.c under the httpd-2.2.x/modules/generators directory. From there you can just run

apxs -cia mod_info.c

Here apxs is a Perl script that manages Apache module compilation, installation and activation. Check the httpd.conf file and you will find that the following line is added:

LoadModule info_module modules/mod_info.so

To use this module, you also need to add the following configuration directive:

<Location /server-info>
  SetHandler server-info
</Location>

Then you can visit the local server info page to see a lot of details about the Apache server.

In fact, PHP is also installed as an Apache module and the installation is quite similar. Following we describe PHP installation in detail.

PHP installation

PHP is a very popular scripting language for developing Web sites. Although it is possible to use PHP to write stand-alone scripts, it is almost used ubiquitously as an Apache module for developing MySQL-backed dynamic web sites. As a glue language, it depends on quite a few libraries to do the heavy lifting, such as curl, libxml2 and etc.

It is important that when configuring PHP you specify the right paths for Apache and MySQL. You can run the following commands to install PHP.

cd $HOME/build/php-5.x.x
./configure --prefix=/var/tmp/local/php --with-apxs2=/var/tmp/local/apache/bin/apxs --with-mysql=/var/tmp/local/mysql
gmake
gmake install

Once it is done, you will see PHP is activated automatically in your Apache configuration file /var/tmp/local/apache/conf/httpd.conf with the following line added

LoadModule php5_module modules/libphp5.so

You also need to copy the file php-5.x.x/php.ini-recommended to /var/tmp/local/php/lib and rename it as php.ini. Later on you need to modify this file to suit your site's need.

You also need to tell Apache if a file with suffix .php or .phtml is requested, the PHP module should be used to handle it. This can be done by adding the following line:

AddType application/x-httpd-php .php .phtml

You may also want to add the following lines so that if a visitor visits a link that maps to a directory, either index.html or index.php (in that order) under that directory will be served.

<IfModule dir_module>
  DirectoryIndex index.html index.php
</IfModule>

Now you can restart Apache with the command apachectl restart. You can test if PHP is working by creating a simple PHP file info.php and copy it to your Apache's document root, i.e., /opt/dev/apache/htdocs to test it.

The code for info.php is show below and once you visit the http://<your_host>:8888/info.php page, it should return a wealth of information about your PHP installation.

<?php
  phpinfo();
?>

Common problems and resolutions

Sometime you may get error message saying that certain header files or libraries are unavailable. In that case, either you need to install those additional libraries or you need to disable those features that depend the missing libraries when you configure those packages.

Back to articles on setup