A Series of Three Parts: WordPress as a SaaS Platform, The Second Part
So, you’ve decided to use WordPress as your application platform. Where do you start so your application performs well and scales easily? Well, setting up an efficient WordPress application is one part web server configuration and two parts clever application software programming. Start with a good server architecture, well programmed plugins and themes, the right WordPress installation mode and you’ll set yourself up for success.
A good server setup is key to having a well-performing WordPress application. Shared hosting solutions you may normally use for a WordPress blog are not going to cut it for a full-blown application that thousands (and eventually hopefully millions) of users will be logging in and out of and interacting with on a regular basis. Your best bet is your own custom software stack, or a server, as a solution partner if you’re less command-line inclined (more on that later).
LNMP, is the new hotness. LAMP is old and busted.
For Hello Bar we set up a higher performance web server stack using an LNMP stack (Linux, nginx, MySQL and PHP-FPM). The combination of nginx and PHP-FPM (PHP as a FastCGI Processed Module) with APC installed make for a much faster, lighter weight, lower memory use server stack that can help you scale your application easily and not force you to scale as early. Iliya Polihronov recently gave a talk at WordCamp San Francisco about setting up a WordPress server using this stack and provided a great starter configuration for your own stack. EndofWeb also has a great article on setting up a full server stack from scratch using Ubuntu.
Cache ALL the Things!
When dealing with thousands of requests per second, caching is the best thing you can do to speed up your web application. With Hello Bar we utilized everything including APC for Object caching and for speeding up PHP in general, Batcache with Memcached to cache publicly viewed pages, and even file caching with nginx and URL rewrite capabilities. Caching systems that allow your web server to pull the cached data directly, avoiding PHP all together, provides a huge performance boost to those portions of your application. There are many caching technologies available and numerous caching plugins to help cache your web application. Just make sure that you are writing your application to clear caches when they need to be. You don’t want a list of entries a user has made to not update because the cache is stale.
All the Things! from Hyperbole and a Half
When running a high traffic site, one of the biggest bottlenecks becomes the database. Luckily, WordPress’ database library is implemented in a manner so that it can easily be adapted to a multiple database solution. By using the HyperDB database plugin, you can configure WordPress to distribute individual tables into multiple databases, provide failover support (when a connection to the database couldn’t be made), and most importantly, provide support for replicated MySQL database clusters. MySQL can be configured to automatically replicate any command sent to it to another MySQL database on another server. This creates two or more nearly real-time synced databases that WordPress can read from and serve back data. How To Forge has a good article about setting up a replicated MySQL server cluster and this article by HBY Consultancy also provides some good insight on getting this setup.
Ensuring Good Communication
Communication with your users is important and it’s most likely that you’ll be using email as your primary means of communication. To ensure the greatest deliverability of your email communications and to avoid a lot of the problems that go along with keeping your web server’s IP address white-listed, I recommend that you use an external email service. SendGrid is an easy to setup SMTP server that you can get integrated on the cheap. Just use a simple SMTP plugin such as WP Mail SMTP or WP SendGrid for your WordPress installation and all your wp_mail() function emails will be sent through the reliable, white-listed and stats rich SendGrid service instead of your own host’s email.
Setting up WordPress for Success
While a good hardware configuration makes for a solid foundation for your application, it won’t mean a thing with poorly configured software.
Intelligent and Picky Shopper from Shutterstock
Good Plugin Authoring
If you aren’t picky with the community plugins you choose to run or you’re a little sloppy with your own plugin authoring, then your WordPress application can become a bloated beast rapidly. Now, this doesn’t make WordPress a bad platform, it does however require some strategic planning when preparing it for use as an application platform due to its diverse community and ease of entry for plugin authorship. When authoring your own plugins and evaluating community plugins for use, consider some of the following practices:
- Use good programming techniques to keep your code as flexible as possible. Take advantage of the numerous functions WordPress has made available to you for querying posts, database querying, object caching and transient caching. All of these plugins are already setup to be adaptable and flexible for caching and scaling, no need to write it all from scratch.
- Take advantage of the numerous constants WordPress defines to make your plugin data dynamic and flexible. WP Engineer has a great article on some of the most useful constants you’ll want to take advantage of.
- Follow the Plugin Development Suggestions that the WordPress group provides in their codex.
- Make sure to validate your data. Poorly validated or non-validated data submission can be the death of your application. The WordPress Codex has a great article on how you should be approaching data validation and sanitization.
- The more files you load the larger your memory footprint. Try and load only those files that are necessary for your immediate needs. Take advantage of Class autoloading and PHP5’s magic methods for OOP development to load files only as they are needed.
Sometimes of course there will be plugins that you’ll absolutely want to use in your application, but you don’t need them to load everywhere. Check out something like the Disable Plugins MU plugin for a lightweight solution for filtering out plugins from being loaded where you don’t need them.
Pick the Right WordPress Operating Mode
Depending on the needs of your application, you may want to consider operating WordPress differently. WordPress out of the box is configured to run as a single website installation and this works fine for most situations. If your application is offering lots of user controlled, maintained, and published content, you may want to consider running WordPress Network. This will provide additional super-administrative controls to you as the application owner and provide a better setup for horizontal hardware scaling and opportunities for sharding your databases with the previously mentioned HyperDB or ShardDB.
Lever of Switching photo from Shutterstock
A lesser known mode for WordPress to operate in is a headless mode that gives you greater control on exactly what is being loaded and used to process your data. To enable this, just add define(‘SHORTINIT’, true); in your wp-config.php file before the require_once(ABSPATH . ‘wp-settings.php’); line. This will load the bare minimum of the WordPress system and give you better control of customizing what gets loaded. Check out this article on How WordPress Boots Up to get greater insight on what this constant does to your WordPress installation’s normal operating process to see if its the right move for your application.
Creating a high performance, WordPress-powered application is founded in careful planning and judicious selection of plugins from the community. So with the next WordPress app you build, roll up your sleeves and do some planning beforehand. Make sure the plugins you choose are of good quality, plan your plugin creation strategy for speed and modularity, and lay out your server setup for easy scaling and you’ll set yourself up for success.
Any good suggestions or techniques for setting up WordPress for speed and scalability that I didn’t cover? Contribute to the knowledge pool in the comment thread below!