Create A PHP-MySQL Web App in Azure App Service and Deploy Using FTP A2 Slot PRJ Deployment
Create A PHP-MySQL Web App in Azure App Service and Deploy Using FTP A2 Slot PRJ Deployment
PHPMySQL + FTP
Create a PHPMySQL web app in Azure App Service and deploy using
FTP
By Robert McMurray https://github.com/rmcmurray
Updated: 11082016
In this article:
This tutorial shows you how to create a PHPMySQL web app and how to deploy it using FTP. This tutorial assumes you have PHP
http://www.php.net/manual/en/install.php, MySQL http://dev.mysql.com/doc/refman/5.6/en/installing.html, a web server, and an FTP client installed on your
computer. The instructions in this tutorial can be followed on any operating system, including Windows, Mac, and Linux. Upon completing this guide, you will
have a PHP/MySQL web app running in Azure.
You will learn:
How to create a web app and a MySQL database using the Azure Portal. Because PHP is enabled in Web Apps by default, nothing special is required to run
your PHP code.
How to publish your application to Azure using FTP.
By following this tutorial, you will build a simple registration web app in PHP. The application will be hosted in a Web App. A screenshot of the completed
application is below:
Note:
If you want to get started with Azure App Service before signing up for an account, go to Try App Service http://go.microsoft.com/fwlink/?
LinkId=523751, where you can immediately create a shortlived starter web app in App Service. No credit cards required, no commitments.
3. In the search type Web app + MySQL and click on Web app + MySQL.
4. Click Create. Enter a unique app service name, a valid name for the resource group and a new service plan.
5. Enter values for your new database, including agreeing to the legal terms.
6. When the web app has been created, you will see the new app service blade.
7. Click on Settings > Deployment credentials.
8. To enable FTP publishing, you must provide a user name and password. Save the credentials and make a note of the user name and password you create.
Copy
2. In your web server's root directory, create a folder called registration and create two files in it one called createtable.php and one called index.php .
3. Open the createtable.php file in a text editor or IDE and add the code below. This code will be used to create the registration_tbl table in the
registration database.
<?php
Copy
//DBconnectioninfo
$host="localhost";
$user="username";
$pwd="password";
$db="registration";
try{
$conn=newPDO("mysql:host=$host;dbname=$db",$user,$pwd);
$conn>setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="CREATETABLEregistration_tbl(
idINTNOTNULLAUTO_INCREMENT,
PRIMARYKEY(id),
nameVARCHAR(30),
emailVARCHAR(30),
dateDATE)";
$conn>query($sql);
}
catch(Exception$e){
die(print_r($e));
}
echo"<h3>Tablecreated.</h3>";
?>
Note:
You will need to update the values for $user and $pwd with your local MySQL user name and password.
4. Open a web browser and browse to http://localhost/registration/createtable.php http://localhost/tasklist/createtable.php. This will create the
registration_tbl table in the database.
5. Open the index.php file in a text editor or IDE and add the basic HTML and CSS code for the page the PHP code will be added in later steps.
<html>
<head>
<Title>RegistrationForm</Title>
<styletype="text/css">
body{backgroundcolor:#fff;bordertop:solid10px#000;
color:#333;fontsize:.85em;margin:20;padding:20;
fontfamily:"SegoeUI",Verdana,Helvetica,SansSerif;
}
h1,h2,h3,{color:#000;marginbottom:0;paddingbottom:0;}
h1{fontsize:2em;}
h2{fontsize:1.75em;}
h3{fontsize:1.2em;}
table{margintop:0.75em;}
th{fontsize:1.2em;textalign:left;border:none;paddingleft:0;}
td{padding:0.25em2em0.25em0em;border:0none;}
</style>
</head>
<body>
<h1>Registerhere!</h1>
<p>Fillinyournameandemailaddress,thenclick<strong>Submit</strong>toregister.</p>
<formmethod="post"action="index.php"enctype="multipart/formdata">
Name<inputtype="text"name="name"id="name"/></br>
Email<inputtype="text"name="email"id="email"/></br>
<inputtype="submit"name="submit"value="Submit"/>
</form>
<?php
?>
</body>
</html>
6. Within the PHP tags, add PHP code for connecting to the database.
Copy
//DBconnectioninfo
Copy
$host="localhost";
$user="username";
$pwd="password";
$db="registration";
//Connecttodatabase.
try{
$conn=newPDO("mysql:host=$host;dbname=$db",$user,$pwd);
$conn>setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(Exception$e){
die(var_dump($e));
}
Note:
Again, you will need to update the values for $user and $pwd with your local MySQL user name and password.
7. Following the database connection code, add code for inserting registration information into the database.
if(!empty($_POST)){
Copy
try{
$name=$_POST['name'];
$email=$_POST['email'];
$date=date("Ymd");
//Insertdata
$sql_insert="INSERTINTOregistration_tbl(name,email,date)
VALUES(?,?,?)";
$stmt=$conn>prepare($sql_insert);
$stmt>bindValue(1,$name);
$stmt>bindValue(2,$email);
$stmt>bindValue(3,$date);
$stmt>execute();
}
catch(Exception$e){
die(var_dump($e));
}
echo"<h3>Your'reregistered!</h3>";
}
8. Finally, following the code above, add code for retrieving data from the database.
$sql_select="SELECT*FROMregistration_tbl";
$stmt=$conn>query($sql_select);
$registrants=$stmt>fetchAll();
if(count($registrants)>0){
echo"<h2>Peoplewhoareregistered:</h2>";
echo"<table>";
echo"<tr><th>Name</th>";
echo"<th>Email</th>";
echo"<th>Date</th></tr>";
foreach($registrantsas$registrant){
echo"<tr><td>".$registrant['name']."</td>";
echo"<td>".$registrant['email']."</td>";
echo"<td>".$registrant['date']."</td></tr>";
}
echo"</table>";
}else{
echo"<h3>Nooneiscurrentlyregistered.</h3>";
}
Copy
4. Make note of the values for Database , Host , UserId , and Password .
5. From your web app, click the Download publish profile link at the bottom right corner of the page:
<publishProfilepublishMethod="FTP"publishUrl="ftp://[mysite].azurewebsites.net/site/wwwroot"ftpPassiveMode="True"userName="[username
Copy
...
</publishProfile>
Copy
$host="valueofDataSource";
$user="valueofUserId";
$pwd="valueofPassword";
$db="valueofDatabase";
Next steps
For more information, see the PHP Developer Center /develop/php/.