How to Optimize your PHP Applications on Clever Cloud

PHP is quite popular in website creation. A lot of famous/popular CMS and frameworks are written in this language.

As these CMS and frameworks became extremely complex, it’s important to know that some tricks can be applied to improve overall performances. Communities created a lot of tools or plug-ins. Here are different tips you can easily apply to optimize your PHP Applications.

Varnish, for faster content pages

Let’s begin with a simple cache service, Varnish. Varnish is a HTTP Cache on the server side that can boost the response time of a request. It will handle the requests sent by the browser. And instead of sending all requests to the back-end, it caches the first request into a HTML file. Next time, the raw HTML file will be sent directly. Without bothering the back-end and the database.

With a set of rules and options, it can fit all websites and offer a real breath of fresh air to your website. You can find the Varnish documentation here.

To enable a configured Varnish on Clever Cloud, it’s prety straightforward:

  1. First, create a /clevercloud folder if it doesn’t exist.
  2. In this folder, copy your own varnish.vcl file.
  3. With git, add the file, commit and push your website on Clever Cloud.

If needed, redeploy you app. Varnish will be activated. Also, you should test your new configuration with isvarnishworking.com to check if everything is working well.

Before we continue let me be clear: do not forget that Varnish returns a dumb cached page. In plain HTML. So you might have to adapt your configuration to be sure that dynamic pages are not cached. You have to manage these pages differently, so we recommend the following strategies.

How to decrease load on SQL databases with Redis

Redis is an in-memory data store. It’s a key-value database capable of storing in memory some information to speed up their fetching. Most of the time, you can use Redis along with a classic database (eg. PostgreSQL) to improve response time.

Instead of querying the DB for each request, we poke Redis to get the results of the previous request. The database will not have to process the request, as Redis takes care of it.

Redis associates a hash to each value and will return the result associated with it from the RAM. The delay to access the value is then way faster than querying the SQL database.

For PHP we provide PHPRedis, an extension to communicate with Redis. It’s quite easy to integrate it with your application along with Varnish for HTTP-Caching.

For instance, you can use Varnish to cache pages that do not change often – like the content for anonymous visitors. And Redis to speedup the response time of dynamic pages such as a forum or a e-commerce website.

Storing PHP sessions in Redis

Sessions in PHP are, by default, stored in temporary files. On a file system. And that’s a really bad news. Because file systems are:

  • Slow
  • too complex for a simple
  • and SPOF-free multi tenant file systems are unicorns

So, if you website has to deal with a high traffic, here’s what’s happening:

For each request, PHP will open, read and close the file. Such operations on the file system are transactional. As a consequence, it will slow down the response time. This also requires disk space. If your website keeps sessions for a long time, and for a lot of different users, this might use a lot of space.

Luckily, Redis can help us on this. PHP offers the possibility to change the session handler and how sessions are managed. Using a DB like Redis instead of the FS can improve performance of your application. Especially if you have a lot of visitors

PHPRedis, which we mentioned above, can do this. Clever Cloud gives you a simple way to enable storage of sessions in Redis. You do not even have to change or add a file, it can be setup in the environment variables:

  1. Go in the environment variables section of
    your application in our console.
  2. Create an environment variable named SESSION_TYPE with the value redis.

To words. And nothing more. The sessions of your application will now be stored in a Redis database!

Advice for WordPress websites

Clever Cloud deals with a lot of WordPress websites, so we found multiple ways to improve the performance easily.

We noticed many times that plug-ins such as W3 Total Cache or JetPack can slow down your apps.

The most problematic thing are plug-ins. We noticed many times that plug-ins such as W3 Total Cache or JetPack can slow down your apps. They are providing cache systems, but these are designed for shared hosting. Not Cloud hosting.

As Clever Cloud provides dedicated VMs for each app, these plug-ins create problems. Or slowdowns. So just get rid of them. If you need a caching system, use Varnish instead. There is a varnish.vcl file example in the WordPress documentation.

Also, the previous three optimization methods works great for WordPress of course.

Conclusion

Alongside complex PHP’s CMS evolution, the tools to improve performances also made their way. We hope these few trivial optimizations will help you to scale better and create a great browsing experience.

If you have other tips for these tools, tell us about them in the comments. We are always looking for tips which could improve performance and offer a great service. So if you have any suggestions feel free to contact us at support@clever-cloud.com.

Blog

À lire également

clever cloud fest. is coming

Clever Cloud announces its clever cloud fest. an event to bring together customers, prospects, partners and more.
Company

Clever Cloud: Discover our clever side

Faced with increasingly demanding technical environments, Clever Cloud has established itself as a key player in supporting tech teams and IT decision-makers.
Company

Metabase on Clever Cloud: easily query and visualize your data

Your business generates data, which you need to analyse, understand and make available to your teams, both technical and non-technical. To meet this need, we worked with David Sferruzza to integrate Metabase for the cloud, which is available on our Marketplace and can be easily deployed on Clever Cloud.
Company