## Apache + PHP-FPM + MySQL on CentOS 6.5

At this tutorial/guide/HowTo I will teach you how to install Apache (2.x) with PHP5-FPM and MySQL on CentOS 6. This setup is also well-known as “LAMP” where L stands for Linux, A for Apache, P for PHP and M for MySQL. This tutorial/guide/HowTo might work on CentOS 6.5 (Already test), the rest of it, i don’t know :p

I’m not responsible for any kind of failures or damage!!!

**1\. Installation of additional repositories**

We need to import the Dag GPG key:
    
    
    rpm --import https://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt

Now we’ll install the rpmforge repo:  
for 32bit:
    
    
    wget https://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686.rpm
    rpm -ivh rpmforge-release-0.5.3-1.el6.rf.i686.rpm

for 64bit:
    
    
    wget https://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
    rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

As PHP-FPM can’t be downloaded with the default CentOS repo we need the EPEL and the REMI repo:
    
    
    rpm --import https://fedoraproject.org/static/0608B895.txt

for 32bit:
    
    
    rpm -ivh https://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

for 64bit:
    
    
    rpm -ivh https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

REMI:
    
    
    rpm --import https://rpms.famillecollet.com/RPM-GPG-KEY-remi
    rpm -ivh https://rpms.famillecollet.com/enterprise/remi-release-6.rpm

After that we need to install a plugin for yum that manages repo priorities:
    
    
    yum install yum-priorities

Then
    
    
    vi /etc/yum.repos.d/epel.repo

Add “priority=10” to the [epel] section and save the file

Do the same for REMI repo:
    
    
    vi /etc/yum.repos.d/remi.repo

**2\. Installation of MySQL 5**

Install MySQL 5 with:
    
    
    yum install mysql mysql-server

Then we need to add it to the services:
    
    
    chkconfig --levels 235 mysqld on

Start the service:
    
    
    service mysqld start

Run the secure setup to set a root password and a few others things:
    
    
    mysql_secure_installation

Install Apache with:
    
    
    yum install httpd httpd-tools

Add it to services
    
    
    chkconfig --levels 235 httpd on

Start the service:
    
    
    service httpd start

Apache’s default document root is /var/www/html for CentOS. The configuration file is /etc/httpd/conf/httpd.conf and additional configurations are saved in the /etc/httpd/conf.d/ directory.

**3\. Installation of PHP-FPM**  
Install PHP-FPM and mod_fastcgi:
    
    
    yum install php php-fpm mod_fastcgi

Add PHP-FPM to services:
    
    
    chkconfig --levels 235 php-fpm on

Start the service:
    
    
    service php-fpm start

Restart Apache:
    
    
    service httpd restart

**5\. Configuration of Apache with PHP-FPM**  
Open the fastcgi.conf file:
    
    
    vi /etc/httpd/conf.d/fastcgi.conf

Add this to the end of the file:
    
    
    DirectoryIndex index.html index.shtml index.cgi index.php
    AddHandler php5-fcgi .php
    Action php5-fcgi /php5-fcgi
    Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
    FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization

After that search after “FastCgiWrapper” and make sure it’s set to “off” then save the file.

The /usr/lib/cgi-bin/ directory must exist, so we create it:
    
    
    mkdir /usr/lib/cgi-bin/

If mod_php is installed and enabled, we need to disable it so open the configuration at /etc/httpd/conf.d/php.conf:
    
    
    vi /etc/httpd/conf.d/php.conf

Comment out the AddHandler and AddType lines so it looks like here:
    
    
    #PHP is an HTML-embedded scripting language which attempts to make it easy for developers to write dynamically generated webpages.
    #
    LoadModule php5_module modules/libphp5.so
    LoadModule php5_module modules/libphp5-zts.so
    #Cause the PHP interpreter to handle files with a .php extension.
    #
    #AddHandler php5-script .php
    #AddType text/html .php
    #Add index.php to the list of files that will be served as directory indexes.
    #
    DirectoryIndex index.php
    #Uncomment the following line to allow PHP to pretty-print .phps files as PHP source code:
    #
    #AddType application/x-httpd-php-source .phps

Save the file and restart Apache:
    
    
    service httpd restart

**6\. Install MySQL support for PHP**

Install PHP packages and php-mysql for MySQL support:
    
    
    yum install php-mysql php-gd php-imap php-ldap php-mbstring php-odbc php-pear php-xml php-xmlrpc

Restart PHP-FPM to pickup the new components:
    
    
    service php-fpm restart

Now you should have a working web server with Apache, PHP-FPM and MySQL.  


Is MySQL not starting up?

Edit “my.conf” the MySQL configuration file with a text editor and paste our configuration.

  1. Open “my.cnf”: 
         
         vi /etc/my.cnf

  2. Clean it (delete everything that is inside).
  3. Paste my configuration: 
         
         [mysqld]
         default-storage-engine = myisam
         key_buffer = 1M
         query_cache_size = 1M
         query_cache_limit = 128k
         max_connections=25
         thread_cache=1
         skip-innodb
         query_cache_min_res_unit=0
         tmp_table_size = 1M
         max_heap_table_size = 1M
         table_cache=256
         concurrent_insert=2
         max_allowed_packet = 1M
         sort_buffer_size = 64K
         read_buffer_size = 256K
         read_rnd_buffer_size = 256K
         net_buffer_length = 2K
         thread_stack = 64K

  4. Save the file
  5. Start MySQL: 
         
         service mysqld start




Tunning FPM:  
Open
    
    
    /etc/php-fpm.d/www.conf

Change
    
    
    listen = 127.0.0.1:9000

to
    
    
    listen = /tmp/php5-fpm.sock
    listen.owner = apache
    listen.group = apache

Open
    
    
    /etc/httpd/conf.d/fastcgi.conf

Change
    
    
    FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization

to
    
    
    FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /tmp/php5-fpm.sock -pass-header Authorization