The spring update to Windows Azure was big. Of all the updates, one of the most interesting is the addition of Windows Azure Websites. Essentially, Windows Azure now supports the ability to quickly and easily deploy ASP.NET, Node.js and PHP web-sites to a highly scalable cloud environment that allows you to start small (and for free) and then scale up as your traffic grows. There is already lots of great content out there that covers how to use Windows Azure Websites. However, what I want to do in this post is consider what Windows Azure Websites looks like through a PHP lens. With that said, much of this post is useful regardless of your programming language of choice.
I’ll point out what I think is the biggest improvement first: nearly instant deployment time. If you have deployed PHP code to Windows Azure Web Roles and Worker Roles in the past, you have had to wait 10 to 15 minutes while new VMs were provisioned, configured, started, etc. With Windows Azure Websites, you can see your code in action within seconds (and you have several choices for how you deploy your code). Of course, that isn’t the only improvement. Read on to find out more…
How do I get access to Windows Azure Websites?
1. If you don’t have a Windows Azure account, you’ll need to create one (if you do, skip to step 2). You can get a free account by browsing to http://www.windowsazure.com, clicking Free trial, and following the sign-up steps.
2. Navigate to https://account.windowsazure.com/ and sign in with your Windows Azure account.
3. Click preview features to view the available previews.
4. Scroll down to Web Sites and click try it now.
5. Select your subscription and click the check.
How do I create a website?
There is already a lot of good content out there that walks you through creating a website. Here’s one that I’ll suggest: Create a PHP-MySQL Windows Azure web site and deploy using Git. However, the short story is simply this: Navigate to the management portal, click +New button on the bottom of the page…
…select WEB SITE, then choose QUICK CREATE, CREATE WITH DATABASE, or FROM GALLERY…
…and follow the steps.
How do I deploy my code?
You have 3 options for deploying PHP code to a Windows Azure Website: Git, FTP, and Web Deploy. WebMatrix allows you to deploy directly from the IDE, using either FTP or Web Deploy under the covers. There are already several good tutorials that cover the details for each of these options:
- Create a PHP-MySQL Windows Azure Website and deploy using Git
- Create a PHP-MySQL Windows Azure Website and deploy using FTP
- Create and deploy a PHP-MySQL Windows Azure Website using WebMatrix
However, the short story is that after you create your website, you really just need to…
- Click the cloud icon () to take you to your site’s quick start page, then click Set up Git publishing…
- Get your FTP hostname and user name from your site’s DASHBOARD, then connect with your favorite FTP client…
Note: You might first have to click Reset deployment credentials to create a user name and/or create/reset your password.
- Click WEBMATRIX at the bottom of your site’s DASHBOARD to open your site (publish settings included) in WebMatrix (then you can just click Publish from within the IDE)…
How do I configure a site?
The CONFIGURE tab lets you configure several options for your site. The most useful options for PHP sites are the following:
- PHP Version. PHP is enabled by default. (See How do I configure PHP? below for more detail.)
- Hostnames. You can add additional hostnames for your site. However, this option is only available if you are running on reserved instances (see How do I scale a site? below).
- Diagnostics. These options are turned off by default. By turning them on, you have access to useful server information, which I’ll show you how to get in How to I diagnose issues? below.
- App settings. You can set key-value pairs as application settings. These key-value pairs will be accessible in the $_SERVER variable.
One use case for these settings might be to store database server type and name, or username and password. Then you could change them on the fly without having to redeploy your application.
- Connection strings. If you have set up a database as a linked resource for your website, you can view your connection string.
- Default documents. You can reorder default documents, and you can add new ones.
How do I configure PHP?
As mentioned above, PHP is enabled in Windows Azure Websites by default. However, as with most web hosting, you don’t have full control over the PHP configuration. Right now, PHP 5.3.13 is the only version that is available, and you don’t have access to the php.ini file, so you can’t changes to master configuration values. And, as of this writing, you don’t have the ability to enable/disable extensions. (I’ve attached to this post a .htm file with the output of phpinfo() from a Windows Azure Website so can see all the PHP configuration details.) However, the good news is that you can use the ini_set function to set local configuration values. I’ve found this very helpful during development:
This won’t work for all configuration settings, but it does work for many. For information about what you can set with the ini_set function, see List of php.ini directives.
The Windows Azure Website team is currently thinking about different ways to make PHP configuration/customization more flexible. If you want to share your thoughts, please leave them in the comments and I will make sure the team hears them.
How do I monitor a site?
The Monitor tab gives you both graphical and numerical representations for several key metrics by default:CPU time, requests, data out, data in, and HTTP server errors:
However, you can also add additional metrics by clicking the ADD METRICS button at the bottom of the page:
How do I diagnose issues?
In order to diagnose issues, you need to make sure you have configured your site so that you have access to helpful information. As I mentioned earlier (in How do I configure a site?), turning diagnostics on for Web Server logging, detailed error messages, and failed request tracing is one step toward getting helpful information. After you have turned on those diagnostics, you can use your favorite FTP client to browse and download relevant files.
And, also as I mentioned earlier, using the ini_set function can be helpful in displaying PHP errors (since display_errors isn’t set to On globally).
How do I scale a site?
Windows Azure Websites allows you to scale up (i.e. move to heftier machines) and to scale out (i.e. add instances). You can do either or both with a few clicks on the SCALE tab.
To scale a site up, you will need to switch your site to reserved instances (instead of shared). And, as the note in the screenshot shows, this will come with a cost. Details about pricing are here: https://www.windowsazure.com/en-us/pricing/details/#web-sites
In this screenshot, you can see what scale-up options are available:
Regardless of whether you are in shared or reserved mode, you can scale out by using the slider to increase your instance count:
Note: In the Free Trial, you get up to 10 free websites (in shared mode). Instances do not count toward that free limit. So, you can have 10 sites each running 3 instances.
That’s it! Give Windows Azure Websites a try and let us know what you think.