Compile and Install a LAMP(Linux/Apache/MySQL/PHP) Server from Source

LAMP - Linux, Apache, MySQL, PHP

In the last post, I described the method to install a LAMP server using apt in debian. But in some occasions, we need the latest build of the software – then we have to install it from source.

Before installation, a few points to remember. These instruction are not for a production environment – this is for a development environment. Some of the commands(the make install commands) need root access. You can get that using this command su - and entering the root password at the prompt.

Installing MySQL 5

Download MySQL source tarballs from MySQL.com. Make sure that you have downloaded the latest releases. At the time of writing this, MySQL 5.0 was the latest. Open a terminal and login as the root user. Extract the source to some folder(say ‘/usr/src/mysql’).

$ mkdir /usr/src/mysql
$ cp mysql-VERSION.tar.gz /usr/src/mysql
$ cd /usr/src/mysql
$ gunzip < mysql-VERSION.tar.gz | tar -xvf -
$ cd mysql-VERSION

For added security we will create a new user called ‘mysql’ and use this user while running MySQL.

$ groupadd mysql
$ useradd -g mysql mysql

Now we will compile and install MySQL – this will take some time.

$ ./configure --prefix=/usr/local/mysql [add the necessary extra options here]
$ make
$ make install

After installing, we have to configure MySQL.

$ cp support-files/my-medium.cnf /etc/my.cnf
$ cd /usr/local/mysql
$ bin/mysql_install_db --user=mysql
$ chown -R root  .
$ chown -R mysql lib
$ chgrp -R mysql .

Start MySQL

$ bin/mysqld_safe --user=mysql &

If all goes well, you will be able to connect to the mysql server using some clients like phpMyAdmin or MySQL Frontend. For testing we will use the command line client provided with MySQL. In terminal type…

$ mysql

Now you should see mysql command shell – something like this…

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 5.0.21-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql<

Here you can run SQL command and see the returned results. Try some out…

SHOW DATABASES;

That’s it – we have installed MySQL. You can start the server using the command

cd /usr/local/mysql ; bin/mysqld_safe --user=mysql &

If anything went wrong, cry for sometime and then consult the documentation.

Installing Apache 2

Same as in MySQL, download the source from Apache.org. Extract to /usr/src/httpd.

$ mkdir /usr/src/httpd
$ cp httpd-VERSION.tar.gz /usr/src/httpd
$ cd /usr/src/httpd
$ gunzip < httpd-VERSION.tar.gz | tar -xvf -
$ cd httpd-VERSION

Compile and Install…

$ ./configure --prefix=/usr/local/apache2 [add extra options here]
$ make
$ make install

The configure command I used is given below – you can change it if you feel like it.

./configure --prefix=/usr/local/apache2 --enable-mime-magic --enable-expires \
--enable-headers --enable-ssl --enable-http --enable-info --enable-dir \
--enable-rewrite --enable-so

We will hold off configuration of Apache until after the PHP installation.

Installing PHP 5

You know the drill – download PHP 5, extract to /usr/src/php5.

$ mkdir /usr/src/php5
$ cp php-VERSION.tar.gz /usr/src/php5
$ cd /usr/src/php5
$ gunzip < php-VERSION.tar.gz | tar -xvf -
$ cd php-VERSION

Building PHP is a little more complicated than the other two. PHP have a lot of options, and must be customized according to your needs. You can see all the available configurations by running the command ./configure --help. If you require more information about this, take a look at PHP documentation on configure.

My requirements may not match yours – some you have to make your own decisions here. I am providing the most basic configuration for the build…

./configure \
--prefix=/usr/local/php5 \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-mysql=shared,/usr/local/mysql [add your options here]

You can add all your configuration to this line. The –with-apxs2 lines tells the installer where to find Apache2 executables and the –with-mysql configuration is the location of the mysql libraries. These two lines are a must.

This is the actual command I used…

./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs \
--with-mysql=shared,/usr/local/mysql --with-zlib --with-gettext --with-gdbm --with-sqlite

Now to install the language…

$ ./configure \
$ --prefix=/usr/local/php5 \
$ --with-apxs2=/usr/local/apache2/bin/apxs \
$ --with-mysql=shared,/usr/local/mysql [add your options here]
$ make
$ make install

Now copy the php.ini file to the necessary location…

cp php.ini-dist /usr/local/php5/lib

Wonderful! Now we have everything we need – we just have to configure it.

Configuring Apache

The Apache can be configured by editing a single text file. This file is usually located in Apache_folder/conf/httpd.conf. In our case this will be /usr/local/apache2/conf/httpd.conf . Open this file in you favorite text editor and change the following lines. Please note that some lines may be different in you apache configuration file – so if you can’t find the line when you search with the full line, try to find the line using just the identifier. For example, if you can’t find the text ‘DocumentRoot "/usr/local/apache2/htdocs"‘ in your httpd.conf file, search for the text ‘DocumentRoot‘.

Configuration Options

You may want to change the document root – replace the line

DocumentRoot "/usr/local/apache2/htdocs"

With

DocumentRoot "/var/www/htdocs" # Or whatever folder you want to set as the document root.

Also change the line

<Directory "/usr/local/apache2/htdocs">

to

<Directory "/var/www/htdocs">

I don’t have to tell you that you can use any directory you want – you don’t have to use ‘/var/www/htdocs’ just because I use it. If you are using another directory, make sure that you change both the above given lines to that directory.

If you want to use .htaccess file to configure different folders, find the line

AllowOverride None

inside the <Directory "/usr/local/apache2/htdocs"> ... </Directory> tag and change it to

AllowOverride All

Since we are using PHP, and want to use index.php as the default page in a directory, we have to set that configuration option. Find the line

DirectoryIndex index.html index.html.var

and replace it with, say,

DirectoryIndex index.php index.html index.html.var

This will make sure that the index.php will be called when you try to access a directory. For example, if you try to access, say, http://localhost/ you will get a file listing. But if you put a file named ‘index.php’ in this folder, the server will open this file when someone accesses ‘http://localhost/’. The order of the names are important. If there is a file called ‘index.php’ and a file called ‘index.html’ in the same folder, the first one(in our case index.php) will be opened.

Now we must associate all files with the extension ‘php’ with the PHP scripts handler. For this find the line

AddHandler type-map var

and add the following line below that line – like this

AddHandler type-map var
AddHandler php5-script	php

Below that there is a AddType section. Add the following line to this section.

AddType application/x-httpd-php .php

Start the Server

You can test your installation by starting your server. Open a terminal and run the following command.

/usr/local/apache2/bin/apachectl start

Go to your document root(/var/www/htdocs) and create a php file called ‘info.php’ and put this code inside it…

<?php
phpinfo();

Now open a browser and try to access http://localhost/ – you should see a file listing page with a ‘info.php’ in the list. Click on that link – if you see a PHP information page, your web server is setup correctly.

To make sure MySQL-PHP connection is working, install phpMyAdmin – or write a database connection script – whatever is easier for you.

Shameless Plug: If you are a Linux user, you may want to check out my Linux Blog – LinDesk – its about Linux on the Desktop – Articles, Application Reviews and Tutorials about many aspects of Linux included configuration and scripting.

8 Comments

  1. how i can put the apache how service , becaous this commoand not function

    services httpd start
    /etc/init.d/httpd start

2 Trackbacks / Pingbacks

  1. links for 2008-09-25 « B-link List
  2. Yo Super » Install Apache2 + PHP5 + MySQL5 on Debian

Comments are closed.