linux+apache+mysql+php Optimization Tips

Source: Internet
Author: User
Tags php script web services

The LAMP platform consists of four components in a hierarchical structure. Each layer provides a key part of the entire software stack: Linux, Apache, MySQL, PHP.

 

The origin of the word lamp originated in the German magazine "C ' t Magazine", where Michael Kunze first set together these projects to create the acronym for Lamp in 1990. Although these components are not designed to be used in the beginning, these open source software can be readily available and available free of charge. This leads to the frequent use of these components. The compatibility of these components has improved over the last few years, and the application situation has become very general. In order to improve collaboration among different components, some extension features have been created. Currently, the "LAMP stack" product is included by default in almost all Linux distributions. These products form a powerful Web application platform

The LAMP platform consists of four components in a hierarchical structure. Each layer provides a key part of the entire software stack:

    • The Linux:linux is at the lowest level and provides the operating system. Each of the other components actually runs on Linux as well. However, it is not necessarily limited to Linux and, if necessary, can other components be available at Microsoft? Windows?, Mac OS X, or UNIX? Run on.
    • Apache: Second lower level is Apache, it is a WEB server. Apache provides a mechanism for users to gain access to WEB pages. Apache is a stable, mission-critical server that uses more than 65% of Web sites on the Internet as a Web server. PHP components are actually in Apache, dynamic pages can be created via Apache and PHP.
    • The mysql:mysql provides the data storage side of the LAMP system. With MySQL, you get a very powerful database for running large, complex sites. In a WEB application, all data, products, accounts, and other types of information are stored in this database, which can be easily queried by the SQL language.
    • Php:php is a simple and effective programming language that is like an adhesive that binds all other components of the LAMP system together. You can use PHP to write dynamic content that accesses the data in the MySQL database and some of the features that Linux provides.

[Detection Tool]

For complete debug results, it is recommended that you use software such as Apachebench or Httperf. If you are interested in server testing for non-LAMP architectures, it is recommended that you use Microsoft's free software: Web application Stress Tool (NT or 2000 required). (Other server test tools)

Detects Apache and uses top D 1 to show the CPU and memory of all processes. In addition, the APACHECTL status command is also used

[Hardware optimization]

1, the general rule of upgrading hardware: For the PHP script, the main bottleneck is the CPU, for static pages, the bottleneck is memory and network. A static page downloaded from a normal Pentium machine of up to a Mhz can saturate the T3 line (45Mbps).

2, using Hdparm to optimize the disk, generally can improve the IDE disk read and write performance 200%, but also the effect on the SCSI hard disk. (Comparison of different types of hard drives)

[Strategy optimization]

3, Apache processing PHP script speed is 2-10 times slower than static page, so try to use more static pages, fewer scripts.

4, PHP script if do not buffer, each call need to compile, therefore, install a PHP buffer product can improve the performance of 25-100%.

5, if you adopt a Linux system, it is recommended to upgrade the kernel to 2.4, because the static page is served by the kernel.

6. Another buffer technique is to use HTML buffered output for infrequently modified PHP pages.

7. Do not run X-windows on the WEB server and turn off processes that do not need to run.

8, if you can use the text do not use the image, as far as possible to reduce the size of the picture.

9, distribute the load, put the database server on the other machine up. Using another low-end machine service picture and HTML page, if all the static pages are processed on another server, you can set the keepalives in httpd.conf to off to reduce the time of disconnection.

10, the above all the methods are for single-machine, if you think the system is not fast enough, you can use clustering, load balancing, buffering technology. Squid is used as a buffer to configure Squid method.

[Compilation optimization]

11. Switch the file-based session to a shared memory-based session. Compile PHP with the--WITH-MM option and set the set session.save_handler=mm in PHP.ini. This simple modification can shorten the session management time by half.

12, using the latest version of Apache, and the PHP compiled, or use the DSO mode, do not use CGI method.

13, when compiling PHP, it is recommended to use the following parameters:

--enable-inline-optimization--disable-debug

[Configuration optimization]

14, modify httpd.conf:

# Turn off DNS lookups,php script to take IP address only

Hostnamelookups off

15, if the network congestion, CPU resources are not enough, the use of PHP HTML compression function:

Output_handler = Ob_gzhandler

Users of PHP 4.0.4 should not use it because of a memory leak issue.

16, modify the httpd.conf in the sendbuffersize for your largest paging file size. Increase the TCP/IP write buffer size of the kernel.

17, the use of persistent database connection, do not set the maxrequestsperchild too large.

[third-party software optimization]

18, if you like to modify the Apache source code to start, you can install Lingerd. After the page is generated and sent, each Apache process wastes a time on the customer connection, Lingerd can take over the job and let Apache quickly serve the next customer request.

19, if you are brave enough, you can also use the Silicon Graphics accelerated Apache patch. This project can make Apache 1.3 10 times times faster, making Apache 2.0 4 times times faster.

Installing a PHP buffer product can improve the performance of 25-100%.

[Linux system Optimization]

1. Clean Up server disk fragmentation:

Regardless of the file format used by the Linux file system (ext3, JFS, XFS, ReiserFS), what type of hard disk (IDE, SCSI), the filesystem tends to be fragmented over time. Advanced file systems such as EXT3 and JFS can reduce file system fragmentation, but are not eliminated. In a busy database server, over time, file fragmentation reduces hard disk performance, and hard disk performance is noticed when it is read from or written to the hard disk. A long time will find that each disk does accumulate a lot of junk files, free disk space can help the system to work better. The best way to defragment a disk is to make a full backup, reformat the partition, and then restore the files from the backup. However, it is difficult to work with a 7x24-hour mission-critical server. Kleandisk is an efficient Disk Cleanup tool that divides files on disk into different "groups", such as grouping all "core" files into groups, so you can delete all of the core files by deleting the group. A core file is a file that is generated when the software runs out of error, which is useful for software developers and does not make sense for other users, such as e-mail servers. Therefore, if there is no need for software development, you can remove it if you see the core file.

2. Turn on the hard disk DMA

The IDE hard disk that is used now basically supports dma66/100/133 (direct memory read) but the Linux distribution is generally not open after installation, you can/etc/rc.d/rc.local the last face with one line:/SBIN/HDPARM-D1–X66-C3-M16 /dev/hda so each time the power on, the hard disk DMA will be turned on, do not have to manually set each time. Before and after adding you can use the command: Hdparm-tt/dev/hda to test the comparison.

3. Adjust buffer Refresh Parameters

The Linux kernel contains some parameters that can be set for the system's operating state. The parameters of the buffer refresh can be done by adjusting the/proc/sys/vm/bdflush file, the format of this file is: # Cat/proc/sys/vm/bdflush 30 64 64 256 500 3000 60 0 0

Each column is a parameter, the most important of which is the previous parameters. The first number is when the "dirty" buffer is reached, forcing the wake-up bdflush process to flush the hard disk, and the second number is how many dirty blocks each time the Bdflush process refreshes. The so-called dirty block is a cache block that must be written to disk. The next parameter is to allow the BD flush to queue the number of memory blocks into the free buffer block list each time. The above values are the default values in Rhel 4.0. You can modify it in two ways:

(1) using the command

    1. # echo "0 0" >/proc/sys/vm/bdflush

and add this command to the/etc/rc.d/rc.local file.

(2) Add the following line to the/etc/sysctl.conf file:

    1. Vm.bdflush = 100 128 128 512 5000 3000 60 0 0

The above settings increase the size of the buffer, reducing the frequency of Bdflush is started, the VFS buffer refresh mechanism is one of the reasons for the Linux file system efficiency.

4, optimize the input and output

I/O programs are also important for Linux system performance, and network hardware I/O is especially important for servers. Most Linux servers now use 10/100 MB Ethernet. If you have a heavier network load, you can consider a Gigabit Ethernet card. If you do not have the ability to purchase a gigabit network card, you can use multiple network cards to become a network card, with the same IP address. This technology, in Linux, is called bonding. Bonding is already included in the kernel above Linux2.4, simply select the Bonding driver support in the network device option at compile time as shown in Figure 1. Of course, the use of bonding technology to configure the dual-NIC binding is the same as the two NIC chipset models are the same, and have a separate BIOS chip.

Then, recompile the core, restart the computer, and execute the following command:

    1. #ismod Bonding
    2. #ifconfig eth0 Down
    3. #ifconfig eth1 Down
    4. #ifconfig bond0 IPAddress
    5. #ifenslave bond0 eth0
    6. #ifenslave bond0 eth1

Now two NICs have worked like a piece. This improves data transfer between cluster nodes. Bonding for the server is a better choice, in the absence of gigabit network card, with two 100 gigabit network card for bonding, The bandwidth between the server and the switch can be greatly increased. However, you need to set up a connection on the switch two sub-ports of the bonding NIC are mapped to the same virtual interface. Edit the/etc/modules.conf file and add the following to enable the system to load the bonding module at startup. Alias bond0 bonding Options Bond0 mode=0

The "mode" value indicates the operating mode, with 0, 1, 2, and 34 modes, set to 0. The bonding work in load balancing (the load Balancing (round-robin)) mode, that is, two NICs working simultaneously, then theoretically bonding can provide twice times the bandwidth. The bonding runs in promiscuous (PROMISC) mode of the NIC, and it modifies the MAC address of the two NIC to the same. Promiscuous mode is that the network card no longer only receives the destination hardware address is the data frame of its own MAC address, but can receive all the frames on the network.

5, reduce the number of virtual terminals.

Linux installed system by default is 6 virtual terminal, that is Ctrl+alt F1~f6 six, as a server can be used to switch off four, leaving only Ctrl+alt F1~f2, about 4 Mbytes of memory, but in this way, X-window will be from the original Ctrl+alt F7 into Ctrl+alt F3. Modify the/etc/inittab, the Mingetty 3 to all plus the # font size.

6. Close some unused services

Linux servers start with a number of system services that provide local and network users with a system-functional interface for Linux that is directly targeted to applications and users. The programs that provide these services are executed by daemons running in the background (daemons). A daemon is a process that has a long lifetime. They are independent of the control terminal and periodically perform certain tasks or wait to handle certain occurrences. They often start when the system boots and terminate when the system shuts down. Linux systems have many daemons, and most servers are implemented with daemons. such as Web services HTTP, and so on. At the same time, the daemon completes many system tasks, such as job planning process crond, printing process lqd and so on. Some books and materials also call the daemon a "service". To close the service method, please see the author's article: In-depth understanding of the Linux daemon.

linux+apache+mysql+php Optimization Tips

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.