How to Optimize Your PHP Setup?

Posted on

Currently many big, small applications are running with LAMP technology. Here in an article, I am trying to explain, how to optimize PHP set up. In this we make some changes in php.ini file to achieve the same.
How to Optimize Your PHP Setup?
Things need to disable in PHP
There are several php.ini settings that should be disabled, since they are often used for backward-compatibility:

  • register_globals — This functionality used to be the default before PHP V4.2, where the incoming request variables are automatically assigned to normal PHP variables. Other than the major security issues in doing this (having unfiltered incoming request data being mixed with normal PHP variable content), there is also the overhead of having to do this on every request. So turning this off will keep your application safer and improve performance.
  • magic_quotes_* — This is another relic of PHP V4, where incoming data would automatically escape risky form data. It was designed to be a security feature to help sanitize incoming data before having it sent to a database, but it isn’t very effective since it doesn’t protect users against the more common types of SQL injection attacks out there. Since most database layers support prepared statements that handle this risk much better, turning this off will again remove an annoying performance problem.
  • always_populate_raw_post_data — This is really only needed if for some reason you need to look at the entire payload of the incoming POST data unfiltered. Otherwise, it’s just storing in memory a duplicate copy of the POST data, which isn’t needed.

Disabled this options may cause security issues, but to prevent those we can develop the code which is not depend upon above.

Things need to enable
There are some good performance options you can enable in the php.ini file to give your scripts a bit of a speed boost:

  • variables_order — This directive controls the order of the EGPCS (Environment, Get, Post, Cookie, and Server) variable parsing for the incoming request. If you aren’t using certain superglobals (such as environment variables), you can safely remove them to gain a small speedup from not having to parse them on every request.
  • output_buffering — You should make sure this is on, since it will flush output back to the browser in a large chunk rather than on every echo or print statement, where the latter can very much slow down your request response time.
  • date.timezone — This is a directive that was added in PHP V5.1 to set the default timezone for use with the DateTime functions introduced then. If you don’t set this in the php.ini file, PHP will do a number of system requests to figure out what it is, and in PHP V5.3, a warning will be emitted on every request.

These are considered “low-hanging fruit” in terms of settings that should be configured on your production instance. There is one more thing you should look at as far as PHP in concerned. This is the use of require() and include() (as well as their siblings require_once() and include_once()) in your application. These optimize your PHP configuration and code to prevent unneeded file status checks on every request, which can slow down response times.

Manage include() and require()
While we including files in PHP code, they are check for their existence. The reason to use those method in code to bring the code in your code. The sibling calls of require_once() and include_once() can be more problematic, as they not only need to verify the existence of the file, but also that it hasn’t be included before.

So what to do for deal with this, two ways are:

  • Use absolute paths for all require() and include() calls. This will make it more clear to PHP the exact file you are wishing to include, thus not needing to check the entire include_path for your file.
  • Keep the number of entries in the include_path less. This will help for situations where it’s difficult to provide an absolute path for every require() and include() call (often the case in large applications) by not checking locations where the file you are including won’t be.

Use above to optimize your PHP code. Feel Free to comment on this.

Leave a Reply

Your email address will not be published. Required fields are marked *