app engine 入门_Google App Engine和PHP:入门

app engine 入门

It's been a while since Google announced PHP support on Google App Engine. This article series will take you through all the necessary steps in getting your app up and running on GAE.

自Google宣布对Google App Engine支持PHP已有一段时间了。 本系列文章将引导您完成在GAE上启动和运行应用程序的所有必要步骤。

For this tutorial, we'll be using PhpStorm which supports GAE projects out of the box, but you can use any IDE of your choice.

在本教程中,我们将使用PhpStorm ,它开箱即用地支持GAE项目,但是您可以使用任何选择的IDE。

Start by respecting the prerequisites for your platform. This is necessary because the Google App Engine SDK requires certain software to be runnable locally, namely Python which runs the "server".

首先要考虑平台的先决条件 。 这是必要的,因为Google App Engine SDK要求某些软件可以在本地运行,即运行“服务器”的Python。

If your PhpStorm instance doesn't have the GAE plugin for some reason, go into Preferences and find it in the plugin browser – it's available for download and installation via the plugin repositories.

如果您的PhpStorm实例由于某种原因没有GAE插件,请进入“首选项”并在插件浏览器中找到它-可以通过插件存储库下载和安装。

在GAE上注册应用 (Registering an app on GAE)

To develop an app for GAE, you first need to register it and retrieve an application ID. Go to https://appengine.google.com/ and log in with your Google account. Once done, click the "Create Application" button in the dashboard and enter the required information into the form.

要为GAE开发应用,您首先需要注册它并获取应用ID。 去 https://appengine.google.com/并使用您的Google帐户登录。 完成后,单击仪表板中的“创建应用程序”按钮,然后在表单中输入所需的信息。

alt

开始一个项目 (Starting a project)

To start a new GAE PHP project, simply select it from the drop down menu and fill in the required data.

要开始一个新的GAE PHP项目,只需从下拉菜单中选择它并填写所需的数据。

alt
alt

Once you accept, the IDE will automatically create two files: app.yaml and main.php. If you're not using PhpStorm, you can create these yourself.

一旦接受,IDE将自动创建两个文件: app.yamlmain.php 。 如果您不使用PhpStorm,则可以自己创建这些文件。

app.yaml:

app.yaml

application: sitepoint-test
version: 1
runtime: php
api_version: 1
threadsafe: true

handlers:
- url: .*
  script: main.php

main.php:

main.php

<?php

echo 'Hello, world!';

Let's explain the non self-explanatory YAML attributes.

让我们解释一下不言自明的YAML属性。

threadsafe refers to PHP's thread safety, a highly disputed issue with most people being skeptical of it and still doubting that PHP actually properly supports threads. For more information, read these two StackOverflow questions.

threadsafe是指PHP的线程安全性,这是一个备受争议的问题,大多数人对此表示怀疑,并且仍然怀疑PHP实际上是否正确支持线程。 有关更多信息,请阅读以下 两个 StackOverflow问题。

This value seems to be optional but I generally tend to trust Google and Jetbrains to know what they're doing and seeing as there is no proper documentation on said property, let's leave it in.

这个值似乎是可选的,但是我通常倾向于相信Google和Jetbrains知道他们在做什么,并且看到关于所述属性的适当文档,让我们保留它。

handlers is a set of values that defines which script handles which URL pattern. URL patterns are regular expressions, and this part of the YAML file can be used to define routes.

handlers是一组值,这些值定义哪个脚本处理哪个URL模式。 URL模式是正则表达式,YAML文件的这一部分可用于定义路由。

At this point, you can optionally enter the Project Settings in PhpStorm, and under Google App Engine for PHP you can enter your Google Account username and password, so that you don't get prompted for them when deploying.

此时,您可以选择在PhpStorm中输入项目设置,在PHP的Google App Engine下,您可以输入您的Google帐户用户名和密码,这样在部署时不会提示您输入它们。

测试应用程序 (Testing the application)

We have two ways of testing our "application".

我们有两种测试“应用程序”的方法。

Please note that you HAVE to run the first way at least once before you try the second one, because the first one asks you a question about updating Python which PHPStorm doesn’t currently forward. So if you find your PHPStorm version not working, try to run it via terminal first, say “yes”, then shut it down, and try with PHPStorm again. Thanks to Dave Wyers for this tip.

请注意,在尝试第二种方法之前,您必须至少以第一种方法运行一次,因为第一种方法会询问有关更新PHPStorm当前未转发的Python的问题。 因此,如果您发现PHPStorm版本不起作用,请尝试首先通过终端运行它,说“是”,然后将其关闭,然后再次尝试使用PHPStorm。 感谢Dave Wyers的技巧。

  1. Running from the terminal

    从终端运行
  2. Running from PhpStorm

    从PhpStorm运行

从终端运行应用程序 (Running the app from the terminal)

To run the server from the terminal, enter the following:

要从终端运行服务器,请输入以下内容:

{google_appengine folder}/dev_appserver.py {project folder}

or, in my case specifically when I replace the braced values with real ones:

或者,就我而言,特别是当我用实数替换大括号的值时:

google_appengine/dev_appserver.py gae01/

This runs the application server and points it at the folder provided as the first parameter. Two things happen when you do this: an administration console is run at http://localhost:8000 and the app itself is run at http://localhost:8080. If you visit the admin console, you'll see a plethora of useful information from memcache contents to cronjobs and more:

这将运行应用程序服务器,并将其指向作为第一个参数提供的文件夹。 执行此操作时,会发生两件事:管理控制台在http://localhost:8000运行,而应用程序本身在http://localhost:8080 。 如果您访问管理控制台,则会看到大量有用的信息,从内存缓存内容到cronjobs等等:

alt

Pointing it at http://localhost:8080 will show "Hello World" in the browser.

将其指向http://localhost:8080将在浏览器中显示“ Hello World”。

Note that if you get a The path specified with the --php_executable_path flag () does not exist. error when running the app, this means the app server doesn't know where php-cgi is, and thus cannot run it. Simply add it to the execution line:

请注意,如果您获得了The path specified with the --php_executable_path flag () does not exist. 运行应用程序时出现错误,这意味着应用程序服务器不知道php-cgi在哪里,因此无法运行它。 只需将其添加到执行行即可:

google_appengine/dev_appserver.py --php_executable_path=/usr/bin/php-cgi gae01/

Finding out where your php-cgi is is easy – type whereis php-cgi and if it's in your $PATH variable, its location will be shown in the terminal.

查找您的php-cgi很容易–键入whereis php-cgi ,如果它在$ PATH变量中,则它将在终端中显示其位置。

从PhpStorm运行应用 (Running the app from PhpStorm)

To run it from PhpStorm, click the "Edit Configurations" option in the "Select Run/Debug Configuration" menu:

要从PhpStorm运行它,请在“选择运行/调试配置”菜单中单击“编辑配置”选项:

alt

Most of the information in the dialog that pops up will already be populated. The only missing value will be the "Path to php-cgi executable" which you can find out by typing whereis php-cgi into your terminal. On Linux, mine was at /usr/bin/php-cgi.

弹出的对话框中的大多数信息将已经被填充。 唯一缺少的值是“ php-cgi可执行文件路径”,您可以通过在终端中输入whereis php-cgi来查找。 在Linux上,我的位于/usr/bin/php-cgi

alt

After hitting Apply and OK, click the green arrow next to the "Select Run/Debug Configuration" menu, or just press Shift + F10 as a shortcut to the "Run default" command. The PhpStorm console should output the information about the server it just ran and http://localhost:8000 and http://localhost:8080 should now both be accessible, with the latter producing a "Hello World" output when visited in the browser.

单击“应用”和“确定”后,单击“选择运行/调试配置”菜单旁边的绿色箭头,或者仅按Shift + F10作为“运行默认”命令的快捷方式。 PhpStorm控制台应该输出有关它刚运行的服务器的信息,并且现在应该都可以访问http://localhost:8000http://localhost:8080 ,当在浏览器中访问时,后者会产生“ Hello World”输出。

alt

Running through PhpStorm has the additional advantage of being able to set breakpoints and do step by step debugging. Just click near a line number in your code (in the gutter of the editor window) and a red ball should appear. That's a breakpoint. When running your app, the server will pause as soon as it reaches this point, allowing you to inspect currently defined variables, memory consumption, and more.

通过PhpStorm运行还具有能够设置断点并逐步调试的其他优点。 只需在代码中的行号附近单击(在编辑器窗口的装订线中),就会出现一个红色的球。 那是一个断点。 运行您的应用程序时,服务器将在到达此点时立即暂停,使您可以检查当前定义的变量,内存消耗等。

部署方式 (Deployment)

There are three ways to deploy our application to GAE:

有三种方法可以将我们的应用程序部署到GAE:

1) Through the terminal/command line 2) Through PhpStorm 3) Through Git

1)通过终端/命令行2)通过PhpStorm 3)通过Git

通过终端部署 (Deployment through terminal)

Uploading the app through terminal is as simple as calling {google_appengine}/appcfg.py update {project folder} or in my particular case google_appengine/appcfg.py gae01/.

通过终端上载应用程序就像调用{google_appengine}/appcfg.py update {project folder}一样简单,在我特定的情况下就是google_appengine/appcfg.py gae01/

通过PhpStorm进行部署 (Deployment through PhpStorm)

To deploy from PhpStorm, go to Tools -> Google App Engine for PHP -> Upload App Engine PHP app…

要从PhpStorm进行部署,请转至工具-> Google App Engine for PHP->上载App Engine PHP应用程序…

alt

If you've entered your credentials in the Project Settings, the process will proceed automatically without any questions. Otherwise, you'll be prompted to enter your username and password, and the plugin will then call the same command you call manually when deploying through terminal.

如果您在“项目设置”中输入了凭据,则该过程将自动进行而不会出现任何问题。 否则,将提示您输入用户名和密码,然后该插件将调用通过终端部署时手动调用的相同命令。

通过Git部署(推送部署) (Deployment through Git (Push-to-Deploy))

Push-to-deploy is still in an experimental state right now, but it's well worth your time and effort to master it.

推送部署目前仍处于试验状态,但是值得您花时间和精力来掌握它。

To set up PTD, you need to enter the cloud console and find your app there.

要设置PTD,您需要输入云控制台并在其中找到您的应用。

alt

Click on your app's name, scroll to "Cloud Development" in the left menu, and click on "Create a New Repo".

单击您的应用程序的名称,在左侧菜单中滚动到“ Cloud Development”,然后单击“ Create a New Repo”。

alt

Save the repo URL and click "Get Password". This should produce a string not unlike machine code.google.com login {user} password {pass} with {user} and {pass} replaced with your own data. Copy this string into a file called .netrc which you should create in your home folder. If you're on Windows, it should be called _netrc. Windows also needs to have the HOME variable defined, which you can do by calling setx HOME %USERPROFILE%.

保存回购网址,然后单击“获取密码”。 这应该产生一个与machine code.google.com login {user} password {pass}的字符串machine code.google.com login {user} password {pass}{user}{pass}替换为您自己的数据。 将此字符串复制到一个名为.netrc的文件中,该文件应在主文件夹中创建。 如果您使用的是Windows,则应将其命名为_netrc 。 Windows还需要定义HOME变量,您可以通过调用setx HOME %USERPROFILE%

All you need to do now is link the remote url to the repo you have on your local machine (either add remote or clone directly from the repo URL the cloud console gave you before) and push it live. Since the app we created in this article already exists, let's make a Git repo on top of it and add a remote url to it. To prevent IDE junk from being uploaded alongside our source files, add this .gitignore file to the folder.

现在您需要做的就是将远程URL链接到本地​​计算机上的存储库(添加远程或直接从云控制台之前给您的存储库URL克隆)并将其推送。 由于我们在本文中创建的应用程序已经存在,因此让我们在其顶部创建一个Git存储库,并为其添加一个远程URL。 为防止将IDE垃圾文件与我们的源文件一起上传,请将此.gitignore文件添加到该文件夹​​中。

cd gae01
git init
git add .
git commit -am 'Initial commit'
git remote add appengine REPO_URL

Replace REPO_URL with the repo url the cloud console gave you. What the last command does is it adds a remote link to the local repo that binds it to the remote repo. This link is called "appengine" – appengine is just a label, you can choose your own, but there's not much point in using something other than the self explanatory default demonstrated here.

将REPO_URL替换为云控制台提供给您的仓库URL。 最后一个命令的作用是将远程链接添加到本地存储库,并将其链接到远程存储库。 此链接称为“ appengine” – appengine只是一个标签,您可以选择自己的标签,但是使用此处演示的自我解释默认值以外的其他东西没有多大意义。

Whenever you make changes, use the repo as you usually would. You can even set up a Github repo on the side, and add another remote link to this local repository. This would allow you to have version control on Github if you're used to it more than Google Code, while still deploying to GAE via PTD when you pull changes from Github and push to the appengine link.

每当您进行更改时,都可以像往常一样使用存储库。 您甚至可以在侧面设置一个Github存储库,并向该本地存储库添加另一个远程链接。 如果您不习惯使用Google代码,这将使您可以在Github上进行版本控制,而当您从Github提取更改并推送到Appengine链接时,仍可以通过PTD部署到GAE。

To push the app online now, just call

要立即将应用在线发布,只需致电

git push appengine master

Note that you can have as many branches as you want, just like in any Git repo, but only pushing the master branch will actually deploy to the live environment. Also, your source code is protected from prying eyes, the repo is not public and is visible only to admins and contributors. However, should a user disable PTD and turn it on again, the repo is trashed and a new one is created – there is no way to restore the old one. Be careful about whom you give permission to.

请注意,您可以根据需要拥有任意数量的分支,就像在任何Git存储库中一样,但是只有推送master分支才会实际部署到实时环境中。 而且,您的源代码受到保护,不会被窥探,回购协议不是公开的,并且仅对管理员和贡献者可见。 但是,如果用户禁用PTD并再次将其打开,则该存储库将被丢弃,并创建一个新的存储库–无法恢复旧的存储库。 请小心您允许的对象。

Visiting the appropriate URL now ( your-app-id.appspot.com ) should give you the Hello World message, and clicking on the repo name in the cloud console will give you a version history list and an overview of the source code.

现在访问适当的URL(your-app-id.appspot.com)应该会给您Hello World消息,然后在云控制台中单击回购名称将为您提供版本历史记录列表和源代码概述。

结论 (Conclusion)

Google App Engine is an excellent way to deploy high-traffic highly scalable applications. Their free quotas allow for some 5 million views a month, easily enough for any app, and scaling past the quota once needed is only a mouse click away.

Google App Engine是部署高流量,高度可扩展的应用程序的绝佳方法。 他们的免费配额每月允许大约500万次观看,对于任何应用程序来说都足够容易,只需单击鼠标即可超出所需的配额。

In this article we've covered basic deployment with and without an IDE, and explained the Push-To-Deploy mechanics. Stay tuned for more advanced topics coming soon and let us know your thoughts in the comments below!

在本文中,我们介绍了带有和不带有IDE的基本部署,并介绍了“推送到部署”机制。 请继续关注即将推出的更多高级主题,并在下面的评论中让我们知道您的想法!

翻译自: https://www.sitepoint.com/google-app-engine-php-getting-started/

app engine 入门

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值