optimization.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <html lang="en">
  2. <head>
  3. <meta charset="utf-8" />
  4. <title>Chamilo Optimization Guide</title>
  5. <link rel="stylesheet" href="../main/css/base.css" type="text/css" media="screen,projection" />
  6. <link rel="stylesheet" href="default.css" type="text/css" media="screen,projection" />
  7. <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
  8. </head>
  9. <body>
  10. <div class="navbar navbar-fixed-top">
  11. <div class="navbar-inner">
  12. <div class="container-fluid">
  13. <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
  14. <span class="icon-bar"></span>
  15. <span class="icon-bar"></span>
  16. <span class="icon-bar"></span>
  17. </a>
  18. <a class="brand" href="index.html">Chamilo - Documentation</a>
  19. <div class="nav-collapse">
  20. <ul class="nav">
  21. <li><a href="index.html">Home</a></li>
  22. <li><a href="readme.html">About</a></li>
  23. <li><a href="license.html">License</a></li>
  24. <li><a href="credits.html">Credits</a></li>
  25. <li><a href="dependencies.html">Dependencies</a></li>
  26. <li><a href="changelog.html">Changelog</a></li>
  27. </ul>
  28. </div><!--/.nav-collapse -->
  29. </div>
  30. </div>
  31. </div>
  32. <div class="container">
  33. <h1>Chamilo : Optimization Guide</h1>
  34. <a href="index.html">Documentation</a> &gt; Optimization Guide
  35. <p>In seldom cases, you will need to start looking into efficiency issues with Chamilo. This guide is a work in progress intended to help administrators optimize their Chamilo installation.</p>
  36. <h2><b>Contents</b></h2>
  37. <ol>
  38. <li><a href="#1.Using-XCache">Using xCache, APC or Memcache</a></li>
  39. <li><a href="#2.Slow-queries">Slow queries</a></li>
  40. <li><a href="#3.Indexes-caching">Indexes caching</a></li>
  41. <li><a href="#4.Sessions-directories">Sessions directories</a></li>
  42. <li><a href="#5.Users-upload-directories">Users upload directories</a></li>
  43. <li><a href="#6.Zlib-compression">Zlib compressed output</a></li>
  44. <li><a href="#7.High-numbers-memory">Memory considerations for high numbers of users</a></li>
  45. <li><a href="#8.HTTP-caching-media">HTTP caching for media</a></li>
  46. <li><a href="#9.Avoid-non-fixed-values">Avoiding non-fixed values</a></li>
  47. </ol>
  48. <h2><a name="1.Using-XCache"></a>1. Using xCache or APC</h2>
  49. See <a href="http://xcache.lighttpd.net/">xCache's website</a> for summary documentation.<br />
  50. <ul>
  51. <li>On Debian/Ubuntu: sudo apt-get install php5-xcache</li>
  52. </ul>
  53. Set your xcache.ini configuration (/etc/php5/conf.d/xcache.ini) to match your system. For example, you *could* have something like this (intentionally hiding comments here):
  54. <pre>
  55. xcache.shm_scheme = "mmap"
  56. xcache.size = 32M
  57. xcache.count = 2
  58. xcache.slots = 8K
  59. xcache.ttl = 0
  60. xcache.gc_interval = 0
  61. xcache.var_size = 16M
  62. xcache.var_count = 16
  63. xcache.var_slots = 8K
  64. xcache.var_ttl = 60
  65. xcache.var_maxttl = 300
  66. xcache.var_gc_interval = 300
  67. xcache.test = Off
  68. </pre>
  69. xCache will feel useless until you actually start to put some variables in cache. If you're showing the "Who is online" counter, that's one of the best item there is to implement xCache.<br />
  70. For example, you could implement it this way (in main/inc/lib/banner.lib.php):<br />
  71. <pre>
  72. $xc = function_exists('xcache_isset');
  73. $number = 0;
  74. if ((api_get_setting('showonline', 'world') == 'true' AND !$user_id) OR (api_get_setting('showonline', 'users') == 'true' AND $user_id) OR (api_get_setting('showonline', 'course') == 'true' AND $user_id AND $course_id)) {
  75. if ($xc &amp;&amp; xcache_isset('campus_chamilo_org_whoisonline_count_simple')) {
  76. $number = xcache_get('campus_chamilo_org_whoisonline_count_simple');
  77. } else {
  78. $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
  79. xcache_set('campus_chamilo_org_whoisonline_count_simple',$number);
  80. }
  81. }
  82. $number_online_in_course = 0;
  83. if(!empty($_course['id'])) {
  84. if ($xc &amp;&amp; xcache_isset('campus_chamilo_org_whoisonline_count_simple_'.$_course['id'])) {
  85. $number_online_in_course = xcache_get('campus_chamilo_org_whoisonline_count_simple_'.$_course['id']);
  86. } else {
  87. $number_online_in_course = who_is_online_in_this_course_count(api_get_user_id(), api_get_setting('time_limit_whosonline'), $_course['id']);
  88. xcache_set('campus_chamilo_org_whoisonline_count_simple_'.$_course['id'],$number_online_in_course);
  89. }
  90. }
  91. </pre>
  92. Note that, as xCache is a shared caching system, it is very important to prefix your variables with a domain name or some kind of identifier, otherwise it would end up in disaster if you use a shared server for several portals.<br />
  93. If you use php5-memcache, then this piece of code would look like this (you need to adjust depending on your settings):
  94. <pre>
  95. global $_configuration;
  96. $_course = api_get_course_info();
  97. $course_id = api_get_course_id();
  98. $user_id = api_get_user_id();
  99. $html = '';
  100. $xc = method_exists('Memcache','add');
  101. if ($xc) {
  102. // Make sure the server is available
  103. $xm = new Memcache;
  104. $xm->addServer('localhost', 11211);
  105. $xc = $xc && ($xm->getServerStatus('localhost',11211)!=0);
  106. // The following concatenates the name of the database + the id of the
  107. // access url to make it a unique variable prefix for the variables to
  108. // be stored
  109. $xs = $_configuration['main_database'].'_'.$_configuration['access_url'].'_';
  110. }
  111. $number = 0;
  112. if ((api_get_setting('showonline', 'world') == 'true' AND !$user_id) OR (api_get_setting('showonline', 'users') == 'true' AND $user_id) OR (api_get_setting('showonline', 'course') == 'true' AND $user_id AND $course_id)) {
  113. if ($xc && $xm->get($xs.'wio_count_simple')) {
  114. $number = $xm->get($xs.'wio_count_simple');
  115. } else {
  116. $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
  117. $xm->set($xs.'wio_count_simple',$number,0,30);
  118. }
  119. $number_online_in_course = 0;
  120. if(!empty($_course['id'])) {
  121. if ($xc && $xm->get($xs.'wio_count_simple_'.$_course['id'])) {
  122. $number_online_in_course = $xm->get($xs.'wio_count_simple_'.$_course['id']);
  123. } else {
  124. $number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('time_limit_whosonline'), $_course['id']);
  125. $xm->set($xs.'wio_count_simple_'.$_course['id'],$number_online_in_course,0,30);
  126. }
  127. }
  128. </pre>
  129. <br />
  130. An optional additional caching mechanism you may use is the realpath_cache_size and realpath_cache_ttl php.ini parameters. See <a href="http://php.net/manual/en/ini.core.php">the PHP documentation</a> for more details.
  131. <br />
  132. <br />
  133. If you prefer using <a href="http://php.net/manual/en/book.apc.php">APC</a>, you can use the same kind of trick as above, just changing the code a little:
  134. <pre>
  135. $xc = function_exists('apc_exists');
  136. $number = 0;
  137. if ((api_get_setting('showonline', 'world') == 'true' AND !$user_id) OR (api_get_setting('showonline', 'users') == 'true' AND $user_id) OR (api_get_setting('showonline', 'course') == 'true' AND $user_id AND $course_id)) {
  138. if ($xc) {
  139. $apc = apc_cache_info(null,true);
  140. $apc_end = $apc['start_time']+$apc['ttl'];
  141. if (apc_exists('my_campus_whoisonline_count_simple') AND (time() < $apc_end) AND apc_fetch('my_campus_whoisonline_count_simple') > 0 ) {
  142. $number = apc_fetch('my_campus_whoisonline_count_simple');
  143. } else {
  144. $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
  145. apc_clear_cache();
  146. apc_store('my_campus_whoisonline_count_simple',$number,15);
  147. }
  148. } else {
  149. $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
  150. }
  151. $number_online_in_course = 0;
  152. if (!empty($_course['id'])) {
  153. if ($xc) {
  154. $apc = apc_cache_info(null,true);
  155. $apc_end = $apc['start_time']+$apc['ttl'];
  156. if (apc_exists('my_campus_whoisonline_count_simple_'.$_course['id']) AND (time() < $apc_end) AND apc_fetch('my_campus_whoisonline_count_simple_'.$_course['id']) > 0) {
  157. $number_online_in_course = apc_fetch('my_campus_whoisonline_count_simple_'.$_course['id']);
  158. } else {
  159. $number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('time_limit_whosonline'), $_course['id']);
  160. apc_store('my_campus_whoisonline_count_simple_'.$_course['id'],$number_online_in_course,15);
  161. }
  162. } else {
  163. $number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('time_limit_whosonline'), $_course['id']);
  164. }
  165. }
  166. ...
  167. </pre>
  168. <hr />
  169. <h2><a name="2.Slow-queries"></a>2. Slow queries</h2>
  170. Enable slow_queries in /etc/mysqld/my.cnf, restart MySQL then follow using sudo tail -f /var/log/mysql/mysql-slow.log
  171. <br /><br />
  172. In Chamilo 1.9 in particular, due to the merge of all databases into one, you might experience performance issue if you have many learning paths with many items in them.<br />
  173. To solve this performance issue, you can execute the following queries manually in your database:<br />
  174. <pre>
  175. ALTER TABLE lp_item ADD INDEX idx_c_lp_item_cid_lp_id (c_id, lp_id);
  176. ALTER TABLE lp_item_view ADD INDEX idx_c_lp_item_view_cid_lp_view_id_lp_item_id (c_id, lp_view_id, lp_item_id);
  177. </pre>
  178. These will be available in Chamilo 1.10 directly, but we cannot put them into Chamilo 1.9 from now on for organizational reasons.<br />
  179. <h3>InnoDB Engine and table locking vs row locking</h3>
  180. <p>
  181. InnoDB is one of the table engines you can use in MySQL. The main advantage of InnoDB is that you can have table locking per row instead of table locking per table. This means that, if one single insert or update query is very slow and executes on a critical table in Chamilo (user, course, etc), it will lock the whole table and no other query will be able to execute, which might seriously affect the efficiency of your database.</p>
  182. <p>Luckily, you can change the engine for one table "on-the-fly", which allows you to effectively check whether this makes a considerable difference. Our recommendation: only do that when seeing that a "SHOW FULL PROCESSLIST" in your database client shows many "Waiting for lock on table [...]".
  183. </p>
  184. <p>
  185. To change these engines, just launch the following command:
  186. <pre>
  187. ALTER TABLE course ENGINE=INNODB;
  188. ALTER TABLE user ENGINE=INNODB;
  189. ALTER TABLE session ENGINE=INNODB;
  190. ALTER TABLE session_rel_course ENGINE=INNODB;
  191. ALTER TABLE session_rel_course_rel_user ENGINE=INNODB;
  192. </pre>
  193. If used on large tables, this might take a considerable time (can take around 60s for a million rows), so try to execute at night or during lower usage periods.
  194. </p>
  195. <hr />
  196. <h2><a name="3.Indexes-caching"></a>3. Indexes caching</h2>
  197. One good reference: <a href="http://dev.mysql.com/doc/refman/5.1/en/multiple-key-caches.html">MySQL documentation on multiple key caches</a><br />
  198. <hr />
  199. <h2><a name="4.Sessions-directories"></a>4. Sessions directories</h2>
  200. php_admin_value session.save_path 1;/var/www/test.chamilo.org/sessions/
  201. <hr />
  202. <h2><a name="5.Users-upload-directories"></a>5. Users upload directories</h2>
  203. Create 10 directories inside the main/upload/users directory (from 0 to 9) and update your admin settings. This has to be done at install &amp; configuration time, otherwise you might loose user data (or have to write a script for data distribution).
  204. <hr />
  205. <h2><a name="6.Zlib-compression"></a>6. Zlib compressed output</h2>
  206. Although this will not make your server faster, compressing the pages you are sending to the users will definitely make them feel like your website's responses are a lot faster, and thus increase their well-being when using Chamilo.<br /><br />
  207. Zlib output compression has to be set at two levels: PHP configuration for PHP pages and Apache for images and CSS.<br /><br />
  208. To update the PHP configuration (either in php.ini or in your VirtualHost), use the <a href="http://php.net/manual/en/zlib.configuration.php">zlib.output_compression</a>. If you set this inside your Apache's VirtualHost, you should use the following syntax.
  209. <pre>
  210. php_value zlib.output_compression 1
  211. </pre>
  212. <br />
  213. Configuring your Apache server to use output compression is a bit trickier. You have to use <a href="http://httpd.apache.org/docs/2.2/mod/mod_deflate.html">the mod_deflate module</a> to do it. Your configuration should look like something like this (please read the corresponding documentation before implementing in production).<br />
  214. Easy mode:
  215. <pre>
  216. AddOutputFilterByType DEFLATE text/html text/plain text/xml
  217. </pre> or, for every content type (dangerous) you can put the following inside a location or directory block:<pre>SetOutputFilter DEFLATE</pre>
  218. <br />
  219. Advanced mode:
  220. <pre>
  221. <Location />
  222. # Insert filter
  223. SetOutputFilter DEFLATE
  224. # Netscape 4.x has some problems...
  225. BrowserMatch ^Mozilla/4 gzip-only-text/html
  226. # Netscape 4.06-4.08 have some more problems
  227. BrowserMatch ^Mozilla/4\.0[678] no-gzip
  228. # MSIE masquerades as Netscape, but it is fine
  229. # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  230. # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
  231. # the above regex won't work. You can use the following
  232. # workaround to get the desired effect:
  233. BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
  234. # Don't compress images
  235. SetEnvIfNoCase Request_URI \
  236. \.(?:gif|jpe?g|png)$ no-gzip dont-vary
  237. # Make sure proxies don't deliver the wrong content
  238. Header append Vary User-Agent env=!dont-vary
  239. </Location>
  240. </pre>
  241. <hr />
  242. Don't have time or resources to optimize your Chamilo installation yourself? Hire an <a href="http://www.chamilo.org/en/providers">official Chamilo provider</a> and get it sorted out professionally by specialists.
  243. <a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" style="margin: 1em; float: right;" height="31" width="88" /></a>
  244. <a href="http://jigsaw.w3.org/css-validator/">
  245. <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" style="margin: 1em; float: right;" alt="Valid CSS" />
  246. </a>
  247. <hr />
  248. <h2><a name="7.High-numbers-memory"></a>Memory considerations for high numbers of users</h2>
  249. Some administration scripts *have to* handle lists of all users, and this might have a considerable impact on portals with very high numbers of users. For example, the main/admin/add_users_to_session.php script that handles the registration of users into a specific session, if used with the (non-default) full list of users, will devour about 3KB per user, which, for 100,000 users, translates into the need for around 300MB of RAM just to show this page, and to around 3GB for 1,000,000 users.<br />
  250. This mode is not loaded by default, but could still be selected, leading to a "Fatal error: Allowed memory size ... exhausted" message.<br />
  251. The only non-scripted solution here is to allow for the corresponding amount of RAM for your PHP configuration (<em>memory_limit = 300M</em>) or your specific VirtualHost if you use mod-php5 (<em>php_value memory_limit 300M</em>).<br/>
  252. <hr />
  253. <h2><a name="8.HTTP-caching-media">HTTP caching for media</a></h2>
  254. <p>Images are (always) part of the heaviest resources around for a website, together with CSS an JavaScript libraries.
  255. Obviously, audio files and videos can be much heavier still, but are much less frequent (for now).</p>
  256. <p>Any web browser will allow you to play a little with HTTP headers.
  257. Describing what HTTP really is, is not meant to be in this quick guide, but let's just say that, with the right configuration in your web server, you can "tell" the user's browser to re-use content he already downloaded instead of asking it again from the web server.</p>
  258. <p>It's easy to underestimate the potential gains of doing this. i
  259. On a typical web page, your browser will have between 20 (highly optimized) and 200 (not optimized at all) different "items" to download.
  260. In Chamilo LMS 1.9, this number is around 40 per page. The first one is the .php file, which tells the browser which other resources to load, then the browser sends requests for these.
  261. This includes icons (around 20, depending on the page), JS libraries (around 6), CSS files (around 12) and any other multimedia resource you might have.</p>
  262. <p>Something that is rarely understood by most webmasters or sysadmins is that each one of these resources, unless otherwise configured, will imply a separate request to the server.
  263. Even if the server has a mechanism to indicate that a file hasn't changed since the last time, and as such shouldn't be downloaded again, the request is still sent by the browser, received by the server, checked, answered and received again by the browser.
  264. While you're waiting for your page to load, your browser is likely to have sent 40 requests and received 40 answers potentially thousands of miles away. You could reduce this to 5, if planned properly.</p>
  265. <p>Even if this last sentence didn't impress you, you'll have to understand that each of these requests comes to your server, uses a server client (or thread, in the best case scenario) which requires memory, reads on disk (unless you've got some nice reverse-proxy mechanism), replies, writes the log for that request and finally goes back to sleep (or to answer the next request).
  266. This might all seem invisible for a few requests, but when you start having hundreds of users connecting to your Chamilo site every few seconds, you'll start noticing.</p>
  267. <h4>So, how do we make this better?</h4>
  268. <p>There is one mechanism that will improve this situation. It's called setting "Expiry" headers, and it is based on the idea that, if we tll the browser how long to keep a specific image in cache, the browser will save it locally, identify it by its URL (so the same image offered on 2 different URLs will be considered as 2 different images) and, when a new page is loaded, if the same image is requested and the expiry date and time have not been reached, it will just load it from cache.<br />
  269. No call to the server, no loading the image, no web server processing, no I/O on the server's disk.<br />
  270. This is probably the single best optimization you can setup on your site.</p>
  271. <p>This measure has to be taken with caution though... some images might be dynamic, or you might want to ensure that the image can only be loaded when the user is connected...<br />
  272. So only a few images (but that's enough by us) will be configurable this way.<br />
  273. And that's alright, because we know exactly which images can be configured this way:<br />
  274. <ul>
  275. <li>All the interface icons</li>
  276. <li>The CSS files (unless you change them often</li>
  277. <li>The homepage images (just make sure you change the name of the files when updating them)</li>
  278. <li>Even the JS files can be cached</li>
  279. </ul>
  280. </p>
  281. <h3>Setting up expiry headers in Apache</h3>
  282. <p>
  283. The following example is made to be inserted in an Apache's &lt;VirtualHost&gt; block, and should be adapted to fit in other web servers configurations. Some of the settings here might require the activation of the ModExpire and the ModHeader modules:
  284. <pre>
  285. ExpiresActive On
  286. &lt;Directory "/var/www/chamilo/main/img"&gt;
  287. AllowOverride All
  288. Order allow,deny
  289. Allow from all
  290. ExpiresByType image/gif "access plus 1 month"
  291. ExpiresByType image/jpg "access plus 1 month"
  292. ExpiresByType image/png "access plus 1 month"
  293. ExpiresByType application/x-shockwave-flash "access plus 1 month"
  294. &lt;/Directory&gt;
  295. &lt;Directory "/var/www/chamilo/main/inc/lib"&gt;
  296. AllowOverride All
  297. Order allow,deny
  298. Allow from all
  299. ExpiresByType image/gif "access plus 1 month"
  300. ExpiresByType image/jpg "access plus 1 month"
  301. ExpiresByType image/png "access plus 1 month"
  302. ExpiresByType text/css "access plus 1 month"
  303. ExpiresByType application/javascript "access plus 1 month"
  304. &lt;/Directory&gt;
  305. &lt;Directory "/var/www/chamilo/main/css"&gt;
  306. AllowOverride All
  307. Order allow,deny
  308. Allow from all
  309. ExpiresByType image/gif "access plus 1 month"
  310. ExpiresByType image/jpg "access plus 1 month"
  311. ExpiresByType image/png "access plus 1 month"
  312. ExpiresByType text/css "access plus 1 month"
  313. &lt;/Directory&gt;
  314. &lt;Directory "/var/www/chamilo/home"&gt;
  315. AllowOverride All
  316. Order allow,deny
  317. Allow from all
  318. ExpiresByType image/gif "access plus 3 hours"
  319. ExpiresByType image/jpg "access plus 3 hours"
  320. ExpiresByType image/png "access plus 3 hours"
  321. &lt;/Directory&gt;
  322. </pre>
  323. These settings explicitly mention the file MIME types that will be put in cache, and for how long. For heavily-optimized sites, where all editors are aware of the settings, understand and apply them, it is common to find cache expiry periods of one full year for images.<br />
  324. If you want to change some content put in cache by the client, though, you will need to change the name of the file (and its reference in the HTML/PHP file that includes it).<br />
  325. This is an easy way to massively reduce your bandwidth (80% reduction?), the speed of page loading for your users and the use of resources on your web server.
  326. </p>
  327. <h2><a name="#9.Avoid-non-fixed-values"></a>Avoiding non-fixed values</h2>
  328. Many things in Chamilo are written focusing on the ease of use, even for the administrator. Sometimes, these settings are weighing a little bit more on the system. This is the case, between others, of the mail.conf.php file (being loaded unconditionally) and its CONSTANT "IS_WINDOWS_OS", which is defined by a function call (api_is_windows_os()) at the beginning of main_api.lib.php.
  329. The definition of this constant (which is executed at *every* page load) can easily be avoided, and the only place where it is used inconditionally (mail.conf.php) can be modified to set the line as you expect it (depending on whether you use sendmail/exim or smtp).
  330. <pre>
  331. $platform_email['SMTP_MAILER'] = 'smtp';
  332. </pre>
  333. or
  334. <pre>
  335. $platform_email['SMTP_MAILER'] = 'mail';
  336. </pre>
  337. In fact, the complete loading of mail.conf.php can also be avoided if loaded conditionally (with <i>require_once</i>) when sending an e-mail (which is the only case where it is useful).
  338. <hr />
  339. <h2>Authors</h2>
  340. <ul>
  341. <li>Yannick Warnier, Zend Certified PHP Engineer, BeezNest Belgium SPRL, <a href="mailto:ywarnier@beeznest.net">ywarnier@beeznest.net</a></li>
  342. </ul>
  343. </div>
  344. </body>
  345. </html>