Build and deploy a PHP service

Learn how to create a simple Hello World application, package it into a container image, upload the container image to Container Registry, and then deploy the container image to Cloud Run. You can use other languages in addition to the ones shown.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Install and initialize the Cloud SDK.
  5. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  7. Install and initialize the Cloud SDK.

Writing the sample application

To write an application in PHP:

  1. Create a new directory named helloworld-php and change directory into it:

    mkdir helloworld-php
    cd helloworld-php
    
  2. Create a file named index.php and paste the following code into it (make sure you add <?php to the top of the file):

    
    $name = getenv('NAME', true) ?: 'World';
    echo sprintf('Hello %s!', $name);
    

    This code responds to requests with our "Hello World" greeting. HTTP handling is done by an Apache web server in the container.

  3. Create a new file named Dockerfile in the same directory as the source files. The PHP Dockerfile starts an Apache web server that listens on the port defined by the PORT environment variable:

    
    # Use the official PHP image.
    # https://hub.docker.com/_/php
    FROM php:8.0-apache
    
    # Configure PHP for Cloud Run.
    # Precompile PHP code with opcache.
    RUN docker-php-ext-install -j "$(nproc)" opcache
    RUN set -ex; \
      { \
        echo "; Cloud Run enforces memory & timeouts"; \
        echo "memory_limit = -1"; \
        echo "max_execution_time = 0"; \
        echo "; File upload at Cloud Run network limit"; \
        echo "upload_max_filesize = 32M"; \
        echo "post_max_size = 32M"; \
        echo "; Configure Opcache for Containers"; \
        echo "opcache.enable = On"; \
        echo "opcache.validate_timestamps = Off"; \
        echo "; Configure Opcache Memory (Application-specific)"; \
        echo "opcache.memory_consumption = 32"; \
      } > "$PHP_INI_DIR/conf.d/cloud-run.ini"
    
    # Copy in custom code from the host machine.
    WORKDIR /var/www/html
    COPY . ./
    
    # Use the PORT environment variable in Apache configuration files.
    # https://cloud.google.com/run/docs/reference/container-contract#port
    RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
    
    # Configure PHP for development.
    # Switch to the production php.ini for production operations.
    # RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
    # https://github.com/docker-library/docs/blob/master/php/README.md#configuration
    RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
    
  4. Add a .dockerignore file to exclude files from your container image.

    # The .dockerignore file excludes files from the container build process.
    #
    # https://docs.docker.com/engine/reference/builder/#dockerignore-file
    
    # Exclude locally vendored dependencies.
    vendor/
    
    # Exclude "build-time" ignore files.
    .dockerignore
    .gcloudignore
    
    # Exclude git history and configuration.
    .gitignore

Your app is finished and ready to be deployed.

Deploying to Cloud Run from source

Important: This quickstart assumes that you have owner or editor roles in the project you are using for the quickstart. Otherwise, refer to Cloud Run deployment permissions, Cloud Build permissions, and Artifact Registry permissions for the permissions required.

Deploy from source automatically builds a container image from source code and deploys it.

To deploy from source:

  1. Deploy from source using the following command:

    gcloud run deploy

    If prompted to enable the API, Reply y to enable.

    1. When you are prompted for the source code location, press Enter to deploy the current folder.

    2. When you are prompted for the service name, press Enter to accept the default name, helloworld.

    3. If you are prompted to enable the Artifact Registry API, respond by pressing 'y'.

    4. When you are prompted for region: select the region of your choice, for example us-central1.

    5. You will be prompted to allow unauthenticated invocations: respond y .

    Then wait a few moments until the deployment is complete. On success, the command line displays the service URL.

  2. Visit your deployed service by opening the service URL in a web browser.

Cloud Run locations

Cloud Run is regional, which means the infrastructure that runs your Cloud Run services is located in a specific region and is managed by Google to be redundantly available across all the zones within that region.

Meeting your latency, availability, or durability requirements are primary factors for selecting the region where your Cloud Run services are run. You can generally select the region nearest to your users but you should consider the location of the other Google Cloud products that are used by your Cloud Run service. Using Google Cloud products together across multiple locations can affect your service's latency as well as cost.

Cloud Run is available in the following regions:

Subject to Tier 1 pricing

  • asia-east1 (Taiwan)
  • asia-northeast1 (Tokyo)
  • asia-northeast2 (Osaka)
  • europe-north1 (Finland) leaf icon Low CO2
  • europe-west1 (Belgium) leaf icon Low CO2
  • europe-west4 (Netherlands)
  • us-central1 (Iowa) leaf icon Low CO2
  • us-east1 (South Carolina)
  • us-east4 (Northern Virginia)
  • us-west1 (Oregon) leaf icon Low CO2

Subject to Tier 2 pricing

  • asia-east2 (Hong Kong)
  • asia-northeast3 (Seoul, South Korea)
  • asia-southeast1 (Singapore)
  • asia-southeast2 (Jakarta)
  • asia-south1 (Mumbai, India)
  • asia-south2 (Delhi, India)
  • australia-southeast1 (Sydney)
  • australia-southeast2 (Melbourne)
  • europe-central2 (Warsaw, Poland)
  • europe-west2 (London, UK)
  • europe-west3 (Frankfurt, Germany)
  • europe-west6 (Zurich, Switzerland) leaf icon Low CO2
  • northamerica-northeast1 (Montreal) leaf icon Low CO2
  • northamerica-northeast2 (Toronto)
  • southamerica-east1 (Sao Paulo, Brazil) leaf icon Low CO2
  • us-west2 (Los Angeles)
  • us-west3 (Salt Lake City)
  • us-west4 (Las Vegas)

If you already created a Cloud Run service, you can view the region in the Cloud Run dashboard in the Cloud Console.

Congratulations! You have just deployed a container image from source code to Cloud Run. Cloud Run automatically and horizontally scales out your container image to handle the received requests, then scales in when demand decreases. You only pay for the CPU, memory, and networking consumed during request handling.

Clean up

Removing your test project

While Cloud Run does not charge when the service is not in use, you might still be charged for storing the container image in Artifact Registry. You can delete your image or delete your Cloud project to avoid incurring charges. Deleting your Cloud project stops billing for all the resources used within that project.

  1. In the Cloud Console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next

For more information on building a container from code source and pushing to a repository, see: