Bisakah saya meng-host situs web php di firebase?

Pastikan penagihan diaktifkan untuk project Cloud Anda. Pelajari cara memeriksa apakah penagihan diaktifkan pada suatu proyek

  • Instal Google Cloud CLI
  • Untuk menginisialisasi gcloud CLI, jalankan perintah berikut

    gcloud init
    Catatan. Jika Anda menginstal gcloud CLI sebelumnya, pastikan Anda memiliki versi terbaru dengan menjalankan gcloud components update
  • Di Google Cloud Console, di halaman pemilih project, pilih atau buat project Google Cloud

    Catatan. Jika Anda tidak berencana untuk mempertahankan sumber daya yang Anda buat dalam prosedur ini, buatlah proyek daripada memilih proyek yang sudah ada. Setelah Anda menyelesaikan langkah-langkah ini, Anda dapat menghapus proyek, menghapus semua sumber daya yang terkait dengan proyek tersebut

    Pergi ke pemilih proyek

  • Pastikan penagihan diaktifkan untuk project Cloud Anda. Pelajari cara memeriksa apakah penagihan diaktifkan pada suatu proyek

  • Instal Google Cloud CLI
  • Untuk menginisialisasi gcloud CLI, jalankan perintah berikut

    gcloud init
    Catatan. Jika Anda menginstal gcloud CLI sebelumnya, pastikan Anda memiliki versi terbaru dengan menjalankan gcloud components update
    1. Untuk menyetel project default untuk layanan Cloud Run Anda.
       gcloud config set project PROJECT_ID
      Ganti PROJECT_ID dengan nama proyek yang Anda buat untuk quickstart ini

    Tulis contoh aplikasinya

    Untuk menulis aplikasi dalam PHP

    1. Buat direktori baru bernama

      gcloud init
      _0 dan ubah direktori ke dalamnya

      mkdir helloworld-php
      cd helloworld-php
      
      _
    2. Buat file bernama

      gcloud init
      _1 dan rekatkan kode berikut ke dalamnya (pastikan Anda menambahkan
      gcloud init
      2 ke bagian atas file)

      jalankan/helloworld/index. php

      Lihat di GitHub

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

      Kode ini menanggapi permintaan dengan sapaan "Hello World" kami. Penanganan HTTP dilakukan oleh server web Apache di dalam wadah

    3. Buat file baru bernama

      gcloud init
      _3 di direktori yang sama dengan file sumber. PHP Dockerfile memulai server web Apache yang mendengarkan port yang ditentukan oleh

      jalankan/helloworld/Dockerfile

      Lihat di GitHub

      
      # 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. Tambahkan file

      gcloud init
      5 untuk mengecualikan file dari gambar penampung Anda

      lari/helloworld/. dockerignore

      Lihat di GitHub

      # 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

    Aplikasi Anda sudah selesai dan siap untuk di-deploy

    Terapkan ke Cloud Run dari sumber

    Penting. Quickstart ini mengasumsikan bahwa Anda memiliki peran pemilik atau editor dalam proyek yang Anda gunakan untuk quickstart. Jika tidak, lihat , , dan untuk izin yang diperlukan

    Men-deploy dari sumber secara otomatis membuat image container dari kode sumber dan men-deploy-nya

    Untuk menerapkan dari sumber

    1. Di direktori kode sumber Anda, terapkan dari sumber menggunakan perintah berikut

      gcloud run deploy

      Jika diminta untuk mengaktifkan API, Balas y untuk mengaktifkan

      1. Saat Anda diminta untuk lokasi kode sumber, tekan Enter untuk menggunakan folder saat ini

      2. Saat Anda diminta untuk nama layanan, tekan Enter untuk menerima nama default,

        gcloud init
        6

      3. Jika Anda diminta untuk mengaktifkan Artifact Registry API, tanggapi dengan menekan

        gcloud init
        7

      4. Ketika Anda diminta untuk wilayah. pilih pilihan Anda, misalnya ________0______8

      5. Anda akan diminta untuk mengizinkan pemanggilan yang tidak diautentikasi. menanggapi

        gcloud init
        7

      Kemudian tunggu beberapa saat hingga penerapan selesai. Jika berhasil, baris perintah menampilkan URL layanan

    2. Kunjungi layanan yang Anda terapkan dengan membuka URL layanan di browser web

    Lokasi Cloud Run

    Cloud Run bersifat regional, artinya infrastruktur yang menjalankan layanan Cloud Run Anda berada di region tertentu dan dikelola oleh Google agar tersedia secara redundan di semua zona dalam region tersebut

    Memenuhi persyaratan latensi, ketersediaan, atau daya tahan adalah faktor utama untuk memilih region tempat layanan Cloud Run dijalankan. Biasanya Anda dapat memilih wilayah yang terdekat dengan pengguna Anda, tetapi Anda harus mempertimbangkan lokasi wilayah yang digunakan oleh layanan Cloud Run Anda. Menggunakan produk Google Cloud secara bersamaan di beberapa lokasi dapat memengaruhi latensi dan biaya layanan Anda

    Cloud Run tersedia di region berikut

    Tunduk pada

    •  gcloud config set project PROJECT_ID
      0 (Taiwan)
    •  gcloud config set project PROJECT_ID
      1 (Tokyo)
    •  gcloud config set project PROJECT_ID
      2 (Osaka)
    •  gcloud config set project PROJECT_ID
      3 (Finlandia) CO2 rendah
    •  gcloud config set project PROJECT_ID
      4 (Madrid) CO2 rendah
    •  gcloud config set project PROJECT_ID
      5 (Belgia) CO2 rendah
    •  gcloud config set project PROJECT_ID
      6 (Belanda)
    •  gcloud config set project PROJECT_ID
      7 (Milan)
    •  gcloud config set project PROJECT_ID
      8 (Paris) CO2 rendah
    •  gcloud config set project PROJECT_ID
      9 (Tel Aviv)
    • gcloud init
      8 (Iowa) CO2 rendah
    • mkdir helloworld-php
      cd helloworld-php
      
      _1 (Carolina Selatan)
    • mkdir helloworld-php
      cd helloworld-php
      
      _2 (Virginia Utara)
    • mkdir helloworld-php
      cd helloworld-php
      
      _3 (Columbus)
    • mkdir helloworld-php
      cd helloworld-php
      
      _4 (Dallas)
    • mkdir helloworld-php
      cd helloworld-php
      
      5 (Oregon) CO2 rendah

    Tunduk pada

    • mkdir helloworld-php
      cd helloworld-php
      
      _6 (Hong Kong)
    • mkdir helloworld-php
      cd helloworld-php
      
      _7 (Seoul, Korea Selatan)
    • mkdir helloworld-php
      cd helloworld-php
      
      _8 (Singapura)
    • mkdir helloworld-php
      cd helloworld-php
      
      _9 (Jakarta)
    • -->
      <?php
      
      $name = getenv('NAME', true) ?: 'World';
      echo sprintf('Hello %s!', $name);
      
      0 (Mumbai, India)
    • -->
      <?php
      
      $name = getenv('NAME', true) ?: 'World';
      echo sprintf('Hello %s!', $name);
      
      1 (Delhi, India)
    • -->
      <?php
      
      $name = getenv('NAME', true) ?: 'World';
      echo sprintf('Hello %s!', $name);
      
      2 (Sydney)
    • -->
      <?php
      
      $name = getenv('NAME', true) ?: 'World';
      echo sprintf('Hello %s!', $name);
      
      3 (Melbourne)
    • -->
      <?php
      
      $name = getenv('NAME', true) ?: 'World';
      echo sprintf('Hello %s!', $name);
      
      4 (Warsawa, Polandia)
    • -->
      <?php
      
      $name = getenv('NAME', true) ?: 'World';
      echo sprintf('Hello %s!', $name);
      
      5 (London, Inggris Raya)
    • -->
      <?php
      
      $name = getenv('NAME', true) ?: 'World';
      echo sprintf('Hello %s!', $name);
      
      6 (Frankfurt, Jerman)
    • -->
      <?php
      
      $name = getenv('NAME', true) ?: 'World';
      echo sprintf('Hello %s!', $name);
      
      7 (Zurich, Swiss) CO2 rendah
    • -->
      <?php
      
      $name = getenv('NAME', true) ?: 'World';
      echo sprintf('Hello %s!', $name);
      
      8 (Montreal) CO2 rendah
    • -->
      <?php
      
      $name = getenv('NAME', true) ?: 'World';
      echo sprintf('Hello %s!', $name);
      
      9 (Toronto) CO2 rendah
    • 
      # 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"
      
      0 (Sao Paulo, Brasil) CO2 rendah
    • 
      # 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"
      
      _1 (Santiago, Chili)
    • 
      # 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"
      
      _2 (Los Angeles)
    • 
      # 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"
      
      _3 (Kota Salt Lake)
    • 
      # 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 (Las Vegas)

    Jika sudah membuat layanan Cloud Run, Anda dapat melihat region di dasbor Cloud Run di Google Cloud Console

    OKE

    Selamat. Anda baru saja men-deploy image container dari kode sumber ke Cloud Run. Cloud Run secara otomatis dan horizontal menskalakan image container Anda untuk menangani permintaan yang diterima, lalu menskalakannya saat permintaan menurun. Anda hanya membayar CPU, memori, dan jaringan yang digunakan selama penanganan permintaan

    Membersihkan

    Hapus proyek pengujian Anda

    Meskipun Cloud Run tidak mengenakan biaya saat layanan tidak digunakan, Anda mungkin masih dikenakan biaya untuk menyimpan image container di Artifact Registry. Anda dapat atau menghapus proyek Cloud Anda untuk menghindari timbulnya biaya. Menghapus project Cloud Anda akan menghentikan penagihan untuk semua resource yang digunakan dalam project tersebut

    Bisakah saya menghosting situs web saya dengan Firebase?

    Firebase Hosting mendukung semua jenis konten untuk hosting, mulai dari file CSS dan HTML hingga Express. layanan mikro atau API js . Setiap file yang Anda unggah di-cache di SSD di tepi CDN di seluruh dunia dan berfungsi sebagai gzip atau Brotli.

    Mana yang lebih baik PHP atau Firebase?

    Artikel ini akan mengeksplorasi perbedaan antara PHP vs Firebase. . Perbedaan antara Firebase dan PHP

    Bisakah saya menghosting situs web PHP saya?

    Skrip PHP hanya dapat ditafsirkan pada server dengan PHP terinstal, jadi jika Anda ingin menikmati manfaat hosting PHP, Anda harus memilih host web yang mendukung . .

    Bisakah Firebase menghosting MySQL?

    Dengan Firebase MySQL Integration, Anda dapat menggunakan UI Firebase yang fleksibel, andal, dan sederhana dengan database yang andal seperti MySQL, semuanya dalam satu paket .