The Google Cloud CLI includes a local development server
(dev_appserver.py
). You can use the local development server to simulate
running your App Engine application in production as well as use it to
access App Engine bundled services.
The simulated environment enforces some sandbox restrictions, such as restricted system functions and PHP 5 module imports, but not others, like request timeouts or quotas.
The local development server also simulates the services provided by the libraries in the SDK for App Engine, including Datastore, Memcache, and Task Queues, by performing their tasks locally. When your application is running in the development server, you can still make remote API calls to the production infrastructure using Google APIs HTTP endpoints.
Set up the local development server
To run the local development server tool, you must set up the following:
Verify that you have installed both Python interpreters on your machine:
- A Python 2 interpreter of version 2.7.12 or later
- A Python 3 interpreter that is compatible with the Python version required by the gcloud CLI
Both Python interpreters are required even if your app is written in another runtime or language version.
Verify that you have installed the gcloud component that includes the App Engine extension for Python 2 (
app-engine-python
):gcloud components list
If the
app-engine-python
component is not installed, run the following command:gcloud components install app-engine-python
Set the
CLOUDSDK_DEVAPPSERVER_PYTHON
environment variable in your shell to the path of your Python 2 interpreter.Locate the installation path of Google Cloud SDK by running the following command:
gcloud info
The command returns the root directory where you installed Google Cloud SDK, for example:
Installation Root: [/Users/myname/google-cloud-sdk]
Locate the
dev_appserver.py
tool under the/bin/
folder where you installed Google Cloud CLI, for example:CLOUD_SDK_ROOT/bin/dev_appserver.py
Note the path to the
dev_appserver.py
for later.
Run the local development server
After setting up the local development server and creating the
app.yaml
configuration file for your app, you can use the dev_appserver.py
command to
run your app locally.
To start the local development server:
In the directory that contains your
app.yaml
configuration file, run thedev_appserver.py
command.Windows / macOS
Specify the directory path to your app, for example:
python3 [CLOUD_SDK_ROOT]/bin/dev_appserver.py [PATH_TO_YOUR_APP]
Alternatively, you can specify the configuration file of a specific service, for example:
python3 [CLOUD_SDK_ROOT]/bin/dev_appserver.py app.yaml
To change the port, you include the
--port
option:python3 [CLOUD_SDK_ROOT]/bin/dev_appserver.py --port=9999 [PATH_TO_YOUR_APP]
Linux / cust. php-cgi
If you're on Linux or if you want to use a custom version of
php-cgi
, you need to specify the directory path to thephp-cgi
:Build your own version of App Engine PHP Extension on your local machine.
Start the local development server with both the
--php_executable_path
and--php_gae_extension_path
options:python3 CLOUD_SDK_ROOT/bin/dev_appserver.py --php_executable_path=[PATH_TO_PHP_CGI] --php_gae_extension_path=[PATH_TO_APPENGINE_EXTENSION] app.yaml
where
--php_executable_path
is the location of the PHP interpreter you are using, and where--php_gae_extension_path
is the location of thegae_runtime_module.so
file that you built in the previous step.
To learn more about the
dev_appserver.py
command options, see Local Development Server Options.The local development server is now running and listening for requests. You can visit http://localhost:8080/ in your web browser to see the app in action.
If you specified a custom port with the
--port
option, remember to open your browser to that port.
To stop the local server from the command line, press the following:
- macOS or Linux: Control+C
- Windows: Control+Break
Specify application IDs
To access your App ID in the local server, for example to spoof an email
address, use the
AppIdentityService::getApplicationId
function. To get the hostname of the running app, use the
AppIdentityService::getDefaultVersionHostname
function.
Detecting application runtime environment
To determine whether your code is running in production or in the local
development server, you can check the value of the SERVER_SOFTWARE
environment
variable:
if (strpos(getenv('SERVER_SOFTWARE'), 'Development') === 0){
echo 'Local development server';
} else {
echo 'Production';
}
Store data
App Engine for PHP supports reading and writing to
Cloud Storage via PHP's streams
API. You can read and write to Cloud Storage by specifying a
Cloud Storage URI (gs://
) when using any PHP function that supports
PHP Streams implementation such as
fopen,
fwrite or
file_get_contents.
The local development server emulates this functionality by reading and writing to temporary local files that are preserved between requests.
Browse the local Datastore
If your app has written data to your local Datastore using the local development server, you can browse it in the local development console.
To browse local Datastore:
Access the Datastore Viewer in the local development console. (The URL is
http://localhost:8000/datastore
.)View your local Datastore contents.
Use the Users service
App Engine provides a
Users Service to simplify
authentication and authorization for your application. The local development
server simulates the behavior of Google
Accounts
with its own sign-in and sign-out pages. While running under the local
development server, the
createLoginURL
and createLogoutURL
functions return URLs for /_ah/login
and /_ah/logout
on the local server.
Use Mail
The local development server can send email for calls to the App Engine mail service using either an SMTP server or a local installation of Sendmail.
Using SMTP
To enable mail support with an SMTP server, invoke dev_appserver.py
as
follows::
python3 [CLOUD_SDK_ROOT]/bin/dev_appserver.py --smtp_host=smtp.example.com --smtp_port=25 \
--smtp_user=ajohnson --smtp_password=k1tt3ns [PATH_TO_YOUR_APP]
where you set the --smtp_host
, --smtp_port
, --smtp_user
and
--smtp_password
options with your own values.
Using Sendmail
To enable mail support with Sendmail, invoke dev_appserver.py
as follows:
python3 [CLOUD_SDK_ROOT]/bin/dev_appserver.py --enable_sendmail=yes [PATH_TO_YOUR_APP]
The local server will use the sendmail
command to send email messages with
your installation's default configuration.
Use URL Fetch
When your application uses the URL fetch API to make an HTTP request, the local development server makes the request directly from your computer. The URL Fetch behavior on the local server may differ from production App Engine if you use a proxy server for accessing websites.
Debug locally with XDebug
If you have a debugger that is compatible with the
XDebug debugger, and you have the xdebug
module
installed, you can use XDebug with the local development server.
To enable XDebug on the Development Server on Linux or macOS:
Export the
XDEBUG_CONFIG
environment variable with an idekey for your IDE to connect toexport XDEBUG_CONFIG="idekey=netbeans-xdebug remote_host=localhost"
Invoke the Development Server with
--php_remote_debugging=yes