Netling
Home » Devops » How to setup a WordPress blog with a Ruby on Rails web app?

How to setup a WordPress blog with a Ruby on Rails web app?

These days, a great blog alongside its main web app is a must have for any SaaS provider. While Ruby on Rails is best when it comes to building a web app, WordPress is number #1 when it comes to blogging.

These days, a great blog alongside its main web app is a must have for any SaaS provider. While Ruby on Rails is best when it comes to building a web app, WordPress is number #1 when it comes to blogging.

Why WordPress on Rails?

A blog can definitely be built using Rails itself. But WordPress is already so well optimised and ready to use in a few clicks, that you don’t need to do code a blog from scratch.

Building your own blog solution, or using some existing gems, might still be the best option if you want an entire Rails integration (login, etc…). Or maybe you particularly enjoy doing it. Or maybe you require a very simple version of a blog. Otherwise, and the vast majority of case, it is just unnecessary additional work. WordPress on Rails is the best solution for these cases.

Best WordPress + RubyOnRails Setup

WordPress & Rails don’t usually play nice together. So the best option is host RubyOnRails web app on your root domain and host WordPress blog on a subdomain.

For example:

  • A Ruby on Rails app can be hosted on Heroku: https://www.awesomeapp.com
  • A WordPress blog can hosted on WPEngine: https://blog.awesomeapp.com

You can obviously host both main and sub-domain on single server as well. Netling’s website and client login is built on RubyOnRails and our blog is running on WordPress hosted on a single DigitalOcean VPS instance managed with Plesk Onyx AdminPanel.

However, this is not an ideal situation. For maximum search engine optimisation you want your blog to be accessed as a subdirectory — not as a subdomain. So essentially you want to achieve something like this:

  • A Ruby on Rails app hosted on Heroku: https://www.awesomeapp.com
  • A WordPress blog hosted on WPEngine: https://www.awesomeapp.com/blog

This brings us to the main question,

How to host a RubyOnRails app on a root domain and a Wordpess blog on a subdomain and still be able to access blog as a sub-directory?

The answer is, put WordPress blog behind a reverse proxy!

Setup root domain server hosting RubyOnRails app as reverse proxy to forward all requests made for your blog to subdomain running WordPress. For example, setup https://www.awesomeapp.io as a reverse proxy to forward any requests made to https://www.awesomeapp.com/blog to https://blog.awesomeapp.com.

Typically, this requires changing configuration of your Apache or NGINX instance, but we will use a handy rails gem to achieve the same results – rack-reverse-proxy .

On your Rails app

Add the rack-reverse-proxy gem to your gemfile:

gem 'rack-reverse-proxy', :require => 'rack/reverse_proxy'

Then, you need your Rails app to redirect /blog/ to your WordPress blog on subdomain. Go to config.ru and add the following code:

use Rack::ReverseProxy do
reverse_proxy(/^\/blog(\/.*)$/, 'https://blog.awesomeapp.com$1', opts = { preserve_host: true })
end

Note: Make sure you don’t have a trailing “/” in your URL. It should be https://blog.awesomeapp.com$1 and not https://blog.awesomeapp.com/$1

Since the blog is technically hosted at /blog/, we need to forward the /blog requests there (notice the non-trailing slash).

Add to config/routes.rb:

get "/blog", to: redirect('https://www.awesomeapp.com/blog/', status: 301)

On your WordPress blog

In your wp-config.php add following lines:

/**
* Handle SSL reverse proxy
*/
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
$_SERVER['HTTPS']='on';

if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}

$_SERVER['REQUEST_URI'] = "/blog".$_SERVER['REQUEST_URI'];

Conclusion

Voilà, you have your beautiful WordPress blog alongside your powerful Ruby on Rails app.

Thanks for reading! I will keep it updated and add more relevant info in it as needed. If you need any help in getting this setup, don’t hesitate to tweet me: @netlings.

Dipesh Batheja

Dipesh is a netling working as a Marketing Head at Netling. He loves to explore cutting edge technology and share his write-ups through this blog. An avid learner, RubyOnRails lover, passionate technology enthusiasts, and interested in everything tech.