All Products
Search
Document Center

:How to add a Web site to a Linux ECS instance

Last Updated:Mar 23, 2022

Overview

This topic describes how to add a Web site to a Linux ECS instance.

Description

Add a Tomcat site to a Linux ECS instance

Tip: The following content is used in this example. If you add a Tomcat site in a custom environment, configure the installation path.

  1. Run the following command to go to the directory where the bound domain name is located and find the configuration file:
    cd /alidata/server/nginx/conf/vhosts/
  2. Run the following command to copy the file:
    cp test.conf new.conf
  3. Run the following command and click i on the keyboard to edit the file:
    vim new.conf
  4. Add the following configuration in the code:
    listen 80 default; 
    server_name www.example.com ; # Bound website domain name
    index index.html index.htm index.jsp; # Set the default homepage
    root /alidata/www/webb/; # Specify the directory of the website, which needs to be the same
    location ~ \.php$ {
    proxy_pass http://127.0.0.1:8080;
    }
    location ~ .*\.(gif | jpg | jpeg | png | bmp | swf)$
    {
    expires 30d;
    }
    location ~ .*\.(js | css)?$
    {
    expires 1h;
    }
    access_log /alidata/log/nginx/access/default.log;
    }
  5. Click the Esc on the keyboard to exit the edit mode, then Save to exit.
  6. Run the following command to restart NGINX:
    /alidata/server/nginx/sbin/ngins -s restart
  7. Go to the /alidata/server/tomcat7/conf/ directory and modify the server.xml file.
  8. Click shift and g to go to the bottom of the page, about 124 lines. Change "localhost" in the Host name=“localhost" to the target domain name, use the docBase parameter to specify the Web root directory, add a set of hosts at the same time, and then save and exit.

    Note: The web root directory specified by the docbase parameter must be consistent with the NGINX configuration. When you configure only one site, you do not need to set docBase. When you set multiple sites, you need to specify Web root directory separately.
  9. Run the following command to restart Tomcat:
    service restart tomcat
  10. Enter the website domain name in the browser to confirm that the access is successful.

Add an Apache site to the Linux ECS instance

The configuration file of Apache is generally in the /etc/httpd/conf directory. httpd.conf is the main configuration file of Apache. When you configure the configuration file, you can configure the configuration file of the virtual host separately, such as vhost.conf. Then, add "Include /etc/httpd/conf/vhost.conf" to http.conf to include the configuration file of vhost.conf.

Note: If you want to add an Apache site in a custom environment, set the parameters based on the actual installation path.

IP address-based

Scenarios

This parameter is applicable when a server has multiple IP addresses.

Note: Currently, only one public IP address and one private IP address can be bound to Alibaba Cloud Elastic Compute Service. Therefore, this method is not applicable.

Port number-based

Scenarios
  • It is suitable for situations where different ports are used to identify different websites.
  • It is suitable for situations where the domain name of the website is short but the port number of the server is sufficient.
Implementation

When visiting a site, use the URL plus the port number. After configuring this mode, you need to add the port number at the end of the website domain to access different websites. as shown in the following code:

  • http://port.example.com:80
  • http://port.example.com:81
  • http://port.example.com:82

Tip: This method has disadvantages. You must add a port number to the website, which is not conducive to user access.

Based on the host name

Scenarios

This parameter is applicable when multiple websites run on one server.

Implementation

Different domain names are used to distinguish different websites. All domain names are resolved to the same IP address. Apache uses the host parameter included in the HTTP header to determine which website the user needs to visit. as shown in the following code:

  • http://test1.example.com
  • http://test2.example.com
  • http://test3.example.com
Example explanation

The CentOS 6.5 system is used as an example to describe how to configure an Apache virtual host based on a hostname.

  1. Open the vhost module of Apache. In the http.conf configuration file, find the following information, delete the "#" sign in front of it, and open the vhost module.
    #LoadModule vhost_alias_module modules/mod_vhost_alias.so
  2. Create the vhost.conf file in the Apache configuration folder to use as the configuration file of the virtual host.

  3. Edit the vhost.conf file and add the content of the virtual host.
    NameVirtualHost *:80 
    ServerName *
    DocumentRoot /www/html # is the directory of the website by default
  4. Add the following configuration to the vhost.conf file based on the actual situation:
    1. At any address, listen for HTTP requests on port 80.
      ServerAdmin username@example.com # contact information of the website administrator? 
      DocumentRoot "/var/www/html/test1" # The website directory
      the ServerName test1.example.com # hostname. apache uses this address to identify different website
      ErrorLog "log s/test1.example.com-error_log" # error log path
      CustomLog "log s/test1.example.com-access_log" common # Access log path
    2. If the access error message 403 because the access directory does not have permissions, add the following code:
      <Directory /var/www/html/test1>
      Options FollowSymLinks
      AllowOverride None
      Order deny,allow
      Deny from all
      </Directory>
  5. As many websites as there are on the server, configure as many copies of the above information respectively, and modify the contents according to the actual situation. The configuration on the server page is similar to the following.
  6. Add the following configuration to the http.conf file, including the content of the vhost.conf file.
    Include /etc/httpd/conf/vhost.conf
  7. In the /etc/hosts file, bind the domain name of the website to the local loopback address as follows.
    127.0.0.1 test1.example.com
    127.0.0.1 test2.example.com
    127.0.0.1 test3.example.com
  8. Run the following command to reload the configuration file:
    service httpd reload
    Note: You can also run the following command to restart the Apache process:
    service httpd restart
  9. Perform a test to confirm that different domain names are accessed and different website content is returned.

Applicable scope

  • Elastic Compute Service (ECS)