×
Community Blog Build Multiple Websites on CentOS 7

Build Multiple Websites on CentOS 7

This article describes how to use NGINX to build multiple websites on an ECS instance that runs CentOS 7.

By Alibaba Cloud ECS Team

This article describes how to use NGINX to build multiple websites on an ECS instance that runs CentOS 7.

Prerequisites

Background Information

This tutorial is intended for users who are familiar with Linux and want to improve O&M efficiency by making efficient use of resources and managing sites in a centralized manner. For example, you can configure multiple blogging platforms of different categories or build multiple websites for sophisticated businesses on an instance.

In this tutorial, the Testpage-1 and Testpage-2 sites are simultaneously built on an instance deployed with an LNMP environment and then accessed.

The following instance configurations are used in the example:

  • Instance type: ecs.c6.large
  • Operating system: CentOS 7.8 64-bit

Create Test Sites

1.  Connect to the instance that is deployed with an LNMP environment. For more information, see Connect to a Linux instance by using a password.

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 information of the test websites, which is the project code.

mkdir Testpage-1
mkdir Testpage-2

4.  Configure information of Testpage-1.

a) Run the following command to go to Testpage-1:

cd /usr/share/nginx/html/Testpage-1/

b) Run the following command to create and edit the index.html file:

vim index.html

c) Press the I key to switch to the edit mode and 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 the edit mode.

5.  Configure information of Testpage-2.

a) Run the following command to go to Testpage-2:

cd /usr/share/nginx/html/Testpage-2/

b) Run the following command to create and edit the index.html file:

vim index.html

c) Press the I key to switch to the edit mode and 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 the edit 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.

include /etc/nginx/conf.d/*.conf; indicates that NGINX will obtain site information from all files in the .conf format in this path, as shown in the following figure.

1

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

cd /etc/nginx/conf.d

3.  Create and configure the NGINX configuration file for Testpage-1.

a) Run the following command to create and edit the configuration file:

vim Testpage1.conf

b) Press the I key to switch to the edit mode and enter the following content.

For the commented content, replace the server domain name and the project path with your actual parameter values.

server {
    listen       80;
    server_name  testpage1.com;    #The test domain name is used here. 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 site path is used here. 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 the edit mode.

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

a) Run the following command to create and edit the configuration file:

vim Testpage2.conf

b) Press the I key to switch to the edit mode and enter the following content.

For the commented content, replace the server domain name and the project path with your actual parameter values.

server {
    listen       80;
    server_name  testpage2.com;    #The test domain name is used here. 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 site path is used here. 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 the edit mode.

5.  Run the following command to restart the NGINX server:

systemctl restart nginx

(Optional) Configure the hosts file on the local host

You must configure IP mapping in the local hosts file because all of the information used in this tutorial is test values. If you use the actual server domain name when you configure the sites, skip this step. In this tutorial, the local physical machine uses the Windows operating system.

1.  Go to the C:WindowsSystem32driversetc directory.

2.  Copy the hosts file for backup.

Keep the hosts - copy file to restore the hosts file to its initial status after the configuration is completed.

3.  Modify the hosts file.

Append the following content to the end of the file:

<The public IP address of the instance> testpage1.com
<The public IP address of the instance> testpage2.com

Save the file and exit.

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

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

6.  Run the following command in the command line to immediately apply the configurations of the hosts file:

ipconfig /flushdns

Result

You can access the two test sites from a browser on the local host.

If you go to testpage1.com/, you can view the content of the Testpage-1 site, as shown in the following figure.testpage1

2

If you go to testpage2.com/, you can view the content of the Testpage-2 site, as shown in the following figure.testpage2

3

Multiple websites have been built. In your actual operation, you only need to make sure that the domain names and project paths are correctly configured, and then you can access these websites.

0 1 0
Share on

Alibaba Cloud Community

879 posts | 198 followers

You may also like

Comments

Alibaba Cloud Community

879 posts | 198 followers

Related Products