Linux 4 All

Best Linux tricks source

Posts in the php category

Here is how to configure PHP-FPM for an Apache virtual host. Just add the following line to the vhost.conf file:

ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/dev/shm/php.sock|fcgi://$DOCUMENTROOT/

The following one-liner will display number of Apache PIDs and sort them after memory usage:

ps aux | awk '$11 ~ /httpd/ {c++; SUM +=$6; print $6/1024" MB, PID:", $2|"sort -rn| head"} END {print c" Total Apache Processes"} END {print SUM/1024" MB Total Memory"} END {print "Top 10 Memory users:"}'

You can avoid your server being compromised by simply disabling PHP execution for upload folders. By default those folders should only contain documents and images, thus PHP execution should not be allowed in there. Here is how:

For mod_php:

php_flag engine off

For fastcgi:

    AddHandler /dev/null .php

Restrict access to whatever.php.old or similar:

    Order allow,deny
    Deny from all
    Satisfy All

It’s easy to run Apache with mod_fastcgi and php-fpm. On a RedHat/CentOS box, assuming you have IUS repository enabled, and php 5.4 installed from IUS, here are the steps:

1. Install and start php-fpm for php54:

yum install php54-fpm

2. Edit /etc/php-fpm.d/www.conf and change to listen on socket to avoid tcp overhead:

listen = /dev/shm/apache-php.sock
service php-fpm start
chkconfig php-fpm on

2. Get mod_fastcgi

tar xvzf mod_fastcgi-current.tar.gz
cd mod_fastcgi-[[tab-tab]]
cp Makefile.AP2 Makefile
make top_dir=/usr/lib64/httpd
make install top_dir=/usr/lib64/httpd

3. Create /etc/httpd/conf.d/mod_fastcgi.conf

# cat /etc/httpd/conf.d/mod_fastcgi.conf
LoadModule fastcgi_module modules/

AddType application/x-httpd-php .php
Action application/x-httpd-php /php.fcgi
DirectoryIndex index.php index.html index.htm
FastCGIExternalServer /dev/shm/apache-php.fcgi -socket /dev/shm/apache-php.sock -flush

4. Disable Apache mod_php

mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.disabled
echo "# PHP is running as mod_fastcgi" >/etc/httpd/conf.d/php.conf

5. Enable PHP engine, by adding the following to your Apache Virtual Host:

# FastCGI handler for PHP-FPM
# See conf.d/fastcgi.conf
Alias /php.fcgi /dev/shm/apache-php.fcgi

6. Restart Apache

service httpd restart