How to build a website with Apache Mysql PHP(LAMP) on CentOS6 x86&x64

edited August 2014 in Proposed How-Tos
LAMP means Apache MySQL and PHP installed on Linux OS System.

Apache is a web server application notable for playing a key role in the initial growth of the World Wide Web. Originally based on the NCSA HTTPd server, development of Apache began in early 1995 after work on the NCSA code stalled.

Here is the steps to how to install, I used the Centos6.5_x86 as testing Linux system, All steps are running as root:

1. System set-up
To update your system is to run:
[root@vultr ~]# yum update

Then we should check the system iptables setting and disable SELinux.
[root@vultr ~]# vi /etc/sysconfig/iptables
Modify the iptables to allow Port 80 and 443 is open to internet. The inserted rules must right after the line “-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT” . DO NOT add them at the end of file.Here is the modified sample. It only shows modified part of it:
#########################################################

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited

#########################################################
After that we must restart iptables by run this,
[root@vultr ~]# service iptables restart

Then Let's check the CentOS selinux setting is to run:
[root@vultr ~]# cat /etc/selinux/config
We should make the value of SELINUX is disabled. By default you don't need do anything on this.

2. Apache installation
To install is to run:
[root@vultr ~]# yum install httpd
Answer “y” when the screen appeared some messages. Start the Apache is to run:
[root@vultr ~]# service httpd start
It will show some warning messages like this, that is ok, we could fix it later.
#########################################################
Starting httpd: httpd: apr_sockaddr_info_get() failed for vultr.guest
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
#########################################################
Now open the address http://serverip ,You will see the Apache 2 Test Page. If you web browser can’t show the page. It means in the step 1, you have some problem. Please check it carefully.

Tips: You can find your VPS public IP from vultr website control panel or use the following command in SSH:
[root@vultr ~]# curl -s ifconfig.me
If your CentOS is lack of curl command, To install curl is to run:
[root@vultr ~]# yum install curl

3. MySQL installation
To install is to run:
[root@vultr ~]# yum install mysql mysql-server
[root@vultr ~]# service mysqld start
Then do a secure installation to set the root password, To set is to run:
[root@vultr ~]# mysql_secure_installation
Before you run "mysql_secure_installation", you must make sure the mysql is running on your CentOS system. After set the root password for mysql, You could restart the mysql is to run:
[root@vultr ~]# service mysqld restart

4. PHP installation
Run command below to install PHP:
[root@vultr ~]# yum install php php-fpm php-mysql

Then we may need install some PHP modules, You could choose some of them to install, It depends on what you need in the future. Of course you can choose to install these modules when you need it any time later. To install is to run:
[root@vultr ~]# yum install php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath

Now Let's create a testing PHP file (info.php). The file will display lots of useful details about our PHP installation, such as the installed PHP version and PHP modules.
[root@vultr ~]# vi /var/www/html/info.php
##########################################################
<?php

phpinfo();
?>
##########################################################
When you copy it, please manually delete the unnecessary part after "php" in the first line.

5. Configuration
Firstly, We could set the apache mysql and php will run automatically after booting the VPS each time. To set is to run:
[root@vultr ~]# chkconfig httpd on
[root@vultr ~]# chkconfig mysqld on
[root@vultr ~]# chkconfig php-fpm on

Apache main configuration file:/etc/httpd/conf/httpd.conf
Apache other confiration directory:/etc/httpd/conf.d/
Apache website root directory:/var/www/html/
Apache log directory:/var/log/httpd
MySQL my.cnf configuration file:/etc/my.cnf
MySQL database file directoty:/usr/lib/mysql

To fix the Apache start-up warning message, This error is caused by the missing configuration of "ServerName" in "/etc/httpd/conf/httpd.conf". to fix is to change the setting in Line276:
[root@vultr ~]# vi /etc/httpd/conf/httpd.conf
Here is the modified sample. It only shows modified part of it, Of course you can replace 127.0.0.1 with your domain
#########################################################
# If your host doesn't have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make
# redirections work in a sensible way.
#
ServerName 127.0.0.1
#########################################################

After we change anything on them, Please remember to restart them by run these:
[root@vultr ~]# service httpd restart
[root@vultr ~]# service mysqld restart
[root@vultr ~]# service php-fpm restart

6. phpMyAdmin installation:
This step is optional, you can choose to run it or not. phpMyAdmin is a web interface through which you can manage your MySQL databases.

First we enable the RPMforge repository on our CentOS system as phpMyAdmin is not available in the official CentOS 6.5 repositories:
On i386 systems:
[root@vultr ~]# yum install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686.rpm
On x86_64 systems:
[root@vultr ~]# yum install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

phpMyAdmin can now be installed as follows:
[root@vultr ~]# yum install phpmyadmin
Now we configure phpmyadmin. We change the Apache configuration so that phpmyadmin allows connections not just from localhost.
[root@vultr ~]# vi /etc/httpd/conf.d/phpmyadmin.conf
Here is the modified full content sample. (Uncomment Line 5-9)
#########################################################
#
# Web application to manage MySQL
#

#
# Order Deny,Allow
# Deny from all
# Allow from 127.0.0.1
#

Alias /phpmyadmin /usr/share/phpmyadmin
Alias /phpMyAdmin /usr/share/phpmyadmin
Alias /mysqladmin /usr/share/phpmyadmin
#########################################################

Next we could change the authentication in phpMyAdmin from cookie to http(Line 29):
[root@vultr html]# vi /usr/share/phpmyadmin/config.inc.php
Here is the modified sample. It only shows modified part of it.
#########################################################
[...]
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'http';
[...]
#########################################################

Don't forget restart Apache to make the change alive:
[root@vultr html]# service httpd restart

Finally you can access phpmyadmin via http://serverip/phpmyadmin . Finally It will appear a window, input username (default is root) and password (We set it on step 3)

Done!

All steps are tested successfully on CentOS6_i386 and CentOS6_X64 of vultr VPS.

Version infomation
httpd 2.2.15-31.el6.centos
mysql 5.1.73-3.el6_5
mysql-server 5.1.73-3.el6_5
php 5.3.3-27.el6_5.1
php-fpm 5.3.3-27.el6_5.1
php-mysql 5.3.3-27.el6_5.1
Sign In or Register to comment.