Development

Optimizing web applications on the LAMP Stack

An image with some code on black background

Optimizing web applications is a critical part of the web development process. It is important to optimize your web application in order to: reduce the server’s load, decrease page load times, improve the user experience, and in some cases, reduce hosting expenses.

When optimizing your web application, two primary areas of focus are the code base and the development stack. The code base should be optimized first and foremost – development stack optimizations should not be considered an alternative to writing good code. Once the code base has been refined, page load times may still be slow. This may be due to mandatory complex database queries or unavoidable nested logic. This is when optimizing the development stack can help. Daylight’s development tool of choice for most projects is the LAMP stack (Linux, Apache, MySQL, and PHP). This will be focus of this post.

Linux

First we will look at Linux. There are 100s of Linux distributions (“distros”) available, some of the more popular contenders are AWS-AMI, CentOS, RedHat, and Ubuntu.

Ubuntu Linux is a popular choice for PHP applications. Ubuntu is great because the community is very large, resulting in answers to common questions and support being readily accessible. If you are hosting your app on Amazon Web Services, you can opt for their AWS-AMI Linux, which is a “…supported and maintained Linux image designed for stability, security, and high performance”. (AWS AMI) The file structure and package manager is similar to that of CentOS/RHEL. AWS-AMI linux comes lightweight, without extra utilities and services. If your app requires horizontal scaling, launching servers to meet your growing traffic needs, then AWS-AMI is the way to go. If you plan to host your app on one server indefinitely then Ubuntu should be fine. RHEL is also a great Linux distro to consider, however it is not free like the aforementioned. CentOS is similar to RHEL and is free. Choose a distro that you are comfortable with as there are no significant performance improvements when comparing the various major Linux distributions.

Apache

Next we can look at our choice of web server. The two most common are Apache and NGinx. Which to go with? That depends on your app’s performance on each. Currently at Daylight we are building out our larger apps where we have more control over the server environment in Laravel 5.1 LTS (Long term support). In general, Laravel has shown slightly better performance on NGinx/PHP-FPM vs Apache. To find out more visit Apache vs NGinx.

To test our webserver’s performance we can use ApacheBench command line tool. To get started with ApacheBench, we need to determine how many concurrent requests our browser can handle, and how many requests an average page requires. We are using Chrome, which allows for 6 concurrent requests at a time(each browser is different, but 6 is the most common). Next, to get the average number of requests per page we open up Chrome developer tools and click the tab titled ‘Network’. Navigate to a few of the most popular pages on your app with the developer tools still open and get the average total number of requests each page requires. 

A screenshot of a project in a computer screen

As an example, let’s say we have a page that makes 35 requests. Each asset on a page will require a single request so requests can add up quickly on a large page.

Okay, now let’s use ApacheBench to simulate concurrent users on our site. We would like to be able to handle at least 30 concurrent users at a time.

[30 users] x [6 concurrent connections for Chrome] = 180 concurrent connections

[35 requests per page] x [30 users] = 1,050 requests

The following command will simulate 30 concurrent users on our site: 1,050 requests with 180 concurrent connections.

ab -n 1050 -c 180 http://www.yoursite.com/

We saw more requests per second on NGinx vs Apache, which is why we went with NGinx. If you do not need a lot of web server features and do not want to take the time to test both, NGinx is the way to go.

MySQL

Next we look at databases. The two contenders are PostgreSQL and MySQL. If you have complex queries with heavy work loads, Postgres may perform faster.  With that said, simple primary key lookups with InnoDB are generally faster on MySQL. In most cases you either know MySQL or PostGreSQL, however, if you know both you can test each one to see which performs better for your application. If your app requires complex queries, test putting logic in the database versus the app to see which performs better.

PHP

Last in the stack is PHP. It is recommended to run the latest version of PHP and update it on a regular basis. Recently PHP 7.0 was released and it has shown significant speed improvements when compared to the previous PHP release, PHP 5.6. Another option to consider is to run PHP 5.6 on Facebook’s open source Hip-Hop Virtual Machine and compare to PHP 7.0. Here are some benchmark results provided by Zend – the results are stunning!

Once you have optimized your LAMP development stack, you should see improvements in page load times for a better user experience as well as reductions in your server’s CPU and memory use for a potentially less expensive hosting fee. If you host a scalable app that serves thousands or even millions of users, it is possible to reduce the load so greatly that you can reduce the number of servers by up to half or more and still serve the same number of users! Imagine cutting your hosting fee in half and improving the user experience in one fell swoop!

Newsletter

Sign up for the Daylight Newsletter to receive helpful blog posts, insights and featured client projects.

Our Clients

Aurea Logo
Wacom Logo
Chateau Ste Michelle Logo
Ste. Michelle Logo
SheerID Logo
Hanzo Logo
Black and decker Logo
Zapproved Logo
Marchex Logo
Pacvue logo
Burley Logo
Nike Logo
Coca-Cola Logo
Intel Logo
Providence Logo
Webtrends Logo
Sutter Home Logo
Trinchero Logo
Dakine Logo
Newmans Own Logo
Brooks Logo
DeWALT Logo