Mulai menggunakan PHP di Compute Engine

Tutorial ini menunjukkan cara memulai Compute Engine. Ikuti tutorial ini dengan men-deploy aplikasi web PHP Hello World ke Compute Engine. Untuk mendapatkan bantuan memulai App Engine, lihat lingkungan standar App Engine.

Tujuan

  • Gunakan Cloud Shell untuk mendownload dan men-deploy aplikasi contoh Hello World.
  • Deploy aplikasi sampel Hello World ke satu instance Compute Engine.

Biaya

Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih berikut:

Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda, gunakan kalkulator harga.

Pengguna Google Cloud baru mungkin memenuhi syarat untuk mendapatkan uji coba gratis.

Sebelum memulai

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Compute Engine API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. Di konsol Google Cloud , buka aplikasi di Cloud Shell.

    Buka Cloud Shell

    Cloud Shell menyediakan akses command line ke resource cloud Anda langsung dari browser.

  9. Jika Anda setuju untuk meng-clone repositori, klik Confirm untuk mendownload kode contoh dan beralih ke direktori aplikasi.

  10. Di Cloud Shell, konfigurasikan gcloud CLI untuk menggunakan project Google Cloud baru Anda:
    # Configure gcloud for your project
    gcloud config set project YOUR_PROJECT_ID

Menjalankan aplikasi di Cloud Shell

  1. Instal dependensi aplikasi Anda menggunakan composer:

    composer install
    
  2. Jalankan server web bawaan PHP:

    php -S localhost:8080
    
  3. Di Cloud Shell, klik Pratinjau web , lalu pilih Pratinjau di port 8080. Tindakan ini akan membuka jendela baru dengan aplikasi yang sedang berjalan.

  4. Untuk menghentikan server web lokal, tekan Control+C.

Men-deploy ke instance tunggal

Bagian ini akan memandu Anda menjalankan satu instance aplikasi di Compute Engine.

Deployment instance tunggal.

Dari Cloud Shell, Anda dapat men-deploy ke satu virtual machine (VM) instance Compute Engine yang menjalankan aplikasi Anda.

Menggunakan skrip startup untuk menginisialisasi instance

Anda memerlukan cara untuk menginstruksikan instance Anda agar mendownload dan menjalankan kode Anda. Instance dapat memiliki skrip startup yang berjalan setiap kali instance dimulai atau dimulai ulang.

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 &

Skrip startup akan melakukan tugas-tugas berikut:

  • Menginstal PHP 7 dan PHP 7 FPM.

  • Menginstal dan mengonfigurasi NGINX.

  • Mendownload Composer dan menjalankannya untuk kode aplikasi.

  • Menginstal agen Cloud Logging dan mengonfigurasinya untuk memantau log aplikasi. Artinya, logging yang dikonfigurasi pada langkah-langkah sebelumnya dalam tutorial ini diupload seolah-olah Anda menggunakan App Engine.

Membuat dan mengonfigurasi instance Compute Engine

  1. Buat instance Compute Engine

    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

    Ganti zona dengan zona pengembangan, misalnya us-central1-a. Untuk mengetahui informasi selengkapnya tentang wilayah dan zona, lihat Geografi dan wilayah.

    Tindakan ini akan membuat instance baru, mengizinkannya mengakses layanan Google Cloud, dan menjalankan skrip startup Anda. Nama instance-nya adalah my-app-instance.

  2. Periksa progres pembuatan instance:

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

    Ganti YOUR_ZONE dengan zona tempat Anda men-deploy instance.

    Setelah skrip startup selesai, Anda akan melihat pesan berikut:

    startup-script: INFO Finished running startup scripts.
    
  3. Buat aturan firewall untuk mengizinkan traffic ke instance Anda:

    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"
    

  4. Dapatkan alamat IP eksternal instance Anda:

    gcloud compute instances list
    
  5. Untuk melihat aplikasi Anda berjalan, masukkan URL ini di browser Anda:

    http://YOUR_INSTANCE_IP
    

    Ganti YOUR_INSTANCE_IP dengan alamat IP eksternal instance Anda.

Mengelola dan memantau instance

Anda dapat menggunakan konsol Google Cloud untuk memantau dan mengelola instance.

  1. Di konsol Google Cloud , buka halaman VM instances.

    Buka instance VM

  2. Di daftar instance virtual machine, klik SSH di baris instance yang ingin Anda hubungkan.
  3. Untuk melihat semua log yang dihasilkan oleh resource Compute Engine Anda, buka halaman Logs Explorer.

    Buka Logs Explorer

    Cloud Logging otomatis dikonfigurasi untuk mengumpulkan log dari berbagai layanan umum, termasuk syslog.

Pembersihan

Agar tidak perlu membayar biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam tutorial ini, hapus project yang berisi resource tersebut, atau simpan project dan hapus setiap resource.

Menghapus project

  1. Di Konsol Google Cloud , buka halaman Manage resources.

    Buka Kelola resource

  2. Pada daftar project, pilih project yang ingin Anda hapus, lalu klik Delete.
  3. Pada dialog, ketik project ID, lalu klik Shut down untuk menghapus project.

Menghapus resource satu per satu

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

Langkah berikutnya