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
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.
Run the following command to go to the configured website root directory:
cd /usr/share/nginx/html
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
Configure the information of the Testpage-1 website.
Run the following command to go to the Testpage-1 folder:
cd /usr/share/nginx/html/Testpage-1/
Run the following command to create and edit the
index.html
file:sudo vim index.html
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.
Configure the information of the
Testpage-2
website.Run the following command to go to the
Testpage-2
folder:cd /usr/share/nginx/html/Testpage-2/
Run the following command to create and edit the
index.html
file:sudo vim index.html
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
Run the following command to check the
nginx.conf
configuration file:cat /etc/nginx/nginx.conf
View the
include
configuration information in thehttp{}
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.Run the following command to go to the
/etc/nginx/conf.d
path:cd /etc/nginx/conf.d
Create and configure the NGINX configuration file for the
Testpage-1
website.Run the following command to create and edit the configuration file:
sudo vim Testpage1.conf
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.
Create and configure the NGINX configuration file for the
Testpage-2
website.Run the following command to create and edit the configuration file:
sudo vim Testpage2.conf
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.
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.
Go to the
C:\Windows\System32\drivers\etc
directory.Copy the hosts file.
Retain the
hosts - copy
file, which can be used to restore thehosts
file to its initial state after the test is complete.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.
Go back to the Windows desktop and press
Win+R
.In the Run dialog box, enter cmd and click OK.
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 theTestpage-1
website, as shown in the following figure.Access
testpage2.com/
to view the content of theTestpage-2
website, as shown in the following figure.
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.