Solving Magento

Solutions for Magento E-Commerce Platform

by Oleg Ishenko

Improving Magento Performance with APC Opcode Caching

What is Opcode and why cache it?

As you know, PHP is an interpreted language, and every instruction in your code has to be compiled into intermediate language before being passed over to the web server for execution. This intermediate language is known as Opcode. To see a dump of Opcode generated for your script you can use VLD extension developed by Derick Rethans and available at http://pecl.php.net/package/vld. A sample opcode output for a “Hello World” script would be like this:

<?php echo “Hello”.” “.”World!”; ?>

Output

function name:    (null)
number of ofs:    5
compiled vars: none
line    #  *  op            fetch    ext    return    operands
----------------------------------------------------------------------------------------
   2    0  >    EXT_STMT    
    1    CONCAT                    ~0    'Hello', '+'
    2    CONCAT                    ~1    ~0m 'World%21'
    3    ECHO                        ~1
   3    4    >    RETURN                        1

branch: # 0;    line:    2-    3;    sop:    0;  eop:    4

Similarly every line in your code is compiled down every time your server works on a request. Depending on complexity of your application (and Magento, as we know, is quite complex) this process can take rather long, especially when under heavy load. So why not storing processed opcodes in some sort of a cache and return them when necessary instead of going through the entire routine? This what PHP life cycle looks like with and without opcode caching [1]:

Continue reading

Quickly Create Magento Module Structure with a Powershell Script

Your work on a new module usually begins with a tedious task of creating the basic folder structure. You go to your app folder, cd to code, then to local, then to you working package directory. You add module folder and then etc, Block and Helper. Before you actually get to programming you logic you have to add and edit config.xml, helper.php. And when you are done and ready to fire the first test you remember you forgot to add an xml module file to app/etc/modules.

Sounds familiar? But you are a programmer; so why not try to solve this problem like a programmer?

Its fairly easy to write a shell script for Linux that would do all this dull work for you. But for those who work in Windows (I do) automating this task would mean diving into decades old DOS scripting to create a bat file. Fortunately there is a better alternative: PowerShell. This object oriented scripting framework was first introduced in 2006 and since then had evolved into an incredibly nifty tool for every advanced Windows user.

After some tinkering I was able to produce a script that offers GUI to enter destination app folder, package and module names and creates the basic folder structure and essential files in no time. No installation required: download, unzip, and double-click!

Download it now, its free to use, modify and distribute:

 

Symbolic Links

It is surprising how many people don’t know about symbolic links and do not realize how useful they can be. For instance you are running a test server with multiple instances of out shop configured as separate installations. You can easily update the source code by pulling it from a common repository. But what if you need to update you media files, which normally are not under version control? And what if there are tens of gigabyte of them? You don’t want to copy the files to each installation, instead copy them into one folder and set up symbolic links for the media folders of your test sites:

Creating a symbolic link (syntax):

ln -s [TARGET DIRECTORY OR FILE] [SHORTCUT]

For example:

ln -s /var/media /var/www/vhosts/test01/media

ln -s /var/media /var/www/vhosts/test02/media

and so on…

 

 

Using Macros for Apache Virtual Hosts Configuration for Magento Multistore Environment

When working with a multistore environment it is often necessary to setup several virtual hosts whose configuration is essentially the same apart from one or two settings. Instead of copying and pasting each configuration twice (do not forget about the SSL) one can use a third party extension for Apache web server called mod_macro. This module allows you setting up templates (macros) of your configuration. Your actual vhosts entries will contain references to these templates providing values for variables you defined.

Installing mod_macro on a Ubuntu server is simple by running the apt-get utility:

sudo apt-get install libapache2-mod-macro

Alternatively, download the module and perform a direct installation as described here.

Sample usage

Consider you have to configure two shops running from the same installation. You want them to have two different top level domains (myserver.com and myserver.de) and keep logs separate.
Create a new virtual host configuration file under /etc/apache2/sites-available/ and create your template as follows:

<Macro MyTld $tld>
    ServerAdmin admin@myserver.com
    ServerName www.myserver.$tld
    ServerAlias myserver.$tld
    ErrorLog ${APACHE_LOG_DIR}/error_www.myserver.$tld.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access_www.myserver.$tld.log vhost_combined
    DocumentRoot /var/www/www.myserver.com

    <Directory /var/www/www.myserver.com>
        SetEnv MAGE_RUN_CODE "$tld"
        Options  FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
 </Macro>

Also set up an SSL macro:

<Macro MySSL>
    SSLEngine on
    SSLCertificateFile    /etc/ssl/certs/www.myserver.com-cert.pem
    SSLCertificateKeyFile /etc/ssl/private/www.myserver.com.pem
    <FilesMatch "\.(cgi|shtml|phtml|php)$">
       SSLOptions +StdEnvVars
    </FilesMatch>
    BrowserMatch "MSIE [2-6]" \
        nokeepalive ssl-unclean-shutdown \
        downgrade-1.0 force-response-1.0
   # MSIE 7 and newer should be able to use keepalive
    BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
 </Macro>

Finally add your virtual hosts:

NameVirtualHost *:80
NameVirtualHost *:443

 <VirtualHost *:80>
     Use MyTld com
 </VirtualHost>

 <VirtualHost *:443>
     Use MyTld com
     Use MySSL
 </VirtualHost>

 <VirtualHost *:80>
     Use MyTld de
 </VirtualHost>

 <VirtualHost *:443>
     Use MyTld de
     Use MySSL
 </VirtualHost>

Advantages of using mod_macro are self-evident:

  • smaller configuration files to maintain
  • less bugs due to copy-pastes only partially updated
  • better readability

More information on mod_macro can be found here.

Theme: Esquire by Matthew Buchanan.