An overview of building your own Linux web server platform with free open source software components – Apache, MySQL, PHP and PHPMyAdmin. This is a companion tutorial to ‘Data driven websites in PHP using MySQL’.

By Joe Barber


LAMP is an acronym for the components of a free, open source web platform, consisting of the following:

L – Linux, an operating system based on Unix
A – Apache HTTP Server, the most widely-used web server
M – MySQL, a fully featured database management system
P – PHP, a programming language designed to produce dynamic web pages on the server-side

These components form the basis of a web server platform, and represent a popular combination when compared to proprietary alternatives such as Microsoft/Oracle as all the components are free, open-source (therefore easily adaptable) and bundled with most distributions of Linux.

This article attempts to cover the basic steps of what is involved in the process of building a LAMP web server from its individual components; however, it is by no means exhaustive, and assumes a basic understanding of command-line Linux. Some people prefer to install one pre-configured LAMP package for their server such as the Ubuntu-based package here. It is possible to run Apache, MySQL and PHP outside of Linux on platforms such as Windows and Mac OS, and WAMP/MAMP packages can be downloaded and installed for these particular setups.

Assuming we are installing each component separately on a Linux-based server, the following steps apply:

Installing Apache

Download to your /home directory the latest tarball (tar.gz) version of the Apache HTTP Server at http://httpd.apache.org/ – at the time of writing (December 2009), the latest available version was 2.2.14. From the home directory, perform the following actions:

gzip -d httpd-2.2.14.tar.gz
tar xvf httpd-2.2.14.tar
mv /home/httpd-2.2.14 /home/apache
/* rename the new directory /home/httpd-2.2.14 to /home/apache for ease of use */
cd apache
./configure –prefix=/home/apache –enable-mods-shared=most
/*
* if installing with openSSL, add following options:
* –enable-ssl –with-ssl=[path to openSSL directory])
*/

make
make install
/home/apache/bin/apachectl -k start

Installing MySQL

Download to your /home directory the latest tarball (tar.gz) version of the MySQL Community Server at http://dev.mysql.com/downloads/ – at the time of writing, the latest available version was 5.1. The installation of MySQL requires the use of a C/C++ compiler, so you may prefer to download a tarball that comes compiled with one of these; downloads are currently available with the Intel CC compiler. From the home directory, perform the following actions:

gzip -d mysql-5.1.42-linux-i686-icc-glibc23.tar.gz
tar xvf mysql-5.1.42-linux-i686-icc-glibc23.tar
mv /home/mysql-5.1.42-linux-i686-icc-glibc23.tar /home/mysql
/* rename the directory to /home/mysql for ease of use */
cd mysql
./configure –prefix=/usr/local/mysql –localstatedir=/usr/local/mysql/data –disable-maintainer-mode –with-mysqld-user=mysql –enable-large-files-without-debug
make
make install
/scripts/mysql_install_db
chown -R root:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/data
killall mysqld

To log into MySQL securely, we need to reset the root user password on the server to something more secure. Create a text file named flush.txt containing the following, and upload it to your /home/websites directory or equivalent:

UPDATE mysql.user SET Password=PASSWORD(‘MyNewPass’) WHERE User=’root’;
FLUSH PRIVILEGES;

Start your MySQL server with the following command; after the server has started correctly, delete flush.txt.

mysqld_safe –init-file=/home/websites/flush.txt &

Installing PHP

Download to your /home directory the latest tarball (tar.gz) version of PHP at http://php.net/downloads.php – at the time of writing, the latest available version was 5.3.1. From the home directory, perform the following actions:

gzip -d php-5.3.1.tar.gz
tar xvf php-5.3.1.tar
cd php-5.3.1
./configure –prefix=/usr/local/php –with-apxs2=/home/apache/bin/apxs –with-mysql=/usr/local/mysql
make
make test
make install
cp php.ini-recommended /usr/local/php/lib/php.ini

Optional – Installing PHPMyAdmin

PHPMyAdmin is a free software tool written in PHP intended to handle the administration of MySQL over the World Wide Web. PhpMyAdmin supports a wide range of operations with MySQL; the most frequently used operations are supported by the user interface (managing databases, tables, fields, relations, indexes, users, permissions, etc), while you still have the ability to directly execute any SQL statement.

Download to your /home/websites directory the latest tarball (tar.gz) version of PHPMyAdmin at http://www.phpmyadmin.net/home_page/downloads.php – at the time of writing, the latest available version was 3.2.4. From the home directory, perform the following actions:

gzip -d phpMyAdmin-3.2.4-all-languages.tar.gz
tar xvf phpMyAdmin-3.2.4-all-languages.tar
mv /home/websites/phpMyAdmin-3.2.4-all-languages /home/websites/phpmyadmin
/* rename the directory to /home/websites/phpmyadmin for ease of use */
cd phpmyadmin
./configure

Copy config.sample.inc.php, add a value for ‘blowfish_secret’ to enable cookie based authentication, save it as config.inc.php and upload it to /home/websites/phpmyadmin/.

Lastly, you’ll need to set up a domain for this site, eg. phpmyadmin.yourdomain.com, point it at your server IP address and update your Apache vhosts config file to point to /home/websites/phpmyadmin/index.php for this domain.

If you have followed this far, you should now have a fully operational web server on which you can start hosting your website! If you’d like to dive straight into building websites using PHP and MySQL, then continue to our tutorial on ‘Data driven websites in PHP using MySQL’.