screens

PHP Server redirect

After a couple months of moving, putting together IKEA furniture and reconfiguring servers and devices there is still one thing missing; a static IP. I am currently running OpenProject as my choice of project management tool. The challenge of using this is that it runs on Ruby, so it has to be hosted on one of my servers and these are placed in my apartment at the moment.

Normally, when having a static IP you would simply add a CNAME record for the IP and it would have been working as intended. At the moment this is unfortunately not the case. So how do we get around this?

In my case, only HTTP needs to be supported at the moment so the solution was making a simple PHP script handling the redirection. This stores the server IP on request and redirects everything else. It also handles subdirectories and GET requests correctly.

To use the script, create a folder with the desired name of your server(server1.your-domain.com) would be achieved by creating a folder named server1 in the root directory of your web host. Then all you have to do is extract the source in that directory and create a cron job on your server for updating the server IP. Remember to update your auth token in index.php.

Cron Job setup

Append the following content to your crontab:

*/10 * * * * wget -q -O /dev/null "http://<folder-name>.your-domain.com/?action=set&auth=<your-auth-code>"

Save and exit.

Example usage

Manually update your server IP to your current public IP
http://server1.your-domain.com/?action=set&auth=eVZKwTTFYASm6orAgzjm
Retrieve the server IP in a browser
http://server1.your-domain.com/?action=get
Get your server IP in a terminal
$ echo Server ip is: $(curl -sL http://server1.your-domain.com/?action=get)

Get the code

You can either download the source, or browse it on the wiki.

Leave a Reply

Your email address will not be published. Required fields are marked *