This tutorial shows how to get started with Compute Engine.
Follow this tutorial by deploying a Hello World PHP web
app to Compute Engine. For help getting started with App Engine, see
the [App Engine standard environment](https://docs.cloud.google.com/php/getting-started/tutorial-app).

## Objectives

- Use Cloud Shell to download and deploy a Hello World sample app.
- Deploy a Hello World sample app to a single Compute Engine instance.

## Costs


In this document, you use the following billable components of Google Cloud:


- [Compute Engine](https://cloud.google.com/compute/all-pricing)


To generate a cost estimate based on your projected usage,
use the [pricing calculator](https://docs.cloud.google.com/products/calculator).
New Google Cloud users might be eligible for a [free trial](https://docs.cloud.google.com/free).

<br />

## Before you begin

1.
   In the Google Cloud console, open the app in [Cloud Shell](https://docs.cloud.google.com/shell).


   [Go to Cloud Shell](https://docs.cloud.google.com/console/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/getting-started-php.git&working_dir=gce)


   Cloud Shell provides command-line access to your cloud resources
   directly from the browser.
2. If you agree to clone the repository, click
   **Confirm** to download the sample code and change into the app
   directory.

3. In Cloud Shell, configure the gcloud CLI to use your new Google Cloud project:

   ```bash
   # Configure gcloud for your project
   gcloud config set project YOUR_PROJECT_ID
   ```


## Running the app in Cloud Shell


1. Install your app dependencies using `composer`:

   ```php
   composer install
   ```
2. Run the [PHP built-in web server](https://www.php.net/manual/en/features.commandline.webserver.php):

   ```php
   php -S localhost:8080
   ```
3. In Cloud Shell, click **Web preview**
   ,
   and select **Preview on port 8080**. This opens a new window with your running app.

4. To stop the local web server, press <kbd>Control+C</kbd>.

<br />

## Deploying to a single instance


This section walks you through running a single instance of your app
on Compute Engine.

![Single-instance deployment.](https://docs.cloud.google.com/static/languages/images/compute-single-instance.svg)

From Cloud Shell, you can deploy to a single Compute Engine
instance virtual machine (VM) which runs your app.

### Use a startup script to initialize an instance

You need a way to instruct your instance to download and run your code.
An instance can have a startup script that runs whenever the instance is
started or restarted.


```bash
set -e
export HOME=/root

# Install PHP and dependencies from apt
apt-get update
apt-get install -y git nginx php7.2 php7.2-fpm php7.2-mysql php7.2-dev \
    php7.2-mbstring php7.2-zip php-pear pkg-config

# Install Composer
curl -sS https://getcomposer.org/installer | \
    /usr/bin/php -- \
    --install-dir=/usr/local/bin \
    --filename=composer

# Get the application source code
git clone https://github.com/googlecloudplatform/getting-started-php /opt/src
ln -s /opt/src/gce /opt/app

# Run Composer
composer install -d /opt/app --no-ansi --no-progress --no-dev

# Disable the default NGINX configuration
rm /etc/nginx/sites-enabled/default

# Enable our NGINX configuration
cp /opt/app/config/nginx/helloworld.conf /etc/nginx/sites-available/helloworld.conf
ln -s /etc/nginx/sites-available/helloworld.conf /etc/nginx/sites-enabled/helloworld.conf
cp /opt/app/config/nginx/fastcgi_params /etc/nginx/fastcgi_params

# Start NGINX
systemctl restart nginx.service

# Install Fluentd
curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash

# Enable our Fluentd configuration
cp /opt/app/config/fluentd/helloworld.conf /etc/google-fluentd/config.d/helloworld.conf

# Start Fluentd
service google-fluentd restart &
```

The startup script performs these tasks:

- Installs PHP 7 and PHP 7 FPM.

- Installs and configures NGINX.

- Downloads Composer and runs it for the app code.

- Installs the Cloud Logging agent and configures it to
  monitor the app logs. This means that the logging
  configured in the previous steps of this tutorial are uploaded just as if you
  were using App Engine.

<br />

### Create and configure a Compute Engine instance


1. Create a Compute Engine instance:


   ```bash
   MY_INSTANCE_NAME='my-app-instance'
   ZONE=us-central1-f

   gcloud compute instances create $MY_INSTANCE_NAME \
       --image-family=ubuntu-1804-lts \
       --image-project=ubuntu-os-cloud \
       --machine-type=g1-small \
       --scopes userinfo-email,cloud-platform \
       --metadata-from-file startup-script=scripts/startup-script.sh \
       --zone $ZONE \
       --tags http-server
   ```

   <br />


   Replace the zone with a development zone, for
   example `us-central1-a`. For more information on regions and zones, see
   [Geography and regions](https://docs.cloud.google.com/docs/geography-and-regions.md.txt).

   This creates a new instance, allows it to access Google Cloud
   services, and runs your startup script. The instance name is
   `my-app-instance`.
2. Check the progress of the instance creation:

   ```
   gcloud compute instances get-serial-port-output my-app-instance --zone YOUR_ZONE
   ```


   Replace `YOUR_ZONE` with the zone you deployed your
   instance to.

   When the startup script is complete, you see the following message:

   ```
   startup-script: INFO Finished running startup scripts.
   ```
3. Create a firewall rule to allow traffic to your instance:


   ```php
   gcloud compute firewall-rules create default-allow-http-80 \
       --allow tcp:80 \
       --source-ranges 0.0.0.0/0 \
       --target-tags http-server \
       --description "Allow port 80 access to http-server"
   ```

   <br />

4. Get the external IP address of your instance:

   ```php
   gcloud compute instances list
   ```
5. To see your app running, enter this URL in your browser:

   ```
   http://YOUR_INSTANCE_IP
   ```

   Replace `YOUR_INSTANCE_IP` with the external IP
   address of your instance.

<br />

### Manage and monitor an instance

You can use the Google Cloud console to monitor and manage your instance.

1. In the Google Cloud console, go to the **VM instances** page.

   [Go to VM instances](https://console.cloud.google.com/compute/instances)
2. In the list of virtual machine instances, click **SSH** in the row of the instance that you want to connect to.
3. To view all of the logs generated by your Compute Engine resources,
   go to the **Logs Explorer** page.

   [Go to Logs Explorer](https://console.cloud.google.com/logs/query)


   [Cloud Logging](https://docs.cloud.google.com/logging) is automatically configured to gather logs from
   various common services, including `syslog`.

<br />

<br />

## Clean up


To avoid incurring charges to your Google Cloud account for the resources used in this
tutorial, either delete the project that contains the resources, or keep the project and
delete the individual resources.

### Delete the project

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

1. In the Google Cloud console, go to the **Manage resources** page.

   [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)
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.

### Delete the individual resources

```bash
gcloud compute instances delete my-app-instance --zone=YOUR_ZONE --delete-disks=all
gcloud compute firewall-rules delete default-allow-http-80
```

## What's next

- [Create managed instance groups](https://docs.cloud.google.com/compute/docs/instance-groups/creating-groups-of-managed-instances.md.txt).
- [Create a load balancer](https://docs.cloud.google.com/load-balancing/docs/https/https-load-balancer-example).
- [Handle sessions with Firestore](https://docs.cloud.google.com/php/getting-started/session-handling-with-firestore.md.txt).
- [Add background processing with Pub/Sub and Cloud Run](https://docs.cloud.google.com/php/getting-started/background-processing.md.txt).
- [Deploy an app to Google Kubernetes Engine](https://docs.cloud.google.com/kubernetes-engine/docs/quickstarts/deploy-app-container-image).
- [Authenticate users with Identity-Aware Proxy](https://docs.cloud.google.com/php/getting-started/authenticate-users.md.txt).