2

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?

its_me
  • 225
  • 1
  • 7
  • 23

2 Answers2

2

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
GioMac
  • 4,544
  • 4
  • 27
  • 41
2

Just found out. It's possible to disable PHP processing in a directory (and thereby its subdirectories) by adding something like this in your httpd.conf file:

<Directory "/var/www/example.com/public/uploads">
  # Turn PHP parsing off
  php_flag engine off

  # Make .htaccess ineffective in the said directory
  AllowOverride None

  # Make index.php ineffective
  DirectoryIndex Off

  # (Optional) Show content of PHP files in browser (as if it were a text file)
  AddType text/plain .php .php3 .php4 .php5 .php6 .phtml

  RewriteEngine On

  # Return '403 Forbidden' for PHP files
  RewriteRule \.php$ - [F,L]
</Directory>

If using .htaccess file, drop one in your static content directory (in my case '/var/www/example.com/public/uploads') with the following entry:

# Turn PHP parsing off
php_flag engine off

# Make index.php ineffective
DirectoryIndex Off

# (Optional) Show content of PHP files in browser (as if it were a text file)
AddType text/plain .php .php3 .php4 .php5 .php6 .phtml

RewriteEngine On

# Return '403 Forbidden' for PHP files
RewriteRule \.php$ - [F,L]

Also, I just realized that if your application already uses a caching layer, something like Varnish, for example, Varnish in front of Apache serving cached pages and static content, you don't need to do anything!

its_me
  • 225
  • 1
  • 7
  • 23
  • see updated answer – GioMac Sep 18 '13 at 00:20
  • varnish is just another layer, it still won't cache everything and still needs to request data from webserver serving static content. It doesn't solve general idea. You can say the same regarding anything related to caching, even use memcached, or use apc for php and give it more RAM. This is global architectural improvement, not the apache thing – GioMac Sep 18 '13 at 00:23
  • I believe this is actually the answer the poster was looking for. Definitely produces significant performance improvements in Apache 2.4/windows. Empirically, some requests that would randomly wait for PHP engine to respond for >5.8 seconds before now respond immediately. – Sajid Jul 30 '16 at 23:06