Voiced by Amazon Polly |
Overview
A reliable and scalable web hosting infrastructure is in high demand in today’s digital environment. Hosting several websites without sacrificing security or performance is a problem for businesses, and individuals are looking for efficient solutions. Amazon Elastic Compute Cloud (EC2), a robust and functional Amazon Web Services (AWS) service, fills this need.
This guide will show you how to set up and manage several websites on a single Amazon EC2 server, with step-by-step instructions and best practices to help you get the most out of Amazon EC2.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
We are using Ubuntu for this process and Apache2 for the web server. The capacity to host several websites on a single server is one advantage of utilizing Ubuntu and Apache. This is extremely cost-effective because it lets you use a single server for all domains.
Virtual hosting is the term for this function. It runs various domains, such as website1.com and website2.com, on a single Ubuntu server’s IP address.
The virtual host for Apache points users to various folders where the domain files are stored. No matter how many virtual hosts the server manages, the client accessing the website will never know.
On your Apache server running on Ubuntu, there are no limits on how many websites you can host. Nevertheless, confirm that your server can manage the traffic and disc space. So, let’s dive into the process.
Step-by-Step Guide
Step 1: Installing Apache web server
Firstly, install the Apache web server in your Ubuntu instance. Apache is a widely used, open-source web server software that serves web content to users upon request. For installing Apache, run the following command on your server.
1 2 |
sudo apt-get update sudo apt-get install apache2 |
You can confirm the installation by opening the IP address of your server in your browser. Copy the public IPv4 address and paste it into your browser.
When you open the IP address of your server, you should see the default Apache page, which will look like this.
Step 2: Setting up website file/directory structure
We will need to construct a directory structure to host the data for our website. Apache has a default directory structure, “/var/www” where it looks for websites. We will need to create sub-directories in this path for our domains so that the Apache server can look for them.
To do this, run the following commands in your terminal:
1 2 |
sudo mkdir -p /var/www/website1.com sudo mkdir -p /var/www/website2.com |
Step 3: Changing directory ownership
We need to change the ownership of the directories created above. The root user owns the directories created above, so we need to change their ownership to allow the currently logged user to modify the files. The web server has the appropriate permissions to access the website directories. To do that, run the following command in the terminal:
1 2 |
sudo chown -R $USER:$USER /var/www/website1.com sudo chown -R $USER:$USER /var/www/website2.com |
Step 4: Modifying file permission
The two directories that we previously created must be given read access. This will make the web pages accessible publicly, ensuring that our two websites are correctly served when a browser requests them.
1 |
sudo chmod -R 755 /var/www |
Step 5: Create sample content for both domains
We have now set up our files and directory structure properly. Using any file editor like Vim or Nano, create a sample index.html file for each website.
1 |
sudo nano /var/www/website1/index.html |
Add the following text in the editor:
1 |
<h1> Hi this is website 1 <h1> |
The same will be done for our second website.
1 |
sudo nano /var/www/website2/index.html |
Add the following text in the editor:
1 |
<h1> Hi this is website 2 <h2> |
Remember to save and close each file by CTRL+X and Y, then press enter.
Step 6: Create a virtual host configuration
By setting up virtual hosts in Apache, you can host numerous websites, each with its name and content, on a single server. Follow these steps to build a virtual host setup for each website:
- Open the Apache sites-available directory using the command:
1 |
sudo nano /etc/apache2/sites-available/website1.com.conf |
- Inside the configuration file, define the virtual host for your website. Here is a basic template for configuring a virtual host for “website1”:
1 2 3 4 5 6 7 8 |
<VirtualHost *:80> ServerAdmin Admin@website1.com DocumentRoot /var/www/website1.com ServerName website1.com ServerAlias www.website1.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> |
- ServerAdmin: Specify the email address of the website administrator.
- DocumentRoot: Define the directory where the website files are stored.
- ServerName: Specify the domain name of your website.
- ServerAlias: Add any additional domain names (if applicable) that should point to this website.
- ErrorLog and CustomLog: Configure the log file paths for error logging and access logging.
We need to repeat the same procedure for the website2.com virtual host.
1 |
sudo nano /etc/apache2/sites-available/website2.com.conf |
Then again, add the following content to the file:
1 2 3 4 5 6 7 8 |
<VirtualHost *:80> ServerAdmin webmaster@website2.com DocumentRoot /var/www/website2.com ServerName website2.com ServerAlias www.website2.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> |
Save the file and exit.
Step 7: Enable both virtual hosts
Now that we have two configuration files for our two virtual hosts, we need to create a symbolic link and enable the configuration of the virtual hosts by the commands below:
1 2 |
sudo a2ensite website1.com sudo a2ensite website2.com |
Step 8: Restart Apache
We now need to restart our Apache web server for the changes to take effect after adding virtual hosts to our server. We can do that by the command:
1 |
sudo systemctl restart apache2 |
Step 9: Edit your computer’s local hostfile
After following all of the above steps, you can access your virtual hosts. We need to modify the local host file on the local PC. This will make it possible for our local computer to find the right public IP address of our Amazon EC2 server with the desired content (our websites). You need to enter the public IP address of your Amazon EC2 server with the domains in the local host file so that when you enter the website domain, your browser can fetch the websites hosted in Amazon EC2 for you.
If you are running Linux, you need to edit the /etc/hosts file using the command below:
1 |
sudo nano /etc/hosts |
Add the below entries and save the file (assuming 1111.1111.1111.1111 as your Amazon EC2 instance public IP address).
1 2 |
1111.1111.1111.1111 website1.com 1111.1111.1111.1111 website2.com |
If your local computer is running on Windows OS, open the file c:\windows\system32\drivers\etc\hosts in a text editor like notepad and enter the two items below.
Remember to replace 1111.1111.1111.1111 with the public IP of your Amazon EC2 instance.
Step 10: Test your domain in your browser
Finally, it’s time to visit your websites, website1.com and website2.com, and you should see the content we created for the virtual hosts as shown below:
Conclusion
Hosting several websites on a single Amazon EC2 server can be a cheap and effective way to manage many online properties. You may construct a streamlined and scalable hosting environment with the proper configuration, which includes setting the web server, creating virtual hosts, and putting in place strong security measures. You can prepare your Amazon EC2 instance to host several websites using the procedures described in this article. This guarantees your diverse web apps a seamless and safe online presence.
Drop a query if you have any questions regarding Hosting websites on Amazon EC2 server and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, AWS EKS Service Delivery Partner, and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best-in-industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. Can I host unlimited websites on a single Amazon EC2 instance?
ANS: – While Amazon EC2 instances can handle multiple websites, the actual number depends on the size and traffic requirements of each website, as well as the instance’s available resources. Monitoring resource usage and considering scaling options as your hosting needs evolve is essential.
2. How can I ensure the security of my multiple websites hosted on a single Amazon EC2 server?
ANS: – Implementing robust security measures such as regularly updating software, configuring firewalls, utilizing SSL certificates, and managing user access can help enhance the security of your hosted websites.
3. What are the best practices for optimizing the performance of multiple websites on a single Amazon EC2 server?
ANS: – Optimizing performance involves implementing caching mechanisms, leveraging content delivery networks (CDNs), and fine-tuning the server configurations. Additionally, monitoring resource usage, identifying and resolving performance bottlenecks, and implementing efficient coding practices can improve website performance and user experience.
WRITTEN BY Ritushree Dutta
Click to Comment