Optimize PHP and Accelerate Apache 191
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."
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)
Re:want performance from php? (Score:5, Funny)
Re:want performance from php? (Score:4, Interesting)
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:2)
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: (Score:2)
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.
Re: (Score:2)
Re: (Score:2)
You are correct. [slashcode.com]
Re: (Score:2)
I find it rather ironic how few people here would say good things about Perl.
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, Interesting)
Re: (Score:2)
Re: (Score:2)
FCGI doesn't seem maintained. mod_fastcgi has no releases after 2004:
http://www.fastcgi.com/dist/ [fastcgi.com]
and mod_cgid [coremail.cn] always suffered from instability [google.com] under serious loads, which was never solved. There is no fastcgi module from the ASF...
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.
Re: (Score:2)
Re: (Score:2)
Hah, that is good to know. Actually, I've always been a big Monty Python fan and never realized that it mentions a snake in its name. Damn, now rather than use Python I'm probably going to not watch Monty Python either!
Only joking, the real reason I don't use python is I have never seen any real major advantage in using it over Perl/PHP/Java. Having said that, the couple of times I've played around with it I did keep on getting this
Re: (Score:2)
Re: (Score:2)
Or not all have mem leaks and/or don't care for bugs closed months ago.
PHP is slower than Perl or Python for most stuff.
And your filesystem, database or network is even slower. Benchmarks != real applications.
PHP security?
Maybe show security problems on Flickr or in Yahoo apps or comparable apps written in PHP.
Re: (Score:2)
Exactly my feeling. I've settled with a frontend Apache, running an event MPM, along with a backend apache with a prefork MPM. The frontend handles URL-rewriting, keepalive connections and mod_deflate filtering, then serves all static content. Non-static co
But I Can't Pronounce "LLMP"! (n/t) (Score:4, Funny)
Re: (Score:2, Funny)
Re: (Score:2)
Re: (Score:3, Funny)
Re: (Score:2, Insightful)
Re:want performance from php? (Score:5, Informative)
Re: (Score:2)
To what extent have you used sqlrelay? Any particular shortcomings? It sounds like it would be a nice solution to use where you have multiple database servers and only want to use one for updates/inserts but not a lot of people use it for some reason.
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.
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.
Re: (Score:2)
What number to set it to depends on the size of each Apache process, and how much memory you can spare in the system.
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
I assume this behaviour is documented somewhere, but I haven't seen it.
Re: (Score:2)
The os will always queue incomming requests for a given port if a program(apache) have been bound to that port.
The request will then be in queue until apache calls accept() and accept the new connection.
There is ofcause a limit to how much the os will queue, and after that it just refuse new connections similary to if no webserver were running.
Re: (Score:2)
So I guess what could also happen is you could exhaust the operating system's ability to handle connections, right? So if you hit MaxClients early and you have thousands and thousands of users trying
Re: (Score:2, Insightful)
Re: (Score:2)
And just because I am bored now, I looked op SOMAXCONN which is defined to 128, so the default limit for linux(Fedora core 5, kernel 2.6.20) is 128 which is actuelly rather small.
Re:want performance from php? (Score:5, Interesting)
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
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
Re: (Score:2)
I have benchmarked [2bits.com] APC vs. eAccelerator [eaccelerator.net] and found that eAccelerator is some 13% faster. It also has significantly lower memory consumption than APC.
Re: (Score:2)
Consider a chat-application (I've written and I'm maintaining one) based on AJAX. To feel responsive, every parttaker in a chatsession should at least fire one XMLHttpRequest each second. More would be nice. Lighttpd can handle that with two or three "chatters" online. More becomes painstaking. The same is true with Apache 2 using the default settings, but you can increase the maximum it can
The real way to improve server performance... (Score:5, Funny)
Here are a few tips:
Best of luck!
Re: (Score:2)
Dump Apache! its the slowest link!"
Then dump php, and get a real webapp tool.
Re: (Score:2)
Of course! (Score:5, Funny)
Re: (Score:2)
Yeah one of the servers on a large site I manage runs Gentoo which I installed.
Its a smaller server than the others but it can keep up well.
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)
Really dump them???? (Score:2)
Besides that white paper do you have more info about it ?
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)
Re: (Score:2)
http is the problem (Score:5, Funny)
Hello mac troll! (Score:2)
actually (Score:2)
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.
Re: (Score:2)
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.
Re: (Score:2)
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.
Re: (Score:2)
That said, PHP is only a constant factor slower than C. A big constant factor, granted, but not 100,000. It appears that your perform improvement with the C rewrite came from algorithmic improvements, not a simple tran
Re: (Score:2)
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)
PHP is still the hottest. (Score:2, 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.
Re: (Score:2)
Well, the list is too long. First, not scriping languages, but just as fast and robust to develop in :
But I can give you very quickly the list of scripts that have as bad or worse function/feature consistency than php:
Re: (Score:2)
Perl [tnx.nl], for one.
To summarise:
Which all adds up to one thing: PHP is a simple language that's actually rather hard to program in. Or maintain.
Re: (Score:2)
Honestly, if I were to reach for another language to get something done, I'd go for Python or Ruby before I even considered Perl as an option. Both are far more elegant choices.
Re: (Score:2)
True, I'd even agree with you there. I simply picked Perl because I've done a lot of work with it and someone had made a really good page comparing the built-in functions of both PHP and Perl. And the core of the PHP language is clearly heavily influenced by Perl. Sadly the original PHP developers show none of the care and insight t
Re: (Score:2)
I wanted to discuss this one... I've heard this particular criticism of PHP many times, although I haven't seen any suggestions about what precisely PHP should do to fix this. (Or what precisely is wrong with having a lot of functions in the "core," a term which is also unclear.)
A fresh PHP 5 install with no optional libraries included has 1,282 functions defined. Is that a lot? The standard C library has around 300 functions. A lot of PHP's functions are things tha
Re: (Score:2)
What I see as wrong with having lots of built-in functions:
Re: (Score:2)
This, by itself, I don't see as valid -- it's difficult to remember any large list of things, regardless of whether they're all included by default. However:
This, I definitely agree with, and it is harder to remember any list of things when they're inconsistently named, so alleviating this would alleviate the first issue. Plus, namespaces would help a great deal.
Re: (Score:2)
Off the top of my head, I can't think of another scripting language that has no support whatsoever for namespaces. So while I can't necessarily show you a language whose function naming doesn't suck, neither can I show you any whose sucks as badly.
Re: (Score:2)
Re: (Score:2)
I like to use atop [atconsultancy.nl] to figure these things out. It breaks down system resource use into a couple of distinct categories, like mem, cpu, and swap, and visually indicates which one is the limiting factor at the moment. It'll also tell you which processes are allocating and deallocating memory. It's not particula
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
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.
Re: (Score:2)
Re: (Score:3, Informative)