LAMP

PHP OPCodes Cached with APC – part 2

Before reading this post, I recommend going over part 1 PHP OPCodes Cached with APC – part 1

In the first part we tackled what OPCodes are and how APC helps us streamline webserver performance by caching recurring script execution.

In this second part, we will mostly look at the APC configuration main variable and thus further understand how the APC engine works.

APC cache size and shared memory

apc.shm_segments

By default this value is set to ‘1′; that is so say, there would be only one shared memory segment to allocate to the cache

apc.shm.size

This is the size in MB of each shared memory segment. The default is of 30Mb.

Please note that you cannot set a higher value than the maximum size of shared memory segment defined in your kernel distribution.

cat /proc/sys/kernel/shmmax
33554432

Read the rest of this entry »

PHP OPCodes Cached with APC – part 1

There are many caching system in use to optimize the execution of PHP script on busy web/database servers. Today we will focus on the OPcode caching method using APC.

Before we start… what is an OPcode?

The OPcode is an executable code generated each time a PHP script is interpreted and compiled. Each time you visit a webpage, the webserver (apache for example) would generate an OPcode of the PHP script serving your request. They are therefore simply C data structure which are interpreted by the PHP Virtual Machine (Zend Engine).

Now you can imagine, generating the OPcode can be a drain on the server and quite useless if the code does not change often. This is where the OPcode caching system comes into play; but before we go on, let’s see some OPcode example using the Vulcan Logic Disassembler.

First, we create a file test.php in which we will execute a unix ls -l command

test.php – <? system(“ls -l”); ?>

wrk01:/var/www# php -d vld.active=1 test.php
Branch analysis from position: 0
Return found
filename:       /var/www/test.php
function name:  (null)
number of ops:  4
compiled vars:  none
line     #  op                           fetch          ext  return  operands
——————————————————————————-
2     0  SEND_VAL                                                 ‘ls+-l’
1  DO_FCALL                                      1          ’system’
4     2  RETURN                                                   1
3* ZEND_HANDLE_EXCEPTION

Now let’s try a simple ( echo “hello world!” ) Read the rest of this entry »

5 basic Apache security tips

Here are just few things to keep in mind when setting up Apache and undergoing a quick security audit.

1. Turn off TRACE or TRACK Methods on Apache

A Trace or Track method is a debug method used to debug webserver connections. A server supporting Trace connections can be subject to cross-site scripting attacks, dubbed XST for “Cross-Site Tracing” (cf to http://www.apacheweek.com/issues/03-01-24)

To disable Trace or Track methods… make sure first that mod_rewrite is enabled, then

RewriteCond %{REQUES_METHOD]^(TRACE|TRACK)
RewriteRule .* – [F]

Read the rest of this entry »

MySQL Select Optimization line

Weather used in php or any other scripting language… sometimes we have the need to query a database in order to fetch or manipulate certain data.

One of the most common request, widely written and used is

SELECT * FROM MyTable WHERE ID=’ValueX’ and NAME=’ValueY’;

now, ID and NAME are just columns… so nothing special

However.. how could someone optimize that line of code? Fact is, lots of coders out there, do not realize how unoptimize this line could be and the different problems it could generate.

So how do I optimize this line… although not much syntax will be needed to change

1) Put INDEXES key on both ID and NAME (or whatever are your column)…

2) Find which combination will return less values and use that combination as the first condition…

That’s it :-)

10 tips to optimize your PHP code

1. if you were to use between print or echo… use echo (Echo is known to be faster than print)

2. when doing string searches or action, do not simply/quickly jump on regex, but first have a look at php api’s string functions such as strpbrk, stripos etc..

3. Display smart error messages… A lot of young developers like to display a custom error or show systems errors whenever something break. Although it is good practice to alert the user of any error, keep in mind printing Error cost a lot in resources. Go for general error display then specifics.

4. Close your database connections when you are done processing mysql datas

5. Use variables instead of global variables

6. Always initialize your variables… It seems too common for coders to just declare a variable without initializing it and process it later with increments etc… Remember you are loosing on speed with none initialized variables

7. Whenever you echo a string on the string.. use ‘ ‘ instead of ” “… why? because PHP will look inside the ” ” declaration for any variables “$”… the process is therefore slower

8. Use mem-cached with apache as to cache memory objects. This will highly speed up the runtime execution of your web application

9. Use mod_gzip to compress data delivery

10. Implement data structure as array and not as class

and yes… :-) use less OOP as possible, being a JAVA and .NET programmer, I can guarantee that OOP in PHP is just a big overhead.

till later,