edit-icon download-icon

How to add a website in Linux Web environment

Last Updated: Feb 26, 2018

How to add a site by using Tomcat

Note: The configuration path mentioned in this document is based on the installation path in the standard environment of one-click installation package for Web environment running ECS Linux [professional edition] provided by Alibaba Cloud. If you add a Tomcat site in a custom environment, perform configuration based on the actual installation path.

  1. Run cd /alidata/server/nginx/conf/vhosts/ to enter the directory of the bound domain name, and find the configuration file.

  2. Run cp test.conf new.conf to make a copy of the configuration file. Run vim new.conf to create a configuration file. You can rename it to “test.conf”.

  3. Click the letter I to edit the file. Enter the following content:

    1. listen 80 default;
    2. server_name www.new.com # Name of the bound domain.
    3. index index.html index.htm index.jsp; # Set the default homepage.
    4. root /alidata/www/webb/; # Directory of the specified website, which must be the same as that set by Tomcat.
    5. location ~ \.php$ {
    6. proxy_pass http://127.0.0.1:8080;
    7. }
    8. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    9. {
    10. expires 30d;
    11. }
    12. location ~ .*\.(js|css)?$
    13. {
    14. expires 1h;
    15. }
    16. access_log /alidata/log/nginx/access/default.log;
    17. }
  4. Press Esc to exit the editing mode. Enter wq to save the settings and exit.

  5. Enter /alidata/server/nginx/sbin/ngins -s restart to restart the Nginx test.

  6. Go to the /alidata/server/tomcat7/conf/ directory to modify the server.xml file.

  7. Press Shift+G to switch to the bottom of the page (at about line 124).

  8. Change localhost in Host name="localhost" to the target domain name.

  9. Use docBase to specify the Web root directory, and add a group of hosts. Save the settings and exit.

    Note: The Web root directory specified by docBase must be the same as that configured in Nginx. If you configure only one site, you do not need to set docBase. If you configure multiple sites, respectively specify the Web root directories.

  10. Restart Tomcat.

    restart Tomcat

  11. Enter the website domain name in the browser and test the settings.

How to add a site by using Apache

The Apache configuration file is generally located in the /etc/httpd/conf directory. httpd.conf is the main configuration file of Apache. When you configure the site, you can separately configure the configuration file of the instance, for example, name it as “vhost.conf”. Add Include /etc/httpd/conf/vhost.conf to the http.conf file to include the configuration file vhost.conf.

Note: If you add an Apache site in a custom environment, perform configuration based on the actual installation path.

Multiple websites can be built on one server in any of the following ways:

1. Based on the IP address (not applicable)

Scenario: This method is applicable when a server has multiple IP addresses.

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

2. Based on the port

Scenario:

  • This method is applicable when different ports are used to identify different websites.
  • This method is applicable when the website domain names are insufficient but the server port numbers are sufficient.

How to achieve: Use the website address and port number to access a site. After this method is used, you must add a port number at the end of a website domain name to access a website.

For example,

Defect: A port number must be added to the website, which is inconvenient for user access.

3. Based on the host name

Scenario: This method is applicable when multiple websites are built on a server.

How to achieve: Use different domain names for different websites. All resolved domain names point to the same IP address. Apache uses the host parameter included in the HTTP header to determine which website is to be accessed. For example,

Instruction: Take CentOS 6.5 as an example to describe how to configure an instance running Apache based on the host name.

  1. Start the vhost module of Apache. In the http.conf configuration file, find the following code line, delete # in the front, and start the vhost module.

    #LoadModule vhost_alias_module modules/mod_vhost_alias.so

  2. Create a vhost.conf file in the configuration folder of Apache as the configuration file of the instance.

  3. Compile the content of the instance, and add the default file header.

    1. NameVirtualHost *:80
    2. ServerName *
    3. DocumentRoot /www/html #Website directory by default
  4. Configure the following items as required:

    • Monitor the HTTP requests on port 80 from any address.

      1. ServerAdmin test@huigher.cn #Contact information of the website administrator?
      2. DocumentRoot "/var/www/html/test1" #Website directory
      3. ServerName test1.huigher.cn #Host name, through which Apache identifies different websites
      4. ErrorLog "logs/test1.huigher.cn-error_log" #Path of the error logs
      5. CustomLog "logs/test1.huigher.cn-access_log" common #Path of the access logs
    • If the error code 403 is returned, add the following code in the vhost.conf file. The reason is that you do not have the permission to access the directory.

      1. <Directory /var/www/html/test1>
      2. Options FollowSymLinks
      3. AllowOverride None
      4. Order deny,allow
      5. Deny from all
      6. </Directory>
  5. Configure the mentioned information for each website on the server, and modify the content based on the actual conditions.

  6. Add Include /etc/httpd/conf/vhost.conf to the http.conf file to include the configuration file vhost.conf.

  7. Bind the website domain name to a local loopback address in the /etc/hosts file.

    1. 127.0.0.1 test1.huigher.cn
    2. 127.0.0.1 test2.huigher.cn
    3. 127.0.0.1 test3.huigher.cn
  8. Use service httpd reload to reload the configuration file, or use service httpd restart to restart the Apache program.

  9. The test results indicate that different website contents are returned by different domain names.

If the problem persists, log on to Alibaba Cloud community for free consultation, or contact Alibaba Cloud Marketplace sellers for help.

Thank you! We've received your feedback.