How to improve php performance with web hosting server tweaks
The web is overrun with PHP.
As per statistics, 83.6% of all websites run on PHP .
So, for web hosts, ensuring good performance for PHP is a matter of necessity, not of choice.
Here at Bobcares, we help web hosts deliver highly responsive PHP hosting through server optimization, preventive maintenance and 24/7 monitoring.
Today we’ll go through a few server optimization tricks we use in our customer servers.
1. Enable OpCode caching
This is perhaps the easiest and most impactful of PHP optimizations.
Disk access is costly. Every access to include scripts, templates, layouts and database records involves disk access.
By enabling OpCode cache like APC, we’ve been able to cut down disk access by nearly 70%.
That’s because repeat requests are served from cache that runs on system memory,
It’s lightning fast compared to the slow, mechanical Hard Disk Drive.
2. Use a fast PHP engine and handler
We’ve seen many Apache web hosting servers run PHP 5.6 by default with mod_php as the handler.
mod_php runs a PHP interpreter in each Apache process. It makes each site visit heavy on memory use, and contributes to high server load.
On top of that PHP 5.6 uses old Zend engine which is 95% slower than the latest PHP 7 powered by the PHPNG engine.
We’ve seen the best results for performance and security by using PHP 7.2 engine and php-fpm as the handler.
By replacing the default combination of PHP 5.6 + modPHP + Apache with PHP 7.2 + php-fpm + Nginx, we’ve been able to boost site speeds by almost 150%.
3. Optimize the database queries, tables and settings
Almost all PHP sites use MySQL (or similar) database to store data.
Many times while troubleshooting site performance issues, we’ve traced it to the underlying database.
We’ve seen slow queries, fragmented tables, and unoptimized settings causing databases to slow down, and thereby affect PHP site speed.
Slow queries can be fixed only by rewriting the application code, which is not practical for most web hosts. In such cases, we minimize the severity of the performance issues by using opcode caches.
However, we can improve the performance of the database server itself by periodic db server audits and optimization. Some common settings we tweak are:
- innodb_buffer_pool_size – The size of buffer (cache of old queries) in InnoDB databases. Usually set to 50-70% of available memory.
- key_buffer_size – The buffer size for MyISAM databases. Usually set to 20% of available memory.
- max_connections – Number of connections available to each user. Affects memory usage per user.
- query_cache_size – The total memory size used to hold old queries.
4. Use SSD for databases
Hard disk is the slowest component in a server.
In busy eCommerce hosting servers, we’ve been able to cut down performance lags by more than 50% by mounting the database directories on an SSD instead of the mechanical HDD.
So, if you’ve tried everything and is bogged down by heavy I/O, get an SSD for /var directory.
5. Quarantine or throttle memory hogs
Not every PHP app is created equal.
Some are well coded, performance profiled and fine-tuned for performance. Others are haphazardly coded and contain memory leaks.
In shared servers, often it’ll be the poorly coded PHP sites that cause high server load.
To prevent these sites affecting other server users, we either set hard limits to their memory allocation, or put them into a virtual server with hard limits set on resources.
This way, even if one or two sites misbehave, a vast majority of your PHP customers will have a fast hosting experience.
PHP dominates the web hosting market, and every web host needs to tweak their servers to provide the fastest hosting possible. Today we’ve discussed a few tips we’ve learned along the way to deliver a super fast PHP hosting experience.