Skip to main content

PHP Quick Start

Sample code:

https://github.com/TencentCloudBase/cloudbase-examples/tree/master/cloudbaserun/php

Or deploy to cloud hosting with one click:

Step 1: Write the Basic Application

Create a new directory named helloworld-php and navigate into it:

mkdir helloworld-php
cd helloworld-php

Create a file named index.php and paste the following code into it:

<?php
echo sprintf("Hello World!");

This code responds to all requests with "Hello World". HTTP requests are processed by the Apache Web server in the container.

Step 2: Containerize the Application

In the project root directory, create a file named Dockerfile with the following content:

# Use the official PHP image
# https://hub.docker.com/_/php
FROM php:8.4-apache

# Copy the local code to the container
COPY index.php /var/www/html/

# Apache uses port 80 in the configuration file
RUN sed -i 's/80/80/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf

# Configure PHP for Development Environment
# If you need to configure for a production environment, you can run the following command
# RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
# Reference: https://hub.docker.com/_/php#configuration
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

Add a .dockerignore file to exclude files from the container image:

Dockerfile
README.md
vendor

Step 3 (Optional): Build and Run Locally

If you have Docker installed locally, you can run the following command to build the Docker image locally:

docker build -t helloworld-php .

After a successful build, run docker images to view the built image.

REPOSITORY        TAG       IMAGE ID         CREATED           SIZE
helloworld-php latest 1c8dfb88c823 8 seconds ago 411MB

Then you can upload this image to your image repository.

Run the following command to start the container:

docker run -p 80:80  helloworld-python

Access http://localhost, you should see the "Hello World!" output.

Step 4: Deploy to CloudBase Cloud Run

If you have already installed the CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:

tcb cloudrun deploy

After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting. For more deployment methods, refer to Deploying Services.