Windows Azure上PHP和MySQL:Git入门和部署

By now, I'm sure you've heard of SitePoint's and Windows Azure's Push The Web Forward contest. If not, it's a contest that requires you to sign up for a trial account on Windows Azure, and deploy any app there to be eligible for a draw in which you can win up to $3000 per app. You can even check out the current entries by visiting the gallery.

到目前为止,我确定您已经听说过SitePoint和Windows Azure的Push The Web Forward竞赛。 如果不是,那是一场竞赛,要求您在Windows Azure上注册一个试用帐户,然后在该处部署任何应用程序才能有资格参加抽奖,每个应用程序最多可以赢得$ 3000。 您甚至可以通过访问画廊来查看当前条目。

As a developer, you can never have enough technologies in your toolbelt, so let's take a look at this behemoth of an environment.

作为开发人员,您的工具带中永远不会拥有足够的技术,因此让我们看一下环境的这一庞然大物。

配置 (Setting up)

In this article, we'll go through the procedure of setting up a Hello World PHP app on Windows Azure and connecting it to the database on that very environment. We'll also deploy via FTP and we'll set up auto-deployment through Github.

在本文中,我们将介绍在Windows Azure上设置Hello World PHP应用程序并将其连接到该环境中的数据库的过程。 我们还将通过FTP进行部署,并通过Github设置自动部署。

If you haven't done so yet, please sign up for a free trial account. Once you reach the management dashboard (seen in the screenshot below), we'll set up our starting website.

如果您还没有这样做,请注册一个免费试用帐户 。 当您到达管理控制台(在下面的屏幕截图中看到)时,我们将建立我们的起始网站。

alt

在Windows Azure上创建新PHP项目 (Creating a new PHP project on Windows Azure)

Near the bottom of the screen, click New -> Compute -> Website -> From Gallery, as seen in the screenshot below.

在屏幕底部附近,单击新建->计算->网站->从库,如下面的屏幕快照所示。

alt

This should open the gallery of web site starter templates, which offers anything from Ruby apps to ready-to-use forums. We're aiming for a from-scratch approach, so scroll down until you reach "PHP Empty Site", and on the next screen, provide an arbitrary URL name for it. I chose SitePoint01 for this exercise.

这应该会打开网站启动模板的库,其中提供了从Ruby应用程序到即用型论坛的所有内容。 我们的目标是从零开始,因此向下滚动直到您到达“ PHP空站点”,然后在下一个屏幕上为其提供一个任意的URL名称。 在本练习中,我选择了SitePoint01。

alt

A few seconds later, you app should be up an running. In fact, you should see it in the list of Web Sites and it should have a status of "Running".

几秒钟后,您的应用程序应该开始运行了。 实际上,您应该在网站列表中看到它,并且它的状态应该为“正在运行”。

alt

Click the name of the app to enter its dashboard, and take some time to look around – get familiar with the various screens. The first screen you see is the "Quick Start" screen, designed to get you up and running with the most common features quickly, and the rest are self explanatory.

单击应用程序的名称以进入其仪表板,并花一些时间环顾四周-熟悉各种屏幕。 您看到的第一个屏幕是“快速入门”屏幕,该屏幕旨在使您快速启动并运行最常用的功能,其余的功能不言而喻。

If you visit your app in the browser now, you'll get an empty screen. This is fine. After all, we didn't do anything yet. Let's configure a database before moving on.

如果您现在在浏览器中访问您的应用,则会出现一个空白屏幕。 这可以。 毕竟,我们还没有做任何事情。 让我们在继续之前配置数据库。

在Windows Azure上配置数据库 (Configuring a database on Windows Azure)

Go to "Linked Resources" in your app's dashboard, and click "Link a resource". We want to create a new resource, and we'll choose MySQL database. On the next screen of the wizard, give it a name, agree to the terms, and click the "Complete" tick in the bottom right corner of the popup window.

转到应用程序信息中心中的“链接资源”,然后单击“链接资源”。 我们要创建一个新资源,然后选择MySQL数据库。 在向导的下一个屏幕上,给它命名,并同意条款,然后单击弹出式窗口右下角的“完成”打勾。

After a few seconds of setting up, the resource should be ready and linked.

设置几秒钟后,该资源应已准备就绪并已链接。

alt

If you go to the dashboard now, the first option under "Quick Glance" should be "View Connection Strings", which, when clicked, produces a popup with database credentials you can use to access the database we've just set up.

如果现在转到仪表板,则“快速浏览”下的第一个选项应该是“查看连接字符串”,单击该选项时将产生一个弹出窗口,其中包含可用于访问我们刚刚设置的数据库的数据库凭据。

alt

To manage our database, we'll use Oracle's MySQL Workbench in this article (feel free to use any other SQL management tool if you prefer). When you create a MySQL database on Windows Azure, the database is actually created on ClearDB. ClearDB databases can be connected to remotely, and that's what we'll do.

为了管理我们的数据库,我们将在本文中使用OracleMySQL Workbench(如果愿意,可以使用任何其他SQL管理工具)。 在Windows Azure上创建MySQL数据库时,该数据库实际上是在ClearDB创建的 。 ClearDB数据库可以远程连接,这就是我们要做的。

Download MySQL Workbench and opt to create a new connection. In the window, fill in the data from the previous screenshot – the host name, user name and password. You can even download SSL keys from ClearDB (see screenshot below) and use them in the SSL tab of the connection in MySQL Workbench to make your connection more secure.

下载MySQL Workbench并选择创建一个新连接。 在窗口中,填写上一个屏幕截图中的数据-主机名,用户名和密码。 您甚至可以从ClearDB下载SSL密钥(请参见下面的屏幕截图),并在MySQL Workbench中连接的SSL选项卡中使用它们,以使连接更加安全。

alt

After pushing Test Connection, Workbench should connect successfully. After pushing connect, it should display an empty database.

按下“测试连接”后,Workbench应该成功连接。 推送连接后,它将显示一个空数据库。

alt

To set up a basic table with some sample data we'll be retrieving, paste the following SQL statements into the SQL tab in Workbench, and click the "Execute" button, represented by the thunderbolt icon in the SQL tab's toolbar. Naturally, replace "sitepoint01" with the name of your own database.

要建立一个包含一些示例数据的基本表,请将以下SQL语句粘贴到Workbench的“ SQL”选项卡中,然后单击“执行”按钮,该按钮由“ SQL”选项卡的工具栏中的雷电图标表示。 自然地,用您自己的数据库名称替换“ sitepoint01”。

USE `sitepoint01` ;

CREATE TABLE IF NOT EXISTS `sitepoint01`.`hello_world` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `data` VARCHAR(45) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

START TRANSACTION;
USE `sitepoint01`;
INSERT INTO `sitepoint01`.`hello_world` (`id`, `data`) VALUES (1, 'I was retrieved from the database!');

COMMIT;

We now have data in our MySQL database. Keep in mind that you can also use Workbench's excellent Model editor to build tables and relations graphically, then export it all directly to your remote database. Remember to save often, though – Workbench is a fantastic tool, but it can be very buggy and often crashes.

现在,我们MySQL数据库中有数据。 请记住,您还可以使用Workbench出色的模型编辑器以图形方式构建表和关系,然后将其全部直接导出到远程数据库。 不过,请记住要经常保存– Workbench是一个很棒的工具,但是它可能会非常容易出错,并且经常崩溃。

将文件上传到Windows Azure (Uploading files to Windows Azure)

Uploading files to Windows Azure apps can be done in two ways: through source control, or via FTP.

可以通过两种方式将文件上传到Windows Azure应用程序:通过源代码控制或通过FTP。

使用FTP将文件上传到Windows Azure (Using FTP to upload files to Windows Azure)

If you go to your app's dashboard, you should see the FTP Host Name in the right hand side menu – but no user. We'll need to set this up to try it out. Under "Quick Glance" again, click "Set Up Deployment Credentials" to create an FTP user. Once done, make sure you have an FTP client installed on your machine – I'll use FileZilla for the purpose of this article.

如果转到应用程序的仪表板,则应该在右侧菜单中看到“ FTP主机名”,但没有用户。 我们需要进行设置以进行尝试。 再次在“快速浏览”下,单击“设置部署凭据”以创建FTP用户。 完成后,请确保您在计算机上安装了FTP客户端-出于本文的目的,我将使用FileZilla

Connecting couldn't be more straightforward. Simply enter the credentials into the New Site dialog of FileZilla as shown below, and click connect.

连接再简单不过了。 只需将凭据输入FileZilla的“新建站点”对话框,如下所示,然后单击“连接”。

alt

As soon as the directory tree opens, you can navigate to /site/wwwroot/ where you'll find two files: favicon.ico and index.php. Download the index.php file with FileZilla into a local folder you decide will hold the app's contents, open it in your favorite code editor and change the contents to the following:

目录树一打开,您就可以导航到/site/wwwroot/ ,在这里您将找到两个文件: favicon.icoindex.php 。 将带有FileZilla的index.php文件下载到本地文件夹中,您决定保留该应用程序的内容,在您喜欢的代码编辑器中将其打开,然后将内容更改为以下内容:

<?php
echo "Hello World";
?>

Then, upload the file back to Azure. If you're asked to overwrite the original file, do it. Now if you visit your web app's URL (the URL can be seen in the Dashboard among the other data in the right hand side column), you should see the infamous Hello World message.

然后,将文件上传回Azure。 如果要求您覆盖原始文件,请执行此操作。 现在,如果您访问Web应用程序的URL(可以在仪表板的右侧列中的其他数据中找到该URL),您应该会看到臭名昭著的Hello World消息。

alt

However, using FTP to upload your site's files is so very… nineties. Let's enter the Information Era.

但是,使用FTP上载站点的文件是如此……90年代。 让我们进入信息时代。

将Github存储库连接到Windows Azure (Connecting a Github repo to Windows Azure)

To deploy via Github, we'll need to "pimp" our app's local folder a bit first. Add the following files into it: .gitignore and README.md.

要通过Github进行部署,我们需要先“拉皮拉”应用程序的本地文件夹。 将以下文件添加到其中: .gitignoreREADME.md

These just make for a clean repository. Every repo should have a README file, no matter how trivial, and a .gitignore file which makes sure no junk is commited.

这些只是一个干净的存储库。 每个回购都应该有一个README文件(无论多么琐碎)和一个.gitignore文件,以确保没有垃圾邮件被提交。

If you're on Windows, and you haven't installed Git tools for Windows yet, do it. These tools come with a bash app (among other useful items) which makes the Git repo setup process identical on all operating systems.

如果您使用的是Windows,并且尚未安装Windows的Git工具 ,请执行此操作。 这些工具带有一个bash应用程序(以及其他有用的项目),这使Git存储库设置过程在所有操作系统上都相同。

Now cd into the app's local folder, and run the commands:

现在cd到应用程序的本地文件夹,然后运行命令:

git init
git add .
git commit -am 'First commit'

I'm currently on Windows, and this is what the process looks like:

我当前在Windows上,过程如下所示:

alt

Now create a new repository on Github. I called mine "sitepoint-azure1". Add the origin to the repo we just initialized locally, as per Github's instructions.

现在在Github上创建一个新的存储库。 我称我为“ sitepoint-azure1”。 按照Github的说明,将原点添加到我们刚刚在本地初始化的仓库中。

git remote add origin git@github.com:Swader/sitepoint-azure1.git
git push -u origin master

With our app's repo now online, let's configure it in Azure's management panel. Go to either the dashboard of your web site, or to the Quick Start screen, and click the "Set up deployment from source control" option. In the pop up that appears, select Github, click next, authorize whatever Azure is asking you to authorize and find the repo you just created in the list of offered repositories.

通过我们应用的存储库现在在线,我们可以在Azure的管理面板中对其进行配置。 转到网站的仪表板或“快速入门”屏幕,然后单击“从源代码管理设置部署”选项。 在出现的弹出窗口中,选择Github,单击下一步,对Azure要求您进行授权的任何内容进行授权,并在提供的存储库列表中找到刚创建的存储库。

alt

A few seconds should go by and the deployment should be ready. In fact, as soon as it finishes processing, your app's files on the Azure instance will already be updated. If you refresh the view in your FTP client now, you'll notice our README and .gitignore files are there too. It's all automatic. It really is that simple! Now whenever one of your fellow devs updates your Github app and you accept their pull request into the master branch, Azure will automatically take care of keeping it up to date. You can even force a sync manually with the buttons in the footer of the Deployment tab if you feel like it isn't doing a quick enough job.

几秒钟后,部署应该已经准备就绪。 实际上,一旦处理完成,您的应用程序在Azure实例上的文件将已经更新。 如果现在在FTP客户端中刷新视图,您会注意到我们的README和.gitignore文件也在那里。 都是自动的。 真的就是这么简单! 现在,只要有一位开发人员更新您的Github应用程序,并且您接受他们的拉入请求到master分支中,Azure就会自动负责保持其最新状态。 如果您觉得同步做得不够快,甚至可以使用“部署”标签页脚中的按钮手动强制进行同步。

alt

We'll demonstrate how it auto-updates in the next section.

我们将在下一节中演示如何自动更新。

将PHP应用程序连接到Windows Azure上MySQL数据库 (Connecting a PHP app to a MySQL database on Windows Azure)

First, let's consider some security. When connecting to the database, we need to let PHP know which credentials to use to connect to MySQL. But if we include them directly into the file, everyone will be able to see our password in Github. Hence, create a config.php file in the app's local folder with the following contents (replace the credentials to match yours, of course):

首先,让我们考虑一下安全性。 连接到数据库时,我们需要让PHP知道要使用哪些凭据来连接到MySQL。 但是,如果我们将它们直接包含到文件中,则每个人都可以在Github中看到我们的密码。 因此,请在应用程序的本地文件夹中创建具有以下内容的config.php文件(当然,请替换凭据以使其与您的凭据匹配):

<?php
// Replace with your own
$sUsername = 'user';
$sPassword = 'pass';
$sHost = 'host';
$sDb = 'db';

Then, make sure it never gets commited by adding the following block at the top of our .gitignore file:

然后,通过在.gitignore文件顶部添加以下代码块,确保它永远不会提交:

# Configuration
config.php

Finally, upload the config.php file to the server manually via FTP. This makes sure it stays put – it will be ignored by the auto-deployment and by our local Git repo.

最后,通过FTP手动将config.php文件上传到服务器。 这样可以确保它保持放置状态-自动部署和我们本地的Git存储区将忽略它。

Now re-open the local index.php file, and change its contents to the following:

现在,重新打开本地index.php文件,并将其内容更改为以下内容:

<?php

require_once 'config.php';

try {
    $oConn = new PDO('mysql:host='.$sHost.';dbname='.$sDb, $sUsername, $sPassword);
    $oConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $oStmt = $oConn->prepare('SELECT data FROM `hello_world`');
    $oResult = $oStmt->fetchAll();

    foreach ($oResult as $aRow) {
        print_r($aRow['data']);
    }

} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

Save, run git commit -am 'Reading from DB' in your app's folder, followed by a git push.

保存,在应用程序的文件夹中运行git commit -am 'Reading from DB' ,然后执行git push

Observe the deployment screen in your app's management panel – the deployment history will be growing with every push to the master branch. Your app should already be up-to-date – Azure instantly pulls the Github content.

观察应用程序管理面板中的部署屏幕-每次推送到master分支,部署历史记录都会不断增长。 您的应用程序应该已经是最新的– Azure会立即提取Github内容。

If you now re-visit your app's URL, you should get an "I was retrieved from the database!" message displayed.

如果现在重新访问应用程序的URL,则应获得“我已从数据库中检索到!”。 显示消息。

alt

结论 (Conclusion)

As you can see, building and deploying an app on Windows Azure is dead simple. You can have an automated deployment process set up in minutes, all hosted on a highly scalable and reliable infrastructure with built-in metrics and beautiful dashboards to make managing your app as easy as possible.

如您所见,在Windows Azure上构建和部署应用非常简单。 您可以在几分钟之内建立一个自动部署过程,所有这些过程都托管在具有内置指标和精美仪表板的高度可扩展且可靠的基础架构上,从而使您的应用程序管理变得尽可能容易。

Armed with this knowledge, why not have a go at the PTWF contest and see if you can take some prizes home?

有了这些知识,为什么不参加PTWF竞赛,看看您是否可以将一些奖品带回家?

If you'd like to see a more specific Azure topic covered, or you just have some general feedback, please leave it in the comments below!

如果您希望看到涵盖的更具体的Azure主题,或者只是有一些一般性反馈,请在下面的评论中保留它!

翻译自: https://www.sitepoint.com/php-mysql-windows-azure-getting-started-deploying-git/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值