ã泚æ Microsoft Web Platform Installerã€ã³ã¹ããŒã©ãŒã䜿çšããŠã PHPãSQL Server ExpressãSQL Server for PHPã®Microsoftãã©ã€ããŒãããã³IISãã€ã³ã¹ããŒã«ããã³æ§æã§ããŸãã
ãã®ã¬ã€ãã§èª¬æããå 容ïŒ
- 管çããŒã¿ã«ïŒãã¬ãã¥ãŒïŒã䜿çšããŠWindows Azure Webãµã€ããšSQLããŒã¿ããŒã¹ãäœæããæ¹æ³ã PHPãã¯ãããžã¯Windows Azure Webãµã€ãã§ããã©ã«ãã§æå¹ã«ãªã£ãŠãããããPHPã³ãŒããå®è¡ããããã«ç¹å¥ãªã¢ã¯ã·ã§ã³ã¯å¿ èŠãããŸããã
- Gitã䜿çšããŠWindows Azureãã©ãããã©ãŒã ã§ã¢ããªã±ãŒã·ã§ã³ãå ¬éããã³åå ¬éããæ¹æ³ã
ãã®ã¬ã€ãã®æ瀺ã«åŸã£ãŠãç»é²çšã®ç°¡åãªPHP Webã¢ããªã±ãŒã·ã§ã³ãäœæããŸãã ãã®ã¢ããªã±ãŒã·ã§ã³ã¯ãWindows Azure Webãµã€ãã§ãã¹ããããŸãã å®æããã¢ããªã±ãŒã·ã§ã³ã®ã¹ã¯ãªãŒã³ã·ã§ããã以äžã«ç€ºããŸãã
Windows Azure Webãµã€ããæ§ç¯ããGitã®å ¬éãæ§æããŸãã Windows Azureã¢ã«ãŠã³ããäœæãã
Webãã©ãŠã¶ãŒãéãã httpïŒ//www.windowsazure.comã«ã¢ã¯ã»ã¹ããŸã ã ç¡æã¢ã«ãŠã³ããéå§ããã«ã¯ã ç¡æè©Šçšçã®å³äžé ãã¯ãªãã¯ããŠã次ã®æé ã«åŸã£ãŠãã ããã æ¬äººç¢ºèªãè¡ãã«ã¯ãã¯ã¬ãžããã«ãŒããŸãã¯æºåž¯é»è©±çªå·ã®æäŸãå¿ èŠã«ãªãå ŽåããããŸãã è«æ±æžã¯çºè¡ãããŸããã
Windows Azure Webãµã€ããæå¹ã«ãã
https://account.windowsazure.com/ã«ã¢ã¯ã»ã¹ããWindows Azureã¢ã«ãŠã³ãã§ãµã€ã³ã€ã³ããŸãã ãã¬ãã¥ãŒæ©èœèŠçŽ ãã¯ãªãã¯ããŠãå©çšå¯èœãªãã¬ãã¥ãŒãªãã·ã§ã³ã確èªããŸãã
[ Webãµã€ã ]ãŸã§ã¹ã¯ããŒã«ãã[ ä»ããè©Šã ]ãã¯ãªãã¯ããŸãã
ãµãã¹ã¯ãªãã·ã§ã³ãéžæããŠããã§ãã¯ããã¯ã¹ããªã³ã«ããŸãã
Windows Azure Webãµã€ããšSQLããŒã¿ããŒã¹ãäœæããã«ã¯ã次ã®æé ãå®è¡ããŸãã
Windows AzureããŒã¿ã«ïŒãã¬ãã¥ãŒïŒã«ãµã€ã³ã€ã³ããŸãã ããŒã¿ã«ã®å·Šäžã«ãã[ +æ°èŠ ]ã¢ã€ã³ã³ãã¯ãªãã¯ããŸãã
[ Webãµã€ã]ãã¯ãªãã¯ãã[ ããŒã¿ããŒã¹ã§äœæ]ãã¯ãªãã¯ããŸãã
[ URL]ãã£ãŒã«ãã«å€ãå ¥åãã[ DATABASE]ããããããŠã³ãªã¹ããã[ Create a New SQL Database]ãéžæãã[ REGION]ããããããŠã³ãªã¹ãã§Webãµã€ãã®ããŒã¿ã»ã³ã¿ãŒãæå®ããŸãã ãã€ã¢ãã°ããã¯ã¹ã®äžéšã«ããç¢å°ãã¯ãªãã¯ããŸãã
NAMEãã£ãŒã«ãã«ããŒã¿ããŒã¹ã®å€ãå ¥åãã EDITION ïŒWEBãŸãã¯BUSINESSïŒã®å€ãéžæããããŒã¿ããŒã¹ã®MAX SIZEã®å€ãšCOLLATIONã®å€ãèšå®ã ã NEW SQL Database serverãéžæããŸã ã ãã€ã¢ãã°ããã¯ã¹ã®äžéšã«ããç¢å°ãã¯ãªãã¯ããŸãã
管çè åãšãã¹ã¯ãŒããå ¥åãïŒãã¹ã¯ãŒãã確èªïŒãSQLããŒã¿ããŒã¹ãµãŒããŒãäœæããå°åãéžæãã
Allow Windows Azure Services to access the server
ãã§ãã¯ããã¯ã¹ããªã³ã«ããŸãã
Webãµã€ãã®äœæåŸãWebãµã€ã '[SITE_NAME]'ã®äœæãæ£åžžã«å®äºããŸãããšããã¡ãã»ãŒãžã衚瀺ãããŸãã ããã§ãGitå ¬éãæå¹ã«ã§ããŸãã
Webãµã€ãã®ãªã¹ãã«è¡šç€ºãããŠããWebãµã€ãã®ååãã¯ãªãã¯ããŠãWebãµã€ãã®ã¯ã€ãã¯ã¹ã¿ãŒãããã«ãéããŸãã
[ã¯ã€ãã¯ã¹ã¿ãŒã]ããŒãžã®äžéšã«ãã[ Gitå ¬éã®ã»ããã¢ãã ]ãã¯ãªãã¯ããŸãã
Gitã®å ¬éãæå¹ã«ããã«ã¯ããŠãŒã¶ãŒåãšãã¹ã¯ãŒããæäŸããå¿ èŠããããŸãã äœæãããŠãŒã¶ãŒåãšãã¹ã¯ãŒããèŠããŠãããŠãã ããã ïŒGitãªããžããªãŒã以åã«æ§æãããŠããå Žåããã®ã¢ã¯ã·ã§ã³ã¯ã¹ããããããŸããïŒ
ãªããžããªã®ã»ããã¢ããã«ã¯æ°ç§ããããŸãã
ãªããžããªã®æºåãæŽããšãã¢ããªã±ãŒã·ã§ã³ãã¡ã€ã«ããªããžããªã«é 眮ããæé ã衚瀺ãããŸãã ãããã®æ瀺ã¯åŸã§å¿ èŠã«ãªããããèŠããŠãããŠãã ããã
SQLããŒã¿ããŒã¹æ¥ç¶æ å ±ã®ååŸ
Windows Azure Webãµã€ãã§å®è¡ãããŠããSQLããŒã¿ããŒã¹ã€ã³ã¹ã¿ã³ã¹ã«æ¥ç¶ããã«ã¯ãæ¥ç¶æ å ±ãå¿ èŠã§ãã SQLããŒã¿ããŒã¹ãžã®æ¥ç¶ã«ã€ããŠã¯ã次ã®æé ãå®è¡ããŸãã
管çããŒã¿ã«ïŒãã¬ãã¥ãŒïŒã§ã[ ãªã³ã¯ããããªãœãŒã¹ ]ãã¯ãªãã¯ããããŒã¿ããŒã¹åãã¯ãªãã¯ããŸãã
[ æ¥ç¶æååã衚瀺]ãã¯ãªãã¯ããŸã ã
éãããã€ã¢ãã°ããã¯ã¹ã®PHPã»ã¯ã·ã§ã³ã§ã
SERVER
ã
DATABASE
ãããã³
USERNAME
å€ãèŠããŠãããŠãã ããã
ã¢ããªã±ãŒã·ã§ã³ã®ããŒã«ã«ã§ã®äœæãšãã¹ã
ç»é²ã¢ããªã±ãŒã·ã§ã³ã¯ããŠãŒã¶ãŒã®ååãšã¡ãŒã«ã¢ãã¬ã¹ãå ¥åããŠã€ãã³ãåå è ãç»é²ããããã®ã·ã³ãã«ãªPHPã¢ããªã±ãŒã·ã§ã³ã§ãã 以åã®ç»é²è ã«é¢ããæ å ±ãè¡šã«è¡šç€ºãããŸãã ç»é²æ å ±ã¯ãSQLããŒã¿ããŒã¹ã®ã€ã³ã¹ã¿ã³ã¹ã«ä¿åãããŸãã ãã®ã¢ããªã±ãŒã·ã§ã³ã¯2ã€ã®ãã¡ã€ã«ã§æ§æãããŠããŸãïŒã³ããŒãšè²Œãä»ãã®ã³ãŒãã以äžã«ç€ºããŸãïŒã
- index.php ç»é²ãã©ãŒã ããã³ç»é²ãããåå è ã«é¢ããæ å ±ãå«ãè¡šã衚瀺ããŸãã
- createtable.php ã¢ããªã±ãŒã·ã§ã³ã®SQLããŒã¿ããŒã¹ããŒãã«ãäœæããŸãã ãã®ãã¡ã€ã«ã¯äžåºŠã ã䜿çšãããŸãã
ã¢ããªã±ãŒã·ã§ã³ãããŒã«ã«ã§å®è¡ããã«ã¯ã次ã®æé ãå®è¡ããŸãã ããŒã«ã«ã³ã³ãã¥ãŒã¿ãŒã«PHPãSQL Server Expressãããã³WebãµãŒããŒãã€ã³ã¹ããŒã«ãããŠããã SQL Serverã®PDOæ¡åŒµæ©èœãæå¹ã«ãªã£ãŠããããšãåæãšããŠããŸãã
registration
ãšããSQL ServerããŒã¿ããŒã¹ãäœæããŸãã ããã¯ã次ã®ã³ãã³ãã䜿çšããŠ
sqlcmd
ã³ãã³ãã©ã€ã³ããå®è¡ã§ããŸãã
> sqlcmd -S localhost \ sqlexpress -U <ããŒã«ã«ãŠãŒã¶ãŒå> -P <ããŒã«ã«ãã¹ã¯ãŒã>
1>ããŒã¿ããŒã¹ç»é²ãäœæãã
2> GO
WebãµãŒããŒã®ã«ãŒããã£ã¬ã¯ããªã«
registration
ãšãããã©ã«ããŒãäœæãããã®äžã«
createtable.php
ãšããååãš
index.php
ãšããååã®2ã€ã®ãã¡ã€ã«ãäœæããŸãã
ããã¹ããšãã£ã¿ãŸãã¯IDEã§
createtable.php
ãéãã以äžã®ã³ãŒããè¿œå ããŸãã ãã®ã³ãŒãã¯ãããŒã¿ããŒã¹ã«
registration_tbl
ããŒãã«ãäœæããããã«äœ¿çšãããŸãã
<?php // DB connection info $host = "localhost\sqlexpress"; $user = "user name"; $pwd = "password"; $db = "registration"; try{ $conn = new PDO( "sqlsrv:Server= $host ; Database = $db ", $user, $pwd); $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $sql = "CREATE TABLE registration_tbl( id INT NOT NULL IDENTITY(1,1) PRIMARY KEY(id), name VARCHAR(30), email VARCHAR(30), date DATE)"; $conn->query($sql); } catch(Exception $e){ die(print_r($e)); } echo "<h3>Table created.</h3>"; ?>
ã泚æ SQL Serverã®ããŒã«ã«ãŠãŒã¶ãŒåãšãã¹ã¯ãŒãã®äž¡æ¹ã§å€ãæŽæ°ããå¿ èŠããããŸãã
Webãã©ãŠã¶ãŒãéãã httpïŒ//localhost/registration/createtable.phpãã¡ã€ã«ãåç §ããŸãã ããŒã¿ããŒã¹ã«ããŒãã«ãäœæãããŸãã
ããã¹ããšãã£ã¿ãŸãã¯IDEã§index.phpãã¡ã€ã«ãéããããŒãžã®åºæ¬çãªHTMLããã³CSSã³ãŒããè¿œå ããŸãïŒPHPã³ãŒãã¯åŸã®æ®µéã§è¿œå ãããŸãïŒã
<html> <head> <Title>Registration Form</Title> <style type="text/css"> body { background-color: #fff; border-top: solid 10px #000; color: #333; font-size: .85em; margin: 20; padding: 20; font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif; } h1, h2, h3,{ color: #000; margin-bottom: 0; padding-bottom: 0; } h1 { font-size: 2em; } h2 { font-size: 1.75em; } h3 { font-size: 1.2em; } table { margin-top: 0.75em; } th { font-size: 1.2em; text-align: left; border: none; padding-left: 0; } td { padding: 0.25em 2em 0.25em 0em; border: 0 none; } </style> </head> <body> <h1>Register here!</h1> <p>Fill in your name and email address, then click <strong>Submit</strong> to register.</p> <form method="post" action="index.php" enctype="multipart/form-data" > Name <input type="text" name="name" id="name"/></br> Email <input type="text" name="email" id="email"/></br> <input type="submit" name="submit" value="Submit" /> </form> <?php ?> </body> </html>
PHPã¿ã°å ã«ãããŒã¿ããŒã¹ã«æ¥ç¶ããPHPã³ãŒããè¿œå ããŸãã
// DB connection info $host = "localhost\sqlexpress"; $user = "user name"; $pwd = "password"; $db = "registration"; // Connect to database. try { $conn = new PDO( "sqlsrv:Server= $host ; Database = $db ", $user, $pwd); $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); } catch(Exception $e){ die(var_dump($e)); }
ã泚æ MySQLã®ããŒã«ã«ãŠãŒã¶ãŒåãšãã¹ã¯ãŒãã®äž¡æ¹ã§å€ãæŽæ°ããå¿ èŠããããŸãã
ããŒã¿ããŒã¹æ¥ç¶ã³ãŒãã®åŸã«ââãç»é²ããŒã¿ãããŒã¿ããŒã¹ã«æ¿å ¥ããã³ãŒããè¿œå ããŸãã
if(!empty($_POST)) { try { $name = $_POST['name']; $email = $_POST['email']; $date = date("Ymd"); // Insert data $sql_insert = "INSERT INTO registration_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're registered!</h3>"; }
æåŸã«ãäžèšã®ã³ãŒãã®åŸã«ââãããŒã¿ããŒã¹ããããŒã¿ãååŸããã³ãŒããè¿œå ããŸãã
$sql_select = "SELECT * FROM registration_tbl"; $stmt = $conn->query($sql_select); $registrants = $stmt->fetchAll(); if(count($registrants) > 0) { echo "<h2>People who are registered:</h2>"; echo "<table>"; echo "<tr><th>Name</th>"; echo "<th>Email</th>"; echo "<th>Date</th></tr>"; foreach($registrants as $registrant) { echo "<tr><td>".$registrant['name']."</td>"; echo "<td>".$registrant['email']."</td>"; echo "<td>".$registrant['date']."</td></tr>"; } echo "</table>"; } else { echo "<h3>No one is currently registered.</h3>"; }
ããã§ã httpïŒ//localhost/registration/index.phpãã¡ã€ã«ã«ç§»åããŠãã¢ããªã±ãŒã·ã§ã³ããã¹ãã§ããŸãã
ã¢ããªã±ãŒã·ã§ã³ãå ¬é
ã¢ããªã±ãŒã·ã§ã³ãããŒã«ã«ã§ãã¹ãããããšã«ãããGitã䜿çšããŠWindows Azure Webãµã€ãã«å ¬éã§ããŸãã ãã ããæåã«ã¢ããªã±ãŒã·ã§ã³ã®ããŒã¿ããŒã¹æ¥ç¶æ å ±ãæŽæ°ããå¿ èŠããããŸãã 以åã«ååŸããããŒã¿ããŒã¹æ¥ç¶æ å ±ïŒ ãSQLããŒã¿ããŒã¹æ¥ç¶æ å ±ã®ååŸããåç § ïŒã䜿çšããŠã äž¡æ¹ã®ãã¡ã€ã«ã®é©åãªå€ã§æ¬¡ã®æ å ±ãæŽæ°ããŸãã
// DB connection info $host = "tcp:<value of SERVER>"; $user = "<value of USERNAME>@<server ID>"; $pwd = "<your password>"; $db = "<value of DATABASE>";
ã泚æ SERVERã®å€ãè¿œå ããå¿ èŠããããŸããå€ã¯ãUSERNAMEã '@'ãããã³ãµãŒããŒèå¥åã®å€ãçµåããŸãã ãµãŒããŒèå¥åã¯ãSERVERå€ã®æåã®10æåã§æ§æãããŸãã
ããã§ãGitã®å ¬éãèšå®ããã¢ããªã±ãŒã·ã§ã³ãå ¬éããæºåãã§ããŸããã
ã泚æ ãWindows Azure Webãµã€ãã®äœæãããã³ãGitçºè¡ã®æ§æãã»ã¯ã·ã§ã³ã®æåŸã«èšèŒãããŠããã®ãšåãæé ã«åŸããŸãã
GitBashïŒãŸãã¯Gitãããå Žåã¯ã¿ãŒããã«ïŒãéãããã£ã¬ã¯ããªãã¢ããªã±ãŒã·ã§ã³ã®ã«ãŒããã£ã¬ã¯ããªã«å€æŽãã次ã®ã³ãã³ããå®è¡ããŸãã
git init
git addã
git commit -m "åæã³ããã"
git remote add azure [ãªã¢ãŒããªããžããªã®URL]
git push azure master
åã«äœæãããã¹ã¯ãŒãã®å ¥åãæ±ããããŸãã
httpïŒ// [site_name] .azurewebsites.net / createtable.phpã«ã¢ã¯ã»ã¹ããŠãã¢ããªã±ãŒã·ã§ã³ã®MySQLããŒãã«ãäœæããŸãã httpïŒ// [website_name] .azurewebsites.net / index.phpã«ã¢ã¯ã»ã¹ããŠãã¢ããªã±ãŒã·ã§ã³ã®äœ¿çšãéå§ããŸãã
å ¬éã¢ããªã±ãŒã·ã§ã³ã«å€æŽãå ããŠãGitã䜿çšããŠå ¬éã§ããŸãã
ã¢ããªã±ãŒã·ã§ã³ãžã®å€æŽã®æçš¿
ã¢ããªã±ãŒã·ã§ã³ã®å€æŽãéä¿¡ããã«ã¯ã次ã®æé ãå®è¡ããŸãã
ã¢ããªã±ãŒã·ã§ã³ãããŒã«ã«ã§å€æŽããŸãã
GitBashïŒãŸãã¯Gitãããå Žåã¯ã¿ãŒããã«ïŒãéãããã£ã¬ã¯ããªãã¢ããªã±ãŒã·ã§ã³ã®ã«ãŒããã£ã¬ã¯ããªã«å€æŽããŠã次ã®ã³ãã³ããå®è¡ããŸãã
git addã
git commit -mãå€æŽã説æããã³ã¡ã³ãã
git push azure master
åã«äœæãããã¹ã¯ãŒãã®å ¥åãæ±ããããŸãã
httpïŒ// [website_name] .azurewebsites.net / index.phpã«ã¢ã¯ã»ã¹ããŠãå€æŽã衚瀺ããŸãã