• Sales: 1-800-961-2888
  • Support: 1-800-961-4454

CentOS - Apache and PHP Install


Contents

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

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

sudo /usr/sbin/apachectl 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 (or whatever address you see there, usually your main IP address) is used as the server name 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'll find a section that starts with ServerName and gives the example:

#ServerName www.example.com:80

All you need to do is enter your Cloud Server host name or a fully-qualified domain name:

ServerName demo

Note that my Cloud Server host name is “demo”.

Now just reload Apache:

sudo /usr/sbin/apachectl restart

And the warning has gone.

Firewall

Notice that in some versions of CentOS, a firewall is installed by default which will block access to port 80, on which Apache runs. The following command will open this port:

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT

Remember to save your firewall rules after adding that instruction so your web server will be accessible the next time you reboot:

sudo service iptables save

For more information on firewalls and their configuration, it is strongly recommended to read the Firewalls section of our knowledge base.

Default Page

If you navigate to your Cloud Server IP address:

http://123.45.67.89

You will see the default CentOS Apache welcome screen:

 centos_apache_welcome.jpg

This means the Apache install is a success.

Chkconfig

Now that we have Apache installed and working properly, we need to make sure that it's set to start automatically when the Cloud Server 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

The setting works.

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-mysql php-devel php-gd php-pecl-memcache php-pspell php-snmp php-xmlrpc php-xml

Once done, reload Apache:

sudo /usr/sbin/apachectl restart

Almost done

The last thing we need to do is configure Apache for our setup so we can host multiple sites. We'll cover that in the next article in this series.



© 2011-2013 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER

28 Comments

Your link under "Almost Done" at the bottom of the article is broken :)

Gah, thanks for pointing that out Nathaniel. I'll get it fixed.

This iptables parameters work for my centos 5.5

$iptables -I INPUT -p tcp --dport 80 -j ACCEPT

The one posted up there did not.
forgeting to save it means you will need to run it after reboot cos it will all be gone
save it like this

$service iptables save

Agreed, the instructions would be better if they included the "service iptables save" command after editing the firewall.

An excellent point about saving. Edited the article to add that instruction. Thanks!

Can you suggest the correct repo config to upgrade php? Instructions from CenOS wiki return errors.

I assume you mean the wiki entry at "http://wiki.centos.org/HowTos/PHP_5.1_To_5.2". You should be able to get away with just making the changes to the "CentOS-Testing.repo" file it describes, then running a "sudo yum update" (or install PHP at that point to get the newer version). That's the most common approach.

If you are running into problems with the dev repository you could try looking for someone else's RPMs instead. For example, this person created RPMs for PHP 5.3.2 to run on CentOS: http://forum.slicehost.com/comments.php?DiscussionID=4603

I get an error that the module is not available for php-mhash and php-mcrypt. What gives?

The php-mcrypt package should be good, but php-mhash did get bundled into another package. I'll update the article with a different install command:

sudo yum install php-mysql php-devel php-gd php-mcrypt php-pecl-memcache php-pspell php-snmp php-xmlrpc php-xml

If you're still getting an error, let me know which version of CentOS you're using and we'll see what else we can find out.

Still getting an error no package mcrypt available

164.177.158.78 Red Hat Enterprise Linux 6.1

Please help

Looks like Red Hat dropped php-mcrypt from their repository for 6, so I removed it from the install command for popular PHP modules. Sorry for not catching that sooner.

If you do require php-mcrypt you can install the EPEL repository for yum/rpm and then the package will be available. You can install EPEL by running:

sudo rpm -Uvh http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm

After running that you should be able to "sudo yum install php-mcrypt". It will ask you to accept the GPG key for EPEL if you do - that's normal, and should only happen the one time.

hello,

i tried the last suggestion and got the message below. i am on centos 6 and the reason i am wanting php-mcrypt is that phpMyAdmin shows an error message without it:

The mcrypt extension is missing. Please check your PHP configuration.

[root@**** ~]# sudo rpm -Uvh http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm
Retrieving http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm
curl: (6) Couldn't resolve host 'download.fedora.redhat.com'
error: skipping http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm - transfer failed

thank you.

The domain name for the EPEL repository has changed. I believe you'll want to remove the old EPEL RPM from your system and install a newer one.

You can see [our article on adding EPEL](http://www.rackspace.com/knowledge_center/content/installing-rhel-epel-repo-centos-5x-or-6x) for details, but the download links to the current RPMs are...

CentOS 6:
[http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm](http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm)

CentOS 5:
[http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm](http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm)

hi all,

gradually moving through the error messages on phpmyadmin:

have successfully intsalled EPEL and remi repositories and i think now when i type in basic yum install commands it fetches more recent versions of software which is great. i installed php 5.4 for example by yum install php, and installed standard sql via yum install mysql-server. then had to install php-msqli, php-mcrypt and php-mbstring in the same way as phpmyadmin was recognising that they weren't there. a few little 'conflicts' i am coming across now, one mainly being:

Your PHP MySQL library version 5.5.11 differs from your MySQL server version 5.1.61. This may cause unpredictable behavior.

there's very little on google for this error message, so just wondering if anyone could help me sort this one out!

thanks!

figured it out! woo hoo!

in putty, type:

yum --enablerepo=remi update mysql

then restart apache:

service httpd restart

then (and this is the important part, otherwise the same warning message is displayed):

clear your browser cache!

then the warning message goes away!

(something i did before this step as well was just yum update, and installed everything that was returned, so that was a bit drastic, and people looking for a solution may not want to try that without having backups etc).

hello,

in the tutorial it says that after starting apache you will get this message:

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

however where it says 127.0.0.1 i get my domain ip address - maybe the tutorial should be updated to reflect this? and if it is displaying my domain ip address, does that mean i don't have to change it?

thanks!

I'm not entirely sure why the tutorial says 127.0.0.1, unless they mean it as a placeholder for the external IP address. The behavior you saw is the normal one: It will display your "main" IP address in that message.

You'll still want to set a hostname in apache's config so that it will stop displaying that message when it restarts.

Hi needed to add this line before PHP would work for me on the Red Hat server:
sudo /usr/sbin/apachectl graceful

Recommend adding to this tutorial. Thanks!

When I try to restart apache by running the following:

sudo /usr/sbin/apachectl restart

I am getting the following error message:

apachectl: Configuration syntax error, will not run "restart":
httpd: Syntax error on line 104 of /etc/httpd/conf/httpd.conf: <IfModule takes one argument, Container for directives based on existance of specified modules

I'm not sure how to remedy this; any ideas?

GAH!
Your tutorial is the second one I've come to while trying to install Wordpress on Centos6

Originally, typing in my server's IP in browser would get me no page at all

After following your tutorial, I now get a "Error Establishing Database Connection" error

Any idea how to fix? I'd love to get this working soon, I need to install Wordpress and PHPBB3!

John,

Sorry to hear you are having trouble. When you get the "Error Establishing Database Connection" there can a number of issues however as you referenced this error in the context of setting up Wordpress I will offer the following bits of advice. First ensure your database service is installed and running, this can be done with ps aux | grep <service name>. If your database is up and working correctly, ensure the database and user/password you are including in your wp-config.php exist within the database. If they do not exist in the database, login to the database shell and add these with the CREATE DATABASE and CREATE USER commands. Hope this helps. Remember Rackspace offers 24/7 support toll free at 1-800-961-4454.

Thanks,
Evan
Rackspace Product Evangelist

I'm having trouble installing php5 into CentOS-64, using Linux. Every time I enter the sudo yum install code I keep getting this error: "Existing lock /var/run/yum.pid: another copy is running as pid 2599" and it just loops round. Any ideas how to fix this? I have successfully installed mysql and apache, just having issues with php5.

A file ending in ".pid" usually tracks the process ID of a running program. So in this case, "yum.pid" would be a file tracking an already-running copy of yum (or that tracked a previous instance of yum but wasn't deleted when yum finished).

It's possible there's an old copy of yum still running or that there's a hung process - run "ps -ef | grep yum", and if there's a yum process running, try killing it off. If not, then you might need to just remove the "/var/run/yum.pid" file and see if that lets you run the yum install without issues.

anyone else noticed the 890 in the clound ip address ???

just kidding.. thank you so much for this amazingly detailed tutorial :)

Heh. This wasn't one of mine, but I sometimes do that with sample IP addresses to make absolutely sure they don't actually point somewhere. It might be more of a distraction than a boon, though, if it jumps out like that. I'll clean it up (and get that screenshot size under control too).

Glad to hear the article was helpful!

Awesome! you save my hair!

Thank you for putting this quality article out. It was incredibly helpful and worked like a charm!

Problem Solved.

Add new comment