What this is
Virtual hosts (Apache) and server blocks (Nginx) let you host multiple websites/domains on one VPS.
What it is for
- Run multiple domains on a single server
- Keep configurations separated and organized
Prerequisites
- Domain(s) pointing to your server IP
- Apache or Nginx installed
- Sudo access
Apache (VirtualHost)
Step 1) Create site directory
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com
Step 2) Create a simple index
echo "Hello from example.com" | sudo tee /var/www/example.com/public_html/index.html
Step 3) Create VirtualHost config
sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
Step 4) Enable site and reload
sudo a2ensite example.com.conf
sudo apachectl configtest
sudo systemctl reload apache2
Nginx (server block)
Step 1) Create config file
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Step 2) Enable site
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
Step 3) Test and reload
sudo nginx -t
sudo systemctl reload nginx
Final verification
curl -I http://example.com
Warnings & notes
- On some RHEL systems, Nginx configs are under
/etc/nginx/conf.d/. - For production, add HTTPS with Certbot after confirming HTTP works.
Conclusion
You can now host multiple sites cleanly using Apache VirtualHosts or Nginx server blocks.