In today’s digital landscape, efficient resource management is crucial, especially when running multiple websites. Consolidating them onto a single server using Apache simplifies management and reduces costs. In this blog, we’ll explore how to consolidate several websites onto one AWS EC2 server using Apache, walking through server setup, Apache configuration, and managing multiple websites under one roof.
1. Introduction to Server Consolidation
Server consolidation involves hosting multiple websites on a single physical or virtual server. This reduces infrastructure costs, streamlines server management, and optimizes server resources. Apache’s Virtual Hosts feature allows different websites to be served from the same server, making it an ideal solution for website consolidation.
2. Setting Up AWS EC2 Instances
Begin by launching an EC2 instance on AWS. Choose a Linux distribution such as Ubuntu or Amazon Linux for compatibility with Apache.
Steps:
- Log into the AWS Console.
- Navigate to EC2 services and click on “Launch Instance.”
- Select the appropriate Amazon Machine Image (AMI).
- Configure instance details, including storage and security group settings.
- SSH into the instance using the provided key pair.
ssh -i “your-key.pem” ubuntu@your-instance-ip
3. Installing and Configuring Apache
Once logged into the EC2 instance, install Apache to handle incoming HTTP requests.
sudo apt update
sudo apt install apache2
After installation, start and enable Apache to run on system boot.
sudo systemctl start apache2
sudo systemctl enable apache2
4. Downloading and Preparing Website Source Codes
Download the source code of all the websites you want to host. If you are migrating from different servers, use SSH to transfer files from the original servers to the new consolidated server.
scp -i “your-key.pem” -r user@old-server-ip:/path/to/website /var/www/
5. Organizing Source Code in Document Roots
To organize multiple websites, create separate directories for each site in the Apache document root.
sudo mkdir /var/www/website1.com
sudo mkdir /var/www/website2.com
Place each website’s source code in its respective directory. Ensure that Apache has proper ownership and permissions:
sudo chown -R www-data:www-data /var/www/website1.com
sudo chmod -R 755 /var/www/website1.com
6. Verifying Website Accessibility
At this point, Apache should serve as the default website. Test the server by navigating to your server’s public IP in a web browser. If Apache is running, you should see the default Apache page.
http://your-server-ip
7. Enhancing Accessibility with Local DNS Configuration
Configure DNS records for each domain to avoid accessing websites via IP addresses. Create “A” records pointing to your EC2 instance’s public IP address in your domain registrar’s control panel. You can modify your local machine’s /etc/host file to map the domains to the server’s IP for testing.
sudo nano /etc/hosts
Add entries such as:
your-server-ip website1.com
your-server-ip website2.com
8. Merging Websites: From Separate Servers to a Unified Domain
If your websites were on separate servers, it’s time to transfer all of them to your consolidated EC2 instance. Establish SSH connections to the old servers, copy the source code, databases, and configuration files, and transfer them to your consolidated server.
rsync -avz user@old-server-ip:/path/to/website /var/www/
9. Establishing SSH Connectivity Between Servers
Ensure the source and destination servers have SSH configured for secure transfers between servers. Use scp or rsync commands to transfer website files and data securely. Set up SSH keys for passwordless authentication if needed.
ssh-keygen -t rsa
ssh-copy-id user@destination-server-ip
10. Implementing Virtual Hosts for Efficient Resource Management
Apache’s Virtual Hosts allow you to serve multiple websites from a single server. Configure Virtual Hosts for each domain by creating separate configuration files in the /etc/apache2/sites-available/ directory.
sudo nano /etc/apache2/sites-available/website1.com.conf
Add the following configuration:
<VirtualHost *:80>
ServerAdmin admin@website1.com
ServerName website1.com
DocumentRoot /var/www/website1.com
ErrorLog ${APACHE_LOG_DIR}/website1.com-error.log
CustomLog ${APACHE_LOG_DIR}/website1.com-access.log combined
</VirtualHost>
Repeat this for all your websites. Enable each site configuration:
sudo a2ensite website1.com.conf
sudo a2ensite website2.com.conf
sudo systemctl reload apache2
11. Final Checks and Ensuring Seamless Operation
After configuring Virtual Hosts and transferring all websites, test each site by navigating to its domains in a web browser. Ensure each site loads correctly and write logs for troubleshooting purposes.
For added security and performance, consider implementing SSL certificates using Let’s Encrypt and optimizing Apache’s performance settings.
Conclusion
Consolidating multiple websites onto a single Apache server offers immense cost reduction and simplified management benefits. You can host multiple websites on a single server while ensuring seamless operation by leveraging AWS EC2, Apache Virtual Hosts, and secure SSH connectivity.