During the creation of a site, we discussed a lot of things not directly related to it, but very useful in our job. In this chapter, we will see how to apply some of these concepts and understand how the others work. We will also look at some of the best extensions developed by the eZ Publish community.
We have discussed APC, both in the cache chapter and in the previous. Now, we will understand better what it is and how it works.
To publish the web pages, eZ Publish has to elaborate on a lot of data. In some cases, this work could turn out to be a CPU-eater and thus slow down the response of the server. For this reason, it is useful to install and use an opcode cache system such as APC. This kind of system will save the PHP intermediate code that is generated by the PHP interpreter in memory, and re-use it when called by the interpreter.
As we can see in the following schema, the page requests are first analyzed, to see if they are declared as either cacheable or not. If they are cacheable, the system will check to see if there is suitable content in the cache, or if it has to create (and save) a new one.
To install APC, we have to be sure that our Linux server has the necessary packages to compile it.
These packages are related to the building system (GCC, Make, Glibc, and so on), that is, the development headers for Apache and PHP.
If our distribution is a derivation of GNU Linux Debian (like Ubuntu), we can install it using apt-get
, by using the root account or a user in the sudoers group.
sudo apt-get install build-essential
sudo apt-get install php5-dev apache2-dev
Otherwise, if we have a Red Hat based distribution (such as Fedora or CentOS), we have the building packages installed by default. We only need to install the PHP and Apache development packages, by using a root account.
yum install php-devel httpd-devel
After we are sure that we have all of the required packages, we can execute the following commands from a shell, in order to download and compile APC from the source code:
cd /usr/local/src
wget http://pecl.php.net/get/APC
tar -xzf APC-x.x.x.tgz
cd APC-x.x.x
phpize
./configure enable-apc-mmap
make
sudo make install
Another way of installing APC is by using PECL. This is a module repository for PHP, with a lot of different modules. Moreover, when installed, PHP has a PECL shell command available by default, which can be used to install or remove modules.
As for the source code, we have to ensure that we have the PHP development packages before installing APC.
The installation should be done by a root user from the command line, by executing the following code:
pecl install apc
APC has a lot of features, all of them well explained in the online manual (http://php.net/manual/book.apc.php). We will now configure our installation to work with eZ Publish.
To do this, we have to create a file called apc.ini
, and place it in the correct path in the base of the Linux flavor we are using:
/etc/php5/conf.d/apc.ini
/etc/php.d/apc.ini
Now, in the file we have to enter the following lines:
# Load APC extension
extension=apc.so
apc.enabled = 1
apc.shm_size=64
apc.filters=cache
apc.file_update_protection = "0"
apc.include_once_override = "1"
Whereas the first two lines simply enable the module, the others are required to optimize APC for our purposes.
We will now describe this code, line by line:
apc.shm_size
: This defines the size of each shared-memory segment, in MB.apc.filters
: This specifies a list of comma-separated values of regular POSIX
expressions. If any pattern matches a source filename, that file will not be cached. In our case, we are configuring APC to not cache the eZ Publish cache.apc.file_update_protection
: The file_update_protection
parameter is used to lock the file for a certain time slot. By default, this time slot is two seconds, but we can also deactivate it (with the 0 value) if we are sure that our file will be accessed (or modified) with atomic instructions (as rsync does).apc.include_once_override
: This value allows us to enable the optimizer for the PHP require_once()
and include_once()
functions, in order to avoid the expensive system call used.If we install APC with PECL, we will find a file named apc.php
in the /usr/share/php
folder. This file can be used to analyze the APC works. To use it, we have to copy the file in the HTTP server directory (such as /var/www/)
and then browse it using our preferred browser.
The result will be as shown here:
Other than the cache, with this script we can also check the memory usage to better configure the shm_size
parameter for the server that we are using.
As a simple example, we will run the site with and without APC, on our development laptop, using ApacheBench Version 2.3 (http://httpd.apache.org/docs/2.2/programs/ab.html). Here are the results:
Without APC |
With APC |
---|---|
|
Time taken for tests: 8.112 seconds
Requests per second: 12.33 [#/sec] (mean) Time per request: 405.600 [ms] (mean) Time per request: 81.120 [ms] (mean, across all concurrent requests)
|
By enabling APC, we can see that the execution time for running the tests is half, and that the requests for the second are twice that of the previous one.
3.17.79.206