Optimize PHP and Accelerate Apache
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."
That sounds like a rather dumb comment. I also bet you are comparing lighty to apache 1.3. Or you do not know what you`re talking about. I have had (until february) a P3-400 w. 256 MB with around 200 Domains configured (of which 2 make up for 98% of traffic)
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, so I have to keep a certain quantity of processes in reserve (20).
This leads to memory problems, especially since, with mod_php, the process size alway increases but never decreases with apache-process age. Thrading (where basically memory is shared) and Fastcgi for the php should cure that. And that is what lighty does. But: with apache2, worker mpm and mod_fcgid, it works, too.
Then all you need to do is keep keepaliverequests reasonable and keepalivetimeout low. And get a reverse proxy (apache2.2).
Lighty is probably good, but I do not think it has much advantage over a well tuned apache.
I would love to dump Apache, but Lighttpd and the other alternatives simply don't provide the power, functionality and customisation I need from my servers. Then there are the many inequitable Apache extensions which are simply a necessity in some situations.
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.
Maybe I'm being a cynical bastard again, but that article is REEEALLY light on content. Compared to the other featured articles from IBM, which are usually very rich and informative, this one is more like an "idiots guide to apache", the kind that belongs on Digg's mountain of filth. This is little more than a rehash of the Readme files for Apache and PHP combined. It's about as deep as telling a windows user how to make their PC faster by changing to the Windows NT theme. Of much greater value to web professionals is this article from a fellow OSDN site (!) Lighttpd can lighten Apache's load
-Billco, Fnarg.com
[troll]Use Perl, mod_perl, profile your applications, and cache, cache, cache.[/troll]
One thing lighttpd has going for it is SCGI, FCGI really does need to die.
It's common knowledge that Apache certainly isn't the fastest out there to server web pages. For static pages lighttpd and, heck, even a basic stand-alone Tomcat (yup, a Java server!) can easily serve as many pages as Apache. That was already the case for, say, Java 1.5 / Tomcat 5.5. Now take Java 1.6 and Tomcat 6 (or, better, Resin [not completely free]) and suddenly Apache is looking even more ridiculous.
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.
- 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."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.
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.
I routinely experience Apache crashes while using eAccelerator, APC or Zend Optimizer, even with the latest stable PHP. If you use one of these products, be sure to also run some kind of watchdog that will restart httpd if (when) the stack crashes. They're totally worth running in any case. Zend Optimizer speeds up my Drupal installation by a huge amount because of the time saved parsing code.
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.
std::disclaimer<std::legalese> sig=new std::disclaimer; sig->dump(); delete sig;
It includes:
Disclaimer: this is stuff that I have written.
2bits.com, Inc: Drupal, WordPress, and LAMP performance tuning.
FastCGI is over-complex and was abandoned tech until the rails guys started using it. mod_${script_host} was deemed a better solution by almost everybody back in the day.
Compare the specs: SCGI FCGI
You could probably write a SCGI daemon in your lunch hour.
Yes, it is safe, it doesn't add or remove anything from a safety perspective. The atime is however useful in case you regularly have to audit your system for financial reasons (SoX) but otherwise not necessary for working. Make a backup of your filesystem and then disable it, no harm will be done.
Custom electronics and digital signage for your business: www.evcircuits.com
...for most many cases, is to setup an edge server / reverse proxy. It will stop the load BEFORE it even reaches lighty / apache / whatever.
:-)
It's very easy to do, 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. He sell a "turbo charged" Wordpress, 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.