Udostępnij za pośrednictwem


Windows Azure Websites: A PHP Perspective

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 https://www.windowsazure.com, clicking Free trial, and following the sign-up steps.

image

2. Navigate to https://account.windowsazure.com/ and sign in with your Windows Azure account.

3. Click preview features to view the available previews.

image

4. Scroll down to Web Sites and click try it now.

image

5. Select your subscription and click the check.

image

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…

image

…select WEB SITE, then choose QUICK CREATE, CREATE WITH DATABASE, or FROM GALLERY

image

…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:

However, the short story is that after you create your website, you really just need to…

  • Click the cloud icon (image) to take you to your site’s quick start page, then click Set up Git publishing

image

-OR-

  • Get your FTP hostname and user name from your site’s DASHBOARD, then connect with your favorite FTP client…

image

Note: You might first have to click Reset deployment credentials to create a user name and/or create/reset your password.

image

-OR-

  • 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)…

image

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.)

image

  • 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).

image

 

  • 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.

image

 

  • App settings. You can set key-value pairs as application settings. These key-value pairs will be accessible in the $_SERVER variable.

image

 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.

image

 

  • Default documents. You can reorder default documents, and you can add new ones.

image

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:

ini_set(“display_errors”, “On”);

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:

image

However, you can also add additional metrics by clicking the ADD METRICS button at the bottom of the page:

image

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.

image

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

image

In this screenshot, you can see what scale-up options are available:

image

Regardless of whether you are in shared or reserved mode, you can scale out by using the slider to increase your instance count:

image

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.

Thanks.

-Brian

phpinfo.zip

Comments

  • Anonymous
    September 17, 2012
    So far I'm quite annoyed by how Azure handles PHP errors - despite setting display_errors, I still only get a very useless 500 error. Also the other error logs you can activate are not helpful for PHP at all.

  • Anonymous
    September 17, 2012
    @Goetz - I wonder if I can help. Can you share some of the code you are running? And maybe give me some idea of the type of error information you want to get, but aren't?

  • Anonymous
    September 18, 2012
    Hi Brian, Thanks for your reply! This evening I set up a local test environment and spotted that ini_set(“display_errors”, “On”); triggered a notice in PHP. I've now changed it to ini_set('display_errors', 'On'); Now I finally see PHP error messages on Azure :) Cheers, Goetz

  • Anonymous
    September 18, 2012
    Great! Glad I could be of help. ;-)

  • Anonymous
    November 06, 2012
    Great help to me, thank you. Best regards: Joyfax Server

  • Anonymous
    June 06, 2013
    you now have the ability to use user.ini files and access way more settings. PHP is also offered in 5.3 and 5.4 now. You also have the ability to turn on server monitoring that will give robust message better than php error messages.

  • Anonymous
    January 21, 2014
    FYI The list of disable_functions is empty by now!