PageRenderTime 70ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/boomdelicious/azure-content
Markdown | 223 lines | 134 code | 89 blank | 0 comment | 0 complexity | a897d19b9f54c94632dee21b170da17b 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, MySQL, PHP, WebMatrix" metaDescription="Learn how to create and deploy a PHP-MySQL website to Windows Azure using WebMatrix." linkid="dev-php-tutorials-mysql-website-webmatrix" urlDisplayName="Create and Deploy a PHP-MySQL Windows Azure Website Using WebMatrix" headerExpose="" footerExpose="" disqusComments="1" />
  2. #Create and deploy a PHP-MySQL Windows Azure Website using WebMatrix
  3. This tutorial shows you how to use WebMatrix to develop and deploy a PHP-MySQL application 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 [MySQL][install-mysql] installed on your computer so that you can test an application locally. However, you can complete the tutorial without having MySQL installed. Instead, you can deploy your application directly to Windows Azure Websites.
  5. Upon completing this guide, you will have a PHP-MySQL website running in Windows Azure.
  6. You will learn:
  7. * How to create a Windows Azure Website and a MySQL 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. ##Prerequisites
  13. 1. Download the Tasklist application files from here: [http://go.microsoft.com/fwlink/?LinkId=252506][tasklist-mysql-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 MySQL database. The application consists of these files:
  14. * **index.php**: Displays tasks and provides a form for adding an item to the list.
  15. * **additem.php**: Adds an item to the list.
  16. * **getitems.php**: Gets all items in the database.
  17. * **markitemcomplete.php**: Changes the status of an item to complete.
  18. * **deleteitem.php**: Deletes an item.
  19. * **taskmodel.php**: Contains functions that add, get, update, and delete items from the database.
  20. * **createtable.php**: Creates the MySQL table for the application. This file will only be called once.
  21. 2. Create a local MySQL database called `tasklist`. You can do this from the MySQL command prompt with this command:
  22. mysql> create database tasklist;
  23. This step is only necessary if you want to test your application locally.
  24. <h2 id="CreateWebsite">Create a Windows Azure Website and MySQL database</h2>
  25. ### Create a Windows Azure account
  26. <div chunk="../../Shared/Chunks/create-azure-account.md" />
  27. ### Enable Windows Azure Web Sites
  28. <div chunk="../../Shared/Chunks/antares-iaas-signup.md" />
  29. ### Create a website and MySQL database
  30. 1. Login to the [Preview Management Portal][preview-portal].
  31. 2. Click the **+ New** icon on the bottom left of the portal.
  32. ![Create New Windows Azure Website][new-website]
  33. 3. Click **WEB SITE**, then **CREATE WITH DATABASE**.
  34. ![Custom Create a new Website][custom-create]
  35. <div class="dev-callout">
  36. <b>Note</b>
  37. <p>In the preview release of Windows Azure Websites, you cannot create a MySQL Database for a website after creating the website. You must create a website and a MySQL database as described in the steps below.</p>
  38. </div>
  39. 4. Enter a value for **URL**, select **Create a New MySQL 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.
  40. ![Fill in Website details][website-details]
  41. 5. Enter a value for the **NAME** of your database, select the data center for your database in the **REGION** dropdown, and check the box that indicates you agree with the legal terms. Click the checkmark at the bottom of the dialog.
  42. ![Create new MySQL database][new-mysql-db]
  43. When the website has been created you will see the text **Creation of Web Site [SITENAME] completed successfully**.
  44. Next, you need to get the MySQL connection information.
  45. 6. Click the name of the website displayed in the list of websites to open the websites Quick Start page.
  46. ![Open website dashboard][go-to-dashboard]
  47. 7. Click the **CONFIGURE** tab:
  48. ![Configure tab][configure-tab]
  49. 8. Scroll down to the **connection strings** section. The values for `Database`, `Data Source`, `User Id`, and `Password` are (respectively) the database name, server name, user name, and user password. Make note of the database connection information as it will be needed later.
  50. ![Connection string][connection-string]
  51. ##Install WebMatrix and develop your application
  52. You can install WebMatrix from the [Preview Management Portal][preview-portal].
  53. 1. After logging in, navigate to your website's Quick Start page, and click the WebMatrix icon at the bottom of the page:
  54. ![Install WebMatrix][install-webmatrix]
  55. Follow the prompts to install WebMatrix.
  56. 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**.
  57. ![Download website][download-site]
  58. 3. From the available templates, choose **PHP**.
  59. ![Site from template][site-from-template]
  60. 4. The **Empty Site** template will be selected by default. Provide a name for the site and click **NEXT**.
  61. ![Provide name for site][site-from-template-2]
  62. Your site will be opened on WebMatrix with some default files in place.
  63. 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.
  64. 5. With your site open in WebMatrix, click **Files**:
  65. ![WebMatrix - Click files][site-in-webmatrix]
  66. 6. Add your application files by clicking **Add Existing**:
  67. ![WebMatrix - Add existing files][add-existing-files]
  68. 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.
  69. 7. Next, you need to add your local MySQL 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.)
  70. // DB connection info
  71. $host = "localhost";
  72. $user = "your user name";
  73. $pwd = "your password";
  74. $db = "tasklist";
  75. Save the `taskmodel.php` file.
  76. 8. 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.
  77. ![WebMatrix - Launch createtable.php in browser][webmatrix-launchinbrowser]
  78. 9. 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.
  79. <h2 id="Publish">Publish your application</h2>
  80. 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 MySQL Database](#CreateWebsite) section).
  81. 1. Open the `taskmodel.php` file by double clicking it, and update the database connection information in the `connect` function.
  82. // DB connection info
  83. $host = "value of Data Source";
  84. $user = "value of User Id";
  85. $pwd = "value of Password";
  86. $db = "value of Database";
  87. Save the `taskmodel.php` file.
  88. 2. Click **Publish** in WebMatrix, then click **Continue** in the **Publish Preview** dialog.
  89. ![WebMatrix - Publish][publish]
  90. 3. Navigate to http://[your website name].azurewebsites.net/createtable.php to create the `items` table.
  91. 4. Lastly, navigate to http://[your website name].azurewebsites.net/index.php to being using the running application.
  92. ##Modify and republish your application
  93. 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.
  94. 1. Open the `index.php` file by double-clicking it.
  95. 2. Change **My ToDo List** to **My Task List** in the **h1** tag and save the file.
  96. 3. Click the **Publish** icon, then click **Continue** in the **Publish Preview** dialog.
  97. 4. When publishing has completed, navigate to http://[your website name].azurewebsites.net/index.php to see the published changes.
  98. # Next Steps
  99. 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:
  100. * [WebMatrix for Windows Azure](http://go.microsoft.com/fwlink/?LinkID=253622&clcid=0x409)
  101. * [WebMatrix website](http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200106398)
  102. [install-mysql]: http://dev.mysql.com/doc/refman/5.6/en/installing.html
  103. [running-app]: ../Media/tasklist_app_windows.png
  104. [tasklist-mysql-download]: http://go.microsoft.com/fwlink/?LinkId=252506
  105. [install-webmatrix]: ../Media/install-webmatrix.png
  106. [download-publish-profile]: ../../Shared/Media/download_publish_profile.jpg
  107. [webmatrix-templates]: ../../Shared/Media/webmatrix_templates.jpg
  108. [webmatrix-php-template]: ../../Shared/Media/webmatrix_php_template.jpg
  109. [webmatrix-php-emptysite]: ../../Shared/Media/webmatrix_php_emptysite.jpg
  110. [webmatrix-files]: ../../Shared/Media/webmatrix_files.jpg
  111. [webmatrix-delete-indexphp]: ../../Shared/Media/webmatrix_delete_indexphp.jpg
  112. [webmatrix-add-existing]: ../../Shared/Media/webmatrix_add_existing.jpg
  113. [webmatrix-launchinbrowser]: ../Media/launch-in-browser.png
  114. [webmatrix-publish]: ../../Shared/Media/webmatrix_publish.jpg
  115. [webmatrix-import-pub-settings]: ../../Shared/Media/webmatrix_import_pub_settings.jpg
  116. [webmatrix-pubcompat-continue]: ../../Shared/Media/webmatrix_pubcompat_continue.jpg
  117. [webmatirx-pubpreview]: ../../Shared/Media/webmatrix_pubpreview.jpg
  118. [preview-portal]: https://manage.windowsazure.com
  119. [php-site-from-template]: ../../Shared/Media/php_site_from_template.png
  120. [php-empty-site-template-installed]: ../../Shared/Media/php_empty_site_template_installed.png
  121. [new-website]: ../../Shared/Media/new_website.jpg
  122. [custom-create]: ../Media/custom_create.jpg
  123. [website-details]: ../../Shared/Media/website_details.jpg
  124. [new-mysql-db]: ../Media/new_mysql_db.jpg
  125. [go-to-dashboard]: ../Media/go_to_dashboard.jpg
  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
  133. [configure-tab]: ../Media/configure-tab.png
  134. [connection-string]: ../Media/connection-string.png