Ever think about hosting WordPress sites on Heroku? Think again. It’s not as easy as deploying Rails applications. In fact, it’s a headache. Besides the limits you find in the official document, I am showing you some of my thoughts on Heroku WordPress hosting.
Heroku is great for prototyping Rails apps. But, for #WordPress sites? No, a big no. Click To Tweet
Heroku’s Ephemeral Filesystem
Each dyno(Heroku’s term on its servers) gets its own ephemeral file system. When you uploaded an image, the image is stored on one of the dyno only. The images will be erased after dyno restart. Worse still, Dynos crash if you try to write to its file systems.
Here comes the solution: Offload media files to Amazon S3. Delicious Brains have written an excellent plugin for hooking S3 into WordPress’ media library seamlessly.
Besides, you need to stop editing the source code from WP admin. Add these two lines into wp-config.php
define('DISALLOW_FILE_EDIT', false);
define('DISALLOW_FILE_MODS', false);
Also, auto WordPress core / theme / plugin updates should be disabled. This handy plugin does the job. Managing the updates by Git or Dropbox is necessary.
Moreover, the ephemeral filesystem might break your caching and minifying plugins. You have to Batcache with the Memcachier add-on, or plugins that write to an external server.
Heroku doesn’t Send Emails
To send emails from Heroku dyno, you must use a SMTP service as Mandrill or SendGrid. Both of them provide similar feature sets and official WordPress plugins (wpMandrill and SendGrid). Just pick one and stick to it.
If you prefer a lightweight solution than the official plugins. You can config WordPress to use the SMTP service in less than 10 lines.
add_action('phpmailer_init', 'wph_phpmailer_init');
function wph_phpmailer_init( PHPMailer $phpmailer )
{
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true; // enable SMTP authentication
$phpmailer->Port = 587; // set the SMTP server port
$phpmailer->Host = 'smtp.mandrillapp.com'; // SMTP server
$phpmailer->Username = 'MANDRILL_USERNAME'; // SMTP server username
$phpmailer->Password = 'MANDRILL_API_KEY'; // SMTP server password
}
Default Builtpack Sucks
Unlike installing LEMP on VPS, every time you deploy new code, Heroku compile all dynos from scratch. The collection of shell scripts to run on every dyno boot-ups called a buildpack.
Heroku provides a default PHP builtpack. Although the default one gives you Apache, Nginx, PHP FPM and HHVM, it is not optimized for WordPress.
Making a custom buildpack isn’t a easy task. You can’t SSH into the dynos. Every changes on buildpacks’ source code takes a long time to re-compile.
Deploy WordPress to Heroku in < 60 seconds or your money back
No worries. If you really have to, Marc Chung and Matt Hoofman have 2 different buildpacks ready for you. Be sure to read their READMEs. They comes with some assumptions, such as file structures, pre-installed plugins, even database selections. Reminder: You should fork and modify the buildpacks to update everything.
Heroku doesn’t Provide MySQL
Unlike setting LEMP up on VPS, the database isn’t installed on the dynos. Heroku Postgres, ClearDB and Amazon RDS suit your needs.
Heroku Postgres which is the SQL database service run by Heroku. First 10k rows are free. Comes with free auto backup service. However, you must have PostgreSQL for WordPress (PG4WP) installed to rewrite MySQL specific queries to generic ones on the fly when needed.
WordPress with PG4WP is expected to be slower than the original WordPress with MySQL because PG4WP does much SQL rewriting for any page view
If you prefer to stay with the WordPress default database engine, ClearDB is the only MySQL database service available on Heroku add-on marketplace. Free plan provides 5MB database size. Barely enough for testing.
Beyond the Heroku add-on marketplace, my choice is Amazon RDS. Extra steps to set up. However, free tier gives you a micro instance with 20GB storage.
Heroku Speeded Up WordPress
Moving to Heroku from DigitalOcean did speed up my site by around 13%. Note that this is not a scientific comparison.
Before (on DigitalOcean):
After (on Heroku):
Marc Chung also have some benchmarks on his WordPress buildpack. Page loads within a second. In his word:
How fast is this? Pretty freaking fast.
— Marc Chung
Heroku does No Auto-scaling
It is common to think a cloud hosting service like Heroku would provide native support on auto-scaling. Sadly, it is not the case. It can’t do load-base auto-scaling like Amazon does.
Luckily, Adept Scale add-on gives you some power over auto-scaling.
Mobile App
Although Heroku haven’t release an official mobile app, Nezumi lets you manage Heroku apps on the go It does capable of scaling dyno sizes.
Yes! The top reason you want a mobile app for Heroku.
Cost Goes Up Quickly
Keep in mind that you want more than one dyno. Heroku restart dynos at least once per day even though the dynos are healthy. Thus, single dyno apps are guaranteed to have downtime daily.
Heroku gives you a 1x dyno (512MB ram) for free. Then, $7 monthly for a never sleep hobby dyno. Standard dynos start with $25 monthly.
Do consider the add-on costs before migrating to Heroku. The costs of database (ClearDB), memcached (Memcachier), logging (Papertrail), monitoring (New Relic) add-ons rocket up along with your dyno size.
Bottom Line: Do I Recommend Heroku for WordPress Hosting?
Unfortunately, not at this moment. In fact, I moved out of Heroku to Amazon OpsWork after 2 weeks. Back to DigitalOcean now.