You are here:Home arrow Home arrow PHP arrow Lighten the load for Zend Framework Application - Part I
  • Narrow screen resolution
  • Wide screen resolution
  • Decrease font size
  • Default font size
  • Increase font size
  • default color
  • green color
  • blue color

Lighten the load for Zend Framework Application - Part I

Wednesday, 13 May 2009

This is one of the series of articles devoted to different aspects of optimizing a web application powered by Zend Framework, for best performance.

In part 1, we take a look at the server requirements for running a Zend Framework application and the key server settings that effect performance in production environment.

This article in a form of general advices provides an overview of methods as well as recommendations to run high traffic websites on a middle priced virtual private server.

Warning: this article contains Linux shell commands. Use at your own risk.

According to official Zend Framework website, Zend Framework requires a PHP 5 interpreter with a web server configured to handle PHP scripts correctly. When planning a project, one has to decide a platform used for deployment. A Linux powered system is a common choice. Let's make a checklist of what we need for a common Zend Framework powered web application:

  • Server: you can run a ZF app perfectly fine even on a shared hosting. But the best place for running it is either a virtual private server (VPS) or a dedicated box.
  • The operating system: any Linux distribution that comes without GUI (no need for it in production environment). For this article, we choose CentOS Linux distribution because it comes with the excellent package manager yum
  • Web server: Apache and/or Lighttpd. Lighttpd is best choice for its high speed io-infrastructure allowing to handle great number of concurrent connections. However you do need to consider Apache if your application relies on Apache modules which Lighttpd does not have analogues for. (Lighttpd has alternative to mod_rewrite though). On CentOS lighttpd can be installed by "yum install lighttpd". Additionally you will have to "yum install lighttpd-fastcgi" for running PHP as FastCGI. You can use default configuration file once installed, located in /etc/lighttpd/lighttpd.conf. Here is a server.modules part of configuration with some modules disabled: server.modules = (
    "mod_rewrite",
    # "mod_redirect",
    # "mod_alias",
    "mod_access",
    # "mod_cml",
    # "mod_trigger_b4_dl",
    # "mod_auth",
    # "mod_status",
    # "mod_setenv",
    "mod_fastcgi",
    # "mod_proxy",
    # "mod_simple_vhost",
    # "mod_evhost",
    # "mod_userdir",
    # "mod_cgi",
    "mod_compress",
    # "mod_ssi",
    # "mod_usertrack",
    "mod_expire",
    # "mod_secdownload",
    # "mod_rrdtool",
    "mod_accesslog" )
    For running Zend Framework application you have to add rewrite rule in configuration file. It is similar to Apache rewrite rule in a way that it rewrites all requests to the index.php that runs your bootstrap. url.rewrite-once = (
    ".*\?(.*)$" => "/index.php?$1",
    ".*\.(js|ico|gif|jpg|png|swf|css|html)$" => "$0",
    "" => "/index.php"
    )
    To enable gzip compression, make sure you have the following lines in your configuration file: #### compress module
    compress.cache-dir = "/var/cache/lighttpd/compress/"
    compress.filetype = ("text/plain", "text/html", "text/css", "text/javascript")
    Next, configure the expire module to send Expires headers with the dates far in the future: $HTTP["url"] =~ "\.(png|jpg|js|swf|css)$" {
    expire.url = ( "" => "access 7 years" )
    }
    This is aggressive caching approach. Users always get files from browser cache after first access via network. Once you change asset files' contents, you will have to rename asset files so that users would download new versions. (i.e. provide versioning numbers when including scripts or css files - scriptname-1.0.1.js).
  • PHP with required extensions. Apart from commonly used modules (i.e., database access - PDO_Mysql), we need a PHP optimizer extension which would provide opcode cache allowing to speed up execution and save time by compiling all scripts just once. Remember, Zend Optimizer does not have opcode cache! It merely optimizes the code while it's running. Some optimizer extensions play nice with Zend Optimizer, some don't. eAccelerator is reported to work fine with Zend Optimizer and reported to be slightly faster and more memory efficient than APC.
  • Configuring server before deploying your app

    Once you get a VPS, you will soon find out that you lack significant amount of memory even with no visitors. To save memory in production environment there are several ways:
    • Disable unnecessary Apache or Lighttpd modules. Every disabled module saves you some amount of memory. As long as you don't use them, why keep them enabled?
    • You can disable InnoDb storage engine in MySQL in case your application does not use it (saves around 100 Megabytes)
    • Installing PHP accelerator extension would help offload server by saving scripts in compiled state. If you are on CentOS, "yum install php-eaccelerator" command in console can help you install the extension for you.

    Configuring server when your application runs

  • Every application is unique, and unique in a way it uses database resources. To effectively function, a clean and correct code is not enough - database should be maintained periodically. Use MysqlTuner script to tweak mysql settings for performance. Simply issue "wget mysqltuner.pl" (that simple) and chmod +x downloaded file, then execute. The script will show you suggestions and will name configuration settings and values that will allow your MySQL server to run more smooth.
  • for Lighttpd there is no .htaccess. You have to configure it for client side performance. Client side performance includes things like sending compressed content, and correct headers with it in order for browser to properly cache non-frequently changing content to its cache

  • To be continued...




Reddit!Del.icio.us!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!
Last Updated ( Monday, 01 June 2009 )

Add comment


Security code
Refresh

< Prev   Next >