Home / guides   Print version

LAMP (Linux, Apache, MySQL, PHP) install tutorial

Publish date 03/11/2008

This is a tutorial to install LAMP: Linux, Apache, MySQL and PHP. Sometimes the P also stands for Perl or Python. But for this tutorial it is just PHP.

In this how-to install everything from source. For most distribution there are binaries provided. Use them, it much, much easier to install and simpler to upgrade later.
Here are links to install LAMP on different flavors of linux:

But source-files can be installed on every (Linux/BSD) system, if you have a compiler like gcc installed.

Get source code

We want to put all our source code someplace central:

cd /usr/local/src

Now you can download the latest version. The source code is usually distributed in "tarballs"(tar.gz).
Tar stands for Tape ARchive. It's also a handy way to pack up multiple files for easy distribution. Use the man tar command to learn more about how to use this tool.

We download the following components

wget http://www.php.net/distributions/php-x.x.x.tar.gz
wget http://apache.oregonstate.edu/httpd/httpd_x.x.xx.tar.gz
Look-up the correct URL for the latest version.
For MySQL, go to http://dev.mysql.com/downloads/mysql/5.0.html and choose an appropriate mirror to get the newest MySQL version.

Unpack the source code

Unpacking is done with the tar tool.
If you have tarballs that ends with tar.bz, use the following command: tar jxf php-x.x.x.tar.bz

tar zxf php-x.x.x.tar.gz
tar zxf apache_x.x.xx.tar.gz
tar zxf mysql-x.x.xx.tar.gz

Build and Install MySQL

If you aren't already, you should now login as root:

su root

First, we create the group and user that "owns" MySQL. For security purposes, we don't want MySQL running as root on the system. To be able to easily identify MySQL processes in top or a ps list, we'll make a user and group named mysql:

groupadd mysql
useradd -g mysql -c "MySQL Server" mysql
If you get any messages about the group or user already existing, that's fine. The goal is just to make sure we have them on the system.

Now go to the directory where you extracted the MySQl-tarball:

cd /usr/local/src/mysql-x.x.xx

You used to do the compiling yourself, but the MySQL-team is so friendly to do it for you.
(compiling is done by ./configure and make && make install)

You can rename or link the long name to mysql:

ln -s full_path-to-mysql-VERSION mysql
Enter the directory:
cd mysql
Set the ownership to mysql-user and group.
chown -R mysql:mysql .
install the test database:
scripts/mysql_install_db --user=mysql
You should now see a new directory data.
The following command start the MySQL-server in the background (&)
bin/mysqld_safe --user=mysql &
You can test if it is running with:
bin/mysqladmin version
MySQL-server is now running, but you still need to configure and secure your server. It is not ready for production.
To shut down the server:
bin/mysqladmin -u root shutdown

Configure MySQL

MySQL is "installed" but we have a few more steps until it's actually "done".
The root account is setup without a password! As is the anonymous account removal.
I always remove the anonymous account: (MySQL must be running. bin/mysqld_safe --user=mysql &)
bin/mysql -u root
DELETE FROM mysql.user WHERE User='';
To set the password for the root user:
bin/mysqladmin -u root -h localhost password "newpassword"
You might want to create extra users for different task or databases. This can be done with GRANT:
bin/mysql -u root -p
GRANT select,insert,update,delete,create,drop ON database.* TO 'username'@'localhost' IDENTIFIED BY 'password';
(You replace "database" with the name of the database, "username" with the name of the user and "password" with the secret password)
This allows the user to connect from localhost only! Which is in most cases enough, and the most secure.

Copy the default configuration file for the expected size of the database (small, medium, large, huge)

cp support-files/my-medium.cnf /etc/my.cnf
chown root:sys /etc/my.cnf
chmod 644 /etc/my.cnf

Now create a start-up script, which enables MySQL auto-start each time your server is restarted.

cp ./support-files/mysql.server /etc/rc.d/init.d/mysql
chmod +x /etc/rc.d/init.d/mysql
/sbin/chkconfig --level 3 mysql on

MySQL Security Issues

Most databases need only be accessed by applications from the server itself.
So we'll tell MySQL not to even listen on port 3306 for TCP connections.
Edit /etc/my.cnf and uncomment the skip-networking line (delete the leading #).

Start MySQL server

if you have everything installed correctly, you can start and stop MySQL with the following commands:

/etc/rc.d/init.d/mysql start
/etc/rc.d/init.d/mysql stop

You're done! MySQL is now installed and running on your server. It is highly recommended that you read about MySQL security and lock down your server as much as possible. The MySQL site has info at http://dev.mysql.com/doc/refman/5.5/en/security.html.

Build and Install Apache

The advantage to building Apache with support for dynamically loaded modules is that in the future, you can add functionality to your webserver by just compiling and installing modules, and restarting the webserver. If the features were compiled into Apache, you would need to rebuild Apache from scratch every time you wanted to add or update a module (like PHP). Your Apache binary is also smaller, which means more efficient memory usage.
The downside to dynamic modules is a slight performance hit compared to having the modules compiled in.

cd /usr/local/src/httpd_x.x.xx

make clean

./configure \
--prefix=/usr/local/apache2 \
--enable-shared=max \
--enable-module=rewrite \
The next part might take a long time so use screen if you are compiling on a slow computer (man screen for more info).
make && make install

Configure Apache

There are so many things you can configure on Apache.
Check out the config file: /etc/apache2/httpd.conf

Start Apache

We want to set Apache up with a normal start/stop script in /etc/rc.d/init.d so it can be auto-started and controlled like other system daemons. Set up a symbolic link for the apachectl utility (installed automatically as part of Apache):

ln -s /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/apache2
Then set up auto-start for runlevel 3 (where the server will go by default):
ln -s /etc/rc.d/init.d/apache2 /etc/rc.d/rc3.d/S90apache2
Then start the daemon:
/etc/rc.d/init.d/apache2 start

Build and Install PHP

Again before you compile take a look at the many option you can configure.

./configure --help | less
Before you compile you might need some other packages.
Download and install libxml2:
ftp fr.rpmfind.net
login anonymous
password blank
cd /pub/libxml/


cd libxml2-x.x.x
make && make install

cd libxslt-x.x.x
make && make install
Then compile php
cd /usr/local/src/php-x.x.x


make && make install

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

if it isn't already done you can create a link in /etc to php.ini.

ln -s /usr/local/lib/php.ini /etc/php.ini

Voila, your done.
Now you can configure and test your site.
the webserver config file can be found in:

read it and if necessary, edit it.
The root-dir for your html files is usually:
But depending on you setting can also be:

The conf file for php is php.ini