Php – With PHP installed, Apache is (comparatively) slow at serving static content

apache-2.2httpd.confperformancePHPstatic-content

First some background on how I installed PHP, Apache, and some Apache modules:

# apt-get -y install php5 php-pear libapache2-mod-php5 php-apc php5-mysql php5-mcrypt php5-xmlrpc php5-curl php5-imagick php5-gd php5-imap php5-pspell

# apt-get -y install apache2 apache2-doc apache2-utils

# a2enmod setenvif headers deflate filter expires rewrite include

And my httpd.conf file looks like this (i.e. I've essentially disabled .htaccess, and have all the rules in the httpd.conf instead):

<Directory /var/www/example.com/public>

  AllowOverride None

  [...]

</Directory>

Considering that it'd give you a basic idea of how my webserver is set up, I'd like to go ahead and ask the questions:

  1. Is it true that Apache is comparatively slow at serving static content with PHP installed? (I guess.)

  2. Lets say, my website's root directory is '/var/www/example.com/public', and I've all the static content (CSS, JS, images) in '/var/www/example.com/public/uploads'; How do I overcome Problem (1) without having to move all static content to a server that doesn't have PHP installed?

Best Answer

It depends on how do you configure apache with php, how you optimize your configuration. If php is configured with CGI interface, then apache will pass only certain type of files to the php externally (same way as done with nginx for example), so there is 0 impact on other files, in case of module it might be faster with dynamic pages, because it's not calling php externally, but might be slower for others because php module is loaded with apache all the time, despite it's still active only for certain type of files (according to mimetype).

  1. Yes/No

  2. Use mod_fcgid with php

Example of php configured as module, which parses only .php files (RHEL5/6, Fedora):

[root@main ~]# cat /etc/httpd/conf.d/php.conf
#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
<IfModule prefork.c>
  LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>


#
# 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