How to install Wordpress within Ruby on Rails app

In this post I show you how quick and easy it is to configure your apache server to handle two (or more) separate applications at the same location using a proxy module.

I’m describing here the simplest, cheapest and fastest solution which makes sense in a low to medium traffic sites. If you have a high volume of users using your site, you may need to consider more optimal solutions (see some suggestions at the end of this article).

The goal is to have the Rails app exactly in the same location as the Wordpress installation. It has tremendous influence on the SEO of your site — Wordpress pages can provide the indexable content while the Rails app can provide the functionalities users are looking for.

The requirements

As an example, I’ll show you the set up for a simple website, where the Rails application serves as a management panel for users to manage their data and the Wordpress installation will provide static content and a blog.

  • Ruby on Rails app running in the root of your domain so you can use it to handle the management panel URLs: /login, /register, /manage
  • Wordpress pages located in the root as easily-editable static pages, / (home page), /about-us, /privacy-policy and a list of posts under /blog

The solution

Although it may seem that Wordpress and a Rails app are hard to maintain together on a single server, especially having Rails app pages mixed together with Wordpress pages, there’s an easy solution for that.

The keyword here is a Proxy configured on your web server so it handles the traffic and decides where to redirect each request. You can set it up on both Apache server and an Nginx server via their proxy modules: Apache mod_proxy and Nginx http_proxy_module.

I’ll show you how to use the Apache mod_proxy in a few simple steps using the practical situation I described above. The setup is very similar with nginx.

1. Make sure you have the module enabled.

a2enmod proxy proxy_http

2. Install Wordpress.

Run a standard Wordpress installation on the Apache server — exactly the same server where you’re going to set up the proxy.

The location of the installation on the server doesn’t matter at all. It’s important though to configure your virtual host so that your domain will point to the Wordpress installation directory.

3. Set up your Rails application.

The next thing is to install and configure your Rails application on the same server as the Wordpress you just installed. For the Rails app, you can use webserver of your choice: Phusion Passenger, Puma, whatever. Just make sure it serves the application under a different port than the Wordpress installation — e.g. Wordpress on port 80, Rails app on port 3000. It’s recommended to install it in a separate directory, and the server preferably running as a different Linux user.

4. Set up the proxy rules.

This is the most important step. Here we do the magic:

ProxyPassMatch "^/manage/(.*)$" "$1"
ProxyPass "/login" ""
ProxyPass "/register" ""

And that’s it! You can see we’ve only defined proxy rules for Rails application URLs. All the Wordpress requests will be handled by the Apache server itself, so they don’t require any proxy.

ProxyPassMatch will pass the requests basing on the regex match, so in our case it will pass all the requests starting with /manage. ProxyPass is a simple match, so only the queries with exact URLs will be passed.

A few ideas for optimizing the setup

The proxy module has a lot of options and allows us to set up the server configuration in any way we want. As you can see in the code above, the module doesn’t require the apps to be on the same server — you just provide the destination address for matched URLs and the rest is handled with grace by the module. So you can try:

  • Each app on a separate server, with a separate IP, with proxy server also on a separate server. This setup is very flexible and easily extensible and allows you to optimize each server’s configuration for specific requirements of each app.
  • Load balancing — by installing and configuring mod_proxy_balancer, the proxy server can also serve as a load balancer for the apps behind it. Having a load balanced traffic allows you to easily take down and replace servers without interrupting the service for the users.
  • Switch to Nginx or use it together with Apache. Nginx has been designed for speed and high concurrency and excels in certain scenarios, like serving static content or passing the requests to other servers.