Slashdot Log In
Optimize PHP and Accelerate Apache
Posted by
CmdrTaco
on Sun May 27, 2007 09:32 AM
from the wouldn't-it-be-nice-if-we-were-faster dept.
from the wouldn't-it-be-nice-if-we-were-faster dept.
An anonymous reader writes "As the load on an application increases, the bottlenecks in the underlying infrastructure become more apparent in the form of slow response to user requests. This article discusses many of the server configuration items that can make or break an application's performance and focuses on steps you can take to optimize Apache and PHP."
This discussion has been archived.
No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
Full
Abbreviated
Hidden
Loading... please wait.
want performance from php? (Score:3, Interesting)
Dump Apache! its the slowest link!
use Lighttpd [lighttpd.net] + latest PHP 5.2.2 + APC [php.net]
Re:want performance from php? (Score:4, Interesting)
Parent
Re:want performance from php? (Score:5, Funny)
Parent
Re:want performance from php? (Score:4, Interesting)
Parent
Re: (Score:3, Interesting)
TFA reads like a newbies blog article, 'OMG I know how to haX0r php.ini and httpd.conf'. Any half decent sys admin
Re: (Score:3, Informative)
running a LAMP Stack w. apache 1.3. The machine serves 6 concurrent users with 8.9 Database-Hits/Sec serving around 30-40G/month.
I have absolutely no problem with apaches speed when it comes to serving pages. It does lag when forking processes, though, s
Reverse proxy (Score:3, Informative)
Re:want performance from php? (Score:5, Insightful)
Now lighttpd serves 6 out of the top 250 sites. Do you think the other 246 run IIS or something?
Lighttpd is good, but is best used in specialised instances, for specific (mainly static content) tasks. Its pointless using it for PHP as the cost of forking out a process to run the script will outweigh any saving from running a lighter-weight http server.
Parent
Re: (Score:2)
Re: (Score:3, Interesting)
Re: (Score:3, Insightful)
or that "shitty non-database" MySQL that manages to store all the bazillion comments/stories and is constantly hit.
Re: (Score:3, Informative)
Personally I feel Lighttpd is just too light, but I agree, Apache is too heavy. I would love to find a happy medium but I am yet to find a httpd server which even approaches Apache and its popularity shows that an awful lot of people agree with this.
Re: (Score:3, Interesting)
So far Apache is good enough for me, but if you're going to replace Apache with something that is fast and has features, you may wish try Zeus.
Anyway, I'd dump PHP first. PHP is slow and has tons of security problems - and judging from the way the devs do stuff, there'll be plenty more to come.
Re: (Score:3)
"lighttpd leaks memory":
http://www.google.com/search?hl=en&safe=off&q=+sit e:trac.lighttpd.net+lighttpd+memory+leak [google.com]
Random complainer: http://hostingfu.com/article/nginx-vs-lighttpd-for -a-small-vps [hostingfu.com]
As for security: Apache isn't what you'd call secure software, but Lighttpd isn't either.
PHP is slower than Perl or Python for most stuff. This should be common knowledge amongst decent programmers- go find/make your own benchmarks and links.
PHP securit
Re:want performance from php? (Score:5, Insightful)
I'd say that in practice (i.e. when performing the vast majority of dynamic web functionality: e.g. database lookups) the opposite is true. Perl & Python are quicker at some tasks, but every-time I've rewritten a website between PHP and Perl (I don't program in Python because it's named after my most hated animal), PHP has come out slightly on top.
Parent
But I Can't Pronounce "LLMP"! (n/t) (Score:4, Funny)
Parent
Re: (Score:2, Funny)
Re: (Score:2)
Re: (Score:3, Funny)
Re: (Score:2, Insightful)
Re:want performance from php? (Score:5, Informative)
- Use apc or eaccelerator. (yahoo uses apc so that is the one we went with). This alone will give considerable benefit.
Apc can defaultly cache any of the php that runs or it can also be used as a local cache for objects you'd like to store programmatically.
- If you need distributed items, especially in a non-sticky load balanced environment, look at memcached.
- Use a query cache for your db
- If your db connections are expensive, look at sqlrelay
- Look at whether a caching proxy is a possibility for you (squid or apache has some mods).
- Benchmark your pages and functions. It is the only way to know if configuration tweaks are adding any value. I usually do this after a full profiling using apd (to help identify the bottlenecks and frequently called functions). I usually run apache's ab to get a look at page benchmarks.
- you can always write c extensions for items in php that are too slow. Of course, you'll have to know c, increased maintenance, development time, etc
There are a million things to be done to increase performance. Obviously, don't use anything blindly. Still, I think the opcode cache (apc or eAccelerator) is probably the easiest and most substantial win.Parent
Re:want performance from php? (Score:5, Informative)
All that apache does when serving PHP requests with mod_php is run the early phases (auth, URL mapping) and pass the request to PHP. Do you really think that Lighttpd + FastCGI is going to be significantly faster than that? The bottleneck is your PHP code, not the web server.
Lighttpd is probably faster at serving static content. Most sites don't have enough bandwidth to find out.
Parent
Re: (Score:2, Insightful)
lighttpd and friends are generally better if you're serving static content, but it's doubtful you'll notice unless you're talking in terms of many thousands of requests per second to a single server. That's not to say there aren't other reasons
Re:want performance from php? (Score:5, Insightful)
Lightppd doesn't have that problem.
Parent
Re:want performance from php? (Score:5, Interesting)
Parent
Its all about how you use Apache (Score:2, Interesting)
The latest performance numbers show that Apache 2.2 w/ worker MPM (threaded) + FastCGI can keep pace with Lighty and LiteSpeed. Many of us large site maintainers prefer Apache's feature set.
I agree that APC rocks, it outpaces Eaccelerator in a big way, and is more stable.
This article is not worth the read, I could conde
The real way to improve server performance... (Score:5, Funny)
Here are a few tips:
Best of luck!
Parent
Re: (Score:2)
Of course! (Score:5, Funny)
Modules (Score:2, Interesting)
I also force PHP to run in FastCGI mode, that way I can use suExec with it to prevent exploits in PHP itself from allowing the entire system to be compromised. Apache suid()s itself after handling the request, but the Apache user can generally write to a few good places, therefore I use suExec.
Re: (Score:2)
"kill off any unneeded modules (come on, how many of you actually use mod_userdir or mod_rewrite"
Are you kidding? If anything, those are the two that are the handiest. Ever use short urls? That's mod_rewrite for you. Ever have users with their own public_html? That's mod_userdir.
Really, if you're going to pick on something, pick on, um, maybe mod_speling. (no that's not a spelling mistake - its a module that tries to guess what you really wanted if you misspelled a url, rather than just sending a 404,
Dunmp then both (Score:2, Interesting)
Re: (Score:3, Funny)
Re: (Score:2, Insightful)
Diet article (Score:5, Informative)
Re: (Score:2)
Well, yes, I agree, you are being a cynical bastard. I just forwarded the link to a couple of people I know who could get some useful tips from that article. You see, not everyone is born knowing it all, some people can profit from stuff that others already know.
And also, information isn't like money, that you want as much as possible. There is such a thing as information overload, information is something that you want exactly as much as you need, not a bit more. Al
Re: (Score:2)
Re: (Score:2, Informative)
http is the problem (Score:5, Funny)
Apache ain't the fastest... (Score:4, Informative)
For anything SSL related IIS makes fun of Apache.
For many concurrent sessions there are proof of concept servers written in functional languages (like erlang) that handle an order of magnitude (!) more simultaneous sessions than what Apache does.
I understand that for many here Apache is all they know and hence regard it as the holy grail but it is far, far, from being that. It is a good overall purpose web server but it is certainly not the fastest.
In the face of the countless security holes that are all too common in C-written apps I'm now more and more installing pure Java web servers. At first I was using Apache + Tomcat now I'm more and more using Tomcat in standalone mode. Easier configuration, no buffer overflows to patch.
Careful with the php accelerators (Score:3, Informative)
You also should of course make sure you've got caching happening at every level of your app. Memcached is a great application level cache. Apache side, make sure you enable and configure the memory cache for static files. (mod_mem_cache) and possibly the file cache (mod_disk_cache). Client side, make sure you're caching static files like images, js, css. Apache's mod_expires gives you good control over this in Apache config.
Of course, all of this could be just spinning your wheels if you have badly optimized sql queries or bad table design to start with. Set mysql's slow query log threshold very low to catch these issues early.
Re: (Score:2)
Of course I am probably going to be modded down here on slashdot for such a posting.
C code is one answer (Score:3, Interesting)
What we did was re-write all of the application except the parts that actually printed HTML to the page in C. We made that library into a php extension, which we added to our php installation. The speed ups varied according to the type of code, of course. MySQL queries changed hardly at all. A simple for loop might speed up from 100 to 1000 times, however, and a for loop that involved some sort of object oriented operations -- say, the creatation of a new object with each iteration, in an extreme example -- could be sped up from 10,000 to 100,000 times.
Now, most code that was sped up 100,000 times, when examined closely, could be sped up in PHP also by writing it smarter. But bad C code still runs faster than good PHP code.
If your disk drives are slow, or your code is printing a debugging log that has every function entry and exit to a network shared disk, or something like that, don't even think about C, just fix the basic problem first.
However, in my experience, twiddling the apache configs for "AllowOverride" and stuff like that will never get you as big a speed up as moving PHP code to C.
Note, that the way we did it, we moved all the core functionality and logic to a C library, but most of the display stuff was left in PHP. This meant that most ongoing development could continue to be done by cheaper, less skilled people who knew only PHP, MySQL, javascript, and CSS.
Performance tuning and optimization of LAMP (Score:5, Informative)
It includes:
Disclaimer: this is stuff that I have written.
Biggest back for the buck... (Score:3, Informative)
It's very easy to do [sufehmi.com], take very little time, reliable & proven (which is not always the case with php accelerators), and will easily drop a double-digit server load to a single one.
It constantly amazes me how people will do the hardest stuff which gave the minimum return first.
Such is life I guess
If you want more proof, read this guy's article [turbochargedcms.com]. He sell a "turbo charged" Wordpress [turbochargedcms.com], got digged and overloaded, optimized his website and still overloaded. Finally his server managed to go through it after he implemented squid's reverse proxy.
In summary - sort out your priorities guys.
Biggest back for the buck first, and then go down from there.
PHP rocks on AMD64? (Score:3, Interesting)
Re: (Score:2)
There are plenty of shared hosting providers who will let you use any apache/php settings you choose. I have a virtual server with bytemark, and have so far been quite happy with it. It doesn't cost a fortune, and is quite capable.
Re:You can talk about this all day, but... (Score:5, Informative)
"I also expect that the language is going to be forked at some point unless the PHP developers clean up the function naming - it's infuriating."
There's nothing to stop you from cleaning up the function naming. We'd all appreciate it :-)
Also, if you really want performance "uber alles", don't bother with apache or any other "pre-made" server - write a custom server in c designed to load modules that serve just the content you want served. You can then handle a thousand requests per second (including time to access the database a half-dozen times per request) on comodity hardware.
Its not like there isn't code out there that shows you how to implement a server in c, how to write and load modules in c, how to use threads in c, and how to access mysql via c. You'll be super fast ... except that your development time will be super slow.
apache+php is a compromose that most people can live with, most of the time.
Yes, the function naming in php is crap. Show us a scripting language where it isn't.
Parent
Re:Just listen to this (Score:4, Interesting)
No, really? I do to too actually.
That job is to send content to the client as fast as possible.
Wrong. That job is to get the job done, fast, good and cheap, and if that doesn't work (which it never does) it's to evaluate the best tradeoffs without bugging the client to much. And by client I mean the one with the two legs and the checkbook that doesn't give a damn wether you're using Ruby or PHP or Django or Symfony or Zope or Rails or whatnot but wants to see his webapp ASAP.
There are numerous tradeoffs in the architecture and configuration of web servers and scripting languages and when you're working with this stuff everyday the sub-optimal becomes irritating.
And you know what the suboptimal and the optimal is? No? Well, then you are aware of more than most posters I sorta quoted above.
If it's all a bit over your head, I suggest you instead participate in discussions at a level you're more comfortable with.
WTF? I've been programming since '86 and have been doing webstuff for a living since 2000. I don't see nothing over my head here. Just a tad incoherent for a community made up of what seems like 90% part-time webdevs with each their own favorite toolset. A community of which 80% act as if they where members of the Linux Kernel Team and come across a bit silly at times. Don't get me wrong, there are interesting posts here. But for each of those it looks as if three 'expert' posts sound like what I was moking. And that what I was poking at with the parent post.
Parent
Re: (Score:3, Informative)