优化LAMP网站以提高速度的最佳做法? [英] Best practices for optimizing LAMP sites for speed?

查看:134
本文介绍了优化LAMP网站以提高速度的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在LAMP堆栈上构建典型站点时如何优化它以实现最佳加载时间.我正在描绘一个典型的数据库驱动站点.

I want to know when building a typical site on the LAMP stack how do you optimize it for the best possible load times. I am picturing a typical DB-driven site.

这是一个高级的外观,可能会引起问题,让我将其分解到堆栈的每一层.

This is a high-level look and could probably pull in question and let me break it down into each layer of the stack.

L-在系统级别(设置和文件系统),您可以提高速度吗?我能想到的一件事是图像大小,这里的压缩可以帮助优化任何东西吗?

L - At the system level, (setup and filesystem) can you do to improve speed? One thing I can think of is image sizes, can compression here help optimize anything?

A-Web服务器中必须进行大量与站点速度相关的设置.不是我的强项.可能在很大程度上取决于有多少个站点同时运行.

A - There have to be a ton of settings related to site speed here in the web server. Not my Forte. Probably depends a lot on how many sites are running concurrently.

M-在数据库驱动站点中的MySQL,数据库性能是关键.是否有更好的规范化方法,即使用链接表? Web开发人员通常只是制作类似于1NF的简单整体表,这会降低性能.

M - MySQL in a database driven site, DB performance is key. Is there a better normalization approach i.e, using link tables? Web developers often just make simple monolithic tables resembling 1NF and this can kill performance.

P-除了诸如缓存之类的提高性能的设置外,程序员还可以做些什么来在很大程度上影响性能?我真的很想知道MVC设计方法对性能的影响是否比一尘不染.有趣的是,其他一些简单的技巧(例如会话速度比cookie更快)很有趣.

P - aside from performance-boosting settings like caching, what can the programmer do to affect performance at a high level? I would really like to know if MVC design approaches hit performance more than quick-and-dirty. Other simple tips like are sessions faster than cookies would be interesting to know.

很显然,您必须认真研究细节,然后找出使您减速的代码.我也意识到许多站点具有许多不同的性能特征,但是让我们假设一个典型的站点具有更多的读写能力.

Obviously you have to get down and dirty into the details and find what code is slowing you down. Also I realize that many sites have many different performance characteristics, but let's assume a typical site that has more reads then writes.

我只是想知道我们是否可以汇编一些最佳实践,并完全期望人们将其他问题联系在一起,以便我们可以有效地编制清单.

I am just wondering if we can compile a bunch of best practices and fully expect people to link other questions so we can effectively workup a checklist.

我的目标是看看,除了性能上的常见问题之外,我们是否还能看到一些您可能不会想到的怪异事物,以及最佳实践摘要.

My goal is to see if even in addition to the usual issues in performance we can see some oddball things you might not think of crop up to go along with a best-practices summary.

所以我的问题是,如果您是从头开始,那么如何确保您的LAMP网站很快?

So my question is, if you were starting from scratch, how would you make sure your LAMP site was fast?

推荐答案

以下是我始终在LAMP应用程序中设置的一些个人必需事项.

Here's a few personal must-dos that I always set up in my LAMP applications.

  • 为Apache安装mod_deflate,然后 不要使用PHP的gzip处理程序. mod_deflate将允许您 压缩静态内容,例如 javascript/css/static html以及 作为通常的动态PHP输出,以及 这是您不必担心的另一件事 关于您的代码.

  • Install mod_deflate for apache, and do not use PHP's gzip handlers. mod_deflate will allow you to compress static content, like javascript/css/static html, as well as the usual dynamic PHP output, and it's one less thing you have to worry about in your code.

.htaccess文件要小心! 为启用.htaccess文件 应用中的目录意味着 Apache必须扫描文件系统 一直在寻找.htaccess 指令.放好多了 主体内部的指令 配置或虚拟主机 配置,它们在哪里加载 一次.任何时候您都可以摆脱 通过移动目录级别的访问文件 将其放入主配置文件中 您可以节省磁盘访问时间.

Be careful with .htaccess files! Enabling .htaccess files for directories in your app means that Apache has to scan the filesystem constantly, looking for .htaccess directives. It is far better to put directives inside the main configuration or a vhost configuration, where they are loaded once. Any time you can get rid of a directory-level access file by moving it into a main configuration file, you save disk access time.

准备应用程序的数据库 层以利用连接管理器 某种形式(我使用Singleton 大多数应用程序).不是很好 很难做到,并减少了数量 数据库连接数 打开应用程序可以节省资源.

Prepare your application's database layer to utilize a connection manager of some sort (I use a Singleton for most applications). It's not very hard to do, and reducing the number of database connections your application opens saves resources.

如果您认为自己的应用程序会 看到很大的负载,memcached可以 创造奇迹.记住这一点 当您编写代码时... 一天而不是创建对象 在飞行中,您将得到他们 来自memcached.一点先见之明 将使实施过程变得轻松.

If you think your application will see significant load, memcached can perform miracles. Keep this in mind while you write your code... perhaps one day instead of creating objects on the fly, you will be getting them from memcached. A little foresight will make implementation painless.

一旦您的应用启动并运行,请进行设置 MySQL的慢查询时间很小 编号并监视慢查询日志 努力.这会告诉你在哪里 您的问题查询来自 并允许您优化您的 查询和索引之前 成为问题.

Once your app is up and running, set MySQL's slow query time to a small number and monitor the slow query log diligently. This will show you where your problem queries are coming from, and allow you to optimize your queries and indexes before they become a problem.

对于性能严重的调节器,您 将要从源代码编译PHP. 从软件包安装会安装 您可能永远都不会拥有的许多库 使用.由于PHP环境是 加载到 Apache线程,甚至5MB的内存 快速增加额外库的开销 当丢失的内存变为250MB时 有50个Apache线程 存在.我保留我的清单 我在以下情况下使用的标准./configure行 在此处构建PHP,然后我找到它 适合我的大多数应用程序.这 缺点是,如果你最终 需要图书馆,你必须 重新编译PHP以获取它.分析 您的代码并进行开发测试 确保您拥有的环境 您需要的一切.

For serious performance tweakers, you will want to compile PHP from source. Installing from a package installs a lot of libraries that you may never use. Since PHP environments are loaded into every instance of an Apache thread, even a 5MB memory overhead from extra libraries quickly becomes 250MB of lost memory when there's 50 Apache threads in existence. I keep a list of my standard ./configure line I use when building PHP here, and I find it suits most of my applications. The downside is that if you end up needing a library, you have to recompile PHP to get it. Analyze your code and test it in a devel environment to make sure you have everything you need.

缩小您的Javascript.

Minify your Javascript.

准备移动静态内容, 例如图像和视频, 非动态Web服务器.写你的 代码,以便图像的所有网址和 视频很容易配置为指向 到将来的另一台服务器.一种 针对静态优化的Web服务器 内容可以轻松投放数十甚至 比...快数百倍 动态内容服务器.

Be prepared to move static content, such as images and video, to a non-dynamic web server. Write your code so that any URLs for images and video are easily configured to point to another server in the future. A web server optimized for static content can easily serve tens or even hundreds of times faster than a dynamic content server.

这就是我能想到的.搜寻有关PHP最佳做法的信息,还会发现很多有关如何编写更快/更好的代码的提示(例如:echoprint快).

That's what I can think of off the top of my head. Googling around for PHP best practices will find a lot of tips on how to write faster/better code as well (Such as: echo is faster than print).

这篇关于优化LAMP网站以提高速度的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆