Linux, Apache, MySQL and PHP (or Perl) is the basis for many Web applications – from to-do list to blog, then e-commerce site. WordPress and Pligg are two high-capacity Web site to support a common package. This architecture referred to as LAMP. Almost every Linux release contains Apache, MySQL, PHP and Perl, so installing LAMP software is very easy.
Install the misleading simplicity of the software run smoothly on their own, but it is not the case. Finally, application load will exceed the set comes with back-end server processing power, application performance will be reduced. LAMP installation requires constant monitoring, tuning and evaluation.
System tuning for different people have different meanings. This series focuses on components of LAMP (Linux, Apache, MySQL and PHP) tuning. On tuning the application itself is another complex issue. Back-end server applications, and there is a symbiotic relationship: the server not properly tuned or even the best application would collapse under the load, and tuning with fully, completely avoid the very bad writing application makes the server slow as an ox. Fortunately, the proper system tuning and monitoring can be pointed out that the application in question.
LAMP Architecture
Tuning of any system is the first step in understanding how it works. According to the most simple form, LAMP-based application is written in scripting languages like PHP, they are running on a Linux host part of the Apache Web server running.
PHP applications through the request URL, all the form data and session information has been captured by any access to information from the client to determine what action should be performed. If necessary, from the MySQL database server (also running on Linux) access to information, some of this information with the Hypertext Markup Language (HTML) templates together, and returns the results to the client. When the user navigate in the application, this process is repeated; when multiple users access the system, this process will be conducted concurrently. However, the data flow is not unidirectional, because you can use the information from the user to update the database, including session data, statistical data (including voting), and user submitted content (like comments or site update). In addition to dynamic elements, there are static elements, such as images, JavaScript code and Cascading Style Sheets (CSS).
LAMP system in the study at the request stream, take a look at where performance bottlenecks can occur. Provides a number of dynamic information database, the database query response delay will be reflected in the client machine. Web server must be able to quickly execute the script, but also be able to handle multiple concurrent requests. Finally, the underlying operating system must be in good condition to support the application. Through the network to share files between different servers in other settings may also become a bottleneck.
Performance metrics
Continuous measure of the performance of help in two ways. First, the performance metrics can help us understand trends, including trends in both good and bad. As a simple way to look at the Web server central processing unit (CPU) usage, CPU can understand is overloaded. Also, see the total bandwidth used in the past and to infer future changes that can help determine when the need for network upgrades. The best measure of integration with other measurements and observations to consider. For example, when users complain about slow application, you can check the disk operation is reaching maximum capacity.
The second use of performance metrics is to determine system performance tuning is a help, or make it worse. Comparison of change is measured before and after results. However, to make a valid comparison, each should only modify a set of appropriate indicators and then compared to determine the effect of changes. Set a time to modify a reason should be obvious: while the two changes made are likely to affect each other. Selected for comparison of more subtle indicators.
The indicators selected reflect the application user must be able to feel the response. If a revised goal is to reduce the memory footprint of the database, then the cancellation of various buffer will certainly help, but at the expense of query speed and application performance. Therefore, we should select the application response time such indicators, which makes tuning in the right direction, not just for the database memory usage.
Can be measured in many ways the application response time. The easiest way might be to use curl command, as shown in Listing 1.
Listing 1. Using cURL measure the response time of Web site
$ Curl-o / dev / null-s-w% {time_connect}:% {time_starttransfer}:% {time_total} \
http://www.test.com
0.081:0.272:0.779
Table 1. curl to use the timer
Timer Description
time_connect TCP connection to the server’s time spent
time_starttransfer After the request, Web server returns the first byte of the data used in the time
time_total The time spent to complete the request
These timers are relative to the transaction start time, or even first in the Domain Name Service (DNS) query. Therefore, after the request, Web server processes the request and sends back data starting time used 0.272 – 0.081 = 0.191 seconds. Download data from the server the client’s time is used by 0.779 – 0.272 = 0.507 seconds.
By observing the curl data and trends over time, one can see the site’s responsiveness to users.
Of course, Web sites not only by the pages. It also has images, JavaScript code, CSS and cookie to deal with. curl is very suitable for understanding the response time of a single element, but sometimes need to understand the entire page loading speed.
For the Tamper Data Firefox browser extensions (see Resources section for a link) can be recorded in the log for each Web browser sends a request, and displays each request with download time. Method is to use this extension, select Tools> Tamper Data to open the Ongoing requests window. To visit the page loaded, and then the browser will see the status of each request sent and the load time spent for each element. Figure 1 shows the results of loading developerWorks home page.
Figure 1. DeveloperWorks home page used to load the request of a breakdown

Each line describes one element of the load situation. The data displayed includes the request time, loading time used, size, and results. Load Duration column lists the time used by the elements themselves, Total Duration column lists all of the time used by child elements. In Figure 1, the main page loading time is used in 516 milliseconds (ms), but everything loads and displays the time used by the entire page is 5101 ms.
Tamper Data extension there is a useful model, the output of the page to load data into the graphics rendering. Ongoing requests window, right-click anywhere in the upper part, and select the Graph all. Figure 1 Figure 2 shows a graphical view of data.
Figure 2. DeveloperWorks home page used to load a graphical view of the request

In Figure 2, the duration of each request is displayed as dark blue, and relative to the start time loading the page display. So, you can see which requests the entire page load slower.
Although the focus is on the page load times and user experience, but do not ignore the core system indicators, such as disk, memory, and network. There are many utilities to capture this information; which may be the most helpful sar , vmstat and iostat . For more information about these tools, see the Resources section.
Adjust the basic system
The system in Apache, PHP and MySQL tuning components, you should take some time to ensure that components of the underlying Linux operating normally. It should also be running services on the reduction, only those services required for the operation. This is not only a good security practice, but it will save memory and CPU time.
Kernel tuning some quick measures
Most Linux distributions are defined in the appropriate buffers and other Transmission Control Protocol (TCP) parameters. These parameters can be modified to allocate more memory, thus improving network performance. Set the kernel parameters is through proc interface, that is, through reading and writing /proc values. Fortunately, sysctl can read /etc/sysctl.conf in the value and need to fill under the /proc , so it can more easily manage these parameters. Listing 2 shows the Internet server Internet server used in some of the more radical of the network settings.
Listing 2. Contains more radical network settings / etc / sysctl.conf
# Use TCP syncookies when needed
net.ipv4.tcp_syncookies = 1
# Enable TCP window scaling
net.ipv4.tcp_window_scaling: = 1
# Increase TCP max buffer size
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
# Increase Linux autotuning TCP buffer limits
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# Increase number of ports available
net.ipv4.ip_local_port_range = 1024 65000
These settings will be added to /etc/sysctl.conf existing content. The first setting enables TCP SYN cookie. Sent from the client when a new TCP connection, the SYN packet bit is set, the server connection for the half-open to create an entry, and with a SYN-ACK packet response. In normal operation, the remote client machine responds with an ACK packet, which will convert the half-open connection to fully open. There is a known as SYN flood (SYN flood) attacks on the network, it can not return the ACK packet, causes the server to run out of memory space, can not handle incoming connections. SYN cookie feature can identify this situation and elegant way to use a reserved space in the queue (for details see reference section). Most systems have this feature enabled by default, but make sure to configure this feature and more reliable.
Enable TCP window stretching to the client to download data at higher speeds. TCP is not allowed to receive confirmation from the remote end of the case to send multiple packets, the default setting is up to 64 KB, and delay in large remote clients to communicate in this setting may not be enough. Retractable window will enable more in the first place, thereby increasing the window size.
Four configuration options increase the TCP after sending and receiving buffer. This allows applications to quickly lose its data, so as to request another service. You can also strengthen the remote client when the server is busy sending data.
Finally, a configuration to increase the number of available local port, so that services can increase the maximum number of connections.
The next boot, or the next time you run sysctl -p /etc/sysctl.conf , these settings will take effect.
Configure disk to improve performance
Disk in the LAMP architecture plays an important role. Static files, templates, and code from disk, composed of the database tables and indexes are from the disk. Many of the tuning disk (especially for the database) to focus on avoiding disk access, because the disk access latency is quite high. Therefore, spend some time on the disk hardware optimization is meaningful.
The first step is to ensure that the file system to disable atime logging features. atime is the time to access files recently, when accessing the file, the underlying file system must record the time stamp. Rarely used because the system administrator atime , disable it can reduce disk access time. This feature is disabled in /etc/fstab to add the fourth column noatime option. Listing 3 shows an example configuration.
Listing 3. Demonstrates how to enable noatime in fstab example
/dev/VolGroup00/LogVol00 / ext3 defaults,noatime 1 1
LABEL=/boot /boot ext3 defaults,noatime 1 2
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
LABEL=SWAP-hdb2 swap swap defaults 0 0
LABEL=SWAP-hda3 swap swap defaults 0 0
In Listing 3, only modified the ext3 file system, because the noatime only
reside on disk file system helpful. To make the changes to take effect,
do not need to reboot; just re-mount each file system. For example,
to re-mount the root file system, run the mount / -o remount.
Combination of a variety of disk hardware and Linux to access the disk may not be able to detect the best way. Can use the hdparm command to identify and set the method used to access IDE disks. hdparm -t /path/to/device speed test, the results of this test can be used as performance benchmarks. To make the results as accurate as possible, run this command in the system should be idle. Listing 4 shows the hda speed test on the results.
Listing 4. In the /dev/hd speed test performed on
# Hdparm-t / dev / hda
/dev/hda: / Dev / hda:
Timing buffered disk reads: 182 MB in 3.02 seconds = 60.31 MB/sec
This test shows that in the read data on the disk per second speed is about 60MB.
Tuning in to try some disk option, you must pay attention to a problem. Wrong setting could damage the file system. Sometimes there will be a warning that this option is not compatible with the hardware; However, sometimes there is no warning message. Therefore, the system into production, you must thoroughly test the settings. All servers are standard hardware would also help.
Table 2 lists some of the more common options.
Table 2. hdparm common options
Options Description
-vi –Vi It supports to set the disk check, and it is set.
-c –C hdparm -c 1 /dev/hda enable this setting.
-m –M uery / set the interrupt for each multi-sector model. If set greater than zero, set the value is the maximum that can be transmitted for each interrupt number of sectors.
-d 1 -X -D 1-X Enable Direct Memory Access (DMA) transfer and set the IDE transfer mode. hdparm man page for details in the -X can be set behind the figures. Only in -vi that is not currently the fastest mode of use cases, only the need for this setting.
Unfortunately, for the Fiber Channel and Small Computer Systems Interface (SCSI) system, tuning depends on the specific drive.
Settings must be added to help start the script, such as rc.local .
Network File System tuning
Network File System (NFS) is a network shared disk method. NFS can help ensure that each host has the same copy of the data and to ensure changes are reflected in all nodes. However, by default, NFS is not configured for high-capacity disk.
Each client should use rsize=32768,wsize=32768,intr,noatime mount remote file system to ensure that:
- Use of large read / write block (number specifies the maximum block size, in this example is 32KB).
- When the pending NFS operations can be interrupted.
- Not continuously updated
atime .
These settings can be placed in /etc/fstab in, see Listing 3 . If you use the automatic mounting device, you should apply these settings in the appropriate /etc/auto.* files.
On the server side, we must ensure that adequate NFS kernel threads to handle all clients. By default, only start a thread, but Red Hat and Fedora system will start 8 threads. For a busy NFS server, this number should be increased, such as 32 or 64. Can nfsstat -rc command client assessment to see whether the phenomenon of blocking, the command displays the client remote procedure call (RPC) statistics.
Listing 5 shows a Web server, client statistics.
Listing 5. Display NFS client RPC statistics
# nfsstat -rc # Nfsstat-rc
Client rpc stats: Client rpc stats:
calls retrans authrefrsh calls retrans authrefrsh
1465903813 0 0 146590381300
The second column retrans is zero, which means that since the last reboot from no need to re-transfer situation. If this number is relatively large, they should consider increasing the NFS kernel threads. Setting method is to pass the required number of threads rpc.nfsd , such as rpc.nfsd 128 starts 128 threads. Any time for such settings. Thread will be needed to start or destroyed. Similarly, this setting should be placed in the startup script, especially in the NFS system to enable scripting.
On the NFS, the last thing to note: if possible, should be avoided NFSv2, because the performance NFSv2 much less than the v3 and v4. In the modern Linux distributions should not be a problem in that, but you can check on the server nfsstat the output to see if there are any NFSv2 call.