How To Build Working CentOS Server with Apache and PHP
| |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.
Prerequisite
- 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:
nano /etc/httpd/conf/httpd.conf
Next you have to find following line:
#ServerName www.example.com:80
then add this right after that line:
ServerName localhost
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!
I have a website and I purchased a domain name through anydomainsdotcom and my question is how can I get my domain name to not resolve to an ip address in the address bar. I would like to keep my website name up there instead but it always resolves to my home IP address.
So sorry but I don’t really understand about the question. Did each time you typed your domain name in address bar then hit enter it redirects to an ip?
Hi,
There is no httpd.conf file in my server. What should I do?
Regards,
Arpan
have you really check it? 🙂
I followed your instruction, installing LAMP via remi. Now Joomla posted an error everytime I attempt to install the system. Database connect Not supported. One of the suggestion is to install php-mysql, however error goes when I executed the command. like this:
Error: Package: php-pdo-5.3.3-22.el6.x86_64 (base)
Requires: php-common(x86-64) = 5.3.3-22.el6
Installed: php-common-5.5.0-1.el6.remi.x86_64 (@remi-test)
php-common(x86-64) = 5.5.0-1.el6.remi
Available: php-common-5.3.3-22.el6.x86_64 (base)
php-common(x86-64) = 5.3.3-22.el6
Error: Package: php-mysql-5.3.3-22.el6.x86_64 (base)
Requires: php-common(x86-64) = 5.3.3-22.el6
Installed: php-common-5.5.0-1.el6.remi.x86_64 (@remi-test)
php-common(x86-64) = 5.5.0-1.el6.remi
Available: php-common-5.3.3-22.el6.x86_64 (base)
php-common(x86-64) = 5.3.3-22.el6
You could try using –skip-broken to work around the problem
You could try running: rpm -Va –nofiles –nodigest
then install php-mysql module. try yum install php-mysql
Hi,
After changed my SSH login user name and disabled root access, I couldn’t install Apache with “yum install httpd”, why is that?
Thanks!
You should try sudo yum install httpd and when prompted please enter the password.
Hi! so I followed this to the letter and when i type in my ip address it fives me a 404 page.
Obviously i missed something but I am having an issue. I added the add on modules you suggested, restarted it, added the local host option, etc. Is there a common mistake people make when doing this?
I can give you my ip if you want to take a look.
Great blog! I used all this information and it worked perfectly. =)
I had configure apache on centos as described by you, its working fine under localhost but I am unable to open site from external . I had already configured dns and its working fine, i can ping to my server with my website, but i cant open it . kindly help
Router Firewall ??
The more modules installed, the more possibilities to leave security hole?
This is a great tutorial, thanks!
However my website did not load when I went to the IP in the browser (but it replied to pings).
It appears that by default (on my system at least), the CentOS firewall is configured to accept SSH traffic, but not HTTP traffic. I followed the instructions on this page http://serverfault.com/questions/402158/how-to-setup-virtualhost-for-domain-on-centos-6-2-using-apache2 to allow HTTP traffic and now my website is available in the browser window.
Just thought I would share this in case it might help anyone else.
Sweet! Thanks for sharing 🙂
Yes! Thank you! Firewall was blocking. Once WWW was allowed, it works in my external browser.
Thank you so much for this!!!!
By default, only port 22 is open due to security for CentOS
you just need to edit /etc/sysconfig/iptables/
add this line
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80-j ACCEPT
Yeah, @Sawiyati you may also include this step to “OPEN PORT 80 for apache to accept the browsers”, as newer version of CentOS like 6.7 or greater has disabled 80 port (I guess 😛 ).
This might help, as it did for me:
$ sudo iptables -I INPUT -p tcp -m tcp –dport 80 -j ACCEPT
$ sudo service iptables save
Thanks and Welcome.
I had the same issue Linda had with iptables blocking all communication other than my custom-defined SSH port. I used the following guide to open the standard ports that web servers use (which I’m hoping with alleviate any issues further on in the tutorial by opening the ports ahead of time). The guide also includes some rules to block some common attacks and what-not.
How To Setup a Basic IP Tables Configuration on Centos 6
Hello Sawiyati, your full tutorial on centos help me to configure my whole new dedication server without any prior knowledge in Linux. first thank you for thousand times. Could you please help me, how to make the keepalive on in my Apache server, as it creates some troubles in my websites.
Thanks in advance.
thx for your suggestion. I’ll consider to write a tutorial about that in the near future. Stay tuned 😉
excellent thank you – I am learning CENTOS and this helped greatly thank you for your time. Very exact and step by step perfect.
any time 🙂
You are leaving something out, have been starting from scratch and followed this guide multiple times and still cant connect from client to the server.
of course you’ll need a DNS server to make your domain pointing to your server. I wrote about that 🙂
For those having problems with not working apache:
You have to open port 80 to reach your server from remote systems.
– To do that just edit iptables with following command:
vi /etc/sysconfig/iptables
– Add the following line and save and exit:
A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
[…]
Then, restart iptables with following command:
service iptables restart
This got my apache up and running 🙂
Typo! Should start with a dash!
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
Careful! That is 2 hyphens in front of ‘state’ and ‘dport’. The text editors are turning those double-hyphens into dashes. When you copy and paste into your iptables, DELETE the dash character and make it 2 hyphens!
Not sure if this will show correctly, either:
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
Nope, the editor jack mine up, too.
hello, i have followed your tutorial 100% . and when i try to connect to the ip via my web browser, i cant… also i cant connect to the ssh… how can i get this fixed ? 🙂
are you sure your vps is not down? 🙂
Yes, its on, and it isent a vps, i use a server on the school to set it up on, and when i type in the ip, it is not geting to the apatch server… so my quesstion is, is it a way that i can check if everything is working? so far ?
So shortly you can’t connect to your vps no matter via ssh or browser, right? Try reinstalling your vps, if then you can access via ssh again, it seems there is something wrong with your LAMP configuration 🙂
Alredy done that 3 times, but how can i fix LAMP conf ?
I put in my static ip address in the web browser and I message Ooops from google chrome I have tried others as well. Please help
eth0 Link encap:Ethernet HWaddr A0:B3:CC:FE:5E:74
inet addr:69.34.236.188 Bcast:69.34.236.255 Mask:255.255.255.128
inet6 addr: fe80::a2b3:ccff:fefe:5e74/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7323 errors:0 dropped:0 overruns:0 frame:0
TX packets:5672 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:764351 (746.4 KiB) TX bytes:902924 (881.7 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Thank you for the wonderful website!
To fix the IPTABLES error in /etc/sysconfig/iptables
pasting this corrected line worked for me:
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
Very good, I will proceed to the next tutorial..
I successfully installed everything.
But when i try to open my my website using the SERVER IP ADDRESS, it shows: webpage not available.
make sure Apache is running. Try restarting it up using “service httpd restart”
I am un- aware with this process of making an unmanged server to a fully managed and workable machine anyone of you can guide me the process and steps that how can i make an unmaged to managed server. Thanks
Thanks for such a great tutorials. I followed your tutorials and was able to set up the web server and I am able to see the test page. There is something I want to do with the running web server and hope you can help me. I am running an application on http://192.168.1.4:8090/myApp on tomacat. I want to access this applcation on any computer on my network. How do I do that using apache webserver. Thanks
Great tutorial !
but i have an issue, i am not able to access my ip address through my mobile browser, does anyone have any clue on that?
Cool. Just found this site. Thank you for your wonderful tutorials. I will try this tonight on a Raspberry Pi running Pidora. Folks just starting their Journey of Linux should seriously consider getting one of these.
Thank you for the tips. After running my PHP on Centos7.. I was unable to bring up the webserver page.. Any idea what I am missing?
Have you tried installing httpd? You can try yum install httpd
Good tutorial.
However, I did struck at one point. When I accessed not as a root but as a new user, I had to change the line root@localhost to newuser@localhost in the /etc/httpd/conf/httpd.conf file – as an additional step in order to allow the connection pass to my page as a non-root user.
Ive installed Vtiger CRM on a cent os server and can access it by using https://localhost/vtigercrm/ but i cant access it when using another PC within the same network using https://192.168.1.10/vtigercrm/ …Kindly assist.
For those who is having port/firewall issue,
try 2 commands below,
# service iptables stop
# systemctl stop firewalld
thanks Sean
this worked out for me 😀
I cant get my access into my server when adding,
myipserver:2030/
Im using chrome, and it cant be reach, please reply into my email
thank u so much, im from indonesia
Ports 80 and 443 are open in the CentOS iptables but i cannot access the Apache test page from a computer on the same network. I know it’s working because I can access it on the browser on the server machine itself but not on another computer on the same network.
have you configured your network properly?
Hi Sawiyati
I got stucek on last step. When I access my ip, the webpage not shown up like your screenshot on the last step.
I have been added the port 80 and ping via terminal the ip responded. But when access through browser client the ip cannot be access “the server taking long respond”
Please help.
Thanks
The problem have been resolve, The problem is from the hosting server doesn’t open port 443.