May 03 2010

FractalizeR: VBulletin 4 extensive optimization guide

Category: Articles,VBulletinFractalizeR @ 6:25 pm

Preface

Actually, I never bothered to optimize my server for VBulletin, because the server we purchased for forum is really powerful. But once upon a time logwatch told me, that smartd told her, that he found several relocated sectors on one of server HDDs. And then instantly I realized, that many time ago when I assembled that server, I configured HDDs to be in RAID-0 (striped mode)… Oh…. When you will make some stupid thing on your server, please keep in mind, that there are some people that can do things even more stupid…. Nothing critical happened, because I, actually, have all database backups. Database snapshots are made on server using this script of mine every hour or so (BTW, database backup on my server of 1.12Gb database (reported at admincp) takes around a minute to complete together with compression).

So, we had to visit datacenter and remove/replace HDDs. To allow our forum users to continue using forum until we fix that, we moved our forum to a new server. After forum move, I upgraded it to v4 to benefit from friendly URLs. And there it started. Server load almost immediately reached 4.0 (server had two dualcore CPUs, so this is almost 100% of standard load) and those were even not peak hours. Of course, that was not because of V4 (I turned off both that huge CMS issuing ~200 queries on home page and blogs). This was because that new server where the forum was moved to was less powerful. Of course, 4.0 is not a heavy load for server (once one of my servers reached around 100.0 and still I was able to login by SSH 🙂 ).

But nevertheless, that was the moment, where optimization hysteria first visited my head.

I used my Linux knowledge combined with Google power to provide you with this optimization guide. It is concentrated around things, that can be easily and simply applied to your server without too much work. Most of the time it explains basic things, that seem apparent. But I am sure, 90% of beginner administrators never use them altogether. This guide does not pretend to be complete one. To follow most of it, you have to have some basic Linux knowledge and root access to your server. However, many of the described optimizations can be done just with Control Panel administrator and FTP accesses. Please feel free to criticize it and add things to it. If you are going to republish it, I will be most pleased, but please keep my copyright on it and a link to my website where I will try to maintain up-to-date version of it with all improvements and corrections you might send me.

This guide mostly provide things, that are safe to your server or could be easily rolled back. However, please keep in mind, that you apply them on your own peril.

If

  • you have RedHat or CentOS operating system on your server
  • your forum have around 200.000 – 400.000 posts or more
  • your forum have around 100 users online or more at regular daytime
  • you have root access to your forum machine and are willing to provide me with root password
  • you are willing to report optimization results after some time on vbulletin.org

you can contact me to help you to apply some or all optimizations I described here. Please note, that giving someone root password to your machine can be dangerous. Root access allows administrator to do EVERYTHING on the machine. Think about it twice before asking someone to help you with forum using root password. In all cases you need to change root password immediately after all tasks you requested are performed. This can be easily done by logging to your machine as root and issuing passwd command.

I cannot promise to do all instantly because of the amount of daily job I have, but will do my best.

Generic all-good server optimizations

This section will list optimizations, that are, actually, good for any forum and any server.

Optimize all *.png images in forum folder

VBulletin forum images and, especially, addon and style images can be optimized reducing their size up to 50% from original. Install OptiPNG tool (for example, from RPMForge repository. If you are on RedHat or CentOS you can install RPMForge repository quickly this way and install OptiPNG from there using yum install optipng after.) and use it to optimize forum images. On Linux you can just enter forum directory and execute there the following command, after you have optipng rpm installed:

find -name "*.png" | xargs optipng

This will run optipng command on every *.png image in the directory you are currently in (check that with pwd, it should be forum directory) and below. OptiPNG will use it’s own default settings this way. They are ok 99% of cases. I leave studying it’s options to you.

If you can’t run optipng on server, just download all images and optimize them on your home PC (there is a Windows version of OptiPNG too). But remember to do that again after forum update, because update package may overwrite your optimized images.

OptiPNG does not affect the quality of them. At least it is not supposed to, so… you have a backup anyway, right? 😉

Prevent browser from resize images

If you are developing a skin or just make new forum status icons, keep in mind, that you should not make browser resize your images (for example, if your icon on disk is 128×128, png, but you specified in your style, that it is only <img width=”48″ height=”48″ src=”…”/>. This will make an impression on user, that page is loading too slow (browser will be busy resizing all your images and that takes CPU time).

Use external YUI

It is faster to use YUI from Yahoo or Google servers. You can set which one to use YUI from in VBulletin Options -> Server optimizations -> Use remote YUI. I prefer Google 🙂

Move your attachments into filesystem

Move your attachments into filesystem. Go to your AdminCP, expand the Attachments menu on the left side and click Attachments Storage Type.

Move Avatars into filesystem

The same as with attachments, you can move avatars into file system. Go to your AdminCP, expand the Avatars menu on the left side and click Avatars Storage Type.

Prevent search engine spiders from visiting areas of your forums that they do not need to spider

Create a robots.txt file with the following and upload it to your public_html directory:

User-agent:  *
Disallow: /forums/admincp/
Disallow:  /forums/clientscript/
Disallow:  /forums/cpstyles/
Disallow:  /forums/customavatars/
Disallow:  /forums/customprofilepics/
Disallow:  /forums/images/
Disallow:  /forums/modcp/
Disallow:  /forums/ajax.php
Disallow:  /forums/attachment.php
Disallow:  /forums/calendar.php
Disallow:  /forums/cron.php
Disallow:  /forums/editpost.php
Disallow:  /forums/global.php
Disallow:  /forums/image.php
Disallow:  /forums/inlinemod.php
Disallow:  /forums/joinrequests.php
Disallow:  /forums/login.php
Disallow:  /forums/member.php
Disallow:  /forums/memberlist.php
Disallow:  /forums/misc.php
Disallow:  /forums/moderator.php
Disallow:  /forums/newattachment.php
Disallow:  /forums/newreply.php
Disallow:  /forums/newthread.php
Disallow:  /forums/online.php
Disallow:  /forums/poll.php
Disallow:  /forums/postings.php
Disallow:  /forums/printthread.php
Disallow:  /forums/private.php
Disallow:  /forums/profile.php
Disallow:  /forums/register.php
Disallow:  /forums/report.php
Disallow:  /forums/reputation.php
Disallow:  /forums/search.php
Disallow:  /forums/sendmessage.php
Disallow:  /forums/showgroups.php
Disallow:  /forums/subscription.php
Disallow:  /forums/threadrate.php
Disallow:  /forums/usercp.php
Disallow:  /forums/usernote.php

If you use something other than /forums/ for your forums directory, you will want to adjust the paths accordingly (just like /usernote.php if your forum is in www root)

Disable File Access Time updates in your filesystem

If your forum is running on VDS/VPS or dedicated server, you need to disable updating access times for your filesystem to improve performance. You can do that by adding noatime attribute into your /etc/fstab so that the partition you have forum files on have this option enabled like this for example:

/dev/md2                 /                       ext3    defaults,noatime        1 1

For changes to take effect, you need to reboot your server with reboot or shutdown -r now command.

Please note, if you make a mistake in editing fstab, you may end up with non-booting system and you will need to boot Linux into single user mode or startup from emergency disk to repair this file. This is especially dangerous if you have no physical access to your server.

Install SPRI

SPRI is a small bash script, that runs regularly and adjusts process priorities to distribute system load. You can get it here. Sometimes benefits of installing SPRI to your server are as much as 20% lowered server load.

You will need to wget it, untar it, and then run /install.sh from unpacked folder.

Install mod_evasive and DDOSDeflate script

If your forum is a constant target for DDOS attacks, to lower their performance impact, you can install Apache module mod_evasive and DDOS Deflate scripts. mod_evasive installation guides are scattered across the net, so I leave you with Google on it’s installation. As for DDOS Deflate, just wget http://www.inetbase.com/scripts/ddos/install.sh and run it (don’t forget to chmod u+x it first). Generally these two doesn’t need to be tuned and their default settings are ok.

Move Avatars and Profile Pics to the file system

You need to move pictures to the filesystem as well. Go to your AdminCP, expand the Avatars menu on the left side and click User Picture Storage Type.

Move CSS to the file system

Go to your AdminCP, expand the vBulletin Options menu and select vBulletin options. From there select Style and Language Options in the drop down menu. Then set “Store CSS Stylesheets as Files?” to Yes and click Submit.

Enable caching of static content

This option will tell user browser to download files from your website less often thus decreasing server load and improving page load times for user. You will have to have mod_expires active in Apache.

##  Expires
<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault "access plus 1 seconds"
 ExpiresByType text/html   "access plus 1 seconds"
 ExpiresByType image/gif "access plus   3456000 seconds"
 ExpiresByType image/jpeg "access plus 3456000   seconds"
 ExpiresByType image/png "access plus 3456000 seconds"
 ExpiresByType text/css "access plus 3456000 seconds"
 ExpiresByType  text/javascript "access plus 3456000 seconds"
 ExpiresByType  application/x-javascript "access plus 3456000 seconds"
 ExpiresByType  application/javascript "access plus 3456000 seconds"
</IfModule>

I don’t recommend here to turn off E-tags because I am not currently convinced this will improve performance. Also turning them off together with enabling mod_expires may lead to server reporting files are up to date when they have updated and client should fetch updated versions of them.

Install and attach MemCached

Consider installing and attaching MemCached to VBulletin (for example, from RPMForge repository. If you are on RedHat or CentOS you can install RPMForge repository quickly this way and install MemCached from there using yum install memcached after. Don’t forget to issue chkconfig memcached on to make sure memcached will auto-start on next server reboot).

You will need to edit your /includes/config.php and uncomment the following lines there:

$config['Datastore']['class'] =  'vB_Datastore_Memcached';
$i  = 0;
// First Server
$i++;
$config['Misc']['memcacheserver'][$i]          = '127.0.0.1';
$config['Misc']['memcacheport'][$i]             =  11211;
$config['Misc']['memcachepersistent'][$i]     = true;
$config['Misc']['memcacheweight'][$i]          = 1;
$config['Misc']['memcachetimeout'][$i]         = 1;
$config['Misc']['memcacheretry_interval'][$i]   = 15;

Also, memcached from RPMForge repository is insecure by default and is listening on all IPs by default (it allows external connections). Either close the port 11211 with firewall, or consider to always start memcached with “-l 127.0.0.1” option to listen only on localhost. On RedHat or CentOS you can do that by editing /etc/sysconfig/memcached file and adding a setting to OPTIONS line like this:

OPTIONS="-l 127.0.0.1"

InnoDB: Migrating

Consider migrating to InnoDB to improve concurrency if you have a really large forum and you want to make it faster. Please note, that on small forums (or on servers with a little of physical memory) this option can even slow things a bit. Be sure you have at least 2-4Gb RAM on your server before considering InnoDB migration.

InnoDB: increase innodb_buffer_pool_size

If you migrated to InnoDB, you need to increase innodb_buffer_pool_size to at least 512Mb depending on your server free RAM available. It is the memory area, used to cache data for InnoDB tables and by default it is very small (8Mb only, suitable for Pentium 166MMX home machine with database of your video tapes).

If your server is running something else together with VBulletin, please consult with your server administrator for this change since it is server-wide.

InnoDB: lower transaction isolation level

VBulletin is running in autocommit mode and does not really use transactions. Default InnoDB transaction isolation level is REPEATABLE READ, which leads to excessive use of system resources in autocommit mode. You can lower it to READ COMMITTED. If you run ONLY VBulletin on your server, to lower transaction isolation level you need to add the following line to /etc/my.cnf (MySQL main configuration file).

transaction-isolation = READ-COMMITTED

If your server is also running something else, that is using InnoDB transactions with another isolation level, you will have to patch /includes/class_core.php instead of changing MySQL default configuration. Open it and add to vB_Database::db_connect before “return $link” the following:

$this->sql = "SET SESSION TRANSACTION ISOLATION  LEVEL READ COMMITTED";
$this->execute_query(true,   $link);

Remember to do this (class_core.php patch) after VBulletin upgrade also. However, if you forget, nothing dangerous will happen. You will just loose this optimization’s benefits.

Tune MySQL for your forum

There are several ways to optimize your MySQL server even if you are not a professional DBA. One is to open PHPMyAdmin, go to Status tab to see server status and look for values in red color there. You will find some hints next to them (although, they could be not so clear).

Another is to use various MySQL analysis scripts, that analyze MySQL status reported by server themselves. You can get one here (it’s a bash script, you need to wget it, then chmod u+x tuning-primer.sh, and then execute: ./tuning-primer.sh) and another in Perl here. They report more understandable resuts.

Before making changes to your my.cnf, please, consider making a backup of it so that you could easily revert corrupted or unoptimal settings.

Optimize your MySQL tables regularly

Consider running the following simple script every several weeks depending on forum load (of course, you should run it at nights when your forum is the least loaded)

<?php
 
//Change  the following line depending on  where you placed this script.
require_once  (dirname(__FILE__) .  '/../www/includes/config.php');
 
ini_set("memory_limit",  "50M");
set_time_limit(0);
 
//Connecting  DB
mysql_connect($config['MasterServer']['servername'],   $config['MasterServer']['username'],
 $config['MasterServer']['password']) or die(mysql_error());
mysql_select_db($config['Database']['dbname'])   or die(mysql_error());
 
$sql = 'SHOW  TABLES';
$alltables  = mysql_query("$sql") or die(mysql_error());
while  ($table  = mysql_fetch_assoc($alltables))
{
 foreach ($table as $db =>  $tablename) {
 mysql_query("OPTIMIZE TABLE  `".$tablename."`;") or die(mysql_error());
 sleep(10); //Let  some queries to execute to lower forum load
 }
}

Don’t put this script inside www root because it can be used as ddos tool if it will be publicly accessible 😉 Keep in mind, that running this script may take a long time depending on your forum (especially, if you switched into InnoDB, because OPTIMIZE TABLE for InnoDB currently is equivalent to ALTER TABLE with full table rebuild. This will also effectively write-lock the table during OPTIMIZE operation).

You can add it to Linux cron if you like.

Consider moving to newer MySQL server version

Generally, newer MySQL server versions are more clever. They know how to optimize queries better, how to handle resources better etc. So, you may receive a small benefit from moving to a newer MySQL server version. If you are on CentOS or RedHat this is complicated, because default repositories have only 5.0 version of MySQL server. So, I cannot recomment that. But you can always get RPM from MySQL website and install it. If you are an IT geek, you can even move to Percona MySQL server. This is a tweaked MySQL server with some optimization patches (from Google for example) applied and improved InnoDB performance. Unlike Oracle’s MySQL, Percona has own RPM repository, that can be installed using this rpm.

Despite performance gains, I don’t recommend this until you are really in need because maintaining another version of MySQL different from the one provided with your OS by default will take much time and resources from you.

Turn off apache modules, that you don’t need

Consider turning off all apache modules you don’t use on server, that holds your forum. For example, on my server httpd.conf has the following LoadModule configuration part:

LoadModule auth_basic_module  modules/mod_auth_basic.so
#LoadModule  auth_digest_module  modules/mod_auth_digest.so
LoadModule  authn_file_module  modules/mod_authn_file.so
#LoadModule  authn_alias_module  modules/mod_authn_alias.so
#LoadModule  authn_anon_module  modules/mod_authn_anon.so
#LoadModule  authn_dbm_module  modules/mod_authn_dbm.so
#LoadModule  authn_default_module  modules/mod_authn_default.so
LoadModule  authz_host_module  modules/mod_authz_host.so
LoadModule  authz_user_module  modules/mod_authz_user.so
#LoadModule  authz_owner_module  modules/mod_authz_owner.so
#LoadModule  authz_groupfile_module  modules/mod_authz_groupfile.so
#LoadModule  authz_dbm_module  modules/mod_authz_dbm.so
#LoadModule  authz_default_module  modules/mod_authz_default.so
#LoadModule  ldap_module modules/mod_ldap.so
#LoadModule   authnz_ldap_module modules/mod_authnz_ldap.so
LoadModule   include_module modules/mod_include.so
LoadModule  log_config_module  modules/mod_log_config.so
LoadModule  logio_module modules/mod_logio.so
LoadModule   env_module modules/mod_env.so
LoadModule  ext_filter_module  modules/mod_ext_filter.so
LoadModule  mime_magic_module  modules/mod_mime_magic.so
LoadModule  expires_module modules/mod_expires.so
LoadModule   deflate_module modules/mod_deflate.so
LoadModule  headers_module  modules/mod_headers.so
#LoadModule  usertrack_module  modules/mod_usertrack.so
LoadModule  setenvif_module modules/mod_setenvif.so
LoadModule   mime_module modules/mod_mime.so
#LoadModule  dav_module modules/mod_dav.so
#LoadModule   status_module modules/mod_status.so
#LoadModule  autoindex_module  modules/mod_autoindex.so
#LoadModule  info_module modules/mod_info.so
#LoadModule   dav_fs_module modules/mod_dav_fs.so
#LoadModule  vhost_alias_module  modules/mod_vhost_alias.so
LoadModule  negotiation_module  modules/mod_negotiation.so
LoadModule  dir_module modules/mod_dir.so
#LoadModule   actions_module modules/mod_actions.so
#LoadModule  speling_module  modules/mod_speling.so
#LoadModule  userdir_module modules/mod_userdir.so
LoadModule   alias_module modules/mod_alias.so
LoadModule  rewrite_module  modules/mod_rewrite.so
#LoadModule  proxy_module modules/mod_proxy.so
#LoadModule   proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule   proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule  proxy_http_module  modules/mod_proxy_http.so
#LoadModule  proxy_connect_module  modules/mod_proxy_connect.so
#LoadModule  cache_module modules/mod_cache.so
#LoadModule   suexec_module modules/mod_suexec.so
#LoadModule  disk_cache_module  modules/mod_disk_cache.so
#LoadModule  file_cache_module  modules/mod_file_cache.so
#LoadModule  mem_cache_module  modules/mod_mem_cache.so
LoadModule  cgi_module modules/mod_cgi.so
LoadModule   version_module modules/mod_version.so

Disabled modules are prefixed with “#” (commented). After you edited httpd.conf, please don’t restart apache right away 😉 First you need to check syntax of the file to be sure, apache will startup normally. You can do this issuing apachectl configtest command. Generally, for example, disabling autoindex module requires some more commenting of httpd.conf because corresponding setting lines are not wrapped into IfModule directives.

Enable KeepAlive in Apache settings

KeepAlive setting allows Apache server to send replies to several requests over the same TCP connection. This is of course faster than to open new TCP connection for each resource sent. To enable KeepAlive search for “KeepAlive” in apache configuration file (httpd.conf) and set it to On:

KeepAlive  On

If there is no such option, you may add it. This will make your forum pages to load faster for endusers. However, this could lead to a need to slightly increase a number of MaxConnections allowed to be accepted by Apache and a little more resources will be consumed especially on forums with many users online.

Turn off PHP modules you don’t need

VBulletin uses the following PHP modules: gd, iconv, mbstring, mysql or mysqli (depending on your VBulletin database connection settings). All others (except for those, that are compiled with PHP by default and were not disabled using –disable-XXX switch during PHP compilation) are not required. You can skip loading them to make PHP occupy less memory and start faster. On RedHat and CentOS you can just comment “extension=” lines in each corresponding file inside /etc/php.d directory.

Turn on PHP bytecode caching

If

  • your PHP is running as apache module (mod_php) or
  • PHP is configured as fast-cgi

you can benefit from bytecode caching. By default, PHP is parsing and compiling to internal byte-codes (like Java) each script and on every client request. This takes time and resources. PHP bytecode caches allows to store once compiled script and reuse already compiled bytecodes. I recomment using APC as your bytecode cache. It can be installed from PECL repository. On RedHat and CentOS you can install it the following commands:

yum install php-devel
pecl install  APC

The first command installs php development package, that is used to compile PHP extensions. The second installs APC itself. You may need to activate it inside your php.ini after compilation. You may need to install basic prerequisites for source compilation:

yum install  autoconf libtool gcc gcc-c++

Please note, that if PHP is running as CGI, turning on bytecode cache will give you no benefits as the cache will be invalidated on each new request.

Optimizations, that will speed up forum for users, but may increase server load

Enable compression of content

If you have some spare CPU power and want to make your forum load faster for users, you can also enable static content compression. You will have to have both mod_headers and mod_deflate active in Apache

#Compression
<IfModule mod_headers.c>
    <IfModule mod_deflate.c>
        AddOutputFilterByType  DEFLATE text/css text/xml text/javascript application/x-javascript  application/javascript
        BrowserMatch ^Mozilla/4  gzip-only-text/html
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    </IfModule>
</IfModule>

You can also set Cookies and HTTP Header Options -> GZIP HTML Output to true to speed up HTML delivery to end user.

Forum settings, that seriously affect database performance

You can tune all below if your forum causes great load to database

  • General Settings ->Thread/Forum Read Marking Type
  • Server Settings and Optimization Options -> Cached Posts Lifespan
  • Server Settings and Optimization Options -> Update Thread Views Immediately
  • Server Settings and Optimization Options -> Update Attachment Views Immediately
  • Message Searching Options -> Maximum Search Results to Return
  • Message Searching Options -> Automatic Similar Thread Search
  • Message Searching Options -> Search Result Sharing
  • Forums Home Page Options -> Display Logged in Users?
  • Forum Display Options (forumdisplay) -> Show Users Browsing Forums
  • Forum Display Options (forumdisplay) -> Highlight Threads in Which User Has Posted
  • Thread Display Options (showthread) -> Show Users Browsing Threads
  • Thread Display Options (showthread) -> Check Thread Rating
  • Thread Display Options (showthread) Check Thread Subscription

Tags:

2 Responses to “FractalizeR: VBulletin 4 extensive optimization guide”

  1. Speed Up Your Forums vBulletin 4! says:

    […] : 17 Thanked: 2 Rep : 1 FractalizeR’s WebSite FractalizeR: VBulletin 4 extensive optimization guide MORE Optimizations! I recommend installing OptiPNG I've implemented some of them on […]

  2. VBulletin 4 extensive optimization guide says:

    […] we can tune up. Here is the full article, check it out and let us know if you have any questions. http://www.fractalizer.ru/frpost_429…ization-guide/ We recently updated the forums, it will take us a few days to get all the kinks out. If you do […]

Leave a Reply

You must be logged in to post a comment. Login now.