Php ini optimization speed

Php ini optimization speed

eLabFTW

Posted on Mar 14, 2019 • Updated on Oct 16, 2020

Php ini optimization speed

This post is intended for PHP devs. I'll show you four ways to improve the speed of your PHP app easily. I'll skip the part where I tell you to use PHP 7, you must know by now that the speed improvement is dramatic… (and PHP 5.x is EOL anyway so…)

TL;DR: -a flag for composer, use opcache, use template engine cache, use fully qualified function names.

0. Use Composer optimization

The prod and dev environments are a little bit different. It shouldn't be a surprise to you if I tell you that you should not install the dev dependencies in prod, right?

composer install --no-dev

Enter fullscreen mode Exit fullscreen mode

That's basic. But did you know you can optimize the autoloader? Because the classes won't be changing once it's deployed, you can add a flag to composer (-a) that will improve the speed of autoloading:

composer install --no-dev -a

Enter fullscreen mode Exit fullscreen mode

Ok, but what does it do you'll ask?

From the help it says: "the Composer autoloader will only load classes from the classmap". It also implies "optimize-autoloader" so you don't have to add it too. You can see it as a way to say "hey, no more classes will be added so you don't need to scan the filesystem for a new class, just use the ones in the autoloader".

1. Use opcache

When a PHP file is read, it is converted in opcode, and then executed by the engine. Because in prod your PHP files won't change, you don't want to convert them to opcode every single time. Enter opcache.

Using opcache can dramatically increase the speed of your PHP application. Make sure that opcache_enable=1 is uncommented in your php.ini file. It will store the opcode for the executed files.

But don't enable it in your dev environment ;) (unless you enable opcache.validate_timestamps and set opcache.revalidate_freq to 0 (thx u/iluuu))

I recommend the opcache-status tool to monitor the use of opcache (at least in the beginning).

Php ini optimization speed

2. Use a template engine with cache

Templating engines like Twig can create a cache of the generated PHP code. The speed gain here can be dramatic. Again, you only want to cache the templates in prod, not in dev. But make sure that the cache is setup. For Twig see the documentation.

3. Qualify standard functions

Normally your code is namespaced (right?). So what happens when you call a function from the standard PHP library? The "compiler" (opcode producer) will look into the current namespace, then go up, and eventually use the global namespace. This means that if you add a "\" in front of standard functions (so effectively namespacing it in the global namespace explicitely), it will result in less opcode instructions, and that means faster code execution. Think it's one of those useless micro-optimization like the use of single vs. double quotes? You're correct, your app won't suddenly be faster (only very marginally), but if we can save CPU cycles, why not do it?

Note also that I prefer to add use function count; (for the count() function) in the use block, it's prettier than using \.

Conclusion

The little things I'm showing in this post won't make your app faster, at best it'll improve marginally. You can consider them as "good practice" because why not take some speed gains if you can, but if you want to get serious about optimizing your PHP app, get a profiler and optimize your SQL queries, because that's what is causing an issue, not the few milliseconds you might get with this kind of things ;)

That's all folks, have fun coding!

And if you like PHP, I'm always happy with contributions on the eLabFTW project, an open source electronic lab notebook. =)

Php ini optimization speed

Tips to Make Your Site Run Faster – Part 2

This post will focus on improving the server side performance of your website. See Part 1 for the client (or browser) side improvements (CSS and Javascript).

From reducing bottlenecks to getting the most website speed out of your dedicated server, these tips will put you on track for a fast environment.

Upgrading your dedicated server hardware is a great way to improve your website, but sometimes that’s not efficient or wise for a business. A fine-tuned configuration can significantly improve website speeds. After altering HTML and CSS on the client side of your site, focus on Apache, MySQL, and PHP changes. Following these tips will not only give your website the appearance of loading faster, but will actually deliver a boost similar to an expensive hardware upgrade.

Optimize MySQL

Databases tend to get larger as your website grows, and queries take longer to execute due to vast amounts of data being stored. You’ll want to tweak your MySQL settings to increase overall speed of your database server, and take advantage of indexes to reduce the time a query needs to run. A properly configured my.cnf file will also go a long way when optimizing your MySQL server. For most servers, MySQL tends to be the biggest bottleneck; so you’ll want to place a lot of focus improving the reliability and speed of your MySQL queries.

Index frequently accessed data. When you know a table is going to be accessed a lot, it’s best to create the index as soon as you create the table. Tables that store a lot of information will benefit from indexes because it will speed up the time to perform queries when the data needs to be retrieved. The following example is how you create an indexed table for people storing their name, date of birth, and a unique ID.

CREATE TABLE people (
name VARCHAR(32),
dob DATE,
unique INT, INDEX (id)
)

Creating indexes can be done during the table’s creation, or after the table is created. It can also be managed through PHP while generating new unique tables that will be accessed frequently. To add an index to an existing table, the following would be used.

ALTER TABLE people (
ADD INDEX id(unique),
ADD INDEX name(name);
)

These methods are very simple, and large tables should always be indexed when there’re a lot of requests to retrieve data.

Consider modifying the default my.cnf file often located in /etc/my.cnf. Don’t forget to make a backup. Each configuration will be unique to each distribution, so read up on MySQL and find the best settings for your server. If you have dedicated server management with us, our level 3 techs will give you the best configuration for your server. Tell us about your environment, and submit a ticket and let us speed up your server’s performance.

Optimize PHP & Apache

Apache and PHP are both highly scalable in most environments, and are very powerful options to consider when deploying a dedicated server; however they both come with most modules and extensions enabled by default. You’ll want to fine tune your configuration to get the most speed out of your server, so you’ll have to make a few changes to your setup.

If you don’t need PHP and plan on serving mostly static pages, you can safely remove PHP from your configuration altogether. If however you do plan on running PHP scripts on your server, consider disabling unnecessary extensions by commenting them out in your php.ini or removing them from PHP entirely. There’re also many PHP extensions intended for speeding up the performance of PHP. Each has it’s own user case and may require certain functions to be called during execution, so research what works best for you. Caching and code optimizers should be options you would want to implement in your distribution. Don’t forget to configure the memory settings in your php.ini file as that will yield a substantial amount of benefits if you have, or are expecting heavy traffic.

When tuning Apache, you’ll want to pay attention to a few settings. KeepAlive, and MaxClients in particular. These options are situational to your web server so find some resources on the Apache Website. Tweaking one setting could substantially increase the speed of your server.

  • KeepAlive: Allows clients to reuse the same connection to facilitate the transfer of multiple files.
    Note: KeepAlive comes with increased memory use.
  • MaxClients: Determines the maximum number of concurrent clients / connections that Apache will serve.
    Note: MaxClients comes with the risk of Syn Floods if set too high..

A good PHP configuration and well tuned Apache configuration will result in a very fast dedicated server. There’re many settings to go over which are out of the scope of this article, so read a little bit about Apache and PHP to get a solid understanding, or look into our server management and let us handle the hard work for you.

Speeding Up a Dedicated Server

To summarize, make your site run faster with these tips:

  • Optimize MySQL, and don’t forget the indexes!
  • Each Apache configuration will be unique to every deployment.
  • Disable unnecessary PHP extensions and fine tune memory use and other settings in the php.ini file.
  • Optimize your files and reuse as much as possible to leverage browser storage.
  • CSS first, JavaScript last.

Don’t have the time to perform these tasks or prefer to focus on what you do best and run your business? Let our level 3 techs handle these tasks for you with our server management services. Our servers are ideal for high speed environments and we utilize high quality upstream providers to compliment a well optimized server.

How can I make PHP run faster?

5 Tips for Making Your PHP Site Faster.
1) Install a PHP Opcode optimizer (like XCache, APC, or memcache) ... .
2) Configure your php.ini file. ... .
3) Test PHP execution times by printing timestamps. ... .
4) Small code tricks. ... .
5) Reduce calls to your database..

What is PHP INI settings?

The php. ini file is the default configuration file for running applications that require PHP. It is used to control variables such as upload sizes, file timeouts, and resource limits. php.

How do I change PHP INI settings?

Modifying the PHP..
Login to the cPanel..
Find the File Manager in File section of the cPanel. ... .
Navigate to the directory where you will either save or edit the PHP. ... .
Edit the section of the PHP. ... .
Click on SAVE CHANGES in the top right hand corner in order to save your modifications or additions to the file..

How do I edit a PHP INI file?

How to edit the php.ini file.
Log in to your HostPapa Dashboard..
Click on My cPanel or My WordPress..
Scroll down to the Software section and click Select PHP Version..
Now click Options..
Locate the PHP directive you wish to amend and click the value..