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 shows how 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 the 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.
If you want to map the registered domain name to a bucket that is located in a region in the Chinese mainland, you must apply for an ICP filing for the domain name at the Ministry of Industry and Information Technology (MIIT) of China. For more information, see Filing.
Step 2: Create a bucket
You must create a public read bucket to host a static website and store website data.
On the Buckets page of the OSS console, click Create Bucket.
In the Create Bucket panel, configure the parameters according to the following table. Retain the default configurations of other parameters that are not described in the table.
Parameter
Description
Bucket Name
The name of the bucket. In this example, set the bucket name to examplebucket.
Region
The region in which the bucket is located. In this example, the China (Hangzhou) region is selected.
Storage Class
Select Standard.
Access Control List (ACL)
Select Public Read.
For more information, see Create buckets.
Click OK.
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. Specify the content of the homepage file based on your 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 file
In this example, the following content is used to generate the 404 error file error.html of the static website. Specify the content of the 404 error file based on your requirements.
<html> <head> <title>Hello OSS!</title> <meta charset="utf-8"> </head> <body> <P>This is the 404 page</p> </body> </html>
Upload the web page objects to the bucket.
In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.
In the left-side navigation tree, choose .
Click Upload.
In the Upload panel, click Select Files and select the two web page files that you created in the Files to Upload section. Retain the default configurations of other parameters.
Click Upload.
Step 4: Configure static website hosting
In the left-side navigation tree, choose .
On the Static Pages page, click Settings. Set Default Homepage to index.html and Default 404 Page to error.html.
NoteTo redirect access to a subdirectory to the index.html object in the subdirectory, you can enable Subfolder Homepage. For more information, see Configure static website hosting.
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 created to access the bucket by using the domain name.
In the navigation tree, choose .
On the Domain Names page, click Map Custom Domain Name.
In the Map Custom Domain Name panel, enter example.com in the Custom Domain Name field, click confirm and enable Automatically Add CNAME Record.
Wait for the Domain Status to become In effect.
Step 5: (Optional) Accelerate access to your website by using Alibaba Cloud CDN
You can use Alibaba Cloud CDN to improve the performance of the website. Alibaba Cloud CDN caches the files of the website, such as HTML files, images, and videos, to data centers around the world. When a visitor requests a file from the 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.
Record the CNAME generated by the CDN-accelerated Domain Name.
In the navigation tree, choose .
On the Domain Names page, click Not Configured in the Alibaba Cloud CDN column of the domain name to go to the Alibaba Cloud CDN console.
On the Add Domain Name page, retain the default settings of all parameters. Click Next, and click Back to Domain Management.
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 bucket.
Resolve CDN-accelerated Domain Name.
On the Domain Name Resolution page of DNS console, find the domain name and click DNS Settings in the Actions column.
Find the CNAME record that is automatically added when you map the custom domain name. Click Modify in the Actions column.
On the Modify DNS Record page, change the value of Record Value to example.com.w.kunlunsl.com. Retain the default configurations for other parameters, then click OK.
Step 6: Test the website
Enter the following URLs in a browser to check whether the website runs as expected.
Visit
http://example.com
to access the homepage of the static website. If static website hosting is correctly configured, a similar page is displayed.Visit
http://example.com/example.txt
to 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 acceleration.
Delete the CNAME record that is added to Alibaba Cloud DNS. For more information, see Delete a DNS Record.
Delete the created buckets and objects that are uploaded to the bucket. For more information, see Delete objects and Delete buckets.