Shared vs dedicated php library - php

I have a server which consists of several Zend Framework application.
I want to know if it is a good idea to upload Zend Library on the server and share it among all the applications instead of uploading it per application.
Does it influence speed if for example multiple applications request a library simultaneously or not.
What is its Pros and Cons?
Thx in advance.

My answer applies in general to shared libraries, as this should not be specific to Zend Library:
PROS of sharing:
Less disk space usage
Possibly less memory usage (depends on many factors, including OS)
Update once, update all (you do not have to update the library for every single app
CONS of sharing:
If you need a particular version of the library for a certain application (for compatibility reasons for example), you cannot do it by sharing the library
Risk of breaking apps by updating to an incompatible library version.

As others have pointed out, there are pros and cons. The big con is that every time you're going to upgrade your library code, you need to test every application, not just the one that needs the upgrade right now.
The big pro is that if you're using an opcode cache like APC (and you should be), you're wasting a fair bit of memory loading identical chunks of library code. Depending on the size of your opcode cache, and how much of the library code actually ever gets run, this could become an issue at some point. If the size of your opcode cache is not big enough to hold everything, you'll end up with a performance hit of some magnitude.
A middle-ground solution is to keep all your libraries some place on the server where they can be shared. Build your apps to use some configuration value for loading.
APP1: config.php
<?PHP
define('ZEND_LIB_PATH','/path/to/ZendFramework-1.9/library');
set_include_path(ZEND_LIB_PATH . PATH_SEPERATOR . get_include_path());
APP2: config.php
define('ZEND_LIB_PATH','/path/to/ZendFramework-1.10.1/library');
set_include_path(ZEND_LIB_PATH . PATH_SEPERATOR . get_include_path());
That way if two apps happen to be using the same version, they can share opcode caches for that version, but you're not tied to it.
DISCLAIMER: I haven't actually done this, so you probably want to test the theory before putting it into practice.

If the applications are separate, I would give each one its own library to avoid issues when you need to upgrade a library on one of the apps, but don't want to have to test and update them all with that library version.
Then again, if those applications share code, they should definitely share the libraries too, to avoid even worse problems with the shared code running under different library version on each site.

Related

Store Zend OpCache as files in PHP 5.6?

I'm trying to obfuscate and speed up my code using Zend OpCache, but OpCache doesn't seem to have the usual functions to store bytecode in a file like other older caching systems did. As OpCache is the native method of caching files in memory in opcode, I'd like to continue using it, but I need to be able to obscure my code in the case of a system breach. (Just another security precaution)
What options do I have available to me to store my PHP files as compiled code in opcode or bytecode?
EDIT: I think people misunderstood what I was looking for for the most part. Please read my comments on the posts. I'm a professional web developer of over 12 years, so I'm not new to this. I just wanted to change my tactics a bit and see if others knew of a way to pull this off that I didn't think of.
It's definitely not a good idea to start developing your own tools for obfuscating or protecting your PHP code.
For protected your code use SourceGuardian or Nu-Coder. Both tools, however, require installing special PHP extension on server, which is not an option for many webhosting companies. These tools provide possibility to lock your code to certain machine (hardware hash, IP binding, ..), control the number of licences, expiration etc.
If you are serious about protecting your code (not only obfuscation) use virtual server + one of the tools mentioned. In run-time, both extensions hold in-memory opcached decrypted low-level code, so besides protecting the code they provide also performance boost. I tested PHP 5.5.x with loaded both SourceGuardian and opcache and there were no conflicts.

Quickest ways to speed up your YII application?

I've just started using YII and managed to finish my first app. unfortunately, launch day is close and I want this app to be super fast. So far, the only way of speeding it up I've come across, is standard caching. What other ways are there to speed up my app?
First of all, read Performance Tuning in the official guide. Additionally:
Check HTTP caching.
Update your PHP. Each major version gives you a good boost.
Use redis (or at least database) for sessions (default PHP sessions are using files and are blocking).
Consider using nginx instead (or with) apache. It serves content much better.
Consider using CDN.
Tweak your database.
These are all general things that are relatively easy to do. If it's not acceptable afterwards, do not assume. Profile.
1. Following best practices
In this recipe, we will see how to configure Yii for best performances and will see some additional principles of building responsive applications. These principles are both general and Yii-related. Therefore, we will be able to apply some of these even without using Yii.
Getting ready
Install APC (http://www.php.net/manual/en/apc.installation.php)
Generate a fresh Yii application using yiic webapp
2.Speeding up sessions handling
Native session handling in PHP is fine in most cases. There are at least two possible reasons why you will want to change the way sessions are handled:
When using multiple servers, you need to have a common session storage for both servers
Default PHP sessions use files, so the maximum performance possible is limited by disk I/O
3.Using cache dependencies and chains
Yii supports many cache backends, but what really makes Yii cache flexible is the dependency and dependency chaining support. There are situations when you cannot just simply cache data for an hour because the information cached can be changed at any time.
In this recipe, we will see how to cache a whole page and still always get fresh data when it is updated. The page will be dashboard-type and will show five latest articles added and a total calculated for an account. Note that an operation cannot be edited as it was added, but an article can.
4.Profiling an application with Yii
If all of the best practices for deploying a Yii application are applied and you still do not have the performance you want, then most probably, there are some bottlenecks with the application itself. The main principle while dealing with these bottlenecks is that you should never assume anything and always test and profile the code before trying to optimize it.
If most of your app is cacheable you should try a proxy like varnish.
Go for general PHP Mysql Performance turning.
1)Memcache
Memcahced open source distributed memory object caching system it helps you to speeding up the dynamic web applications by reducing database server load.
2)MySQL Performance Tuning
3)Webserver Performance turning for PHP

should I add a php APC to my server

A friend has recommended that I install php APC, claiming it will help php run faster and use less memory
sounds promising but I'm a little nervous about adding it to my VPS server
I have one small app that I've built using codeigniter, and several sites that use the popular slideshowpro photo gallery software
could install this break any of the back end code on my sites?
I'm no high tech server guy, but should I give this a try?
Depends entirely on your situation.
Is your site unresponsive or slow at the moment? Is this definitely due to the PHP scripts and not any other data sources such as a database or remote API?
If you answered yes to the above, then installing one of the many PHP accelerators out there would be a good shout. As for using less memory, that's largely dependent on your apache/lightppd/nginx config and php.ini variables.
Most PHP accelerators work by converting the (to be) interpreted PHP code into opcode. This is then stored in memory (RAM) for fast access. If you haven't already implemented file-based caching in CodeIgniter then the benefits of installing a PHP accelerator would be noticeable. If you haven't, then I suggest you do that first before moving straight over to (wasting?) spending time trying to install APC manually.
If your site is currently performing well and you're not too confident in your *nix skills then I suggest you try implementing CodeIgniter caching first rather than try messing with what is an already working VPS.
My personal preference is PHP eAccelerator.
Should installing a PHP cache engine not improve your site's performance then I suggest you look at what other factors influence your application. As stated above, these could be: database or API to name a few.
Hope this helps.
APC is basically a cache engine that stores your compiled php scripts on a temp location on your server. Meaning that these do not have to be interpreted every time someone calls your sccript. It is a PHP extension can can safely be turned ON or OFF and it does not affect your actual code. So... do not fear!
When a php script is processed, there is a compilation phase, where php converts the source code of the php files into "opcodes". APC simply caches the result of this compilation phase, so it should be safe to turn on.
That said, when making such changes to production code it is always wise to run a regression test to ensure no new issues have been introduced.

what is best way to improve performance of zend framework?

zend framework has many components/services I don't need, it has many includes.
All this I think slow down application.
Do you know how to speed up it? may be remove not used(what is common) components, or combine files to one file?
APC or eAccelerator (APC will be included by default in future releases, so I'd recommend using it, even though raw speed is slightly below of eAccelerator)
Two level cache for configuration, full-page, partial views, queries, model objects:
1st level cache (in memory KV-store, APC store or memcached)
2nd level cache (persistent KV-store in files, SQLite or dedicated KV-store)
RDBMS connection pooling, if avaliable.
Before you start worrying about actively modifying things for more performance, you'll want to check the Performance Guide from the manual. One of the simplest steps you can do is to enable an opcode cache (such as APC) on your server - an Opcode cache alone can give you a 3-4x boost.
Code on disk that isn't being called, doesn't take any time. The only way to see what is slow is to measure it. That said, if you aren't running an opcode-cache such as APC, then you are wasting time.
I agree with Topbit, that you should start with code profiling.
Find what is the problem.
I don't think that the problem is just because of ZF has so many files. It uses autoloading, so only files required at the moment are loaded. You definitely shouldn't split different files contents.
For many perfomance problems, caching is your friend.
you can get a bit of extra speed by optimizing the requirements statements
as stated in the optimizing help topic ... first remove all the requirements
and i also recommend using pear naming and overwriting the autoloader,
function __autoload($class) {
require str_replace('_', '/', $class) . '.php';
}
you can find more details here
Are you being forced to use the Zend Framework? If there is no obligation to use it, then not using it would obviously be the fastest way to speed things up. There are several lightweight PHP frameworks that don't come with all the overhead and bulk of Zend. For instance, Codeigniter, Yii, Symfony, and Kohana are all excellent choices and I know at least that codenigniter and Kohana both support the use of Zend components (for instance:Using Zend with Codeigniter).
Good luck!

How do I implement a HTML cache for a PHP site?

What is the best way of implementing a cache for a PHP site? Obviously, there are some things that shouldn't be cached (for example search queries), but I want to find a good solution that will make sure that I avoid the 'digg effect'.
I know there is WP-Cache for WordPress, but I'm writing a custom solution that isn't built on WP. I'm interested in either writing my own cache (if it's simple enough), or you could point me to a nice, light framework. I don't know much Apache though, so if it was a PHP framework then it would be a better fit.
Thanks.
You can use output buffering to selectively save parts of your output (those you want to cache) and display them to the next user if it hasn't been long enough. This way you're still rendering other parts of the page on-the-fly (e.g., customizable boxes, personal information).
If a proxy cache is out of the question, and you're serving complete HTML files, you'll get the best performance by bypassing PHP altogether. Study how WP Super Cache works.
Uncached pages are copied to a cache folder with similar URL structure as your site. On later requests, mod_rewrite notes the existence of the cached file and serves it instead. other RewriteCond directives are used to make sure commenters/logged in users see live PHP requests, but the majority of visitors will be served by Apache directly.
The best way to go is to use a proxy cache (Squid, Varnish) and serve appropriate Cache-Control/Expires headers, along with ETags : see Mark Nottingham's Caching Tutorial for a full description of how caches work and how you can get the most performance out of a caching proxy.
Also check out memcached, and try to cache your database queries (or better yet, pre-rendered page fragments) in there.
I would recommend Memcached or APC. Both are in-memory caching solutions with dead-simple APIs and lots of libraries.
The trouble with those 2 is you need to install them on your web server or another server if it's Memcached.
APC
Pros:
Simple
Fast
Speeds up PHP execution also
Cons
Doesn't work for distributed systems, each machine stores its cache locally
Memcached
Pros:
Fast(ish)
Can be installed on a separate server for all web servers to use
Highly tested, developed at LiveJournal
Used by all the big guys (Facebook, Yahoo, Mozilla)
Cons:
Slower than APC
Possible network latency
Slightly more configuration
I wouldn't recommend writing your own, there are plenty out there. You could go with a disk-based cache if you can't install software on your webserver, but there are possible race issues to deal with. One request could be writing to the file while another is reading.
You actually could cache search queries, even for a few seconds to a minute. Unless your db is being updated more than a few times a second, some delay would be ok.
The PHP Smarty template engine (http://www.smarty.net) includes a fairly advanced caching system.
You can find details in the caching section of the Smarty manual: http://www.smarty.net/manual/en/caching.php
You seems to be looking for a PHP cache framework.
I recommend you the template system TinyButStrong that comes with a very good CacheSystem plugin.
It's simple, light, customizable (you can cache whatever part of the html file you want), very powerful ^^
Simple caching of pages, or parts of pages - the Pear::CacheLite class. I also use APC and memcache for different things, but the other answers I've seen so far are more for more complete, and complex systems. If you just need to save some effort rebuilding a part of a page - Cache_lite with a file-backed store is entirely sufficient, and very simple to implement.
Project Gazelle (an open source torrent site) provides a step by step guide on setting up Memcached on the site which you can easily use on any other website you might want to set up which will handle a lot of traffic.
Grab down the source and read the documentation.

Categories