May 22, 2012

Beginner

How To Install Linux, Apache, MySQL, PHP (LAMP) stack On CentOS 6

About LAMP


LAMP stack is a group of open source software used to get web servers up and running. The acronym stands for Linux, Apache, MySQL, and PHP. Since the server is already running CentOS, the linux part is taken care of. Here is how to install the rest.

Set Up

The steps in this tutorial require the user on the virtual private server to have root privileges. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4.

Step One—Install Apache


Apache is a free open source software which runs over 50% of the world’s web servers.

To install apache, open terminal and type in this command:
sudo yum install httpd

Once it installs, you can start apache running on your VPS:
sudo service httpd start

That’s it. To check if Apache is installed, direct your browser to your server’s IP address (eg. http://12.34.56.789). The page should display the words “It works!" like this.

How to find your Server’s IP address


You can run the following command to reveal your server’s IP address.
ifconfig eth0 | grep inet | awk '{ print $2 }'

Step Two—Install MySQL


MySQL is a powerful database management system used for organizing and retrieving data on a virtual server

To install MySQL, open terminal and type in these commands:
sudo yum install mysql-server
sudo service mysqld start

During the installation, MySQL will ask you for your permission twice. After you say Yes to both, MySQL will install.

Once it is done installing, you can set a root MySQL password:
sudo /usr/bin/mysql_secure_installation

The prompt will ask you for your current root password.

Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter.
Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Then the prompt will ask you if you want to set a root password. Go ahead and choose Y and follow the instructions.

CentOS automates the process of setting up MySQL, asking you a series of yes or no questions.

It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new changes.
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y                                            
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Step Three—Install PHP


PHP is an open source web scripting language that is widely used to build dynamic webpages.

To install PHP on your virtual private server, open terminal and type in this command:
sudo yum install php php-mysql

Once you answer yes to the PHP prompt, PHP will be installed.

PHP Modules


PHP also has a variety of useful libraries and modules that you can add onto your server. You can see the libraries that are available by typing:
yum search php-

Terminal then will display the list of possible modules. The beginning looks like this:
php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-enchant.x86_64 : Human Language and Character Encoding Support
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-imap.x86_64 : A module for PHP applications that use IMAP
To see more details about what each module does, type the following command into terminal, replacing the name of the module with whatever library you want to learn about.
yum info name of the module

Once you decide to install the module, type:
sudo yum install name of the module

You can install multiple libraries at once by separating the name of each module with a space.

Congratulations! You now have LAMP stack on your droplet!

We should also set the processes to run automatically when the server boots (php will run automatically once Apache starts):
sudo chkconfig httpd on
sudo chkconfig mysqld on

Step Four—RESULTS: See PHP on your Server


Although LAMP is installed on your virtual server, we can still take a look and see the components online by creating a quick php info page

To set this up, first create a new file:
sudo nano /var/www/html/info.php

Add in the following line:
<?php
phpinfo();
?>

Then Save and Exit.

Restart apache so that all of the changes take effect on your virtual server:
sudo service httpd restart

Finish up by visiting your php info page (make sure you replace the example ip address with your correct one): http://12.34.56.789/info.php

It should look similar to this.

See More

After installing LAMP, you can go on to do more with MySQL (A Basic MySQL Tutorial), Create an SSL Certificate, or Install an FTP Server.



By Etel Sverdlov

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

61 Comments

Write Tutorial
  • Gravatar dave over 1 year

    After installing Apache I had to start the service before I could view it in the browser: sudo service httpd start

  • Gravatar Moisey over 1 year

    Yup, we'll get the article update for that. Thanks

  • Gravatar Phil Dobbin over 1 year

    You may also want to use: `sudo chkconfig --level 235 httpd on` & similar for mysql to enable the services to run at boot time in case of a re-boot...

  • Gravatar Etel Sverdlov over 1 year

    Thanks for your suggestion! I have updated the article to ensure that the programs run when the server boots.

  • Gravatar jashby2 over 1 year

    Thanks for the article! Very helpful in installing the LAMP stack on CentOS.

  • Gravatar Etel Sverdlov over 1 year

    Thank you! I'm glad it was useful :)

  • Gravatar ostad over 1 year

    how setup sendmail on cent os!!!

  • Gravatar Etel Sverdlov over 1 year

    If you are not familiar with sendmail, we do not recommend starting there. We would only recommend sendmail for experts. If you want to learn more about sendmail, the O'Reilly book is excellent.

  • Gravatar i.manish over 1 year

    Thanks alot.. Your post is really very helpful.

  • Gravatar ewe about 1 year

    ewewe

  • Gravatar David Levy about 1 year

    Does the tut for phpMyAdmin on Ubuntu also work for CentOS?

  • Gravatar webdev.dz 12 months

    Thanks! Note: You can install all php modules at once --------------------------------------------------------------- sudo yum install php-* ---------------------------------------------------------------

  • Gravatar Oleg Telyuk 12 months

    You can also install using the yum groupinstall command. yum groupinstall "Web Server" "PHP Support" "MySQL Database server" "MySQL Database client" yum install php-mysql

  • Gravatar tonyha1090 12 months

    Hi, first thanks for your tutorial, second i think you should configure basic iptables rules for this toturial.

  • Gravatar Oleg Telyuk 12 months

    Type "system-config-firewall-tui" in the command line, and select protocols.

  • Gravatar oosthuizenmn 11 months

    I have always needed WHM to get a LAMP environment up and running on other hosts, but with this tutorial I was up and running quickly and smoothly! I love my new dropbox already :)

  • Gravatar tonyparra216 10 months

    How to save and exit php info page?

  • Gravatar Arunava 10 months

    Thanks for the post. Maybe you can include basiv php.ini settings that should be edited as well here.

  • Gravatar Kamal Nasser 10 months

    @tonyparra216 Press Ctrl-X, y, enter. @Arunava Most php.ini settings are fine out of the box, you might need to edit them based on what suits your app the most which is different for each app.

  • Gravatar Nestor Couvertier 10 months

    No package httpd available. Error: Nothing to do

  • Gravatar Nestor Couvertier 10 months

    Nevermind. Figured it out My yum.conf was messed up.

  • Gravatar sanwar.choudhary10 10 months

    Hi Thanks !!

  • Gravatar linuxtechjason 10 months

    I'm thinking the info.php file should be deleted after checking that it works so potential attackers don't have more information how to break into your server.

  • Gravatar sridharpandu 9 months

    I get the following error message (line3) when I restart apache. Does anyone know why [root@Netraja3 sridhar]# /etc/init.d/httpd restart Stopping httpd: [ OK ] Starting httpd: httpd: apr_sockaddr_info_get() failed for Netraja3 httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName [Tue Jul 23 16:47:35 2013] [warn] NameVirtualHost *:80 has no VirtualHosts [ OK ]

  • Gravatar sridharpandu 9 months

    It would be nice if you mention that while installing PHP using the commands above the version installed is 5.1. Having 5.1 is rather odd because most apps like drush etc require 5.2 and above. To install the latest PHP version i.e. 5.3 the command is sudo yum install php53 php53-mysql.

  • Gravatar Kamal Nasser 9 months

    @sridharpandu: What's the output of "cat /etc/hostname" and "hostname"?

  • Gravatar palash 9 months

    after using the command sudo /usr/bin/mysql_secure_installation it shows Enter current password for root (enter for none): i press enter and it shows error (ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)) what should i do for solving the error

  • Gravatar Kamal Nasser 9 months

    @palash: Is MySQL running? "service mysql start"

  • Gravatar mariomelian 8 months

    I get the same error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock

  • Gravatar Kamal Nasser 8 months

    @mariomelian: Is MySQL running? "service mysql start"

  • Gravatar mariomelian 8 months

    thanks

  • Gravatar mariomelian 8 months

    no is not working "service mysql start" unrecognized service

  • Gravatar mariomelian 8 months

    /etc/init.d/mysqld start mysql works great

  • Gravatar mariomelian 8 months

    then when I try to Restart apache I get this: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

  • Gravatar Kamal Nasser 8 months

    @mariomelian: That's a warning. You can ignore it -- everything will work fine.

  • Gravatar rekcahx 8 months

    Why can't digital ocean just provide a package to install lamp all at once, as a whole. Heck, why not make a nice script to enable me to go down a page and check off everything there is to offer, so I can install everything at once that I need. Enough with reading and figuring out 30 different tutorials on how to install the 30 different scripts I need to install just to get my server up and running. Boy, that would be nice.

  • Gravatar Kamal Nasser 8 months

    @rekcahx: We have a 1-click LAMP stack on Ubuntu installation image: https://www.digitalocean.com/one-click-install-applications

  • Gravatar roshen.raju 8 months

    this is like a open school..thx

  • Gravatar rafablessed 8 months

    how to install phpmyadmin on centos?

  • Gravatar Kamal Nasser 8 months

    @rafablessed: Take a look at this Question: https://www.digitalocean.com/community/questions/phpmyadmin-on-centos

  • Gravatar cfranco80 8 months

    Why my ip/info.php indicate Error Server when I try sudo service httpd restart?

  • Gravatar cfranco80 8 months

    and how to resolve?

  • Gravatar Kamal Nasser 8 months

    @cfranco80: Please paste the output of

    sudo service httpd restart

  • Gravatar yazirarafath 8 months

    Thank you very much for this useful article. I just installed everything on my CentOS VPS, according to this post. Doing great :)

  • Gravatar re_digitalocen 7 months

    mysql_secure_installation ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) To correct this I followed the procedures at http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html

  • Gravatar SoldierCorp 7 months

    Nice, let me create a video-tutorial in spanish for youtube! Regards.

  • Gravatar vikram.handa 7 months

    tanks for article

  • Gravatar Morten Buljo 7 months

    I get this error on my info.php Routing Error No route matches [GET] "/info.php" Try running rake routes for more information on available routes.

  • Gravatar burak 7 months

    Hello, I did all the steps except I installed PHP5.4 via yum. After adding the loadmodule entry for libphp5.so Apache started throwing error that it cannot find the libphp5.so Turns out that the installation did not create any libphp5.so Can anyone help?

  • Gravatar Kamal Nasser 7 months

    @burak: You don't need to add that line, does it work without it?

  • Gravatar joe 6 months

    I was having a problem with getting all of my firewall rules to work... yum install system-config-firewall-tui && system-config-firewall-tui fixed that nicely. Awesome, tip. ;) Nice tutorial, btw: yum groupinstall "Web Server" "PHP Support" MySql Database Server" only get you so far... This got me the rest of the way in a matter of seconds. Thank you.

  • Gravatar qasi123 5 months

    Does the LAMPP package include phpmyadmin as well???

  • Gravatar Kamal Nasser 5 months

    @qasi123: No, it does not. However, you can install it yourself. See https://www.digitalocean.com/community/articles/how-to-install-and-secure-phpmyadmin-on-a-centos-6-4-vps.

  • Gravatar jonathan 3 months

    You need to install Nano in order to do step #4. sudo yum install nano To set this up, first create a new file: sudo nano /var/www/html/info.php

  • Gravatar chandai.us about 1 month

    i can not installing

  • Gravatar nghiemvo about 1 month

    After installed V3.5 on a new server, the index.php page is loading fine. But when I clicked on any of the links on that page I got this error. For example: if I click on http://www.domain,com/channel/music/1/ The error page displayed as below: Not Found The requested URL /channel/music/1/ was not found on this server. Apache/2.2.15 (CentOS) Server at www.domain.com Port 80 Help Please

  • Gravatar sponglewang69 about 1 month

    I was having problems getting to see the default httpd webpage in step 1. The solution was to check and make sure port 80 accepts connections in iptables. see more: http://stackoverflow.com/questions/9936606/apache-and-httpd-running-but-i-cant-see-my-website

  • Gravatar Kamal Nasser about 1 month

    @nghiemvo: Have you enabled mod_rewrite and set AllowOverride to All?

  • Gravatar john.ambiental 18 days

    Hello, first I want to thank you for the tutorial. My question is: How do I install a newer version of php, since by default makes me want to use 5.3.3 and laravel 4 and 5.3.7 minimum asking me this. Thank you.

  • Gravatar a.starr.b 17 days

    @john.ambiental If you really need a newer version of PHP you'll need to enable a third-party repository. Keep in mind that part of the reason for using CentOS is the stability achieved by using a know set of packages that have been tested to work well together. By enabling third-party repositories, you risk losing some of that stability. For more information on EPEL visit: https://fedoraproject.org/wiki/EPEL You can install the package that contains that includes the repository information with: ``` wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm ```

  • Gravatar john.ambiental 15 days

    @a.starr.b tnks! it's just what I am doing.

Leave a Comment

Create an account or login:
Ajax-loader