How install and setup Apache web server and PHP5 on CentOS VPS or Dedicated server. This tutorial will show you all necessary steps and most common Unix commands to install and setup Apache and PHP on a server running CentOS distro. This tutorial is part of what you have to do to install LAMP stack, a collection of open source software used to get web servers up and running so you can host all your site’s files and databases in it.
LAMP is an acronym stands for Linux, Apache, MySQL and PHP. While the server is already running one of most favorite Linux distros, so the “Linux” part is taken care of while the rest are Apache, MySQL and PHP but we’ll talk about MySQL in a separate tutorial.
- I assumed you already have either a VPS or Dedicated server running CentOS and you have access to it. In this tutorial I use CentOS 6.3 X32. See my Playground server.
- I also assumed you knew already how to use Putty or Terminal to SSH-ing a server.
- I believe you knew –at least part of– most common Unix commands used to manage an unmanaged server.
How To Install Apache and PHP5
Step 1 – Login to your server and follow my previous guide about Basic setup for CentOS before you build a live web server. You may and may not follow that tutorial but if you followed, it will give you some basic security tweak to your server.
Before you proceed to the next steps, it is better to explain that all commands in this tutorial are written without the “sudo” prefix. However if you disabled root login and you logged in using another username with root privilege, you can add the “sudo” prefix all by your self. Alternatively you can simply type su, hit Enter and type in your password twice to switch as root.
Step 2 – Install Apache web server on CentOS. Issue following command:
yum install httpd
It looks something like this.
Next it will ask you to confirm. Simply type Y and hit Enter.
That’s it. Once installed it will display something like this:
Step 3 – How To Install PHP5? The most basic command to install latest PHP version is following:
yum install php
But however as you gonna setup LAMP stack, so the most used command to install PHP5 and PHP5 MySQL module is:
yum install php php-mysql
Again, it will tell you total file size will be downloaded and ask you to confirm it. Simply type Y and hit Enter.
Once done, it will display the complete! message along with installed PHP module:
That’s it. Now you have Apache and PHP5 installed. But wait, talking about PHP module, there are several most common modules available you may wish to install as well.
Step 4 – How to install PHP modules? Firstly you have to check all available libraries and modules by issuing this command:
yum search php-
It will then show you all libraries with “php-” prefix:
As you can see that next after each module’s name displayed short information about what the module is for. If you wish, you can also retrieve more detailed information about what each module does. Use this command:
yum info php-module-name
Of course you have to replace “php-module-name” above with actual name of the module. For instance:
Now you knew what each module does and the second thing you have to do is deciding which ones you will install. Once you did, issue this command to install it:
yum install php-module-name
Instead of installing each module one by one, you can simply install multiple libraries at once by separating the name of each module with a space. Here’s the example of command syntax used to install common modules:
yum install php-common php-cli php-devel php-fpm php-gd php-imap php-intl php-mysql php-process php-xml php-xmlrpc php-zts
Step 5 – Finally restart the Apache web server so it can reload appropriately:
service httpd restart
Don’t be panic when you see “Stopping httpd: FAILED” message it is normal as that you don’t start the Apache yet.
So what about another error message? If what you mean is the message that says: “httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName”, so here’s the fix.
Issue this command syntax to edit Apache .conf file:
Next you have to find following line:
then add this right after that line:
So it will look like this:
Once done hit Control+O to save then Control+X to exit Nano editor screen. That’s it. Next time you restart Apache it will simply display the OK message. Congratulation, you’ve been succeed getting rid of “httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName” error.
Step 6 – This very last step is not a must but recommended: adding Apache to start up list. Each time you reboot your server PHP will start automatically but not with Apache. Issue command below to make it start each time whenever the server reboots:
chkconfig httpd on
Congratulation. What’s next? You can give it a test by accessing your server in your browser via its IP address. It will display something like this:
Have a question? Do not hesitate to drop us a comment below. Also do not forget to follow ServerMom on Twitter. Gracias!