For a couple years now, Iâ€™ve been paying for a private-label URL-shortening service.Â Iâ€™ve amassed hundreds of links, and donâ€™t want to lose any of the shortcuts, but the cost has gone up with no change in features.Â I would love to have better metrics, and certainly a lower cost.Â Having a small Azure benefit gives me a level of free hosting with an Azure website.Â I hunted around and found several open source URL shorteners.Â I decided on using YOURLS, a PHP-based application with all the features I want and then some (nice charts, plugins, social sharing, an API and bookmarklets).Â Plus, it looked insanely easy to install.Â Hereâ€™s how I set up YOURLS on Azure (this blog post took longer and is more difficult to read than the actual process, it was that easy).Â There are a couple paths you can follow here, yours may differ from mine since I already had some existing services set up.
Step 1: MySQL on Azure
YOURLS uses MySQL as its database.Â There are two primary ways you can host MySQL on Azure:
- ClearDB offers hosted MySQL databases on Azure, with a 20 MB developer instance for free.Â This is plenty to play around with, but if you want more you can.Â There are two ways to set up CleaDB on Azure: How to Create a MySQL Database in Windows Azure.
- Spin up a VM (Linux or Windows) and host a MySQL instance on it.Â This is probably the more expensive option, but gives you the utmost control.Â If you wanted to use this VM to host your YOURLS, you could, but thatâ€™s another blog post.
I already had my developer instance set up from an earlier WordPress experiment.Â I created this instance as a linked resource for Windows Azure Web Site previously, which is an easy way to get started, but limits you to the 20 MB limit.
Either way, follow one of the sets of instructions at http://www.windowsazure.com/en-us/develop/php/common-tasks/create-mysql-database/ to get started.
If youâ€™re sharing a MySQL database, by default the Yourls tables are created with yourls_ prefix, so you can separate new tables from existing ones.
Step 2: Creating the Web Site
As with the MySQL database, you can do this in one of two ways.Â If you have already followed, or plan to follow, the instructions for creating a MySQL database as a linked resource, use that and skip this step.Â In that process, youâ€™ll create a site in the Azure portal, then create a CleaDB MySQL instance linked to the site.
If you have a existing MySQL database, but need to create the website, you can create the site from within WebMatrix 3, which is what I did (if you donâ€™t have WebMatrix 3, you will need to update to the latest version).
From the start screen, New >> Empty Site starts the process.
The next step is to find a unique name for your site, and a location to host it.Â At this step youâ€™re configuring the long name for your site.Â Later you can configure a custom URL for your website.
A local and remote site are created
At this point, youâ€™re read to start working.Â Youâ€™ll be working in a local version of your website, not directly in the website.
Step 3: Installing and Configuring YOURLS
To get started this way, download the most recent version from https://github.com/YOURLS/YOURLS/tags (cloning and publishing via Git will be discussed in another post).Â Unzip the download into the folder for the website created above.
Configuration options are explained at http://yourls.org/#Config.Â The main ones to configure are:
- YOURLS_DB_PASS (remember this will be stored in plain text)
- YOURLS_DB_PREFIX (this is how you can separate YOURLS tables from any others in the same MySQL database)
- YOURLS_SITE (use the temporary URL until DNS propagates)
- YOURLS_COOKIEKEY (generate at http://yourls.org/cookie)
- $yourls_user_passwords (this is how youâ€™ll log into the admin portal, you can encrypt these per https://github.com/YOURLS/YOURLS/wiki/Username-Passwords).
Do not put the config file in a publicly available location!Â You have secrets in this file, make sure it stays private.
In order to view admin pages, youâ€™ll also need to add a web.config to the root folder of your YOURLS site; see https://github.com/YOURLS/YOURLS/wiki/Web-Config-IIS for a sample file.
Step 4: Publishing to Azure and Installing Database Tables
If you used WebMatrix to create your Azure Web Site, all you need to do now is click Publish and your files will be transferred automatically.Â If you created your database and site via the Azure Portal, youâ€™ll be prompted to either choose an existing Azure Web Site, use WebDeploy, or manually configure FTP.Â This is a one-time configurationâ€”every subsequent time you can just hit Publish.
After the site is deployed, there is a one time installation.Â Simply go to http://<yoursite>.cloudapp.net/admin, log in with the credentials you saved in the config file, and follow whatever prompts youâ€™re given.
Step 5: Custom URL on Azure
Custom URLs for Azure Web Sites are not technically free, but you can apply Azure credits via an MSDN subscription (which I do) or pony up for a paid subscription.Â Instructions for configuring a custom domain name for an Azure Web Site are at http://www.windowsazure.com/en-us/develop/net/common-tasks/custom-dns-web-site/.
Youâ€™ll need to have custom DNS for your site, usually this is done by using your registrarâ€™s nameservers and custom DNS settings.Â If your registrar doesnâ€™t offer such a service, look into a service like https://dnsimple.com/.Â Youâ€™ll need to configure both a CNAME and an A record.Â Do not forward your domain name.Â After you configure your DNS, itâ€™ll take a day or so to propagate completely.Â Once the DNS is propagated, youâ€™ll need to edit the config file and set the YOURLS_SITE to the custom domain name and republish.
Step 6: Track those clicks!
You should be fully running, you can extend YOURLS with some of the plugins found at https://github.com/YOURLS/YOURLS/wiki/Plugin-List.