Monday 23 May 2016

HHVM or PHP-FPM on Apache 2.4 ( Centos 7 )

Now i have been going through a lot of blogs lately for this configuration but only found conf. with nginx, none with apache, so i decided to come up with this tutorial!

Lot of architecture are still using apache due to historic reasons one of them being .htaccess file!

Dont want to touch your apache, or whatever reason you want to stick with it, with this you can update your server performance using this tutorial!

On with installations then:

get lamp stack up and running:

yum install httpd
systemctl start httpd.service
systemctl enable httpd.service

yum install mariadb-server mariadb
systemctl start mariadb
mysql_secure_installation
systemctl enable mariadb.service

yum install php php-mysql php-mbstring php-pear

nano /var/www/html/info.php

<?php phpinfo(); ?>

nano /etc/php.ini

# line 878: uncomment and add your timezone

date.timezone = "Asia/Tokyo"

nano /var/www/html/index.php
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
<?php
   print Date("Y/m/d");
?>
</div>
</body>
</html>

systemctl restart httpd.service

Open Firewall if using firewalld or iptables adjust accordingly:

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Now we fix out PHP-FPM:

yum -y install php-fpm

Normally the mod_proxy_fcgi module comes by default installed and enabled on apache 2.4 which can be verified by the following commands:

ls -l /usr/lib64/httpd/modules/mod_proxy*

cat /etc/httpd/conf.modules.d/00-proxy.conf

httpd -M | grep -i proxy

Now this as verified we need to do this to the vhost conf file: ( just insert the red line )

<VirtualHost *:80>

    ServerName localhost

    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1

    DocumentRoot /var/www/html
    <Directory /var/www/html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/localhost_error.log
    CustomLog /var/log/httpd/localhost_access.log combined
</VirtualHost>

systemctl start php-fpm 
systemctl enable php-fpm 
systemctl restart httpd 

This will make the php-fpm on! not getting into fine tuning php-fpm at the moment!

verify if php-fpm is listening on port 9000 by :

netstat -tulpn | grep 9000

Now on with HHVM :

yum -y install epel-release

yum install cpp gcc-c++ cmake git psmisc {binutils,boost,jemalloc,numactl}-devel \
{ImageMagick,sqlite,tbb,bzip2,openldap,readline,elfutils-libelf,gmp,lz4,pcre}-devel \
lib{xslt,event,yaml,vpx,png,zip,icu,mcrypt,memcached,cap,dwarf}-devel \
{unixODBC,expat,mariadb}-devel lib{edit,curl,xml2,xslt}-devel \
glog-devel oniguruma-devel ocaml gperf enca libjpeg-turbo-devel openssl-devel \
mariadb mariadb-server make -y

rpm -Uvh http://mirrors.linuxeye.com/hhvm-repo/7/x86_64/hhvm-3.12.0-1.el7.centos.x86_64.rpm

Check using this is successfully installed:

hhvm --version

Now do this stuff:

nano /usr/lib/systemd/system/hhvm.service

[Unit]
Description=HHVM HipHop Virtual Machine (FCGI)
[Service]
ExecStart=/usr/local/bin/hhvm
--config /etc/hhvm/server.ini \
--config /etc/hhvm/php.ini \
--config /etc/hhvm/config.hdf
--user apache
--mode daemon
[Install]
WantedBy=multi-user.target

mkdir /var/run/hhvm
chown apache:apache /var/run/hhvm
mkdir /var/log/hhvm
chown apache:apache /var/log/hhvm

Create config.hdf in /etc/hhvm:

ResourceLimit {
    CoreFileSize = 0          # in bytes
    MaxSocket = 10000         # must be not 0, otherwise HHVM will not start
    SocketDefaultTimeout = 5  # in seconds
    MaxRSS = 0
    MaxRSSPollingCycle = 0    # in seconds, how often to check max memory
    DropCacheCycle = 0        # in seconds, how often to drop disk cache
}
Log {
    Level = Info
    AlwaysLogUnhandledExceptions = true
    RuntimeErrorReportingLevel = 8191
    UseLogFile = true
    UseSyslog = false
    File = /var/log/hhvm/error.log
    Access {
        * {
            File = /var/log/hhvm/access.log
            Format = %h %l %u % t \"%r\" %>s %b
        }
    }
}
MySQL {
    ReadOnly = false
    ConnectTimeout = 1000      # in ms
    ReadTimeout = 1000         # in ms
    SlowQueryThreshold = 1000  # in ms, log slow queries as errors
    KillOnTimeout = false
}
Mail {
    SendmailPath = /usr/sbin/sendmail -t -i
    ForceExtraParameters =
}

Create server.ini in /etc/hhvm:

; php options
pid = /var/run/hhvm/pid
; hhvm specific
hhvm.server.port = 8000
;hhvm.server.file_socket = /var/run/hhvm/sock
hhvm.server.type = fastcgi
hhvm.server.default_document = index.php
hhvm.log.use_log_file = true
hhvm.log.file = /var/log/hhvm/error.log
hhvm.repo.central.path = /var/run/hhvm/hhvm.hhbc

Create php.ini in /etc/hhvm:

hhvm.mysql.socket = /tmp/mysql.sock
hhvm.server.expose_hphp = true
memory_limit = 400M
post_max_size = 50M

systemctl enable hhvm
systemctl start hhvm
systemctl status hhvm

Verify if the HHVM is listening on port 8000 by :

netstat -tulpn | grep 8000

Once done we need to edit the vhost file like this:

    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:8000/var/www/html/$1

systemctl restart httpd

Thats it!

You can verify this by opening info.php page, if HHVM has served it, it will show you!

Which ever compiler you go with is completely your choice!

This marks the end of tutorial!

Comments are welcome!