I'm setting up a new web server and was wondering if the following is a valid stack:

Varnish in front to cache and split traffic between nginx for static content and Apache with mod_php for dynamic content.

I'd also be running APC as the opcode cache for PHP and memcached as a data cache.

Is this a valid configuration? Do I need to bother running APC if some of my PHP is already being cached by Varnish?


I see some redundancy in your approach. nginx is quite efficient as a web server and loadbalancer and adding a second load balancer on top of it seems pointless.

I personally would prefer having a fronted nginx which serves static content and load balances requests to apache+php

For caching you might want to look into redis I think it's more powerful than memcached. It is definatelly better to have opcode caching in addition to load balancing. You might want to consider xcache as the opcode caching for php.

| improve this answer | |
  • Interesting. You don't see any benefit to using Varnish in front as a cache as well? – Will KIng Aug 17 '11 at 14:15
  • Are you planning to run everything on the same server. – Yavor Shahpasov Aug 17 '11 at 14:27
  • For now but in the future it might be distributed. – Will KIng Aug 17 '11 at 14:46
  • Am sure varnish has merits, never used it but read good things about it. Having fronted cache is also good. My comment was based on the fact they there was some overlap of systems and too many systems involved for a single server. Given enough memory on the server static files will be cached by the kernel anyway so I still consider varnish redundant. As the system grows you can easily add more layers without changing the backend. This is just my opinion though. – Yavor Shahpasov Aug 17 '11 at 14:56
  • It is probably worth noting that nginx also can do content caching if you need it. wiki.nginx.org/NginxHttpProxyModule#proxy_cache_path – Yavor Shahpasov Aug 17 '11 at 18:30

This is a valid setup although i guess it may be oversized for most uses and would need a lot of RAM to act as intended.

There are several places to reduce layers... e.g. the nginx / static webserver can probably be replaced by a clever caching strategy and using just apache and varnish (cache static content longer and use cache invalidation techniques if you change content). But that depends on your special application and needs.

memcached might not add much to your setup dependig on your data and usage.

"Do I need to bother running APC if some of my PHP is already being cached by Varnish?" They do cache completely different things. APC caches the compiled PHP Code and Varnish or any other webcache caches the content (that may be produces by that code). So this is a valid setup. Nothing to bother.

| improve this answer | |

I would simplify you architecture as much as posible:

First, I would get apache out of the way and use nginx with php-fpm for dynamic content. Then, if you are not using edge side includes or some advanced cache invalidation I would use nginx for dynamic content caching instead of varnish. Static content should be given a large expiration time in nginx config and use versioning in file name to avoid serving stale content.

APC (or xcache for that matter) is a keeper, it avoids recompiling php source to opcodes in each execution. Also, if you are running all in one machine you could use APC for data caching and scale to memcached later if you code a good abstraction from start.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.