Deploying several websites or domains on a single server provides tremendous flexibility and cost optimization. In this in-depth guide, we will learn how to configure the Apache and Nginx web servers to serve unlimited websites from one machine.
Why Virtual Hosting and What Are the Benefits
Hosting multiple sites on one server is made possible by a feature called Virtual Hosting (Apache) or Server Blocks (Nginx). It works by running multiple websites with different domain names and configurations in isolation on a single web server instance.
Some key benefits of virtual hosting are:
- Reduced hosting costs compared to individual servers for each site
- Better utilization of server resources like CPU, memory, disk instead of fragmented usage
- Simplifies migration of websites across machines
- Flexibility to choose different CMS/frameworks per site
- Easier maintenance with one server instead of many
As per surveys, over 75% of web hosting providers use virtualization to host customer websites. It enables them to reduce infrastructure costs by almost 60% while providing flexible and reliable hosting services.
Prerequisites for Hosting Multiple Sites
Before setting up multiple websites, the server should meet the recommended requirements for optimal performance.
Hardware Considerations
Here are some general hardware sizing guidelines:
10 websites or less
- 4 GB RAM
- 2 CPU cores
- 100 GB storage
Between 10 and 30 websites
- 8 GB RAM
- 4 CPU cores
- 500 GB storage
More than 30 large websites
- 16 GB RAM or more
- 8 CPU cores or more
- 1+ TB storage
Also ensure the server has gigabit ethernet connectivity and dedicated IP address.
Software Stack Setup
The host server should have the following software installed:
- Operating System – Ubuntu, Debian, CentOS etc.
- Web server – Apache or Nginx
- Scripting language – PHP, Python, Ruby
- Database server – MySQL, MongoDB etc.
Sample directory structure:
/var/www/example.com
/public_html
/images
/files
/logs
/backups
DNS Configuration
The DNS records for all domains should point to the host server‘s IP address. This enables routing requests for multiple domains to the same server.
Using domain management tools like CPanel, Cloudflare etc. makes it easier.
Security Best Practices
Some security considerations when hosting multiple websites:
- Strong firewall rules to allow only HTTP/HTTPS traffic
- OS hardening like SELinux policies, AppArmor for access control
- Disable unused modules/plugins in Apache/Nginx
- Install mod_security or WAFs to protect against attacks
- Mandatory HTTPS using SSL certificate
Now let‘s move on to the configuration steps…
Setting Up Apache Virtual Hosts
The Apache web server supports hosting an unlimited number of websites through virtual host configuration. All websites can be served from the same web server process using a single IP address.
Here is an in-depth guide on configuring Apache virtual hosts for multisite hosting:
Installing Apache Web Server
First, we install Apache 2.x if not already available:
sudo apt update
sudo apt install apache2
Verify that Apache is up and running using systemctl:
systemctl status apache2
Creating Document Root Directories
Now, create separate directories for serving website files:
sudo mkdir -p /var/www/example1.com/public_html
sudo mkdir -p /var/www/example2.com/public_html
Set the correct permissions:
sudo chown -R www-data:www-data /var/www
sudo chmod -R 755 /var/www
This ensures the Apache process owner can access and serve files.
Configuring Virtual Host Files
We configure each website domain using containers in Apache:
/etc/apache2/sites-available/example1.com.conf
<VirtualHost *:80>
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/example1.com/public_html
ErrorLog /var/www/example1.com/logs/error.log
CustomLog /var/www/example1.com/logs/access.log combined
</VirtualHost>
/etc/apache2/sites-available/example2.com.conf
<VirtualHost *:80>
ServerName example2.com
DocumentRoot /var/www/example2.com/public_html
ErrorLog /var/www/example2.com/logs/error.log
CustomLog /var/www/example2.com/logs/access.log combined
</VirtualHost>
This separates the configuration for each site clearly.
Enabling the Virtual Hosts
We enable the virtual host configs using:
sudo a2ensite example1.com.conf
sudo a2ensite example2.com.conf
Apache will now serve these sites.
Restarting Apache
Finally, restart Apache for changes to apply:
systemctl restart apache2
Now both websites should be live and accessible to visitors!
Securing, Optimizing and Customizing
Some additional best practices include:
- Enabling HTTPS using Certbot SSL certificates
- ModSecurity for filtering web attacks
- Improving caching and compression
- Monitoring website performance metrics
- Custom domain redirects, URL rewrites etc.
With virtual hosting, the customization possibilities are endless!
Next we will explore multi-domain hosting using Nginx…
Configuring Nginx Server Blocks
Like Apache, Nginx web server also supports hosting an unlimited number of websites out of the box using server blocks. Let‘s see how to set it up:
Installing Nginx
First install Nginx from the Ubuntu repos:
sudo apt update
sudo apt install nginx
Verify Nginx is up and running:
systemctl status nginx
Preparing Document Root Directories
Create the doc root dirs:
sudo mkdir -p /var/www/example1.com/html
sudo mkdir -p /var/www/example2.com/html
Set proper permissions:
sudo chown -R www-data:www-data /var/www
sudo chmod -R 755 /var/www
Adding Server Block Configurations
Add server blocks for each site under /etc/nginx/conf.d/:
/etc/nginx/conf.d/example1.com.conf
server {
listen 80;
listen [::]:80;
root /var/www/example1.com/html;
index index.html;
server_name example1.com;
}
/etc/nginx/conf.d/example2.com.conf
server {
listen 80;
listen [::]:80;
root /var/www/example2.com/html;
index index.php;
server_name example2.com;
}
We can have different index files and configs per website.
Reloading Nginx
Finally, reload Nginx for the changes to apply:
sudo systemctl reload nginx
Both websites should now be live and served from the configured server blocks.
Some additional performance optimization and security considerations –
- Enable caching and gzip compression where possible
- Monitor website metrics using tools like Nginx Amplify
- Redirect HTTP to HTTPS
- Protect against DDoS attacks
- Analyze access logs for errors
And many more customizations!
Additional Considerations for Hosting Multiple Sites
Here are some common requirements when managing numerous websites:
Mapping Subdomains
Additional websites can be hosted on subdomains like blog.mycompany.com, api.mycompany.com etc. This is easily achieved by adding subdomain DNS A records pointing to the server IP.
Then add the subdomain configuration in Apache or Nginx.
Domain Redirects
Set up permanent 301 redirects:
- From www to non-www
- HTTP to HTTPS
- Old domain to new domain
For SEO purposes, it is best practice to pick either www or non-www version.
Blocking Hotlinking
Hotlink protection blocks unauthorized use of resource files like images or videos by external sites. This protects the bandwidth and storage usage of hosted websites.
Hotlink protection can be implemented in Apache using mod_rewrite rules and Nginx using valid_referers.
Log File Analysis
It is important to store access logs and error logs for each website in separate files. This enables debugging issues easily and analyzing access patterns per site.
Enable log rotation, compression and central storage for long term analysis.
Tools like GoAccess, AWStats generate actionable insights from access logs.
Conclusion
In this extensive guide, we studied how to configure Apache and Nginx for hosting an unlimited number of websites on a single server using virtual hosts and server blocks respectively.
Some of the key takeways are:
- Virtual hosting reduces infrastructure costs significantly
- Carefully size server hardware capacity for optimum performance
- Separate configuration and files for isolation between sites
- Apply security best practices like HTTPS, firewall rules etc.
- Continuously monitor metrics and access logs of all sites
Both Apache and Nginx are highly flexible, stable and performance oriented web servers perfect for serving multiple websites from one Linux machine economically.
By mastering virtual host configuration, you can easily host unlimited domains and websites on a server like a pro!