optimization.html 37 KB

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