How to have a your /blog hosted on a different IP

Well, I just set up this blog, so let’s talk about it. My whitehat SEO sources tell me that a blog on swish.com/blog is more valuable than one on blog.swish.com. The problem is, swish.com is running a Bitnami image on EC2, and I don’t want to install wordpress when I can just spin up a WordPress Stack image.

Step 1: Configure the blog server

Ok, so I spun up a server on 50.112.240.211. It works, but all the content is at 50.112.240.211/wordpress/xxx. This won’t do, I want it at the root. Let’s edit ~/apps/wordpress/conf/wordpress.conf. The file originally contained

    # Uncomment the following lines to see your application in the root
    # of your URL. This is not compatible with more than one application.
    #RewriteEngine On
    #RewriteRule ^/$ /wordpress/ [PT]

But it’s not enough to uncomment those lines; that only makes / redirect to /wordpress/. Instead, I redirect all traffic to /wordpress/ with:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/wordpress.*$
    RewriteRule ^(.*)$ /wordpress/$1 [PT,QSA]

and sudo ~/stack/ctlscript.sh restart apache to restart the blog server. The PT is critical because /wordpress/ is an alias defined earlier in the file. The RewriteCond ensures that the rule doesn’t get applied more than once.

The blog works, but all the automatically generated links have /wordpress/ in them, which is ugly.  I edited ~/apps/wordpress/htdocs/wp-config.php from

    define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress');
    define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress');

to
    define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
    define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);

For me, editing this file in emacs changed the group of the file from daemon to bitnami. I had to change it back with sudo chgrp daemon /home/bitnami/apps/wordpress/htdocs/wp-config.php. Now the generated links look like http://50.112.240.211/about-us/ — no /wordpress/ !

Step 2: Forward traffic

One way to forward traffic would be a single DNS A record for blog.swish.com. If only it could have been so easy. But the next easiest way to do this is with Apache’s mod_proxy. Thankfully, LAMPStack comes with mod_proxy and mod_proxy_http already installed. Back on swish.com, I created a file with touch ~/stack/apache2/conf/extra/httpd-proxy.conf and added

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyPass /blog http://50.112.240.211
    ProxyPassReverse /blog http://50.112.240.211

ProxyRequests Off is an extra bit of protection against accidentally being an unsecured relay. ProxyPreserveHost On is necessary so that $_SERVER['HTTP_HOST'] above actually returns swish.com. Finally, I added a line to ~/stack/apache2/conf/httpd.conf

    Include conf/extra/httpd-proxy.conf

and sudo ~/stack/ctlscript.sh restart apache to restart the swish.com server.

Step 3: Fix links again

Remember how I made the links all pretty in step 1? Well, I needed to mangle them again to point to swish.com/blog/xxx. Changed ~/apps/wordpress/htdocs/wp-config.php to

    define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/blog');
    define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] . '/blog');

Reflections

If I just wanted to get this done quickly, I probably should have just renamed the wordpress folder to blog. That would have cut out a lot of grief. But devops isn’t about the destination so much as the journey, don’t you think?

-Brad

Discuss this post on Hacker News