All Products
Search
Document Center

Object Storage Service:Static website hosting (PHP SDK V1)

Last Updated:Mar 20, 2026

Configure a bucket to host a static website. Once configured, requests to the website endpoint serve content from the bucket and automatically redirect to the specified default homepage and default 404 page.

This page covers:

Prerequisites

Before you begin, ensure that you have:

  • The oss:PutBucketWebsite permission to configure static website hosting

  • The oss:GetBucketWebsite permission to view the configuration

  • The oss:DeleteBucketWebsite permission to delete the configuration

For more information about RAM (Resource Access Management) policies, see Common examples of RAM policies.

Note: Before using the SDK for static website configurations, call the PutBucketWebsite API operation to set up the static website. For more information, see PutBucketWebsite.

Usage notes

  • The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see Regions and endpoints.

  • All examples create an OSSClient instance using an OSS endpoint. To create an OSSClient instance using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • All examples use EnvironmentVariableCredentialsProvider to load credentials from environment variables. Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running the code.

Configure static website hosting

Imports

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
use OSS\Model\WebsiteConfig;

Sample code

$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint. This example uses the China (Hangzhou) region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket = "examplebucket";

// Set the default homepage to index.html and the default 404 page to error.html.
$websiteConfig = new WebsiteConfig("index.html", "error.html");

try {
    $config = array(
        "provider"         => $provider,
        "endpoint"         => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"           => "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

    $ossClient->putBucketWebsite($bucket, $websiteConfig);
    echo "Static website hosting configured successfully for bucket: " . $bucket . "\n";
} catch (OssException $e) {
    // Output the error message if the operation fails.
    echo $e->getMessage() . "\n";
}

View the static website hosting configuration

Imports

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

Sample code

$provider = new EnvironmentVariableCredentialsProvider();
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket   = "examplebucket";

try {
    $config = array(
        "provider"         => $provider,
        "endpoint"         => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"           => "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

    $websiteConfig = $ossClient->getBucketWebsite($bucket);
    echo "Retrieved website configuration for bucket: " . $bucket . "\n";
    echo $websiteConfig->serializeToXml() . "\n";
} catch (OssException $e) {
    // Output the error message if the operation fails.
    echo $e->getMessage() . "\n";
}

Delete the static website hosting configuration

Imports

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

Sample code

$provider = new EnvironmentVariableCredentialsProvider();
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket   = "examplebucket";

try {
    $config = array(
        "provider"         => $provider,
        "endpoint"         => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"           => "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

    $ossClient->deleteBucketWebsite($bucket);
    echo "Static website hosting configuration deleted for bucket: " . $bucket . "\n";
} catch (OssException $e) {
    // Output the error message if the operation fails.
    echo $e->getMessage() . "\n";
}

References