Install Apache dengan PHP-FPM dan MySQL di CentOS 6

Salah satu solusi yang paling tepat untuk mengurangi memory footprint pada web server Apache adalah dengan menggunakan PHP-FPM.

Sebelumnya update terlebih dahulu:

yum update

Lalu tambahkan repo berikut, pilih sesuai OS:

//CentOS 6 32 bit

rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -ivh http://www.city-fan.org/ftp/contrib/yum-repo/rhel6/i386/city-fan.org-release-1-13.rhel6.noarch.rpm

//CentOS 64 bit

rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -ivh http://www.city-fan.org/ftp/contrib/yum-repo/rhel6/x86_64/city-fan.org-release-1-13.rhel6.noarch.rpm

Lalu enable remi-release:

vi /etc/yum.repos.d/remi.repo

Enable remi repo secara default:

[remi]
name=Les RPM de remi pour Enterprise Linux 6 - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/6/remi/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/6/remi/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Kemudian install Apache, PHP-FPM. Buat supaya berjalan otomatis ketika reboot:

yum install httpd php-fpm php-xml
service httpd start
chkconfig httpd on
chkconfig php-fpm on

Install mod_fastcgi:

yum install mod_fastcgi

Disable mod_php:

mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.bak

Menggunakan Worker dibandingkan dengan Prefork dengan mengedit /etc/sysconfig/httpd dan hilangkan tanda pagar menjadi seperti berikut:

vi /etc/sysconfig/httpd

HTTPD=/usr/sbin/httpd.worker

Konfigurasi FastCGI dengan mendisable FastCgiWrapper pada fastcgi.conf di folder /etc/httpd/conf.d:

vi /etc/httpd/conf.d/fastcgi.conf

# wrap all fastcgi script calls in suexec
FastCgiWrapper Off

Tambahkan kode berikut di fastcgi.conf:

<IfModule mod_fastcgi.c>
    DirectoryIndex index.html index.shtml index.cgi index.php
    AddHandler php-fastcgi .php
    Action php-fastcgi /php-fcgi
    Alias /php-fcgi /var/run/php-fcgi
    FastCgiExternalServer /var/run/php-fcgi -host 127.0.0.1:9000 -pass-header Authorization -idle-timeout 120
</IfModule>

Konfigurasi /etc/php-fpm.d/www.conf:

vi /etc/php-fpm.d/www.conf

listen = 127.0.0.1:9000
listen.owner = apache
listen.group = apache
listen.mode = 0660

Start PHP-FPM:

service php-fpm start

Buat file info.php di /var/www/html/info.php, tambahkan konten berikut:

vi /var/www/html/info.php

<?php
phpinfo();
?>

Restart Apache dan PHP-FPM:

service httpd restart
service php-fpm restart

Sekarang coba kita kunjungi http://alamatIP/info.php. Server API harusnya sudah tertulis FPM-FastCGI.

Sekarang kita install MySQL server.

yum install mysql-server phpmyadmin
service mysqld start
chkconfig mysqld on

Lalu konfigurasi MySQL server supaya aman:

mysql_secure_installation

Konfigurasi phpmyadmin:

vi /etc/httpd/conf.d/phpMyAdmin.conf

# phpMyAdmin - Web based MySQL browser written in php
# 
# Allows only localhost by default
#
# But allowing phpMyAdmin to anyone other than localhost should be considered
# dangerous unless properly secured by SSL

Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8

   <IfModule mod_authz_core.c>
     # Apache 2.4
     Require local
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     # Order Deny,Allow
     # Deny from All
     # Allow from 127.0.0.1
     # Allow from ::1
   </IfModule>
</Directory>

Ubah authentication ke http dan restart Apache:

vi /etc/phpMyAdmin/config.inc.php

$cfg['Servers'][$i]['auth_type'] = 'http';

service httpd restart

Alamat phpmyadmin bisa dikunjungi di http://alamatIP/phpmyadmin.

Untuk menggunakan socket pada PHP-FPM, ubah pada /etc/php-fpm.d/www.conf:

vi /etc/php-fpm.d/www.conf

listen = /var/run/php-fpm.sock

Serta pada /etc/httpd/conf.d/fastcgi.conf:

vi /etc/httpd/conf.d/fastcgi.conf

FastCgiExternalServer /var/run/php-fcgi -socket /var/run/php-fpm.sock -pass-header Authorization -idle-timeout 120

Author: Danang Probo Sayekti

A self-proclaimed geek, business owner, and part-time blogger.

Comments on “Install Apache dengan PHP-FPM dan MySQL di CentOS 6”