How to Install Apache, PHP and MySQL on CentOS

I have seen people installing Apache, PHP and MySQL from source code on a production server. You should NEVER, NEVER, NEVER install these packages from source on an RPM or DEB based Linux distributions. Did I say “NEVER” enough times to make your realize that it is not a good practice?

Installing from source is bad idea; there are many reasons for this but the major one is that you will be spending a lot of time keeping the system updated. If you love installing from source then I would suggest using a different distribution e.g. Gentoo.

You can build from source if the package is not available in yum but still, try to use “yum” (or “apt-get” on Ubuntu) because it will save a lot of your time when upgrading your packages.

So here is the procedure to Install Apache, PHP and MySQL on a CentOS box.

First of all edit the following file using “vi” or any other editor you prefer (I prefer nano)

nano /etc/yum.repos.d/CentOS-Base.repo

Find the line under [centosplus] that reads something like this

enabled = 0

Change it to

enabled = 1

Then do

yum install httpd php mysql-server php-mysql php-gd

This will install Apache, PHP, MySQL and many other common packages

All you need now is to start Apache (httpd) and MySQL (mysqld)

/etc/init.d/httpd start
/etc/init.d/mysqld start

Wow, that was easy, so you got Apache, PHP and MySQL running on your system but still needs to be configured to auto-run on server boot (or reboot). Just punch in the following command and it will be done for both Apache and MySQL (you don’t need to do this for PHP since it is already bind to Apache)

/sbin/chkconfig httpd --level 345 on
/sbin/chkconfig mysqld --level 345 on

345 are basically run level 3, 4 and 5. If you want to study more about Run Levels please consult this article.

That’s it, these simple steps will give install Apache, PHP, MySQL and all the common packages that you will require. Most importantly, you can update these packages with just

yum update

(You may try this command right now but it will not work since you already got all the updated stuff installed)

Updating your system is a breeze now 🙂

Note 1: Please change the password for “root” user in MySQL.

Note 2: Your “DocumentRoot” is /var/www/html.

Update: Added instructions to auto-start Apache and MySQL when system starts / restarts.

2 thoughts on “How to Install Apache, PHP and MySQL on CentOS

  • Hmmm… There are countless reasons why you *should* build and internet facing product from source – the primary one being that you can control exactly what functionality is available, rather then just installing just what the generalist package manager has installed. The second one is being able to use your own judgement as to what security alerts need to be addressed immediately.

    To use Joe's best repository as an alternative is also not very security conscious at all… how many back doors did he put in before he published it???

    Sure there are plenty of times where it is more sensible to to use the delivery rpms ( and reputable 3rd party ones like dag wieers ), but there are just as many times when it makes more sense to roll your own.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.