Create and Deploy a PHP Web Site and SQL Database using WebMatrix
This tutorial shows you how to use WebMatrix to develop and deploy a PHP application that uses a Windows Azure SQL Database to a Windows Azure web site. WebMatrix is a free web development tool from Microsoft that includes everything you need for web site development. WebMatrix supports PHP and includes intellisense for PHP development.
This tutorial assumes you have SQL Server Express installed on your computer so that you can test an application locally. However, you can complete the tutorial without having SQL Server Express installed. Instead, you can deploy your application directly to Windows Azure web sites.
Upon completing this guide, you will have a PHP-SQL Database web site running in Windows Azure.
You will learn:
- How to create a Windows Azure Web Site and a SQL Database using the Management Portal. Because PHP is enabled in Windows Azure Web Sites by default, nothing special is required to run your PHP code.
- How to develop a PHP application using WebMatrix.
- How to publish and re-publish your application to Windows Azure using WebMatrix.
By following this tutorial, you will build a simple Tasklist web application in PHP. The application will be hosted in a Windows Azure web site. A screenshot of the running application is below:

Prerequisites
-
Download the Tasklist application files from here: http://go.microsoft.com/fwlink/?LinkId=252504. The Tasklist application is a simple PHP application that allows you to add, mark complete, and delete items from a task list. Task list items are stored in a SQL Database (SQL Server Express for local testing). The application consists of these files:
- index.php: Displays tasks and provides a form for adding an item to the list.
- additem.php: Adds an item to the list.
- getitems.php: Gets all items in the database.
- markitemcomplete.php: Changes the status of an item to complete.
- deleteitem.php: Deletes an item.
- taskmodel.php: Contains functions that add, get, update, and delete items from the database.
- createtable.php: Creates the SQL Database table for the application. This file will only be called once.
-
Create a SQL Server database called tasklist. You can do this from the sqlcmd command prompt with these commands:
>sqlcmd -S <server name>\sqlexpress -U <user name> -P <password>
1> create database tasklist
2> GO
This step is only necessary if you want to test your application locally.
Create a web site and SQL Database
- Login to the Management Portal.
-
Click the + New icon on the bottom left of the portal.

-
Click WEB SITE, then CUSTOM CREATE.

Enter a value for URL, select Create a New SQL Database from the DATABASE dropdown, and select the data center for your web site in the REGION dropdown. Click the arrow at the bottom of the dialog.

-
Enter a value for the NAME of your database and select NEW SQL Database server. Enter a server login name and password (and confirm the password).Choose the region in which your new SQL Database server will be created.

When the web site has been created you will see the text Creating Web Site ‘[SITENAME]’ succeeded. Next, you will get the database connection information.
-
Click LINKED RESOURCES, then the database's name.

-
Click View connection strings.

From the PHP section of the resulting dialog, make note of the values for UID, PWD, Database, and $serverName. You will use this information later.
Install WebMatrix
You can install WebMatrix from the Management Portal.
-
After logging in, navigate to your web site's Quick Start page, and click the WebMatrix icon at the bottom of the page:

Follow the prompts to install WebMatrix.
-
After WebMatrix is installed, it will attempt to open your site as a WebMatrix project. You can choose to edit your live site directly or download a local copy. For this tutorial, select 'Edit local copy'.
-
When prompted to download your site, choose Yes, install from the Template Gallery.

-
From the available templates, choose PHP.

-
Select the Empty Site template. Provide a name for the site and click NEXT.

Your site will be opened on WebMatrix with some default files in place.
Develop your application
In the next few steps you will develop the Tasklist application by adding the files you downloaded earlier and making a few modifications. You could, however, add your own existing files or create new files.
-
With your site open in WebMatrix, add your application files by clicking Add Existing:

In the resulting dialog, navigate to the files you downloaded earlier, select all of them, and click Open. When prompted, choose to replace the index.php file.
-
Next, you need to add your local SQL Server database connection information to the taskmodel.php file. Open the taskmodel.php file by double clicking it, and update the database connection information in the connect function. (Note: Jump to Publish Your Application if you do not want to test your application locally and want to instead publish directly to Windows Azure Web Sites.)
// DB connection info
$host = "localhost\sqlexpress";
$user = "your user name";
$pwd = "your password";
$db = "tasklist";
Save the taskmodel.php file.
-
For the application to run, the items table needs to be created. Right click the createtable.php file and select Launch in browser. This will launch createtable.php in your browser and execute code that creates the items table in the tasklist database.

-
Now you can test the application locally. Right click the index.php file and select Launch in browser. Test the application by adding items, marking them complete, and deleting them.
Publish your application
Before publishing your application to Windows Azure Web Sites, the database connection information in taskmodel.php needs to be updated with the connection information you obtained earlier (in the Create a Windows Azure Web Site and SQL Database section).
-
Open the taskmodel.php file by double clicking it, and update the database connection information in the connect function.
// DB connection info
$host = "value of $serverName";
$user = "value of UID";
$pwd = "the SQL password you created when creating the web site";
$db = "value of Database";
Save the taskmodel.php file.
-
Click Publish in WebMatrix, then click Continue in the Publish Preview dialog.

-
Navigate to http://[your web site name].azurewebsites.net/createtable.php to create the items table.
-
Lastly, navigate to http://[your web site name].azurewebsites.net/index.php to launch the application.
Modify and republish your application
You can easily modify your application by editing the local copy of the site you downloaded earlier and republish or you can make the edit directly in the Remote mode. Here, you will make a simple change to the heading in in the index.php file and save it directly to the live site.
-
Click on the Remote tab of your site in WebMatrix and select Open Remote View. This will open your remote site for editing directly. 
-
Open the index.php file by double-clicking it. 
-
Change My ToDo List to My Task List in the title and h1 tags and save the file.
-
When saving has completed, click the Run button to see the changes on the live site. 
Next Steps
You've seen how to create and deploy a web site from WebMatrix to Windows Azure. To learn more about WebMatrix, check out these resources: