Adding a WordPress Blog to Your Rails App

If you’re running a rails application with a blog like I was, you we’re probably using something that utilized jekyl. There are some good blogging platforms out there and when I decided to add a blog to my site, I decided to use toto. At the time I thought adding all the html tags myself would be fun and pushing through github was cool but what really ended up happening was that I pushed off writing because it seemed like a chore. And the articles that I did publish were getting less than the expected coverage since publishing with SEO in mind is an extra added layer of work.

Since I’ve been working with WordPress for a decade now, I know it like the back of my hand. And although I have gripes with the platform, the “built for publishing” aspect of it makes it worth switching over. Plus you get tons of SEO juice with a few plugins and configurations. Hopefully, I’ll write a bit more now that I made the switch.

Here’s a quick guide to add WordPress to your rails app in the path /blog:

If you’ve read my post on adding ssl to your rails app and you have a similar nginx configuration, you’ll want to add some rules to use FastCGI to process incoming requests.

If you’ve downloaded php, mysql, and all the wordpress files and added it to a directory separate from your rails files like I did, add the following to your nginx.conf file:

  location ^~ /blog {
    root /var/www/boopis;
    index index.php index.html index.htm;
    try_files $uri $uri/ /blog/index.php;

    location ~ \.php$ {
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
    }
  }

Make sure you add this before your settings to have unicorn handle any of your requests or they will default to unicorn and redirect to your home page if you have a setting in your routes file to redirect since rails is taking over. Define the path of your wordpress files, make sure that index.php files are searched for, and make sure that you FastCGI can find the proper port to call php-fpm (in my case, I had to open /etc/php5/fpm/pool.d/www.conf and look for the listen= which was different from the typically recommended /var/run/php5-fpm.sock path)

Next, deploy your rails app and if you don’t have your cap file set to restart nginx, you’ll have to ssh into your server and run sudo service nginx restart. Then start your unicorn process again (in my case it is /etc/init.d/unicorn_boopis start).

Now you should have the wordpress installation appear in the path /blog on your domain!

Here’s a link to the conf file.