Home > Backend Development > PHP Tutorial > LAMP (Linux+Apache+Mysql+PHP) optimization skills_PHP tutorial

LAMP (Linux+Apache+Mysql+PHP) optimization skills_PHP tutorial

WBOY
Release: 2016-07-15 13:24:08
Original
793 people have browsed it

LAMP: The origin of the word first started with the German magazine "c't Magazine". Michael Kunze first combined these projects in 1990 to create the abbreviation of LAMP. Although these components were not designed to be used together from the beginning, these open source software are readily available and available for free. This results in these components being often used together. Over the past few years, the compatibility of these components has continued to improve, and their use together has become very common. To improve collaboration between different components, certain extensions have been created.

Currently, almost all Linux distributions include the "LAMP stack" product by default. These products form a powerful web application platform.

The LAMP platform consists of four components with a layered structure. Each layer provides a key part of the overall software stack:

Linux: Linux is at the lowest level and provides the operating system. Every other component actually runs on Linux as well. However, it is not necessarily limited to Linux, other components can also run on Microsoft® Windows®, Mac OS X or UNIX® if necessary.

Apache: The next lowest level is Apache, which is a web server. Apache provides mechanisms that allow users to obtain Web pages. Apache is a stable, mission-critical server used as a web server by more than 65% of the websites on the Internet. PHP components are actually in Apache, and dynamic pages can be created through Apache and PHP.

MySQL: MySQL provides the data storage side of the LAMP system. With MySQL, you get a very powerful database suitable for running large and 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 through SQL language.

PHP: PHP is a simple yet effective programming language that acts like the glue that holds all other components of a LAMP system together. You can use PHP to write dynamic content that accesses data in a MySQL database and some of the features provided by Linux.

[Detection Tool]

In order to get complete debugging results, it is recommended that you use software such as ApacheBench or httperf. If you are interested in non-LAMP architecture server testing, it is recommended that you use Microsoft's free software: Web Application Stress Tool (requires NT or 2000). (Other server testing tools)

Detect Apache, use top d 1 to display the CPU and memory status of all processes. In addition, the apachectl status command is also used

[Hardware Optimization]

1. General rules for upgrading hardware: For PHP scripts, the main bottleneck is the CPU. For static pages, the bottlenecks are memory and network. A regular 400 Mhz Pentium machine can saturate a T3 line (45Mbps) with static page downloads.

2. Using hdparm to optimize the disk can generally improve the read and write performance of IDE disks by 200%, but it also has an effect on SCSI hard disks. (Comparison of different types of hard drives)

[Strategy Optimization]

3. Apache processes PHP scripts 2-10 times slower than static pages, so try to use More static pages, less scripts.

4. If the PHP script is not buffered, it needs to be compiled every time it is called. Therefore, installing a PHP buffering product can improve performance by 25-100%.

5. If you use a Linux system, it is recommended to upgrade the kernel to 2.4, because static pages are served by the kernel.

6. Another buffering technology is to use HTML buffering for output of PHP pages that are not frequently modified.

7. Do not run X-Windows on the Web server and close unnecessary processes.

8. If you can use text, don’t use images, and try to reduce the size of the images.

9. Distribute the load and put the database server on another machine. Use another low-end machine to serve images and HTML pages. If all static pages are processed on another server, you can set KeepAlives in httpd.conf to off to reduce the disconnection time.

10. All the above methods are for single machines. If you feel that the system is not fast enough, you can use clustering, load balancing, and buffering technologies. Use Squid as a buffer and configure Squid.

[Compilation Optimization]

11. Switch the file-based session to a shared memory-based session. Use the --with-mm option when compiling PHP and set set session.save_handler=mm in php.ini. This simple change can cut session management time in half.

12. Use the latest version of Apache and compile PHP into it, or use DSO mode instead of CGI.

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, the PHP script only takes the IP address
HostnameLookups off

15、如果网络拥挤,CPU 资源不够用,采用 PHP 的 HTML 压缩功能:
output_handler = ob_gzhandler
PHP 4.0.4 的用户请不要使用,因为存在内存泄漏问题。

16、修改 httpd.conf 中的 SendBufferSize 为你最大的页面文件的大小。加大内核的 TCP/IP 写缓冲大小。

17、采用数据库的持久连接时,不要把 MaxRequestsPerChild 设置得太大。

[第三方软件优化]

18、如果喜欢从修改 Apache 源码入手,可以安装 lingerd。在页面产生和发送后,每个 Apache 进程都会浪费一段时光在客户连接上,Lingerd 能接管这项工作,让 Apache 迅速服务下一个客户请求。

19、如果你足够勇敢的话,还可以采用 Silicon Graphics 的 Accelerated Apache 补丁。这个工程能使 Apache 1.3 快 10 倍,使 Apache 2.0 快 4 倍。

安装一个 PHP 缓冲产品能提升 25-100% 的性能。

[Linux系统优化]

1.清理服务器磁盘碎片:

不论Linux文件系统采用什么文件格式(ext3、JFS、XFS、ReiserFS )、何种类型的硬盘(IDE 、SCSI),随着时间的推移文件系统都会趋向于碎片化。ext3、JFS等高级文件系统可以减少文件系统的碎片化,但是并没有消除。在繁忙的数据库服务器中,随着时间的过去,文件碎片化将降低硬盘性能,硬盘性能从硬盘读出或写入数据时才能注意到。时间长了会发现每个磁盘上确实积累了非常多的垃圾文件,释放磁盘空间可以帮助系统更好地工作。Linux最好的整理磁盘碎片的方法是做一个完全的备份,重新格式化分区,然后从备份恢复文件。但是对于7×24小时工作关键任务服务器来说是比较困难的。Kleandisk是一个高效的磁盘清理工具,它能把磁盘上的文件分成不同的"组",比如把所有的"core"文件归成一组(Group),这样要删除所有core文件时只要删除这个组就行了。core文件是当软件运行出错时产生的文件,它对于软件开发人员比较有用,对于其他用户(比如电子邮件服务器)却没有任何意义。因此,如果没有软件开发的需要,见到core文件就可以将其删除。

2、开启硬盘DMA

现在使用的IDE硬盘基本支持DMA66/100/133(直接内存读取)但是Linux发行版本安装后一般没有打开,可以 /etc/rc.d/rc.local 最後面加上一行: /sbin/hdparm -d1 –x66 -c3 -m16 /dev/hda 这样以后每次开机,硬盘的 DMA 就会开启,不必每次手动设定。添加前后你可以使用命令:hdparm -Tt /dev/hda 来测试对比一下。

3、调整缓冲区刷新参数

Linux内核中,包含了一些对于系统运行态的可设置参数。缓冲刷新的参数可以通过调整 /proc/sys/vm/bdflush文件来完成,这个文件的格式是这样的:

<font color="#000000"></font> 
Copy after login
Copy after login
<font color="#000000"># cat /proc/sys/vm/bdflush 30 64 64 256 500 3000 60 0 0</font>
Copy after login

每一栏是一个参数,其中最重要的是前面几个参数。第一个数字是在"dirty"缓冲区达到多少的时候强制唤醒bdflush进程刷新硬盘,第二个数字是每次让bdflush进程刷新多少个dirty块。所谓dirty块是必须写到磁盘中的缓存块。接下来的参数是每次允许bd flush将多少个内存块排入空闲的缓冲块列表。 以上值为RHEL 4.0中的缺省值。可以使用两种方法修改:

(1)使用命令

<font color="#000000"><em># echo "100 128 128 512 5000 3000 60 0 0">/proc/sys/vm/bdflush</em></font>
Copy after login

并将这条命令加到/etc/rc.d/rc.local文件中去。


(2)在/etc/sysctl.conf 文件中加入如下行:

<font color="#000000"></font> 
Copy after login
Copy after login
<font color="#000000">vm.bdflush = 100 128 128 512 5000 3000 60 0 0 </font>
Copy after login

以上的设置加大了缓冲区大小,降低了bdflush被启动的频度,VFS的缓冲刷新机制是Linux文件系统高效的原因之一。

4、优化输入输出

I/O程序对Linux系统性能也是相当重要的,网络硬件I/O对服务器尤其重要。现在大多数Linux服务器使用10/100 Mb以太网。如果有较重的网络负载,则可以考虑千兆以太网卡。如果没有能力购买千兆网卡的话:可以使用多块网卡虚拟成为一块网卡,具有相同的IP地址。这项技术,在Linux中,这种技术称为Bonding。Bonding在Linux2.4以上内核中已经包含了,只需要在编译的时候把网络设备选项中的 Bonding driver support选中见图1。当然利用Bonding技术配置双网卡绑定的前提条件是两块网卡芯片组型号相同,并且都具备独立的BIOS芯片。

然后,重新编译核心,重新起动计算机,执行如下命令:

<font color="#000000"> </font>
Copy after login
Copy after login
<font color="#000000">#ismod bonding #ifconfig eth0 down #ifconfig eth1 down <p style="TEXT-INDENT: 2em">#ifconfig bond0 ipaddress#ifenslave bond0 eth0#ifenslave bond0 eth1</p></font>
Copy after login

现在两块网卡已经象一块一样工作了。这样可以提高集群节点间的数据传输.bonding对于服务器来是个比较好的选择,在没有千兆网卡时,用两块100兆网卡作bonding,可大大提高服务器到交换机之间的带宽.但是需要在交换机上设置连接bonding网卡的两个子口映射为同一个虚拟接口。编辑 /etc/modules.conf文件,加入如下内容,以使系统在启动时加载Bonding模块。

<font color="#000000"> </font>
Copy after login
Copy after login
<font color="#000000">alias bond0 bonding options bond0 mode=0</font>
Copy after login

“mode”的值表示工作模式,共有0、1、2和3四种模式,这里设定为0。Bonding工作在负载均衡(Load Balancing (round-robin))方式下,即两块网卡同时工作,这时理论上Bonding能提供两倍的带宽。Bonding运行在网卡的混杂(Promisc)模式下,而且它将两块网卡的MAC地址修改为一样的。混杂模式就是网卡不再只接收目的硬件地址是自身MAC地址的数据帧,而是可以接收网络上所有的帧。

5、减少虚拟终端机的数量。

Linux安装后系统默认是6个虚拟终端机,也就是 CTRL+ALT F1~F6 那六个,作为服务器使用可以关掉其中四个,只留下 CTRL+ALT F1~F2,大约省下 4 Mbytes 的内存,但是这样一来,X-Window 会从原来的 CTRL+ALT F7 变成 CTRL+ALT F3 。 修改 /etc/inittab 中,将 mingetty 3 ~6 全部加上 # 字号 。

6. 关闭一些不用的服务

Linux服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户。提供这些服务的程序是由运行在后台的守护进程(daemons)来执行的。守护进程是生存期长的一种进程。它们独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件。他们常常在系统引导装入时启动,在系统关闭时终止。linux系统有很多守护进程,大多数服务器都是用守护进程实现的。如Web服务http等。同时,守护进程完成许多系统任务,比如,作业规划进程crond、打印进程lqd等。有些书籍和资料也把守护进程称作:“服务”。关闭服务方法请查看笔者的文章:深入理解Linux守护进程。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446790.htmlTechArticleLAMP: 这个词的由来最早始于德国杂志“c't Magazine”,Michael Kunze在1990年最先把这些项目组合在一起创造了LAMP的缩写字。这些组件虽然并不...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template