PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/DevCenter/PHP/Tutorials/website-sqlazure-webmatrix.md

https://github.com/dominicbetts/azure-content
Markdown | 216 lines | 132 code | 84 blank | 0 comment | 0 complexity | 5bc535fb4dac0676963a9c7cd696e3ee MD5 | raw file
Possible License(s): CC-BY-3.0
  1. <properties umbracoNaviHide="0" pageTitle="PHP-MySQL Windows Azure Website using WebMatrix" metaKeywords="Windows Azure deployment, Azure deployment, Windows Azure Websites, Windows Azure SQL Database, SQL Database, PHP, WebMatrix" metaDescription="Learn how to create and deploy a PHP website and a SQL Database to Windows Azure using WebMatrix." linkid="dev-php-tutorials-sql-database-website-webmatrix" urlDisplayName="Create and Deploy a PHP Website and SQL Database Using WebMatrix" headerExpose="" footerExpose="" disqusComments="1" />
  2. #Create and Deploy a PHP Website and SQL Database using WebMatrix
  3. 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 Website. WebMatrix is a free web development tool from Microsoft that includes everything you need for website development. WebMatrix supports PHP and includes intellisense for PHP development.
  4. This tutorial assumes you have [SQL Server Express][install-SQLExpress] 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 Websites.
  5. Upon completing this guide, you will have a PHP-SQL Database website running in Windows Azure.
  6. You will learn:
  7. * How to create a Windows Azure Website and a SQL Database using the Preview Management Portal. Because PHP is enabled in Windows Azure Websites by default, nothing special is required to run your PHP code.
  8. * How to develop a PHP application using WebMatrix.
  9. * How to publish and re-publish your application to Windows Azure using WebMatrix.
  10. By following this tutorial, you will build a simple Tasklist web application in PHP. The application will be hosted in a Windows Azure Website. A screenshot of the running application is below:
  11. ![Windows Azure PHP Website][running-app]
  12. <div chunk="../../Shared/Chunks/create-account-and-websites-note.md" />
  13. ##Prerequisites
  14. 1. Download the Tasklist application files from here: [http://go.microsoft.com/fwlink/?LinkId=252504][tasklist-sqlazure-download]. 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:
  15. * **index.php**: Displays tasks and provides a form for adding an item to the list.
  16. * **additem.php**: Adds an item to the list.
  17. * **getitems.php**: Gets all items in the database.
  18. * **markitemcomplete.php**: Changes the status of an item to complete.
  19. * **deleteitem.php**: Deletes an item.
  20. * **taskmodel.php**: Contains functions that add, get, update, and delete items from the database.
  21. * **createtable.php**: Creates the SQL Database table for the application. This file will only be called once.
  22. 2. Create a SQL Server database called `tasklist`. You can do this from the `sqlcmd` command prompt with these commands:
  23. >sqlcmd -S <server name>\sqlexpress -U <user name> -P <password>
  24. 1> create database tasklist
  25. 2> GO
  26. This step is only necessary if you want to test your application locally.
  27. ## Create a website and SQL Database
  28. 1. Login to the [Preview Management Portal][preview-portal].
  29. 2. Click the **+ New** icon on the bottom left of the portal.
  30. ![Create New Windows Azure Website][new-website]
  31. 3. Click **WEB SITE**, then **CREATE WITH DATABASE**.
  32. ![Custom Create a new Website][custom-create]
  33. Enter a value for **URL**, select **Create a New SQL Database** from the **DATABASE** dropdown, and select the data center for your website in the **REGION** dropdown. Click the arrow at the bottom of the dialog.
  34. ![Fill in Website details][website-details-sqlazure]
  35. 4. Enter a value for the **NAME** of your database, select the **EDITION** [(WEB or BUSINESS)][sql-database-editions], select the **MAXIMUM SIZE** for your database, choose the **COLLATION**, and select **NEW SQL Database server**. Click the arrow at the bottom of the dialog.
  36. ![Fill in SQL Database settings][database-settings]
  37. 5. Enter an administrator name and password (and confirm the password), choose the region in which your new SQL Database server will be created, and check the `Allow Windows Azure Services to access the server` box.
  38. ![Create new SQL Database server][create-server]
  39. When the website has been created you will see the text **Creation of Web Site ‘[SITENAME]’ completed successfully**. Next, you will get the database connection information.
  40. 6. Click **LINKED RESOURCES**, then the database's name.
  41. ![Linked Resources][linked-resources]
  42. 7. Click **View connection strings**.
  43. ![Connection string][connection-string]
  44. 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.
  45. ##Install WebMatrix
  46. You can install WebMatrix from the [Preview Management Portal][preview-portal].
  47. 1. After logging in, navigate to your website's Quick Start page, and click the WebMatrix icon at the bottom of the page:
  48. ![Install WebMatrix][install-webmatrix]
  49. Follow the prompts to install WebMatrix.
  50. 2. After WebMatrix is installed, it will attempt to open your site as a WebMatrix project. When prompted to download your site, choose **Yes, install from the Template Gallery**.
  51. ![Download website][download-site]
  52. 3. From the available templates, choose **PHP**.
  53. ![Site from template][site-from-template]
  54. 4. The **Empty Site** template will be selected by default. Provide a name for the site and click **NEXT**.
  55. ![Provide name for site][site-from-template-2]
  56. Your site will be opened on WebMatrix with some default files in place.
  57. ##Develop your application
  58. 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.
  59. 1. With your site open in WebMatrix, click **Files**:
  60. ![WebMatrix - Click files][site-in-webmatrix]
  61. 2. Add your application files by clicking **Add Existing**:
  62. ![WebMatrix - Add existing files][add-existing-files]
  63. In the resulting dialog, navigate to the files you downloaded earlier, select all of them, and click Open. When propted, choose to replace the `index.php` file.
  64. 3. 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](#Publish) if you do not want to test your application locally and want to instead publish directly to Windows Azure Web Sites.)
  65. // DB connection info
  66. $host = "localhost\sqlexpress";
  67. $user = "your user name";
  68. $pwd = "your password";
  69. $db = "tasklist";
  70. Save the `taskmodel.php` file.
  71. 4. 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.
  72. ![WebMatrix - Launch createtable.php in browser][webmatrix-launchinbrowser]
  73. 5. 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.
  74. <h2 id="Publish">Publish your application</h2>
  75. Before publishing your application to Windows Azure Websites, the database connection information in `taskmodel.php` needs to be updated with the connection information you obtained earlier (in the [Create a Windows Azure Website and SQL Database](#CreateWebsite) section).
  76. 1. Open the `taskmodel.php` file by double clicking it, and update the database connection information in the `connect` function.
  77. // DB connection info
  78. $host = "value of $serverName";
  79. $user = "value of UID";
  80. $pwd = "the SQL password you created when creating the website";
  81. $db = "value of Database";
  82. Save the `taskmodel.php` file.
  83. 2. Click **Publish** in WebMatrix, then click **Continue** in the **Publish Preview** dialog.
  84. ![WebMatrix - Publish][publish]
  85. 3. Navigate to http://[your website name].azurewebsites.net/createtable.php to create the `items` table.
  86. 4. Lastly, navigate to http://[your website name].azurewebsites.net/index.php to being using the running application.
  87. ##Modify and republish your application
  88. You can easily modify and republish your application. Here, you will make a simple change to the heading in in the `index.php` file, and republish the application.
  89. 1. Open the `index.php` file by double-clicking it.
  90. 2. Change **My ToDo List** to **My Task List** in the **h1** tag and save the file.
  91. 3. Click the **Publish** icon, the click **Continue** in the **Publish Preview** dialog.
  92. 4. When publishing has completed, navigate to http://[your website name].azurewebsites.net/index.php to see the published changes.
  93. ## Next Steps
  94. 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:
  95. * [WebMatrix for Windows Azure](http://go.microsoft.com/fwlink/?LinkID=253622&clcid=0x409)
  96. * [WebMatrix website](http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200106398)
  97. [install-SQLExpress]: http://www.microsoft.com/en-us/download/details.aspx?id=29062
  98. [running-app]: ../Media/tasklist_app_windows.png
  99. [tasklist-sqlazure-download]: http://go.microsoft.com/fwlink/?LinkId=252504
  100. [install-webmatrix]: ../Media/install-webmatrix.png
  101. [download-publish-profile]: ../../Shared/Media/download_publish_profile.jpg
  102. [new-website]: ../../Shared/Media/new_website.jpg
  103. [custom-create]: ../../Shared/Media/custom_create.jpg
  104. [website-details-sqlazure]: ../Media/website_details_sqlazure.jpg
  105. [database-settings]: ../Media/database_settings.jpg
  106. [create-server]: ../Media/create_server.jpg
  107. [linked-resources]: ../Media/linked_resources.jpg
  108. [connection-string]: ../Media/connection_string.jpg
  109. [download-publish-profile]: ../../Shared/Media/download_publish_profile.jpg
  110. [webmatrix-templates]: ../../Shared/Media/webmatrix_templates.jpg
  111. [webmatrix-php-template]: ../../Shared/Media/webmatrix_php_template.jpg
  112. [webmatrix-php-emptysite]: ../../Shared/Media/webmatrix_php_emptysite.jpg
  113. [webmatrix-files]: ../../Shared/Media/webmatrix_files.jpg
  114. [webmatrix-delete-indexphp]: ../../Shared/Media/webmatrix_delete_indexphp.jpg
  115. [webmatrix-add-existing]: ../../Shared/Media/webmatrix_add_existing.jpg
  116. [webmatrix-launchinbrowser]: ../Media/launch-in-browser.png
  117. [webmatrix-publish]: ../../Shared/Media/webmatrix_publish.jpg
  118. [webmatrix-import-pub-settings]: ../../Shared/Media/webmatrix_import_pub_settings.jpg
  119. [webmatrix-pubcompat-continue]: ../../Shared/Media/webmatrix_pubcompat_continue.jpg
  120. [webmatirx-pubpreview]: ../../Shared/Media/webmatrix_pubpreview.jpg
  121. [preview-portal]: https://manage.windowsazure.com
  122. [sql-database-editions]: http://msdn.microsoft.com/en-us/library/windowsazure/ee621788.aspx
  123. [php-site-from-template]: ../../Shared/Media/php_site_from_template.png
  124. [php-empty-site-template-installed]: ../../Shared/Media/php_empty_site_template_installed.png
  125. [go-to-dashboard]: ../Media/go_to_dashboard.png
  126. [download-publish-profile]: ../Media/download-publish-profile.png
  127. [download-site]: ../Media/download-site-1.png
  128. [site-from-template]: ../Media/site-from-template.png
  129. [site-from-template-2]: ../Media/site-from-template-2.png
  130. [site-in-webmatrix]: ../Media/site-in-webmatrix.png
  131. [add-existing-files]: ../Media/add-existing-files.png
  132. [publish]: ../Media/publish.png