All Products
Search
Document Center

Elastic Compute Service:build multiple websites on a Linux instance

Last Updated:Feb 26, 2024

You can configure multiple websites on an Elastic Compute Service (ECS) instance to reduce operational costs and facilitate website management, including updating software, configuring security settings, and backing up data. You can also flexibly adjust resource allocation as the requirements of the websites change. This topic describes how to use NGINX to build multiple websites on a Linux ECS instance.

Prerequisites

A Linux ECS instance is created and assigned a public IP address. An LNMP stack is deployed on the Linux instance. For information about how to deploy an LNMP stack on a Linux instance, see Deploy an LNMP stack on an Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, CentOS 7 or CentOS 8 instance or Manually build an LNMP environment on an Ubuntu 20 instance.

Create test websites

  1. Connect to the Linux instance on which an LNMP stack is deployed.

    For information about how to connect to an instance, see Connect to a Linux instance by using a password or key.

  2. Run the following command to go to the configured website root directory:

    cd /usr/share/nginx/html
  3. Run the following commands to create two test folders.

    The folders are used to store test website information, which is the project code of the test websites.

    sudo mkdir Testpage-1
    sudo mkdir Testpage-2
  4. Configure the information of the Testpage-1 website.

    1. Run the following command to go to the Testpage-1 folder:

      cd /usr/share/nginx/html/Testpage-1/
    2. Run the following command to create and edit the index.html file:

      sudo vim index.html
    3. Press the I key to enter Insert mode and then enter the following test content:

      Test page 1

      Press the Esc key, enter :wq, and then press the Enter key to save the file and exit Insert mode.

  5. Configure the information of the Testpage-2 website.

    1. Run the following command to go to the Testpage-2 folder:

      cd /usr/share/nginx/html/Testpage-2/
    2. Run the following command to create and edit the index.html file:

      sudo vim index.html
    3. Press the I key to enter Insert mode and then enter the following test content:

      Test page 2

      Press the Esc key, enter :wq, and then press the Enter key to save the file and exit Insert mode.

Configure NGINX

  1. Run the following command to check the nginx.conf configuration file:

    cat /etc/nginx/nginx.conf

    View the include configuration information in the http{} module.

    In this example, include /etc/nginx/conf.d/*.conf; shown in the following figure indicates that NGINX obtains site information from all .conf files in the path.nginx.conf

  2. Run the following command to go to the /etc/nginx/conf.d path:

    cd /etc/nginx/conf.d
  3. Create and configure the NGINX configuration file for the Testpage-1 website.

    1. Run the following command to create and edit the configuration file:

      sudo vim Testpage1.conf
    2. Press the I key to enter Insert mode and then enter the following content.

      In the commented content, replace the server domain name and the website path with the actual values.

      server {
          listen       80;
          server_name  testpage1.com;    #The test domain name. Use the domain name of your server in the actual configuration. 
      
          #charset koi8-r;
          access_log  /var/log/nginx/b.access.log  main;
      
          location / {
              root   /usr/share/nginx/html/Testpage-1;    #The test website path. Use the path of your project code in the actual configuration. 
              index  index.html index.htm;
          }
      
          #error_page  404              /404.html;
      
          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
              root   /usr/share/nginx/html;
          }
      }

      Press the Esc key, enter :wq, and then press the Enter key to save the file and exit Insert mode.

  4. Create and configure the NGINX configuration file for the Testpage-2 website.

    1. Run the following command to create and edit the configuration file:

      sudo vim Testpage2.conf
    2. Press the I key to enter Insert mode and then enter the following content.

      In the commented content, replace the server domain name and the website path with the actual values.

      server {
          listen       80;
          server_name  testpage2.com;    #The test domain name. Use the domain name of your server in the actual configuration. 
      
          #charset koi8-r;
          access_log  /var/log/nginx/b.access.log  main;
      
          location / {
              root   /usr/share/nginx/html/Testpage-2;    #The test website path. Use the path of your project code in the actual configuration. 
              index  index.html index.htm;
          }
      
          #error_page  404              /404.html;
      
          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
              root   /usr/share/nginx/html;
          }
      }

      Press the Esc key, enter :wq, and then press the Enter key to save the file and exit Insert mode.

  5. Run the following command to restart NGINX:

    sudo systemctl restart nginx

(Optional) Configure the hosts file on your on-premises host

All information used in this tutorial is only for reference. You must configure IP mappings in the hosts file on your on-premises host. If you use an actual server domain name when you configure the websites, skip this step. In this tutorial, an on-premises physical server that runs a Windows operating system is used.

  1. Go to the C:\Windows\System32\drivers\etc directory.

  2. Copy the hosts file.

    Retain the hosts - copy file, which can be used to restore the hosts file to its initial state after the test is complete.

  3. Modify the hosts file.

    Append the following content to the end of the hosts file:

    <Public IP address of the ECS instance> testpage1.com
    <Public IP address of the ECS instance> testpage2.com

    Save the hosts file and exit.

  4. Go back to the Windows desktop and press Win+R.

  5. In the Run dialog box, enter cmd and click OK.

  6. At the command prompt, run the following command to immediately apply the configurations of the hosts file:

    ipconfig /flushdns

Result

You can access the two test websites from a browser on your on-premises host.

  • Access testpage1.com/ to view the content of the Testpage-1 website, as shown in the following figure.testpage1

  • Access testpage2.com/ to view the content of the Testpage-2 website, as shown in the following figure.testpage2

Multiple websites are built. In actual scenarios, you need to only make sure that the domain names and project paths are correctly configured in the configuration files to allow access to the websites.

References

If you want to deploy multiple websites on a Windows instance, see Build multiple websites on a Windows instance.