You can host a static website on an Object Storage Service (OSS) bucket and allow users to access the website by using the custom domain name such as example.com that is mapped to the bucket. This tutorial applies when you want to host an existing static website or create a website on an OSS bucket from scratch.
Step 1: Register a domain name
Before you build a static website, you must register a domain name for your website. We recommend that you use Alibaba Cloud Domains to register a domain name for your website. For more information, see How to register an Alibaba Cloud domain name.
In this example,
example.com is used as the domain name of the website.
Step 2: Create a bucket
You must create a public read bucket to host the static website and store website data.
- Log on to the OSS console.
- In the left-side navigation pane, click Buckets. On the Buckets page, click Create Bucket.
- In the Create Bucket panel, configure parameters for the bucket. The following table describes the parameters
that you must configure.
Parameter Description Bucket Name Specify the bucket name. In this example, specify the bucket name as examplebucket. Region Select the region where the bucket is located. In this example, select China (Hangzhou). Storage Class Select Standard. Access Control List (ACL) Select Public Read.
Retain the default values for other parameters. For more information, see Create buckets.
Step 3: Create and upload web page objects
You must create the default homepage and 404 error page objects for the website and upload the objects to the created bucket.
- Create two local HTML files.
- Default homepage file
In this example, the following content is used to generate the homepage file index.html of the static website. Customize the content of the homepage file based on your actual requirements.
<html> <head> <title>Hello OSS!</title> <meta charset="utf-8"> </head> <body> <P>Configure static website hosting for an OSS bucket</p> <P>This is the index page</p> </body> </html>
- Default 404 error document
In this example, the following content is used to generate the 404 error document error.html of the static website. Customize the content of the 404 error document based on your actual requirements.
<html> <head> <title>Hello OSS!</title> <meta charset="utf-8"> </head> <body> <P>This is the 404 page</p> </body> </html>
- Default homepage file
- Upload the web page objects to the bucket.
- Log on to the OSS console.
- In the left-side navigation pane, click Buckets. On the Buckets page, click the name of the bucket to which you want to upload the web page objects.
- In the left-side navigation pane of the page, click Files. On the page that appears, click Upload.
- In the Upload section of the Upload panel, click Upload and select the two web page objects that you created. Retain the default values for other parameters.
Step 4: Configure static website hosting
- In the left-side navigation pane, choose .
- Click Configure. Set Default Homepage to index.html and Default 404 Page to error.html.
- Click Save.
Step 5: Map the custom domain name to the bucket
Map the registered custom domain name
example.com to the examplebucket bucket that you create to use the domain name to access the
- On the Overview page of the bucket to which you want to map the custom domain name, choose in the left-side navigation pane.
- Click Bind Custom Domain Name.
- Enter example.com in the Custom Domain Name field and turn on Add CNAME Record Automatically.
- Click OK.
Step 5: (Optional) Accelerate access to your website by using Alibaba Cloud CDN
You can use Alibaba Cloud CDN to improve the performance of your website. Alibaba Cloud CDN caches the files of your websites, such as HTML files, images, and videos, to data centers around the world. When a visitor requests a file from your website, Alibaba Cloud CDN redirects the request to a copy of the file cached in the data center closest to the region in which the visitor is located. This way, the download is accelerated.
- On the Overview page of the bucket, choose in the left-side navigation pane.
- Click Not Configured on the right side of the domain name to go to the Alibaba Cloud CDN console.
- On the Add Domain Name page, configure the parameters. The following table describes the parameters.
Parameter Description Domain Name to Accelerate Use the default settings. Resource Group Select Default Resource Group. Business type Content delivery varies with business types. Select the appropriate business type based on your stored content and the content usage. Select Image and Small File in this example. Origin Info Use the default settings. Port Select the port used by CDN. Select Port 80 in this example. Region Select the region for which you want to accelerate access. Select Mainland China Only in this example.
- Click Next, and then click Return to Domain Name List.
- Record the CNAME value of the domain name example.com.w.kunlunsl.com. Modify the CNAME record that is added when you map the custom domain name to the
- Log on to the Alibaba Cloud DNS console.
- On the Manage DNS page, click Configure in the Actions column that corresponds to the domain name to which you want to add a CNAME record.
- Find the CNAME record that is automatically added when you map the custom domain name. Click Modify.
- Change Value to example.com.w.kunlunsl.com. Retain the default values for other parameters.
- Click OK.
- (Optional) On the Domain Names tab, turn on Auto CDN Cache Update. If you want that auto CDN cache update can be triggered by specified operations, you can submit a ticket to apply for the feature. After you are authorized to configure auto CDN cache update for specified operations, click Supported Operations in the Auto CDN Cache Update column that corresponds to the domain name and select the operations that can trigger auto CDN cache update. For more information about the supported operations, see Map accelerated domain names.
Step 6: Test the website
http://example.comto access the homepage of the static website. If static website hosting is correctly configured, a similar page is displayed.
http://example.com/example.txtto access an object that does not exist in the bucket. If static website hosting is configured, a similar page is displayed.
Step 7: Clean up resources
The resources created in this tutorial are used only to test the environment. We recommend that you clean up the created resources after the test is complete to avoid unnecessary fees.
- Delete the domain name that is mapped to the bucket. For more information, see Disable or remove an accelerated domain name.
- Delete the CNAME record that is added in Alibaba Cloud DNS. For more information, see Delete a DNS record.
- Delete created buckets and objects uploaded to the bucket. For more information, see Delete objects and Delete buckets.