optimization.html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <html lang="en">
  2. <head>
  3. <meta charset="utf-8" />
  4. <title>Chamilo Optimization Guide</title>
  5. <link rel="stylesheet" href="../web/assets/bootstrap/dist/css/bootstrap.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. <nav class="navbar navbar-default navbar-fixed-top">
  11. <div class="container">
  12. <div class="navbar-header">
  13. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
  14. <span class="sr-only">Toggle navigation</span>
  15. <span class="icon-bar"></span>
  16. <span class="icon-bar"></span>
  17. <span class="icon-bar"></span>
  18. </button>
  19. <a class="navbar-brand" href="#">Chamilo - Documentation</a>
  20. </div>
  21. <div class="collapse navbar-collapse">
  22. <ul class="nav navbar-nav">
  23. <li><a href="index.html">Home</a></li>
  24. <li class="active"><a href="readme.html">About</a></li>
  25. <li><a href="license.html">License</a></li>
  26. <li><a href="credits.html">Credits</a></li>
  27. <li ><a href="dependencies.html">Dependencies</a></li>
  28. <li><a href="changelog.html">Changelog</a></li>
  29. </ul>
  30. </div><!--/.nav-collapse -->
  31. </div>
  32. </nav>
  33. <div class="container">
  34. <h1>Chamilo : Optimization Guide</h1>
  35. <a href="index.html">Documentation</a> &gt; Optimization Guide
  36. <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>
  37. <h2><b>Contents</b></h2>
  38. <ol>
  39. <li><a href="#1.Using-XCache">Using xCache, APC, Memcache or Memcached</a></li>
  40. <li><a href="#2.Slow-queries">Slow queries</a></li>
  41. <li><a href="#3.Indexes-caching">Indexes caching</a></li>
  42. <li><a href="#4.Sessions-directories">Sessions directories</a></li>
  43. <li><a href="#5.Users-upload-directories">Users upload directories</a></li>
  44. <li><a href="#6.Zlib-compression">Zlib compressed output</a></li>
  45. <li><a href="#7.High-numbers-memory">Memory considerations for high numbers of users</a></li>
  46. <li><a href="#8.Avoid-non-fixed-values">Avoiding non-fixed values</a></li>
  47. <li><a href="#9.xsendfile">Speeding file downloads with mod_xsendfile</a></li>
  48. <li><a href="#10.igbinary">IGBinary for faster courses backups and better sessions</a></li>
  49. <li><a href="#11.permissions-check">Removing files download permissions check</a></li>
  50. <li><a href="#12.MySQL-compression">MySQL/MariaDB compression</a></li>
  51. </ol>
  52. <h2><a name="1.Using-XCache"></a>1. Using xCache or APC</h2>
  53. See <a href="http://xcache.lighttpd.net/">xCache's website</a> for summary documentation.<br />
  54. <ul>
  55. <li>On Debian/Ubuntu: sudo apt-get install php5-xcache</li>
  56. </ul>
  57. 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):
  58. <pre>
  59. xcache.shm_scheme = "mmap"
  60. xcache.size = 32M
  61. xcache.count = 2
  62. xcache.slots = 8K
  63. xcache.ttl = 0
  64. xcache.gc_interval = 0
  65. xcache.var_size = 16M
  66. xcache.var_count = 16
  67. xcache.var_slots = 8K
  68. xcache.var_ttl = 60
  69. xcache.var_maxttl = 300
  70. xcache.var_gc_interval = 300
  71. xcache.test = Off
  72. </pre>
  73. 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 />
  74. For example, you could implement it this way (in main/inc/lib/banner.lib.php):<br />
  75. <pre>
  76. $xc = function_exists('xcache_isset');
  77. $number = 0;
  78. 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)) {
  79. if ($xc &amp;&amp; xcache_isset('campus_chamilo_org_whoisonline_count_simple')) {
  80. $number = xcache_get('campus_chamilo_org_whoisonline_count_simple');
  81. } else {
  82. $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
  83. xcache_set('campus_chamilo_org_whoisonline_count_simple',$number);
  84. }
  85. }
  86. $number_online_in_course = 0;
  87. if(!empty($_course['id'])) {
  88. if ($xc &amp;&amp; xcache_isset('campus_chamilo_org_whoisonline_count_simple_'.$_course['id'])) {
  89. $number_online_in_course = xcache_get('campus_chamilo_org_whoisonline_count_simple_'.$_course['id']);
  90. } else {
  91. $number_online_in_course = who_is_online_in_this_course_count(api_get_user_id(), api_get_setting('time_limit_whosonline'), $_course['id']);
  92. xcache_set('campus_chamilo_org_whoisonline_count_simple_'.$_course['id'],$number_online_in_course);
  93. }
  94. }
  95. </pre>
  96. 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 />
  97. If you use php5-memcache, then this piece of code would look like this (you need to adjust depending on your settings):
  98. <pre>
  99. global $_configuration;
  100. $_course = api_get_course_info();
  101. $course_id = api_get_course_id();
  102. $user_id = api_get_user_id();
  103. $html = '';
  104. $xc = method_exists('Memcache','add');
  105. if ($xc) {
  106. // Make sure the server is available
  107. $xm = new Memcache;
  108. $xm->addServer('localhost', 11211);
  109. $xc = $xc && ($xm->getServerStatus('localhost',11211)!=0);
  110. // The following concatenates the name of the database + the id of the
  111. // access url to make it a unique variable prefix for the variables to
  112. // be stored
  113. $xs = $_configuration['main_database'].'_'.$_configuration['access_url'].'_';
  114. }
  115. $number = 0;
  116. 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)) {
  117. if ($xc && $xm->get($xs.'wio_count_simple')) {
  118. $number = $xm->get($xs.'wio_count_simple');
  119. } else {
  120. $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
  121. $xm->set($xs.'wio_count_simple',$number,0,30);
  122. }
  123. $number_online_in_course = 0;
  124. if(!empty($_course['id'])) {
  125. if ($xc && $xm->get($xs.'wio_count_simple_'.$_course['id'])) {
  126. $number_online_in_course = $xm->get($xs.'wio_count_simple_'.$_course['id']);
  127. } else {
  128. $number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('time_limit_whosonline'), $_course['id']);
  129. $xm->set($xs.'wio_count_simple_'.$_course['id'],$number_online_in_course,0,30);
  130. }
  131. }
  132. </pre>
  133. <br />
  134. 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.
  135. <br />
  136. <br />
  137. 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:
  138. <pre>
  139. $xc = function_exists('apc_exists');
  140. $number = 0;
  141. 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)) {
  142. if ($xc) {
  143. $apc = apc_cache_info(null,true);
  144. $apc_end = $apc['start_time']+$apc['ttl'];
  145. if (apc_exists('my_campus_whoisonline_count_simple') AND (time() < $apc_end) AND apc_fetch('my_campus_whoisonline_count_simple') > 0 ) {
  146. $number = apc_fetch('my_campus_whoisonline_count_simple');
  147. } else {
  148. $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
  149. apc_clear_cache();
  150. apc_store('my_campus_whoisonline_count_simple',$number,15);
  151. }
  152. } else {
  153. $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
  154. }
  155. $number_online_in_course = 0;
  156. if (!empty($_course['id'])) {
  157. if ($xc) {
  158. $apc = apc_cache_info(null,true);
  159. $apc_end = $apc['start_time']+$apc['ttl'];
  160. 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) {
  161. $number_online_in_course = apc_fetch('my_campus_whoisonline_count_simple_'.$_course['id']);
  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. apc_store('my_campus_whoisonline_count_simple_'.$_course['id'],$number_online_in_course,15);
  165. }
  166. } else {
  167. $number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('time_limit_whosonline'), $_course['id']);
  168. }
  169. }
  170. ...
  171. </pre>
  172. <br />
  173. If you use php5-memcached (different set of functions than php5-memcache!), then this piece of code would look like this (you need to adjust depending on your settings):
  174. <pre>
  175. global $_configuration;
  176. $_course = api_get_course_info();
  177. $course_id = api_get_course_id();
  178. $user_id = api_get_user_id();
  179. $html = '';
  180. $xc = method_exists('Memcached', 'add');
  181. if ($xc) {
  182. // Make sure the server is available
  183. $xm = new Memcached;
  184. $xm->addServer('localhost', 11211);
  185. // The following concatenates the name of the database + the id of the
  186. // access url to make it a unique variable prefix for the variables to
  187. // be stored
  188. $xs = $_configuration['main_database'].'_'.$_configuration['access_url'].'_';
  189. }
  190. $number = 0;
  191. 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)) {
  192. if ($xc) {
  193. if ($xm->get($xs.'wio_count_simple')) {
  194. $number = $xm->get($xs.'wio_count_simple');
  195. } else {
  196. $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
  197. $xm->set($xs.'wio_count_simple',$number,120);
  198. }
  199. } else {
  200. $number = who_is_online_count(api_get_setting('time_limit_whosonline'));
  201. }
  202. $number_online_in_course = 0;
  203. if (!empty($_course['id'])) {
  204. if ($xc) {
  205. if ($xm->get($xs.'wio_count_simple_'.$_course['id'])) {
  206. $number_online_in_course = $xm->get($xs.'wio_count_simple_'.$_course['id']);
  207. } else {
  208. $number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('time_limit_whosonline'), $_course['id']);
  209. $xm->set($xs.'wio_count_simple_'.$_course['id'],$number_online_in_course,120);
  210. }
  211. } else {
  212. $number_online_in_course = who_is_online_in_this_course_count(api_get_user_id(), api_get_setting('time_limit_whosonline'), $_course['id']);
  213. }
  214. }
  215. // ...
  216. </pre>
  217. <p>It is also worth noting that the Université de Genève, Switzerland, observed that the calculation of the total size used by course documents is one of the heaviest queries in Chamilo, so you might want to cache the results of this one as well, using the same technique.</p>
  218. <p>Finally, if your portal is highly public *and* you are showing the popular courses on the homepage, you might want to also reduce the amount of queries this generates, using the same technique as above, but for the main/inc/lib/auth.lib.php library, looking for the "Tracking::get_course_connections_count()" call:</p>
  219. <pre>
  220. while ($row = Database::fetch_array($result)) {
  221. $row['registration_code'] = !empty($row['registration_code']);
  222. $count_users = CourseManager::get_users_count_in_course($row['code']);
  223. $xc = function_exists('apc_exists');
  224. if ($xc) {
  225. $apc = apc_cache_info(null, true);
  226. $apx_end = $apc['start_time']+$apx['ttl'];
  227. if (apc_exists('my_campus_course_visits_'.$row['code']) AND (time() < $apc_end) AND apc_fetch('my_campus_course_visits_'.$row['code']) > 0) {
  228. $count_connections_last_month = apc_fetch('my_campus_course_visits_'.$row['code']);
  229. } else {
  230. $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time() - (30 * 86400)));
  231. apc_store('my_campus_course_visits_'.$row['code'], $count_connections_last_month, $apc['ttl']);
  232. }
  233. } else {
  234. $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time() - (30 * 86400)));
  235. }
  236. ...
  237. }
  238. </pre>
  239. Finally, the Free Campus of Chamilo has a very specific case of slow query: the courses catalog! Because there might be more than 30,000 courses in there, getting the number of "Connections last month" can be a desastrous query in terms of performances. This is why you should try to cache the results as well.<br />
  240. Obviously, as we are speaking about showing the number of visits this month, it doesn't really matter if the number doesn't refresh for an hour or so...<br />
  241. Locate the main/inc/lib/course_category.lib.php file, open it and go to the browseCoursesInCategory() function.<br />
  242. Locate the $count_connections_last_month = Tracking::get_course_connections_count(...) call, and wrap in into something like this:
  243. <pre>
  244. $xc = method_exists('Memcached', 'add');
  245. if ($xc) {
  246. // Make sure the server is available
  247. $xm = new Memcached;
  248. $xm->addServer('localhost', 11211);
  249. // The following concatenates the name of the database + the id of the
  250. // access url to make it a unique variable prefix for the variables to
  251. // be stored
  252. $xs = $_configuration['main_database'].'_'.$_configuration['access_url'].'_';
  253. }
  254. $result = Database::query($sql);
  255. $courses = array();
  256. while ($row = Database::fetch_array($result)) {
  257. $row['registration_code'] = !empty($row['registration_code']);
  258. $count_users = CourseManager::get_users_count_in_course($row['code']);
  259. if ($xc) {
  260. if ($xm->get($xs.'cccount_'.$row['code'])) {
  261. $number = $xm->get($xs.'cccount_'.$row['code']);
  262. } else {
  263. $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time() - (30 * 86400)));
  264. $xm->set($xs.'cccount_'.$row['code'], $count_connections_last_month, 3600);
  265. }
  266. } else {
  267. $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time() - (30 * 86400)));
  268. }
  269. ...
  270. </pre>
  271. <hr />
  272. <h2><a name="2.Slow-queries"></a>2. Slow queries</h2>
  273. Enable slow_queries in /etc/mysqld/my.cnf, restart MySQL then follow using sudo tail -f /var/log/mysql/mysql-slow.log
  274. <br /><br />
  275. 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 />
  276. To solve this performance issue, you can execute the following queries manually in your database:<br />
  277. <pre>
  278. ALTER TABLE lp_item ADD INDEX idx_c_lp_item_cid_lp_id (c_id, lp_id);
  279. 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);
  280. ALTER TABLE user_rel_tag ADD INDEX idx_user_rel_tag_user (user_id);
  281. </pre>
  282. In Chamilo 1.9.8, we use the c_item_property table more actively. This causes issues with the reporting pages for the assignments. You can reduce the impact by adding the following index:
  283. <pre>
  284. alter table c_item_property add index idx_itemprop_tooliuid(tool, insert_user_id);
  285. </pre>
  286. 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 />
  287. <hr />
  288. <h2><a name="3.Indexes-caching"></a>3. Indexes caching</h2>
  289. 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 />
  290. <hr />
  291. <h2><a name="4.Sessions-directories"></a>4. Sessions directories</h2>
  292. php_admin_value session.save_path 1;/var/www/test.chamilo.org/sessions/
  293. <hr />
  294. <h2><a name="5.Users-upload-directories"></a>5. Users upload directories</h2>
  295. Create 10 directories inside the app/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).
  296. <hr />
  297. <h2><a name="6.Zlib-compression"></a>6. Zlib compressed output</h2>
  298. 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 />
  299. Zlib output compression has to be set at two levels: PHP configuration for PHP pages and Apache for images and CSS.<br /><br />
  300. 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.
  301. <pre>
  302. php_value zlib.output_compression 1
  303. </pre>
  304. <br />
  305. 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 />
  306. Easy mode:
  307. <pre>
  308. AddOutputFilterByType DEFLATE text/html text/plain text/xml
  309. </pre> or, for every content type (dangerous) you can put the following inside a location or directory block:<pre>SetOutputFilter DEFLATE</pre>
  310. <br />
  311. Advanced mode:
  312. <pre>
  313. <Location />
  314. # Insert filter
  315. SetOutputFilter DEFLATE
  316. # Netscape 4.x has some problems...
  317. BrowserMatch ^Mozilla/4 gzip-only-text/html
  318. # Netscape 4.06-4.08 have some more problems
  319. BrowserMatch ^Mozilla/4\.0[678] no-gzip
  320. # MSIE masquerades as Netscape, but it is fine
  321. # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  322. # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
  323. # the above regex won't work. You can use the following
  324. # workaround to get the desired effect:
  325. BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
  326. # Don't compress images
  327. SetEnvIfNoCase Request_URI \
  328. \.(?:gif|jpe?g|png)$ no-gzip dont-vary
  329. # Make sure proxies don't deliver the wrong content
  330. Header append Vary User-Agent env=!dont-vary
  331. </Location>
  332. </pre>
  333. <hr />
  334. 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.
  335. <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>
  336. <a href="http://jigsaw.w3.org/css-validator/">
  337. <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" style="margin: 1em; float: right;" alt="Valid CSS" />
  338. </a>
  339. <hr />
  340. <h2><a name="7.High-numbers-memory"></a>Memory considerations for high numbers of users</h2>
  341. 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 />
  342. This mode is not loaded by default, but could still be selected, leading to a "Fatal error: Allowed memory size ... exhausted" message.<br />
  343. 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/>
  344. <hr />
  345. <h2><a name="8.Avoid-non-fixed-values"></a>Avoiding non-fixed values</h2>
  346. 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.
  347. 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).
  348. <pre>
  349. $platform_email['SMTP_MAILER'] = 'smtp';
  350. </pre>
  351. or
  352. <pre>
  353. $platform_email['SMTP_MAILER'] = 'mail';
  354. </pre>
  355. 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).
  356. <p>
  357. As an additional node, on very active portals with a lot of courses for each users, the icons that appear next to the courses illustrating changes in the corresponding course might be heavyweighted. You can alter slightly the behaviour by not querying for notifications you don't care about, like dropbox, notebook or chat. Change this in main/inc/lib/display.lib.php, in function show_notification().
  358. </p>
  359. <hr />
  360. <h2><a name="9.xsendfile"></a>Speeding file downloads with mod_xsendfile</h2>
  361. <p>It might have come to your attention that file downloads through Chamilo might get slow, under default conditions, in particular using Apache 2.</p>
  362. <p>There are several ways to fix this, one of which is removing the .htaccess inside the courses/ directory. This, however, will remove all permissions checks on the files contained in this directory, so... most of the time, not ideal unless your portal is *really* open to the world.</p>
  363. <p>Another technique, revealed to us by <a href="http://stackoverflow.com/users/46594/virtualblackfox">VirtualBlackFox</a> on <a href="http://stackoverflow.com/questions/3697748/fastest-way-to-serve-a-file-using-php">this Stackoverflow post</a>, is to use the X-SendFile module for Apache 2.2+ (other web servers might offer other solutions, or avoid the problem initially).</p>
  364. <p>Installing the X-SendFile module will depend on your operating system, but if you use Ubuntu, you'll have to check you are including the "universe" repository inside your packages sources (check /etc/apt/sources.list), then:
  365. <pre>
  366. sudo apt-get update
  367. sudo apt-get install libapache2-mod-xsendfile
  368. sudo service apache2 restart
  369. </pre>
  370. Once you're done with installing, you'll have to configure Chamilo to use it.<br />
  371. First, edit your VirtualHost or your Apache configuration in general (in Ubuntu, check the /etc/apache2/ or /etc/apache2/sites-available/ folder). This is done by adding the following line inside your configuration, and reloading Apache (example provided on the basis of a virtual host located in /etc/apache2/sites-available/my.chamilo.net.conf) :
  372. <pre>
  373. sudo vim /etc/apache2/sites-available/my.chamilo.net.conf
  374. # add the following line:
  375. X-SendFile on
  376. # exit the file
  377. sudo service apache2 reload
  378. </pre>
  379. Finally, you'll have to got to your Chamilo configuration file, and add the following line at the very bottom of the file main/inc/conf/configuration.php:
  380. <pre>
  381. $_configuration['enable_x_sendfile_headers'] = true;
  382. </pre>
  383. Done! Now your downloads should go substantially faster. This is still a feature in observation. We're not sure the benefits are sufficient, so don't hesitate to let us know in <a href="https://support.chamilo.org/issues/6853">the related issue in Chamilo's tracking system</a>
  384. </p>
  385. <hr />
  386. <h2><a name="10.igbinary"></a>IGBinary for courses backups and better sessions management</h2>
  387. <p>
  388. <a href="http://pecl.php.net/package/igbinary">IGBinary</a> is a small PECL library that replaces the PHP serializer. It uses less space (so less memory for serialized objects) and is particularly efficient with memory-based storages (like Memcached). Use it for course backups (see <a href="https://support.chamilo.org/issues/4443">issue 4443</a>) or <a href="http://www.neanderthal-technology.com/2011/11/ubuntu-10-install-php-memcached-with-igbinary-support/">to boost sessions management</a>.
  389. </p>
  390. <hr />
  391. <h2><a name="11.permissions-check"></a>Removing files download permissions check</h2>
  392. <p>
  393. This measure is not cumulative with mod_xsendfile explained above. It is not *recommended* either, as it removes an important security layer.<br />
  394. <br />
  395. In Chamilo, for security and tracking purposes, all downloaded files pass through PHP scripts that check whether the user has access to the file given his/her current permissions. This process requires important database accesses and processing, which might terminally affect your server's performance. In particular, this can have a huge effect if having hundreds of simultaneous users accessing learning paths pages composed of local resources.<br /><br />
  396. The logic behind this verification is that, whatever resources that needs to be downloaded/viewed that come from the /courses/ directory, the /courses/.htaccess file with get in the middle and redirect these accesses to a PHP script (usually called download.php but there are more than one depending on the type of resource).<br /><br />
  397. If you want to speed up files accesses and you don't really care about whom can see your files, then an option is to simply remove this redirection to download.php and let Apache treat the file on its own.<br /><br />
  398. Furthermore, using a PHP script for the download (unless you have special rules) will usually prevent static content caching, which will multiply downloads and use large amount of additional bandwidth.<br /><br />
  399. Typically, the .htaccess will look like this (with additional comments):<br />
  400. <pre>
  401. <IfModule mod_rewrite.c>
  402. RewriteEngine On
  403. RewriteBase /courses/
  404. RewriteCond %{REQUEST_URI} !^/main/
  405. RewriteRule ([^/]+)/document/(.*)&(.*)$ $1/document/$2///$3 [N]
  406. RewriteRule ([^/]+)/scorm/(.*)$ /main/document/download_scorm.php?doc_url=/$2&cDir=$1 [QSA,L]
  407. RewriteRule ([^/]+)/document/(.*)$ /main/document/download.php?doc_url=/$2&cDir=$1 [QSA,L]
  408. RewriteRule ([^/]+)/work/(.*)$ /main/work/download.php?file=work/$2&cDir=$1 [QSA,L]
  409. </IfModule>
  410. </pre><br />
  411. The idea is to remove the "scorm" line, so you are left with:
  412. <pre>
  413. <IfModule mod_rewrite.c>
  414. RewriteEngine On
  415. RewriteBase /courses/
  416. RewriteCond %{REQUEST_URI} !^/main/
  417. RewriteRule ([^/]+)/document/(.*)&(.*)$ $1/document/$2///$3 [N]
  418. RewriteRule ([^/]+)/document/(.*)$ /main/document/download.php?doc_url=/$2&cDir=$1 [QSA,L]
  419. RewriteRule ([^/]+)/work/(.*)$ /main/work/download.php?file=work/$2&cDir=$1 [QSA,L]
  420. </IfModule>
  421. </pre><br />
  422. This is easy, doesn't require a server reload and you should see the results pretty quickly. As mentioned above, if security of your content is an issue, though, you should avoid using this technique.
  423. </p>
  424. <p>
  425. You can also mitigate the risk by disabling permissions check only
  426. for some static resource like css,js and fonts files.
  427. <br/>
  428. For that is required to load header module
  429. in apache (check with a2enmod in your favorite root terminal)<br />
  430. add theses line after RewriteBase /courses/:
  431. <pre>
  432. &lt;IfModule mod_headers.c&gt;
  433. # all file name ended with these extensions names will bypass the permission check (and also served by the browser cache at the next request)
  434. &lt;FilesMatch &quot;\.(gif|jpg|jpeg|png|js|pdf|ico|icon|css|swf|avi|mp3|ogg|wav|ttf|otf|eot|woff)$&quot;&gt;
  435. Header unset Cache-Control
  436. Header set Cache-Control &quot;public, max-age=29030400&quot;
  437. RequestHeader unset Cookie
  438. Header unset ETag
  439. &lt;/FilesMatch&gt;
  440. &lt;/IfModule&gt;
  441. # also adjust files here
  442. RewriteRule (\.(html|gif|jpg|jpeg|png|js|pdf|ico|icon|css|swf|avi|mp3|ogg|wav|ttf|otf|eot|woff))$ - [L]
  443. </pre>
  444. </p>
  445. <hr />
  446. <h2><a name="12.MySQL-compression"></a>MySQL/MariaDB compression</h2>
  447. <p>
  448. If your database server is separate from your web server, you have to play with bandwidth, firewalls, and network restrictions in general.<br />
  449. In particular, when dealing with large-scale portals, the time a SQL query will take to return to the web server will take longer and, eventually, in the most critical cases, will take <b>too long</b>, and your web servers will be completely overloaded (load average very high because the system is waiting for I/O operations, but processors usage not being very high is a clear sign of this).<br />
  450. To solve this kind of issues, MySQL and MariaDB offer a data compression mechanism, which will reduce the amount of data passed between PHP and the database server. Ultimately, this reduction will lower bandwidth usage and reduce the impact of numerous and heavy data requests (and save you).<br />
  451. In 1.10.0, we have added the possibility to enable this compression very easily, from the configuration.php file, uncommenting the following line:
  452. <pre>
  453. //$_configuration['db_client_flags'] = MYSQL_CLIENT_COMPRESS;
  454. </pre>
  455. This should have an immediate effect on the load average on your server.
  456. </p>
  457. <hr />
  458. <h2>Authors</h2>
  459. <ul>
  460. <li>Yannick Warnier, Zend Certified PHP Engineer, BeezNest Belgium SPRL, <a href="mailto:ywarnier@beeznest.net">ywarnier@beeznest.net</a></li>
  461. </ul>
  462. <hr />
  463. 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.
  464. <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>
  465. <a href="http://jigsaw.w3.org/css-validator/">
  466. <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" style="margin: 1em; float: right;" alt="Valid CSS" />
  467. </a>
  468. </div>
  469. </body>
  470. </html>