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 ago:

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 ago:

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 ago:

Thanks Mark!

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

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 ago:

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 ago:

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 ago:

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 ago:

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 ago:

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 ago:

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 ago:

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 ago:

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

Thanks for the guide!

Any pointers for installing Tomcat?

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)