optimization.html 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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
  37. with Chamilo. This guide is a work in progress intended to help
  38. administrators optimize their Chamilo installation.</p>
  39. <h2><b>Contents</b></h2>
  40. <ol>
  41. <li><a href="#1.Using-XCache">Using opcaches</a></li>
  42. <li><a href="#2.Slow-queries">Slow queries</a></li>
  43. <li><a href="#3.Indexes-caching">Indexes caching</a></li>
  44. <li><a href="#4.Sessions-directories">Sessions directories</a></li>
  45. <li><a href="#5.Users-upload-directories">Users upload directories</a></li>
  46. <li><a href="#6.Zlib-compression">Zlib compressed output</a></li>
  47. <li><a href="#7.High-numbers-memory">Memory considerations for high numbers of users</a></li>
  48. <li><a href="#8.Avoid-non-fixed-values">Avoiding non-fixed values</a></li>
  49. <li><a href="#9.xsendfile">Speeding file downloads with mod_xsendfile</a></li>
  50. <li><a href="#10.igbinary">IGBinary for faster courses backups and better sessions</a></li>
  51. <li><a href="#11.permissions-check">Removing files download permissions check</a></li>
  52. <li><a href="#12.MySQL-compression">MySQL/MariaDB compression</a></li>
  53. </ol>
  54. <h2><a name="1.Using-XCache"></a>1. Using opcaches</h2>
  55. <h3>Zend OpCode (Zend Optimizer+)</h3>
  56. From version 5.5, PHP includes the Zend OpCache Optimizer, which can
  57. produce considerable efficiency boosts and is very reliable.
  58. Using OpCache should come by default, but if you want to make sure it's
  59. running, just check that your opcache.ini config file says
  60. <pre>opcache.enable = 1</pre>
  61. Some websites will recommend the addition of additional settings, and this
  62. is really up to you. Check <a href="http://php.net/manual/en/opcache.configuration.php">the official OpCache config page for more information</a>.
  63. Zend OpCache is an "opcode" cache, meaning it will compile static code to make their processing faster.
  64. However, this will not allow you to "store" shared variables in memory between all users. To do that, we suggest
  65. you complement Zend OpCache (opcode) with a user-land cache like APCu.
  66. <h3>xCache</h3>
  67. See <a href="http://xcache.lighttpd.net/">xCache's website</a> for summary documentation.<br />
  68. <ul>
  69. <li>On Debian/Ubuntu<=14.04: sudo apt-get install php5-xcache</li>
  70. </ul>
  71. Set your xcache.ini configuration (/etc/php5/conf.d/xcache.ini) to match your
  72. system. For example, you *could* have something like this (intentionally
  73. hiding comments here):
  74. <pre>
  75. xcache.shm_scheme = "mmap"
  76. xcache.size = 32M
  77. xcache.count = 2
  78. xcache.slots = 8K
  79. xcache.ttl = 0
  80. xcache.gc_interval = 0
  81. xcache.var_size = 16M
  82. xcache.var_count = 16
  83. xcache.var_slots = 8K
  84. xcache.var_ttl = 60
  85. xcache.var_maxttl = 300
  86. xcache.var_gc_interval = 300
  87. xcache.test = Off
  88. </pre>
  89. xCache will feel useless until you actually start to put some variables in
  90. cache. If you're showing the "Who is online" counter, that's one of the
  91. best item there is to implement xCache.<br />
  92. For example, you could implement it this way (in main/inc/lib/banner.lib.php):<br />
  93. <pre>
  94. $xc = function_exists('xcache_isset');
  95. $number = 0;
  96. 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)) {
  97. if ($xc &amp;&amp; xcache_isset('campus_chamilo_org_whoisonline_count_simple')) {
  98. $number = xcache_get('campus_chamilo_org_whoisonline_count_simple');
  99. } else {
  100. $number = who_is_online_count(api_get_setting('display.time_limit_whosonline'));
  101. xcache_set('campus_chamilo_org_whoisonline_count_simple',$number);
  102. }
  103. }
  104. $number_online_in_course = 0;
  105. if(!empty($_course['id'])) {
  106. if ($xc &amp;&amp; xcache_isset('campus_chamilo_org_whoisonline_count_simple_'.$_course['id'])) {
  107. $number_online_in_course = xcache_get('campus_chamilo_org_whoisonline_count_simple_'.$_course['id']);
  108. } else {
  109. $number_online_in_course = who_is_online_in_this_course_count(api_get_user_id(), api_get_setting('display.time_limit_whosonline'), $_course['id']);
  110. xcache_set('campus_chamilo_org_whoisonline_count_simple_'.$_course['id'],$number_online_in_course);
  111. }
  112. }
  113. </pre>
  114. Note that, as xCache is a shared caching system, it is very important to prefix
  115. your variables with a domain name or some kind of identifier, otherwise it
  116. would end up in disaster if you use a shared server for several portals.<br />
  117. If you use php5-memcache, then this piece of code would look like this
  118. (you need to adjust depending on your settings):
  119. <pre>
  120. global $_configuration;
  121. $_course = api_get_course_info();
  122. $course_id = api_get_course_id();
  123. $user_id = api_get_user_id();
  124. $html = '';
  125. $xc = method_exists('Memcache','add');
  126. if ($xc) {
  127. // Make sure the server is available
  128. $xm = new Memcache;
  129. $xm->addServer('localhost', 11211);
  130. $xc = $xc && ($xm->getServerStatus('localhost',11211)!=0);
  131. // The following concatenates the name of the database + the id of the
  132. // access url to make it a unique variable prefix for the variables to
  133. // be stored
  134. $xs = $_configuration['main_database'].'_'.$_configuration['access_url'].'_';
  135. }
  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 && $xm->get($xs.'wio_count_simple')) {
  139. $number = $xm->get($xs.'wio_count_simple');
  140. } else {
  141. $number = who_is_online_count(api_get_setting('display.time_limit_whosonline'));
  142. $xm->set($xs.'wio_count_simple',$number,0,30);
  143. }
  144. $number_online_in_course = 0;
  145. if(!empty($_course['id'])) {
  146. if ($xc && $xm->get($xs.'wio_count_simple_'.$_course['id'])) {
  147. $number_online_in_course = $xm->get($xs.'wio_count_simple_'.$_course['id']);
  148. } else {
  149. $number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('display.time_limit_whosonline'), $_course['id']);
  150. $xm->set($xs.'wio_count_simple_'.$_course['id'],$number_online_in_course,0,30);
  151. }
  152. }
  153. </pre>
  154. <br />
  155. An optional additional caching mechanism you may use is the realpath_cache_size
  156. and realpath_cache_ttl php.ini parameters.
  157. See <a href="http://php.net/manual/en/ini.core.php">the PHP documentation</a>
  158. for more details.
  159. <br />
  160. <br />
  161. <h3>APC</h3>
  162. If you prefer using <a href="http://php.net/manual/en/book.apc.php">APC</a>,
  163. you can use the same kind of trick as above, just changing the code a little:
  164. <pre>
  165. $xc = function_exists('apc_exists');
  166. $number = 0;
  167. 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)) {
  168. if ($xc) {
  169. $apc = apc_cache_info(null,true);
  170. $apc_end = $apc['start_time']+$apc['ttl'];
  171. if (apc_exists('my_campus_whoisonline_count_simple') AND (time() < $apc_end) AND apc_fetch('my_campus_whoisonline_count_simple') > 0 ) {
  172. $number = apc_fetch('my_campus_whoisonline_count_simple');
  173. } else {
  174. $number = who_is_online_count(api_get_setting('display.time_limit_whosonline'));
  175. apc_clear_cache();
  176. apc_store('my_campus_whoisonline_count_simple',$number,15);
  177. }
  178. } else {
  179. $number = who_is_online_count(api_get_setting('display.time_limit_whosonline'));
  180. }
  181. $number_online_in_course = 0;
  182. if (!empty($_course['id'])) {
  183. if ($xc) {
  184. $apc = apc_cache_info(null,true);
  185. $apc_end = $apc['start_time']+$apc['ttl'];
  186. 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) {
  187. $number_online_in_course = apc_fetch('my_campus_whoisonline_count_simple_'.$_course['id']);
  188. } else {
  189. $number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('display.time_limit_whosonline'), $_course['id']);
  190. apc_store('my_campus_whoisonline_count_simple_'.$_course['id'],$number_online_in_course,15);
  191. }
  192. } else {
  193. $number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('display.time_limit_whosonline'), $_course['id']);
  194. }
  195. }
  196. ...
  197. </pre>
  198. <br />
  199. <h3>Memcached</h3>
  200. If you use php5-memcached (different set of functions than php5-memcache!),
  201. then this piece of code would look like this (you need to adjust
  202. depending on your settings):
  203. <pre>
  204. global $_configuration;
  205. $_course = api_get_course_info();
  206. $course_id = api_get_course_id();
  207. $user_id = api_get_user_id();
  208. $html = '';
  209. $xc = method_exists('Memcached', 'add');
  210. if ($xc) {
  211. // Make sure the server is available
  212. $xm = new Memcached;
  213. $xm->addServer('localhost', 11211);
  214. // The following concatenates the name of the database + the id of the
  215. // access url to make it a unique variable prefix for the variables to
  216. // be stored
  217. $xs = $_configuration['main_database'].'_'.$_configuration['access_url'].'_';
  218. }
  219. $number = 0;
  220. 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)) {
  221. if ($xc) {
  222. if ($xm->get($xs.'wio_count_simple')) {
  223. $number = $xm->get($xs.'wio_count_simple');
  224. } else {
  225. $number = who_is_online_count(api_get_setting('display.time_limit_whosonline'));
  226. $xm->set($xs.'wio_count_simple',$number,120);
  227. }
  228. } else {
  229. $number = who_is_online_count(api_get_setting('display.time_limit_whosonline'));
  230. }
  231. $number_online_in_course = 0;
  232. if (!empty($_course['id'])) {
  233. if ($xc) {
  234. if ($xm->get($xs.'wio_count_simple_'.$_course['id'])) {
  235. $number_online_in_course = $xm->get($xs.'wio_count_simple_'.$_course['id']);
  236. } else {
  237. $number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('display.time_limit_whosonline'), $_course['id']);
  238. $xm->set($xs.'wio_count_simple_'.$_course['id'],$number_online_in_course,120);
  239. }
  240. } else {
  241. $number_online_in_course = who_is_online_in_this_course_count(api_get_user_id(), api_get_setting('display.time_limit_whosonline'), $_course['id']);
  242. }
  243. }
  244. // ...
  245. </pre>
  246. <p>It is also worth noting that the Université de Genève, Switzerland, observed
  247. that the calculation of the total size used by course documents is one of
  248. the heaviest queries in Chamilo, so you might want to cache the results of
  249. this one as well, using the same technique.</p>
  250. <p>Finally, if your portal is highly public *and* you are showing the popular
  251. courses on the homepage, you might want to also reduce the amount of
  252. queries this generates, using the same technique as above, but for the
  253. main/inc/lib/auth.lib.php library, looking for the
  254. "Tracking::get_course_connections_count()" call:</p>
  255. <pre>
  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. $xc = function_exists('apc_exists');
  260. if ($xc) {
  261. $apc = apc_cache_info(null, true);
  262. $apx_end = $apc['start_time']+$apx['ttl'];
  263. if (apc_exists('my_campus_course_visits_'.$row['code']) AND (time() < $apc_end) AND apc_fetch('my_campus_course_visits_'.$row['code']) > 0) {
  264. $count_connections_last_month = apc_fetch('my_campus_course_visits_'.$row['code']);
  265. } else {
  266. $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time() - (30 * 86400)));
  267. apc_store('my_campus_course_visits_'.$row['code'], $count_connections_last_month, $apc['ttl']);
  268. }
  269. } else {
  270. $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time() - (30 * 86400)));
  271. }
  272. ...
  273. }
  274. </pre>
  275. Finally, the Free Campus of Chamilo has a very specific case of slow query:
  276. the courses catalog! Because there might be more than 32,000 courses in
  277. there, getting the number of "Connections last month" can be a disastrous
  278. query in terms of performances. This is why you should try to cache the
  279. results as well.<br />
  280. Obviously, as we are speaking about showing the number of visits this month,
  281. it doesn't really matter if the number doesn't refresh for an hour or so...<br />
  282. Locate the main/inc/lib/course_category.lib.php file, open it and go to the
  283. browseCoursesInCategory() function.<br />
  284. Locate the $count_connections_last_month = Tracking::get_course_connections_count(...)
  285. call, and wrap in into something like this:
  286. <pre>
  287. $xc = method_exists('Memcached', 'add');
  288. if ($xc) {
  289. // Make sure the server is available
  290. $xm = new Memcached;
  291. $xm->addServer('localhost', 11211);
  292. // The following concatenates the name of the database + the id of the
  293. // access url to make it a unique variable prefix for the variables to
  294. // be stored
  295. $xs = $_configuration['main_database'].'_'.$_configuration['access_url'].'_';
  296. }
  297. $result = Database::query($sql);
  298. $courses = array();
  299. while ($row = Database::fetch_array($result)) {
  300. $row['registration_code'] = !empty($row['registration_code']);
  301. $count_users = CourseManager::get_users_count_in_course($row['code']);
  302. if ($xc) {
  303. if ($xm->get($xs.'cccount_'.$row['code'])) {
  304. $number = $xm->get($xs.'cccount_'.$row['code']);
  305. } else {
  306. $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time() - (30 * 86400)));
  307. $xm->set($xs.'cccount_'.$row['code'], $count_connections_last_month, 3600);
  308. }
  309. } else {
  310. $count_connections_last_month = Tracking::get_course_connections_count($row['code'], 0, api_get_utc_datetime(time() - (30 * 86400)));
  311. }
  312. ...
  313. </pre>
  314. <hr />
  315. <h2><a name="2.Slow-queries"></a>2. Slow queries</h2>
  316. Enable slow_queries in /etc/mysqld/my.cnf, restart MySQL then follow using sudo tail -f /var/log/mysql/mysql-slow.log
  317. <br /><br />
  318. In Chamilo 1.9 in particular, due to the merge of all databases into one, you might experience performance issues.<br />
  319. To solve this performance issue, you can execute the following query manually in your database:<br />
  320. <pre>
  321. ALTER TABLE user_rel_tag ADD INDEX idx_user_rel_tag_user (user_id);
  322. </pre>
  323. <br /><br />
  324. In Chamilo 1.10.0 (the first version of the serie), many indexes were forgotten, so you can boost your database by adding the following indexes:<br />
  325. <pre>
  326. alter table extra_field_values add index idx_extra_field_values (field_id, item_id);
  327. alter table usergroup_rel_user add index idx_usergroup_ru (group_id);
  328. alter table usergroup_rel_user add index idx_usergroup_ru_u (user_id);
  329. alter table c_student_publication add index idxstudpub_cid (c_id);
  330. alter table c_student_publication add index idxstudpub_uid (user_id);
  331. alter table c_quiz_question add index idx_cqq_cid (c_id);
  332. alter table c_quiz_rel_question ADD INDEX idx_cqrq_qid (question_id);
  333. alter table c_quiz_rel_question ADD INDEX idx_cqrq_cid (c_id);
  334. alter table c_quiz_answer add index idx_qa_cidqid (c_id, question_id);
  335. </pre>
  336. <hr />
  337. <h2><a name="3.Indexes-caching"></a>3. Indexes caching</h2>
  338. 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 />
  339. <hr />
  340. <h2><a name="4.Sessions-directories"></a>4. Sessions directories</h2>
  341. php_admin_value session.save_path 1;/var/www/test.chamilo.org/sessions/
  342. <hr />
  343. <h2><a name="5.Users-upload-directories"></a>5. Users upload directories</h2>
  344. 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).
  345. <hr />
  346. <h2><a name="6.Zlib-compression"></a>6. Zlib compressed output</h2>
  347. 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 />
  348. Zlib output compression has to be set at two levels: PHP configuration for PHP pages and Apache for images and CSS.<br /><br />
  349. 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.
  350. <pre>
  351. php_value zlib.output_compression 1
  352. </pre>
  353. <br />
  354. 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 />
  355. Easy mode:
  356. <pre>
  357. AddOutputFilterByType DEFLATE text/html text/plain text/xml
  358. </pre> or, for every content type (dangerous) you can put the following inside a location or directory block:<pre>SetOutputFilter DEFLATE</pre>
  359. <br />
  360. Advanced mode:
  361. <pre>
  362. <Location />
  363. # Insert filter
  364. SetOutputFilter DEFLATE
  365. # Netscape 4.x has some problems...
  366. BrowserMatch ^Mozilla/4 gzip-only-text/html
  367. # Netscape 4.06-4.08 have some more problems
  368. BrowserMatch ^Mozilla/4\.0[678] no-gzip
  369. # MSIE masquerades as Netscape, but it is fine
  370. # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  371. # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
  372. # the above regex won't work. You can use the following
  373. # workaround to get the desired effect:
  374. BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
  375. # Don't compress images
  376. SetEnvIfNoCase Request_URI \
  377. \.(?:gif|jpe?g|png)$ no-gzip dont-vary
  378. # Make sure proxies don't deliver the wrong content
  379. Header append Vary User-Agent env=!dont-vary
  380. </Location>
  381. </pre>
  382. <hr />
  383. 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.
  384. <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>
  385. <a href="http://jigsaw.w3.org/css-validator/">
  386. <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" style="margin: 1em; float: right;" alt="Valid CSS" />
  387. </a>
  388. <hr />
  389. <h2><a name="7.High-numbers-memory"></a>Memory considerations for high numbers of users</h2>
  390. 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 />
  391. This mode is not loaded by default, but could still be selected, leading to a "Fatal error: Allowed memory size ... exhausted" message.<br />
  392. 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/>
  393. <hr />
  394. <h2><a name="8.Avoid-non-fixed-values"></a>Avoiding dynamic values</h2>
  395. Many things in Chamilo are written focusing on the ease of use, even for the
  396. administrator. Sometimes, these settings are weighing a little bit more on
  397. the system. This is the case, between others, of the mail.conf.php file
  398. (being loaded unconditionally) and its CONSTANT "IS_WINDOWS_OS", which is
  399. defined by a function call (api_is_windows_os()) at the beginning of
  400. main_api.lib.php.
  401. The definition of this constant (which is executed at *every* page load) can
  402. easily be avoided, and the only place where it is used unconditionally
  403. (mail.conf.php) can be modified to set the line as you expect it
  404. (depending on whether you use sendmail/exim or smtp).
  405. <pre>
  406. $platform_email['SMTP_MAILER'] = 'smtp';
  407. </pre>
  408. or
  409. <pre>
  410. $platform_email['SMTP_MAILER'] = 'mail';
  411. </pre>
  412. In fact, the complete loading of mail.conf.php can also be avoided if
  413. loaded conditionally (with <i>require_once</i>) when sending an
  414. e-mail (which is the only case where it is useful).
  415. <p>
  416. As an additional node, on very active portals with a lot of courses
  417. for each users, the icons that appear next to the courses illustrating
  418. changes in the corresponding course might be heavyweighted. You can
  419. alter slightly the behaviour by not querying for notifications you
  420. don't care about, like dropbox, notebook or chat. Change this in
  421. main/inc/lib/display.lib.php, in function show_notification().
  422. </p>
  423. <hr />
  424. <h2><a name="9.xsendfile"></a>Speeding file downloads with mod_xsendfile</h2>
  425. <p>It might have come to your attention that file downloads through Chamilo
  426. might get slow, under default conditions, in particular using Apache 2.</p>
  427. <p>There are several ways to fix this, one of which is removing the .htaccess
  428. inside the courses/ directory. This, however, will remove all permissions
  429. checks on the files contained in this directory, so... most of the time,
  430. not ideal unless your portal is *really* open to the world.</p>
  431. <p>Another technique, revealed to us by
  432. <a href="http://stackoverflow.com/users/46594/virtualblackfox">VirtualBlackFox</a>
  433. on <a href="http://stackoverflow.com/questions/3697748/fastest-way-to-serve-a-file-using-php">this Stackoverflow post</a>,
  434. is to use the X-SendFile module for Apache 2.2+ (other web servers might
  435. offer other solutions, or avoid the problem initially).</p>
  436. <p>Installing the X-SendFile module will depend on your operating system,
  437. but if you use Ubuntu, you'll have to check you are including the "universe"
  438. repository inside your packages sources (check /etc/apt/sources.list), then:
  439. <pre>
  440. sudo apt-get update
  441. sudo apt-get install libapache2-mod-xsendfile
  442. sudo service apache2 restart
  443. </pre>
  444. Once you're done with installing, you'll have to configure Chamilo to use it.<br />
  445. First, edit your VirtualHost or your Apache configuration in general (in Ubuntu,
  446. check the /etc/apache2/ or /etc/apache2/sites-available/ folder). This is done
  447. by adding the following line inside your configuration, and reloading Apache
  448. (example provided on the basis of a virtual host located in
  449. /etc/apache2/sites-available/my.chamilo.net.conf) :
  450. <pre>
  451. sudo vim /etc/apache2/sites-available/my.chamilo.net.conf
  452. # add the following line:
  453. X-SendFile on
  454. # exit the file
  455. sudo service apache2 reload
  456. </pre>
  457. Finally, you'll have to got to your Chamilo configuration file, and add the
  458. following line at the very bottom of the file main/inc/conf/configuration.php:
  459. <pre>
  460. $_configuration['enable_x_sendfile_headers'] = true;
  461. </pre>
  462. Done! Now your downloads should go substantially faster. This is still a
  463. feature in observation. We're not sure the benefits are sufficient, so
  464. don't hesitate to let us know in
  465. <a href="https://support.chamilo.org/issues/6853">the related issue in Chamilo's tracking system</a>
  466. </p>
  467. <hr />
  468. <h2><a name="10.igbinary"></a>IGBinary for courses backups and better
  469. sessions management</h2>
  470. <p>
  471. <a href="http://pecl.php.net/package/igbinary">IGBinary</a> is a small PECL
  472. library that replaces the PHP serializer. It uses less space (so less
  473. memory for serialized objects) and is particularly efficient with memory-based
  474. storages (like Memcached). Use it for course backups
  475. (see <a href="https://support.chamilo.org/issues/4443">issue 4443</a>) or
  476. <a href="http://www.neanderthal-technology.com/2011/11/ubuntu-10-install-php-memcached-with-igbinary-support/">to boost sessions management</a>.
  477. </p>
  478. <hr />
  479. <h2><a name="11.permissions-check"></a>Removing files download permissions check</h2>
  480. <p>
  481. This measure is not cumulative with mod_xsendfile explained above. It is not *recommended*
  482. either, as it removes an important security layer.<br />
  483. <br />
  484. In Chamilo, for security and tracking purposes, all downloaded files pass through PHP
  485. scripts that check whether the user has access to the file given his/her current
  486. permissions. This process requires important database accesses and processing, which
  487. might terminally affect your server's performance. In particular, this can
  488. have a huge effect if having hundreds of simultaneous users accessing
  489. learning paths pages composed of local resources.<br /><br />
  490. The logic behind this verification is that, whatever resources that needs to be
  491. downloaded/viewed that come from the /courses/ directory, the /courses/.htaccess
  492. file with get in the middle and redirect these accesses to a PHP script
  493. (usually called download.php but there are more than one depending on the
  494. type of resource).<br /><br />
  495. If you want to speed up files accesses and you don't really care about whom can
  496. see your files, then an option is to simply remove this redirection to
  497. download.php and let Apache treat the file on its own.<br /><br />
  498. Furthermore, using a PHP script for the download (unless you have special rules)
  499. will usually prevent static content caching, which will multiply downloads
  500. and use large amount of additional bandwidth.<br /><br />
  501. Typically, the .htaccess will look like this (with additional comments):<br />
  502. <pre>
  503. <IfModule mod_rewrite.c>
  504. RewriteEngine On
  505. RewriteBase /courses/
  506. RewriteCond %{REQUEST_URI} !^/main/
  507. RewriteRule ([^/]+)/document/(.*)&(.*)$ $1/document/$2///$3 [N]
  508. RewriteRule ([^/]+)/scorm/(.*)$ /main/document/download_scorm.php?doc_url=/$2&cDir=$1 [QSA,L]
  509. RewriteRule ([^/]+)/document/(.*)$ /main/document/download.php?doc_url=/$2&cDir=$1 [QSA,L]
  510. RewriteRule ([^/]+)/work/(.*)$ /main/work/download.php?file=work/$2&cDir=$1 [QSA,L]
  511. </IfModule>
  512. </pre><br />
  513. The idea is to remove the "scorm" line, so you are left with:
  514. <pre>
  515. <IfModule mod_rewrite.c>
  516. RewriteEngine On
  517. RewriteBase /courses/
  518. RewriteCond %{REQUEST_URI} !^/main/
  519. RewriteRule ([^/]+)/document/(.*)&(.*)$ $1/document/$2///$3 [N]
  520. RewriteRule ([^/]+)/document/(.*)$ /main/document/download.php?doc_url=/$2&cDir=$1 [QSA,L]
  521. RewriteRule ([^/]+)/work/(.*)$ /main/work/download.php?file=work/$2&cDir=$1 [QSA,L]
  522. </IfModule>
  523. </pre><br />
  524. This is easy, doesn't require a server reload and you should see the results pretty
  525. quickly. As mentioned above, if security of your content is an issue, though,
  526. you should avoid using this technique.
  527. </p>
  528. <p>
  529. You can also mitigate the risk by disabling permissions check only
  530. for some static resource like css,js and fonts files.
  531. <br/>
  532. For that is required to load header module
  533. in apache (check with a2enmod in your favorite root terminal)<br />
  534. add theses line after RewriteBase /courses/:
  535. <pre>
  536. &lt;IfModule mod_headers.c&gt;
  537. # all file name ended with these extensions names will bypass the permission check (and also served by the browser cache at the next request)
  538. &lt;FilesMatch &quot;\.(gif|jpg|jpeg|png|js|pdf|ico|icon|css|swf|avi|mp3|ogg|wav|ttf|otf|eot|woff)$&quot;&gt;
  539. Header unset Cache-Control
  540. Header set Cache-Control &quot;public, max-age=29030400&quot;
  541. RequestHeader unset Cookie
  542. Header unset ETag
  543. &lt;/FilesMatch&gt;
  544. &lt;/IfModule&gt;
  545. # also adjust files here
  546. RewriteRule (\.(html|gif|jpg|jpeg|png|js|pdf|ico|icon|css|swf|avi|mp3|ogg|wav|ttf|otf|eot|woff))$ - [L]
  547. </pre>
  548. </p>
  549. <hr />
  550. <h2><a name="12.MySQL-compression"></a>MySQL/MariaDB compression</h2>
  551. <p>
  552. If your database server is separate from your web server, you have to play with
  553. bandwidth, firewalls, and network restrictions in general.<br />
  554. In particular, when dealing with large-scale portals, the time a SQL query
  555. will take to return to the web server will take longer and, eventually,
  556. in the most critical cases, will take <b>too long</b>, and your web servers
  557. will be completely overloaded (load average very high because the system
  558. is waiting for I/O operations, but processors usage not being very high
  559. is a clear sign of this).<br />
  560. To solve this kind of issues, MySQL and MariaDB offer a data compression
  561. mechanism, which will reduce the amount of data passed between PHP and
  562. the database server. Ultimately, this reduction will lower bandwidth
  563. usage and reduce the impact of numerous and heavy data requests (and
  564. save you).<br />
  565. In 1.10.0, we have added the possibility to enable this compression very
  566. easily, from the configuration.php file, uncommenting the following line:
  567. <pre>
  568. //$_configuration['db_client_flags'] = MYSQL_CLIENT_COMPRESS;
  569. </pre>
  570. This should have an immediate effect on the load average on your server.
  571. </p>
  572. <hr />
  573. <h2>Authors</h2>
  574. <ul>
  575. <li>Yannick Warnier, Zend Certified PHP Engineer, BeezNest Belgium SPRL, <a href="mailto:ywarnier@beeznest.net">ywarnier@beeznest.net</a></li>
  576. </ul>
  577. <hr />
  578. Don't have time or resources to optimize your Chamilo installation
  579. yourself? Hire an <a href="http://www.chamilo.org/en/providers">official Chamilo provider</a> and get it sorted out professionally by specialists.
  580. <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>
  581. <a href="http://jigsaw.w3.org/css-validator/">
  582. <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" style="margin: 1em; float: right;" alt="Valid CSS" />
  583. </a>
  584. </div>
  585. </body>
  586. </html>