Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP website Optimization

I have a high traffic website and I need make sure my site is fast enough to display my pages to everyone rapidly.

I searched on Google many articles about speed and optimization and here's what I found:

  • Cache the page
  • Save it to the disk

Caching the page in memory:

This is very fast but if I need to change the content of my page I have to remove it from cache and then re-save the file on the disk.

Save it to disk

This is very easy to maintain but every time the page is accessed I have to read on the disk.

Which method should I go with?

like image 666
ana Avatar asked Nov 24 '11 14:11

ana


People also ask

What is the performance of PHP?

Official PHP benchmarks report that PHP 7 can execute twice as many requests per second when compared to PHP 5.6. Kinsta's PHP performance benchmarks show WordPress 5.0 can execute 3x as many transactions as PHP 5.6; and. Pantheon reported that PHP 7 gave a 64% improvement over version 5.3.

What is PHP What does PHP do?

PHP is an acronym for "PHP: Hypertext Preprocessor" PHP is a widely-used, open source scripting language. PHP scripts are executed on the server. PHP is free to download and use.


2 Answers

Jan & idm are right but here's how to:

Caching (pages or contents) is crutial for performance. The minimum calls you request to the database or the file system is better whether if your content is static or dynamic.

You can use a PHP accelerator if you need to run dynamic content:

My recommendation is to use Alternative PHP Cache (APC)

Here's some benchmark:

What is the best PHP accelerator to use?

PHP Accelerators : APC vs Zend vs XCache with Zend Framework

Lighttpd – PHP Acceleration Benchmarks

For caching content and even pages you can use: Memcached or Redis.

Memcached: Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Redis Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

Both are very good tool for caching contents or variables.

Here's some benchmark and you can choose which one you prefer:

Redis vs Memcached

Redis vs Memcached

Redis VS Memcached (slightly better bench)

On Redis, Memcached, Speed, Benchmarks and The Toilet

You can install also Varnish, nginx, or G-Wan

Varnish: Varnish is an HTTP accelerator designed for content-heavy dynamic web sites. In contrast to other HTTP accelerators, such as Squid, which began life as a client-side cache, or Apache, which is primarily an origin server, Varnish was designed from the ground up as an HTTP accelerator.

nginx nginx (pronounced ?engine-x?) is a lightweight, high-performance Web server/reverse proxy and e-mail (IMAP/POP3) proxy, licensed under a BSD-like license. It runs on Unix, Linux, BSD variants, Mac OS X, Solaris, and Microsoft Windows.

g-wan G-WAN is a Web server with ANSI C scripts and a Key-Value store which outperform all other solutions.

Here's some benchmark and you can choose which one you prefer:

Serving static files: a comparison between Apache, Nginx, Varnish and G-WAN

Web Server Performance Benchmarks

Nginx+Varnish compared to Nginx

Apache, Varnish, nginx and lighttpd

G-WAN vs Nginx

like image 162
Book Of Zeus Avatar answered Oct 02 '22 12:10

Book Of Zeus


You have a good idea, which is close to what i do myself. If i have a page that is 100% static, i'll save a html version of it and serve that to the user instead of generating the content again every time. This saves both mysql queries and several io operations in some cases. Every time i make some change, my administration interface simply removes the html file and recreates it.

This method has proven to be around 100x faster on my server.

like image 37
Jan Dragsbaek Avatar answered Oct 02 '22 10:10

Jan Dragsbaek