HOW TO SPREAD THE LOAD IN A PHP + MYSQL WEBSITE
- Category : Server Administration
- Posted on : Jan 07, 2018
- Views : 3,537
- By : Kapueo I.
As the number of visitors to a website increases, so does the strain on the server. This can result in a very high load, especially if the available resources are tight and there is not enough CPU or RAM to serve all requests.
A high server load can significantly increase loading times or even make a website completely unresponsive, so it is very important to handle this issue proactively and spread the load before it becomes a real problem.
There are several methods to reduce server load, depending on the budget at your disposal, we will discuss them briefly starting with the cheapest options.
OPTIMIZE YOUR SERVER
If you can’t afford more resources, the only option is to optimize your website so that it runs best on the current server.
Start by making sure that you only run the applications that you actually need, since every unused service is just draining your CPU and memory. The same is true for plugins, such as the ones installed in WordPress.
Optimizing php is tricky and you’ll probably need help from a programmer, but there are still a few things you can try. If your website is compatible with caching solutions like opcache, memcache or redis, make sure these are activated, since they can make a huge difference.
Every new php version is faster than the previous one and uses fewer resources, so you can update php step by step as long as your site loads correctly. You can jump straight to the latest version if you use a modern CMS platform like WordPress.
Your database server should also use the newest version of MySQL or MariaDB for best performance and resource use. A diagnostic tool such as MySQLTuner can suggest some useful tweaks in your configuration files.
Another great tool is mytop, which allows you to inspect SQL queries as they run. Identify the slow ones that cause high server load and ask your programmer to investigate them.
Finally, an effective caching system can greatly decrease the load on your server and allow it to respond to more requests at the same time.
Even the caching of static files is very helpful but you can also cache dynamic pages as long as the settings are right.
There are many caching options available; every CMS has several internal options or plugins. You can also install Nginx for caching in front of Apache, or use other solutions like Redis or Varnish for this purpose.
SCALE UP
Adding more resources will definitely decrease load, if you can afford them.
This is more difficult on a physical dedicated server, you might be able to add more RAM or migrate to a larger machine but this requires downtime.
However, scaling up is easy on a VPS and every cloud or virtualization solution supports it, a simple server reboot is required. Try to increase the number of CPU cores or the amount of system memory; you can also migrate your disks to a faster storage solution like SSD, if such an upgrade is available.
MOVE TO A MULTI-SERVER ARCHITECTURE
Scaling up on a single server has its limits and spreading the load to multiple servers eventually becomes a more effective option.
You can split the resource pool into several virtual machines with specific roles, depending on the needs of your website.
Linux applications run better on dedicated machines, when they don’t share resources with other services. The most simple multi-server setup for a php + MySQL site uses two machines, one is used only as a database server while the other connects to it remotely and handles the web server and php processing.
If this setup is unable to handle the traffic, it is time to implement a more complex solution. Two or more web servers can share the load between them, in this scenario you will also need a machine that will act as a load balancer in front of them.
The load balancer can be configured with Nginx or HAProxy and its role is to distribute requests to the backend web server that can respond faster. The web servers must share the same document root, which can be achieved by exporting a NFS mount or creating a separate machine for this purpose.
This solution has many advantages: it is easily scalable by adding or removing web servers and also offers some redundancy, since the site stays online even if one of the web servers crashes.
If you have a generous budget, it is also possible to implement a fully redundant solution, with a backup load balancer (using VRRP) and two database machines in a master-master setup.
Optimizing server load is a difficult task and even some of the biggest websites can struggle when the traffic is very intense, for example during Black Friday.
The right configuration depends on the available resources and the specifics of your website; follow the basic steps described in this article to setup a solid initial platform.
Categories
- cPanel Question 47
- cPanel Software Management 29
- cPanel Tutorials 13
- Development 29
- Domain 13
- General 19
- Linux Helpline (Easy Guide) 156
- Marketing 47
- MySQL Question 13
- News 2
- PHP Configuration 14
- SEO 4
- SEO 42
- Server Administration 84
- SSL Installation 54
- Tips and Tricks 24
- VPS 3
- Web Hosting 44
- Website Security 22
- WHM questions 13
- WordPress 148
Subscribe Now
10,000 successful online businessmen like to have our content directly delivered to their inbox. Subscribe to our newsletter!Archive Calendar
Sat | Sun | Mon | Tue | Wed | Thu | Fri |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
Recent Articles
-
Posted on : Sep 17
-
Posted on : Sep 10
-
Posted on : Aug 04
-
Posted on : Apr 01
Tags
- ts
- myisam
- vpn
- sql
- process
- kill
- tweak
- server load
- attack
- ddos mitigation
- Knowledge
- layer 7
- ddos
- webmail
- DMARC
- Development
- nginx
- seo vpn
- Hosting Security
- wireguard
- innodb
- exim
- smtp relay
- smtp
- VPS Hosting
- cpulimit
- Plesk
- Comparison
- cpu
- encryption
- WHM
- xampp
- sysstat
- optimize
- cheap vpn
- php-fpm
- mariadb
- apache
- Small Business
- Error
- Networking
- VPS
- SSD Hosting
- Link Building
- centos
- DNS
- optimization
- ubuntu