Configure Apache HTTPD With PHP-FPM on CentOS 7

Konfigurasi Apache Httpd dengan php-fpm pada CentOS 7

  • Install Apache dan php-fpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install yum-utils -y && yum update -y
yum-config-manager --enable remi-php74
yum install -y httpd php php-common php-fpm php-mcrypt php-cli php-gd php-curl php-mysql php-xml php-mbstring php-zip php-ldap php-xmlrpc php-curl php-fileinfo php-intl php-libvirt php-devel php-pecl-memcache php-pecl-memcached php-pecl-apcu php-pdo

Jalankan service httpd dan php-fpm

systemctl enable httpd
systemctl restart httpd
systemctl enable php-fpm
systemctl restart php-fpm
  • Cek status server MPM, masih menggunakan prefork
[root@web01 conf.d]# httpd -V
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov 10 2021 14:26:31
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"
  • Cek status Server API pada php info lewat web, masih menggunakan Apache 2.0 Handler
<?php phpinfo(); ?>

  • Setting mpm_event_module pada httpd, tambahakan tanda pagar (#) pada baris LoadModule mpm_prefork_module modules/mod_mpm_prefork.so , dan hapus tanda pagar pada baris LoadModule mpm_event_module modules/mod_mpm_event.so
nano /etc/httpd/conf.modules.d/00-mpm.conf
# Select the MPM module which should be used by uncommenting exactly
# one of the following LoadModule lines:

# prefork MPM: Implements a non-threaded, pre-forking web server
# See: http://httpd.apache.org/docs/2.4/mod/prefork.html
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

# worker MPM: Multi-Processing Module implementing a hybrid
# multi-threaded multi-process web server
# See: http://httpd.apache.org/docs/2.4/mod/worker.html
#
#LoadModule mpm_worker_module modules/mod_mpm_worker.so

# event MPM: A variant of the worker MPM with the goal of consuming
# threads only for connections with active processing
# See: http://httpd.apache.org/docs/2.4/mod/event.html
#
LoadModule mpm_event_module modules/mod_mpm_event.so
  • Rename file php.conf menjadi php.conf.bak
mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.bak
  • Tambahakn kembali file php.conf dengan isi file seperti berikut
# Tell the PHP interpreter to handle files with a .php extension.

# Proxy declaration
<Proxy "unix:/var/run/php-fpm/default.sock|fcgi://php-fpm">
        # we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time
        ProxySet disablereuse=off
</Proxy>

# Redirect to the proxy
<FilesMatch \.php$>
        SetHandler proxy:fcgi://php-fpm
</FilesMatch>

#
# Allow php to handle Multiviews
#
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 lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#<FilesMatch \.phps$>
#       SetHandler application/x-httpd-php-source
#</FilesMatch>
  • Edit file /etc/php-fpm.d/www.conf
nano /etc/php-fpm.d/www.conf
; listen = 127.0.0.1:9000
listen = /var/run/php-fpm/default.sock
...
listen.allowed_clients = 127.0.0.1
listen.owner = apache
listen.group = apache
listen.mode = 0660
...
user = apache
group = apache
  • Restart service httpd dan php-fpm
systemctl restart httpd
systemctl restart php-fpm

  • Cek status server MPM, sudah menggunakan event dan threaded
[root@web01 conf.d]# httpd -V
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov 10 2021 14:26:31
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     event
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"
  • Cek status Server API pada php info lewat web, sudah menggunakan FPM/FastCGI

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.