Knowledge Bank

From how to best plan and execute a server migration through to utilising MySQL replication, this section is full of white papers and best practice guidelines, produced by the experts here at ForLinux to help you get the most from your Linux server.

Scaling Apache, MySQL and PHP

10/09/2009

This is a short post on the basic principles of scaling your Apache PHP MySQL based hosted website.

Scaling your website isn't as simple as introducing a new web server. It is easy to upgrade the hardware however, there are the issues associated with migrating the website to the new hardware, such as coding for new versions of software and coordinating an IP address change. Your Apache PHP MySQL host may not be able to provide the same IP address.

There are several routes one can take when considering scaling PHP MySQL hosting websites.

Step 1: Split the Apache PHP and MySQL functions

If your current server is running both Apache PHP and MySQL, the first step is to split them onto their own dedicated server. This should have the effect of reducing load on the Apache PHP web server by about 50% depending on the amount of work the MySQL server is performing. To do this will require a change of the MySQL connection parameters (as long as you have coded your website this way). Having a cross over cable between to two servers provides a dedicated 1Gb interface rather than running across a LAN switch.
Total number of servers 2.

Step 2: Load balance the Apache PHP Web front end

Introducing a hardware load balancer to the solution will provide a method of distributing web traffic to two or more web servers. The load balancer should take care of persistence and redundancy. If one Apache PHP web server crashes, the load balancer will only direct traffic to the remaining live Apache PHP servers. In this scenario, you will have to ensure the remaining web server(s) can adequately cope with the traffic.
Additional web servers will need to be configured to mirror existing web servers. Static files, user account, web configuration files and software versions must match, otherwise you will have inconsistency which will be difficult to troubleshoot.
Total number of servers 4 - Load balancer, 2 web servers, 1 database.

Step 3: Network File Server

If additional web servers are required then the management of static files across multiple web server becomes unmanageable. A network file server provides a single point for the static assets of your website. Using a NFS (Network File System) server you are able to "mount" the NFS partition on to each web server. This provides a method for your web servers to read and write to a central location. There are generally two types of solution for the file server - NAS and SAN. A NAS (Network Attached Storage) is a server with more than 2 drives, configured as large disk. A SAN (Storage Attached Network) is a more complex device with space for many disks and no single points of failure. As such a SAN is more expensive than a NAS.
Total number of servers 6 - Load balancer, 3 web servers, 1 database, 1 NFS

Step 4: Additional Database servers

By this stage the web servers are managing the web traffic. However, the database server may start to have performance problems. One solution is to increase the server specification. Installing additional RAM is the first solution, but this is only a temporary measure and the database is still a single point of failure. There are several options at this stage for scaling the database.

i) Add a replicated MASTER/SLAVE MySQL server and change your code to perform reads from the SLAVE and writes to the MASTER. This has the affect of reducing overall load on the database but doesn't help if there is a failure of the MASTER unless the application takes this into consideration and switches to the SLAVE (which then becomes a single MASTER).

ii) Add a replicated MASTER/MASTER MySQL server and change your database structure to have odd and even indexes. In a Master/Master set up, both servers are able to function for reads and writes. However, as data is replicated between the server you cannot have duplicate records, hence changing the database structure to prevent this.

iii) Add a MemCache server. Not exactly a database solution, but this would ease the pressure from the database and therefore give you more time to decide on the final solution.

iv) MySQL Cluster. With this solution, no code changes are required but the database table types will need to change which can cause complications.

ForLinux provide dedicated Apache Web Server support

Total number of servers 7-9 - Load balancer, 3 web servers, 2-4 database server, 1 NFS

Get In Touch...