Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Slashdot Log In

Log In

Create Account  |  Retrieve Password

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.
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."
+ -
story
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
More
Loading... please wait.
  • by wwmedia (950346) on Sunday May 27 2007, @09:40AM (#19291899)
    want performance from php?

    Dump Apache! its the slowest link!

    use Lighttpd [lighttpd.net] + latest PHP 5.2.2 + APC [php.net]
    • by Ant P. (974313) on Sunday May 27 2007, @09:49AM (#19291949) Homepage
      I'll second that - Apache is as bad as Firefox when it comes to resource waste. On my machine, lighttpd uses about 1MB of RAM, not counting the PHP processes.
    • Re: (Score:3, Interesting)

      by Anonymous Coward
      There are many reasons Apache httpd is the standard and speed isn't one of them but lighttpd is pretty awful although lua integration is cool (much faster than PHP). I might say that if you want performance, avoid crap like PHP and Ruby. That's pretty much true but ignores the fact the bottleneck is usually going to be the database, regardless of whatever other software you happen to be running.

      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)

      by Anonymous Coward
      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, s
      • And get a reverse proxy (apache2.2).
        I have to second this point. I am in the process of setting up a new site that needs to use a PHP based knowledgebase system. The raw performance numbers of the knowledgebase were absolutely abysmal (6 pages/second). I spent half a day working with the Apache 2.2 reverse proxy and got it up to 350 pages/second.
          • by gbjbaanb (229885) on Sunday May 27 2007, @11:05AM (#19292461)
            They are for static content.

            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.

            • 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.
              Lighttpd uses FastCGI for PHP, so the PHP scripts run in a separate process which persists across connections (and several can be used for load balancing). There may be other reasons for not using Lighttpd with PHP (I've not tried), but the cost of fork is not one of them.
            • Re: (Score:3, Interesting)

              As somebody who's writing a program that might be running under fastcgi, I'm genuinely curious as to why fastcgi sucks and scgi is better.
              • Re: (Score:3, Insightful)

                Yep your right. Slashdot runs off Perl. I find it rather ironic how few people here would say good things about Perl.

                or that "shitty non-database" MySQL that manages to store all the bazillion comments/stories and is constantly hit.

    • Re: (Score:3, Informative)

      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.
      • Re: (Score:3, Interesting)

        lighttpd leaks memory, how secure and "lightweight" is that?

        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.
    • no text
    • Re: (Score:2, Insightful)

      Apache is what you *make* of it. If it is heavy look to your sysadmin and cut out the stuff you don't use. The idea that Apache is a bottleneck compared to ANY scripting application is totally misguided nonsense. I can't believe such an ignorant comment got any mod points, even on Slashdot. I can get at least 100Mbs out of a single server pumping out static content with nothing special other than removing unused modules in the compile to save on memory. So guess what, you are WRONG.. I at one point tried
    • by doubledjd (1043210) on Sunday May 27 2007, @11:33AM (#19292665) Homepage
      My history is java but I've come to appreciate php recently. The track record on it when used properly is impressive. In case there are some people just starting with it, the several of the problems with php performance is caching. (this is pretty standard stuff so apoligies to people who know a lot more about it than I do)
      1. 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.
      2. If you need distributed items, especially in a non-sticky load balanced environment, look at memcached.
      3. Use a query cache for your db
      4. If your db connections are expensive, look at sqlrelay
      5. Look at whether a caching proxy is a possibility for you (squid or apache has some mods).
      6. 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.
      7. 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.
    • by consumer (9588) on Sunday May 27 2007, @11:45AM (#19292767)

      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)

        Quite; you can easily use FastCGI with Apache too (though lighttpd's built-in load balancing sounds nice; hopefully mod_proxy{,_balancer} will one day grow useful FastCGI support). Do that and watch as Apache sits using about 1000x less CPU than your backend PHP's.

        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
      • by SQL Error (16383) on Sunday May 27 2007, @01:38PM (#19293555)
        The problem is not that Apache is slow, it's that it uses huge amounts of memory. If your database is running slow for some reason - say, during backups - requests will start to queue up, Apache will start more and more threads to handle those requests, and things will spiral rapidly out of control.

        Lightppd doesn't have that problem.
        • by consumer (9588) on Sunday May 27 2007, @02:26PM (#19293879)
          If you use separate processes for your static content and your PHP (either Apache with FastCGI or a reverse proxy), you won't have problems with memory. The size of an Apache process that's just serving static stuff is tiny. It's the PHP processes that take the RAM, and they will do the same when run from Lighttpd.
    • The reason why Lighty and Litespeed have pulled better numbers than Apache is because they are threaded and use FastCGI, and always get compared to Apache with the prefork MPM using mod_php.

      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
    • by dgym (584252) on Sunday May 27 2007, @08:22PM (#19296285)
      ...is to decrease load on the server. Most of the work can be done on the client anyway, so that is where it belongs.

      Here are a few tips:
      • Databases can get pretty slow with complicated queries, so upload your database to the client when they load the page and then your database queries are distributed.
      • PHP isn't very fast, and neither are Perl or Python, so you don't want to be running them on the server either. Write an interpreter for the language of your choice in Javascript and move your business logic to the client. This will also interface better with the client side database copy.
      • SSL is a performance killer, don't use it. If you need to send something securely just prefix it with a predetermined number of random letters and numbers, no one will think to look beyond them.
      • Writing to databases can be pretty bad too. Try discarding all your changes, your users might not notice the difference, but they will appreciate the performance gain.
      • Even with all the above you might still be getting too many requests. Try designing your site to exploit bugs in the major browsers and reconfigure your users machine to store and load everything itself.
      • Get better hardware.
      • Make your site less interesting, or less reliable. Changing your DNS entry to point to an unrelated site for one week a month can really help reduce load.
      • If you have had success with the other steps then you don't need a dynamic server anymore, code a simple static server in assembly, preferably for the architecture you intend to be running on, and be done with it.

      Best of luck!
      • Speaking as somebody who runs PHP in a threaded environment, PHP is actually pretty damned stable in one (i.e., it has *never* crashed for me). As I understand it, as long as you stay away from the less frequently-used extensions, you'll be fine.
  • Of course! (Score:5, Funny)

    by BabaYama (899483) on Sunday May 27 2007, @10:13AM (#19292089) Homepage
    You should compile it yourself wicked fast compiler optimizations on top of your wicked fast install of Gentoo (which you also compiled yourself)

  • Modules (Score:2, Interesting)

    Apache generally ships (on most distros) with TONS of modules enabled... kill off any unneeded modules (come on, how many of you actually use mod_userdir or mod_rewrite?), tweak your MPM settings, and be done with it.

    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.
    • "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,

  • I have found that a truly scaleable design pattern is using a lean little engine like Jetty and a declarative/Rest based architecture. We have recently ported Sugar CRM PHP/Apache to NetKernel and lost over 95% of the code and subsecond response times. We kept the horrible database design and the workflow in the first version as well, so reworking that will further improve performance and further reduce the need for code. For me performance is important but maintainability is equal. The less code the easier
    • If I took an app, "lost" 95% of the code, then I guarantee it'd work faster. Of course, by "work" I mean, "do nothing" but lets not let a little semantics get in that way of some technical self-aggrandisement. :)
      • Re: (Score:2, Insightful)

        Well, it now has all of the original functionality it had and that is down to the lack of redundant code and frameworks. Declarative based systems are just different. PHP is an awful lot of code. The code seems to me difficult to maintain and modify, but this is just my experience. I stopped using the J2EE frameworks for the same reason. After 30 years of hacking this stuff I have seen an awful lot of 3GL, 4GL .... and other frameworks and they all have to me seemed a bit bulky. Like when I try to find the
  • Diet article (Score:5, Informative)

    by billcopc (196330) <vrillco@yahoo.com> on Sunday May 27 2007, @10:30AM (#19292209) Homepage
    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 [linux.com]
    • Maybe I'm being a cynical bastard again,

      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

    • what's lighty like with mysql in the mix - I run a LAMP installation which could go faster ... an article on fine-tuning LAMP installations would be useful :-)
      • Re: (Score:2, Informative)

        If you want to improve the performance of your database, you should check out Sphinx [sphinxsearch.com] to handle your fulltext searching. In my opinion this is one of the most criminally overlooked pieces of software when it comes to website optimization.
  • by larry bagina (561269) on Sunday May 27 2007, @10:38AM (#19292273) Journal
    Last year, we dumped apache (and http altogether) and went with a gopherd/fastcgi approach for serving up our php pages. For people still stuck on port 80, we have a squid proxy which converts the request to gopher. Since then, traffic has increased 34%, while average load has dropped by 20%.
  • by Anonymous Coward on Sunday May 27 2007, @11:29AM (#19292643)
    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.

  • 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.
    • You can try running php on Windows with IIS. Its alot more stable on the windows platform and IIS is no longer the POS it used to be. Also you may want to use a real mature language like java or .NET if you need stability and maturity. Php is very mixed.

      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)

    by Anonymous Coward on Sunday May 27 2007, @12:34PM (#19293083)
    I had a customer (I am a contract programmer, working on a per-project basis) who had an emmense PHP / MySQL application that ran so slowly it was unuseable. We did every simple optimization such as is described in this article, and more. In the end, the issue was clearly that there was simply a lot of PHP code, and a lot of it using PHP's objects, and searches through arrays, and that sort of thing. The biggest performance increases we were getting was when we could push some sort of comparison or logic from PHP into the SQL query.

    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.
  • by kbahey (102895) on Sunday May 27 2007, @01:45PM (#19293589) Homepage
    This is a set of articles on Drupal performance tuning and optimization for large web sites [2bits.com]. Although it says Drupal, much of it applies to the LAMP stack in general.

    It includes:

    • PHP op-code caches / accelerators: Drupal large site case study
    • Benchmarking APC vs. eAccelerator using Drupal
    • Drupal core caching and contributed content caching modules
    • Installing eAccelerator 0.9.5.1 on Ubuntu Feisty 7.04
    • logwatcher: restart Apache after a segmentation fault
    • MySQL InnoDB: performance gains as well as some pitfalls
    • MySQL my.cnf configuration for a large Drupal site
    • Presentation: Performance tuning and optimization of high traffic Drupal sites
    • Tools for Performance Tuning and Optimization
    • Tuning the Apache MaxClients parameter
    • Links and resources on Drupal performance tuning and optimization


    Disclaimer: this is stuff that I have written.
  • ...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 [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)

    by ceeam (39911) on Monday May 28 2007, @03:21AM (#19298239)
    BTW - I noticed that PHP5.2.1 on my home machine (Sempron64, Kubuntu AMD64, 2.0Ghz) to be consistently at least 1.5-2 times faster on (micro)benchmarks than the same PHP version "hand-built" with usual optimizations and the same AFAICT ZTS and other settings on Core2 Dual Xeon machine on RHEL4 i386 (1.8GHz). How comes? Is AMD64 ABI really that great for PHP or what? Python's benchmarks, OTOH, generally don't show any improvement between i386 and AMD64.
    • Unless these recommendations can implemented in shared hosting environments they are of no real use to someone like me - ie web application designer

      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.
      • "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.

      • by Qbertino (265505) on Sunday May 27 2007, @04:21PM (#19294713)
        Some of us make a living doing this and the tools we use on the server have one job.

        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:3, Informative)

      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.