CentOS - Installing Apache and PHP5

CentOS comes with Apache v.2.2.3 and PHP v.5.1.6 and they are easily installed via the default CentOS Package Manager 'yum'.

The advantage of using yum (as opposed to installing via source code) is that you will get any security updates (if and when distributed) and dependencies are automatically taken care of.


Apache Install

A basic Apache install is very easy:

sudo yum install httpd mod_ssl

ServerName

Oddly, the server does not start automatically when you install it so you have to do this by hand:

sudo /etc/init.d/httpd start

The first thing you will see is this error:

Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name,
using 127.0.0.1 for ServerName

As you can see, the address 127.0.0.1 is used as the ServerName by default. It's a good idea to set the ServerName for the next time the server is started.

Open the main apache config:

sudo nano /etc/httpd/conf/httpd.conf

Towards the end of the file you will find a section that starts with 'ServerName' and gives the example:

#ServerName www.example.com:80

All you need to do is enter your Slice hostname or a FQDN:

ServerName demo

Note that my Slice hostname is 'demo'.

Reload Apache:

sudo /etc/init.d/httpd reload

Now the warning has gone. Nice.

Default Page

If you navigate to your Slice IP address:

http://123.45.67.890

You will see the default CentOS Apache welcome screen:

Apache Welcome Screen Listing

I think that's a good start.

Chkconfig

Now that we have Apache installed and working properly, we need to make sure that it's set to start automatically if the slice is rebooted.

sudo /sbin/chkconfig httpd on

Let's check our work to confirm:

sudo /sbin/chkconfig --list httpd
httpd           0:off        1:off  2:on    3:on    4:on    5:on    6:off

Good.

PHP5 Install

Let's move on to the PHP5 install. I'm not going to install all the modules available. Just a few common ones so you get the idea.

As before, due to using yum to install PHP5, any dependencies are taken care of:

sudo yum install php-common php-gd php-mcrypt php-pear php-pecl-memcache php-mhash php-mysql php-xml

Once done, do a quick Apache reload:

sudo /etc/init.d/httpd reload

Done.

Almost

Well, almost done.

We do need to configure Apache for our setup so we can host multiple sites and so on.

The next articles will go through the Apache layout and configuration as it may differ from a layout you have used previously (especially if you used a Debian based OS).

PickledOnion.

Article Comments:

Mike Boone commented Sun Feb 17 01:32:30 UTC 2008:

I installed Apache using these instructions, and it works fine. However, when I rebooted my CentOS slice, httpd did not start automatically. What do I need to do to ensure it starts when the system boots?

Thanks.

Mark Sundt commented Thu Feb 21 16:15:14 UTC 2008:

You need to use the chkconfig command to auto start the httpd service -

$chkconfig httpd on

You can use 'chkconfig --list' command to see all of the services that are installed and at what run level they are configured to run at.

Mike Boone commented Fri Feb 22 19:27:10 UTC 2008:

Thanks Mark!

Samuel commented Sun Mar 16 18:04:47 UTC 2008:

These tutorials are fantastic! ;)

But I missed a thing that I think that is a good security point.

How I can disable the browsing to direct ip?

i.e. in your example I would like to block browsing to http://123.45.67.890 but allow browsing to any domain hosted using http://domain.com

Appraisers commented Sat Apr 19 16:19:06 UTC 2008:

I had to specify the correct path to use the chkconfig command since it is apparently not in the path variable:

$/sbin/chkconfig --list httpd

So, what about the MySQL service? Does that require manual autostart specification as well?

andylockran commented Thu Jul 17 15:16:06 UTC 2008:

Appraisers, I'd run sudo chkconfig httpd on.

If you run sudo chkconfig --list, it'll list services including mysql. You'll then be able to see if it's turned on or off (most likely it'll be off) and then use sudo chkconfig mysql(d) on.

HTH.

Duncan Robertson commented Tue Sep 23 20:37:50 UTC 2008:

and for MySQL which is also common in the setup above:

sudo yum install mysql-server mysql sudo /sbin/service mysqld start sudo /sbin/chkconfig mysqld on

Duncan Robertson commented Tue Sep 23 20:41:12 UTC 2008:

And again with correct formatting .. doh!. For MySQL which is also common in the setup above:

  • sudo yum install mysql-server mysql
  • sudo /sbin/service mysqld start
  • sudo /sbin/chkconfig mysqld on

Karen commented Tue Sep 30 20:27:07 UTC 2008:

great tutorials guys i have one question i have it all install correctly my only problem is on outside world it says The requested URL /mysql was not found on this server. Apache/2.2.3 (CentOS) Server at athenas-domain.net Port 80

the domain seems to be valid but is unable to connect but it connects on firefox on server side opens to apache test page and everything any idea i have never install lunix before mainly done windows an thanks for the incredible tutorails

Karen commented Tue Sep 30 21:02:29 UTC 2008:

got it to work world wide with apache test page still unsure on where to add url to myslq and set up phpmyadmin

[IMG]http://i279.photobucket.com/albums/kk133/klgr2009/Screenshot.png[/IMG]

Don commented Thu Nov 27 17:18:54 UTC 2008:

How would I run a website on my slice? I have installed Apache,php,and mysql?

Derek commented Fri Dec 12 20:44:23 UTC 2008:

You have "php-pecl-memchache" but I believe it should be "php-pecl-memcache".

Thanks for the guide!

Any pointers for installing Tomcat?

Leslie Huszar commented Fri Feb 13 03:41:22 UTC 2009:

I set up Apache and php..Php works at the command line with php-i but does not work with Apache.

What .conf file do I need to edit ...

Thanks

puneet commented Sun Mar 08 04:21:20 UTC 2009:

How do I upgrade to PHP 5.2 ?

robert commented Wed Mar 25 02:54:50 UTC 2009:

i set up apache and php5 with little to no problems at all. It works great! and I am well on my way to getting the rest of my server configured with such freedom and help at every corner using support chat. thanks!

apriyee commented Mon Apr 13 02:15:45 UTC 2009:

i am has been running step by step above but at trying reload, i am looking message reloading httpd [failed]

ok ... im waiting thans ... muachhhhh !!!!

Gautam Kishore commented Thu May 21 12:44:26 UTC 2009:

PO, Thanks for the article.

What if I want to upgrade to 5.2.x on centOS?

Cheers!!

gpowers01 commented Fri May 22 12:24:44 UTC 2009:

These are great starter tutorials...thanks guys.

I also wanted to point out that there are a bunch of howto's at:

http://www.howtoforge.com/

Look for the "perfect server" series on how to setup common server roles...they are great!

Here is one for centos 5.3 i386 (32-bit):

http://www.howtoforge.com/perfect-server-centos-5.3-i386-ispconfig-2

gpowers01

Oliver commented Tue May 26 20:34:52 UTC 2009:

Thanks a lot, i made all you explained and works 100% for me. Great tutorials on your site.

speedy18us commented Sat Jun 13 15:25:12 UTC 2009:

This is my first journey into centos. I've worked before with debian and ubuntu for about 1 year. I can't believe how easy it is to install a webserver. Remains the security issues :). Thanks for the tutorial!

Bob Lakkakula commented Fri Jul 10 15:55:37 UTC 2009:

Some reason, it didn't set PHP to run under Apache. When I try to run php script, apache ignored and simply offering download of the script.

Here is how I setup which worked without trouble

yum install php httpd mod_ssl php-gd php-mcrypt php-pear php-pecl-memcache php-mhash php-mysql php-xml

So it looks like key is asking php and apache to install at same time.

Kwaku commented Sat Jul 11 12:30:41 UTC 2009:

Beautiful article. With the article I have setup my first website

Nikolay commented Sun Aug 02 21:41:57 UTC 2009:

Big thanks to author.

Luke commented Wed Aug 19 01:39:08 UTC 2009:

Hi!

Followed the instructions, had no issues during setup, httpd seems to be running but I can't access my slice through http://123.456.780.123 (fake ip obviously). It looks like Apache has some issues or something?

arrie commented Wed Aug 19 21:24:41 UTC 2009:

I have install apache using this tutor, works great. but where should i put my file? coz i dont see a public_html or www folder in my slice.

BTW, im using ruby on rails

Jandost commented Thu Sep 10 01:13:01 UTC 2009:

Hi,

I followed all the steps and I get the results as well, but when I browse my domain name it says "Oops! the link is broken". What could be the possible reason?

Note: I got all the output same as yours.

Gustavo commented Wed Jan 13 20:19:39 UTC 2010:

Hello to all, I've the same problem that Luke, I've been followed all the step and it doesn't works, can you help me, please?

Denis commented Thu Jan 14 11:02:40 UTC 2010:

The Tutorials are GREAT!!!

After I've installed PHP as per the "CentOS - Installing Apache and PHP5", I know that php is installed and with phpinfo.php I see that php is there....

So - now - what configurations do I need to do in Apache so that my php files work on my site? If someone can path me in the right directions - it would be very much appreciated.

Regards Denis

Sonteklif commented Tue Apr 13 08:11:21 UTC 2010:

Great article, thanks a lot! I have been googling and this one is the best and the most clear article.

lbuck commented Tue Jun 08 23:00:35 UTC 2010:

For anyone who mentioned not being able to receive apache traffic after following the above instructions and verifying apache is installed and running...

I had the same problem, turns out it was related to the firewall rules. I needed to update iptables to allow traffic on ports 80 and 443. This article was where I found the answer: http://articles.slicehost.com/2010/1/19/barebones-apache-install-for-centos

Sam commented Sun Aug 22 06:08:43 UTC 2010:

For those who would like to upgrade to PHP 5.2 on CentOS via Yum, see: http://forum.slicehost.com/comments.php?DiscussionID=4696

JOGLOHosting commented Wed Jan 12 23:26:01 UTC 2011:

I try this article and success, whch the best prefork or worker?

Jered commented Thu Jan 13 22:14:37 UTC 2011:

JOGLOHosting, if you're using PHP with apache the safest MPM will be prefork. That way you don't have to worry about whether or not the PHP modules you're using are "threadsafe". You can get a little more info on the differences between prefork and worker MPMs in this article.

vinod commented Thu Mar 10 13:34:19 UTC 2011:

toooooooo good . but when i am putting http:myhotname:8080 its showing page cannot displayed . how can i get it ?

Jered commented Thu Mar 10 16:15:37 UTC 2011:

Well, the default port for apache is 80, so it may not be listening to 8080 unless you specifically configure it to do so. If you did configure apache to listen to port 8080, then you might check "iptables -L" to see if your firewall is preventing the connection.

Julius commented Sat Aug 06 04:54:09 UTC 2011:

Great tutorial for beginners! I have installed centOS in my virtual box and it works fine.

Gustaf commented Thu Oct 13 13:05:10 UTC 2011:

Nice article! I may have a newer version of centos (6) but i had to runt: "sudo yum install php" to get it all to work.

Trevor commented Thu Feb 02 19:17:15 UTC 2012:

Hmmmm, I followed this, and then set up my own virtual host, but connecting to the IP address times out. Any ideas?

richi commented Fri Mar 30 11:42:41 UTC 2012:

Thanks a lot men! All works great job, i'm really thankfull for this article

mark commented Mon Apr 30 14:28:03 UTC 2012:

I followed all the steps and I get the results as well, but when I browse my domain name it says "Oops! the link is broken". What could be the possible reason?

Jered commented Thu May 03 19:10:28 UTC 2012:

I'd start troubleshooting that by checking out the DNS for the domain. Make sure the domain resolves to your server's IP address.

If it's not the DNS, check the access log for apache. See if you're getting an entry with a "404" return code, and if so, what apache thought you were asking for. It could be that the default page value is set to look for a file that doesn't exist in your domain's root directory.

drcreazy commented Tue Aug 28 20:52:44 UTC 2012:

I have installed php 5.4.6 from source and now can't add mod_php properly :(

mm commented Tue Dec 04 06:16:30 UTC 2012:

This I know..

diabgate commented Wed Feb 20 15:37:20 UTC 2013:

je cherchais un document pour configurer mon serveur et vous m'avez sauvé Merci

Firestorm commented Sat Feb 23 14:09:23 UTC 2013:

Thanks! Great tutorial!

argurbalbuddy commented Mon Apr 15 02:21:13 UTC 2013:

I cling on to listening to the news bulletin lecture about getting free online grant applications so I have been looking around for the top site to get one. Could you advise me please, where could i get some?

| Professional Ideas On Major Elements For Juicer Reviews

argurbalbuddy commented Mon Apr 15 02:21:18 UTC 2013:

I cling on to listening to the news bulletin lecture about getting free online grant applications so I have been looking around for the top site to get one. Could you advise me please, where could i get some?

| Professional Ideas On Major Elements For Juicer Reviews

RetaKeewmes commented Tue Apr 23 07:51:24 UTC 2013:

I loved as much as you will receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get bought an impatience over that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this increase.

| Revealed - Effortless Solutions In Juicer Reviews

null commented Thu May 02 10:17:55 UTC 2013:

Do you mind if I quote a few of your articles as long as I provide credit and sources back to your blog? My website is in the exact same niche as yours and my users would genuinely benefit from a lot of the information you provide here.

Please let me know if this ok with you. Many thanks!

Also visit my weblog ... [null](http://www.warriorforum. com/ online money "null")

elektrikli soba commented Thu May 16 11:01:16 UTC 2013:

thank u for share

elektrikli soba commented Thu May 16 11:01:38 UTC 2013:

thank u for share

elektrikli soba commented Thu May 16 11:01:48 UTC 2013:

thank u for share

tiny prints coupons commented Wed Jul 24 16:45:35 UTC 2013:

Can I simply say what a relief to discover a person that truly understands what they are discussing online. You certainly know how to bring an issue to light and make it important. A lot more people must look at this and understand this side of your story. I was surprised you're not more popular because you definitely have the gift.

my web blog; tiny prints coupons

tiny prints coupons commented Wed Jul 24 16:45:42 UTC 2013:

Can I simply say what a relief to discover a person that truly understands what they are discussing online. You certainly know how to bring an issue to light and make it important. A lot more people must look at this and understand this side of your story. I was surprised you're not more popular because you definitely have the gift.

my web blog; tiny prints coupons

MyHosting Review commented Sun Aug 04 16:40:44 UTC 2013:

Wow that was unusual. I just wrote an very long comment but after I clicked submit my comment didn't show up. Grrrr... well I'm not writing all that over again. Anyhow, just wanted to say superb blog!

vans hello kitty femme commented Mon Jul 14 06:19:22 UTC 2014:

Woah! I'm really digging the template/theme of this blog. It's simple, yet effective. A lot of times it's challenging to get that "perfect balance" between user friendliness and visual appeal. I must say you have done a amazing job with this. Additionally, the blog loads super fast for me on Internet explorer. Superb Blog!

My site :: vans hello kitty femme

mikors commented Wed Jul 23 02:11:33 UTC 2014:

Hello There

We are mk watch factory and we sell all kinds of China michael kors watch

We provide drop shipping to all over the world,you can update our photoes on your own website or your facebook,and when you get orders,we will ship directly to your customers

For more detail,welcome to check our website and enjoy your shoppping

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)