Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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
share|improve this answer

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!

share|improve this answer
    
see updated answer –  GioMac Sep 18 '13 at 0: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 0:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.