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 a Basic Application

Create a new directory named helloworld-php and change to this directory:

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 will respond with "Hello World" to all requests, and the HTTP handling is performed by the Apache Web Server in the container.

Step 2: Containerize the Application

Create a file named Dockerfile in the project root directory with the following content:

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

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

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

# Configure PHP as a 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): Local Build and Run

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

You can then 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 Hosting

If you have installed 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.