LAMP (Linux, Apache, MySQL, PHP) install tutorialPublish 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:
Get source code
We want to put all our source code someplace central:
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 tarcommand to learn more about how to use this tool.
We download the following componentsLook-up the correct URL for the latest version.
wget http://www.php.net/distributions/php-x.x.x.tar.gz wget http://apache.oregonstate.edu/httpd/httpd_x.x.xx.tar.gz
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:
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: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.
groupadd mysql useradd -g mysql -c "MySQL Server" mysql
Now go to the directory where you extracted the MySQl-tarball:
You used to do the compiling yourself, but the MySQL-team is so friendly to do it for you.
(compiling is done by
make && make install)
You can rename or link the long name to mysql:Enter the directory:
ln -s full_path-to-mysql-VERSION mysqlSet the ownership to mysql-user and group.
cd mysqlinstall the test database:
chown -R mysql:mysql .You should now see a new directory data.
The following command start the MySQL-server in the background (&)You can test if it is running with:
bin/mysqld_safe --user=mysql &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 MySQLMySQL 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 &)To set the password for the root user:
bin/mysql -u root DELETE FROM mysql.user WHERE User=''; FLUSH PRIVILEGES; EXIT;You might want to create extra users for different task or databases. This can be done with GRANT:
bin/mysqladmin -u root -h localhost password "newpassword"(You replace "database" with the name of the database, "username" with the name of the user and "password" with the secret password)
bin/mysql -u root -p GRANT select,insert,update,delete,create,drop ON database.* TO 'username'@'localhost' IDENTIFIED BY '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.The next part might take a long time so use screen if you are compiling on a slow computer (
cd /usr/local/src/httpd_x.x.xx make clean ./configure \ --prefix=/usr/local/apache2 \ --enable-shared=max \ --enable-module=rewrite \ --enable-module=so
man screenfor more info).
make && make install
There are so many things you can configure on Apache.
Check out the config file:
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):Then set up auto-start for runlevel 3 (where the server will go by default):
ln -s /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/apache2Then start the daemon:
ln -s /etc/rc.d/init.d/apache2 /etc/rc.d/rc3.d/S90apache2
Build and Install PHP
Again before you compile take a look at the many option you can configure.Before you compile you might need some other packages.
./configure --help | less
Download and install libxml2:
Then compile php
ftp fr.rpmfind.net login anonymous password blank cd /pub/libxml/ get LATEST_LIBXML2 get LATEST_LIBXSLT quit tar zxf LATEST_LIBXML2 tar zxf LATEST_LIBXSLT cd libxml2-x.x.x ./configure make && make install cd libxslt-x.x.x ./configure make && make install
cd /usr/local/src/php-x.x.x ./configure 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