Point Two Domains to One VPS: Nginx & Apache
This guide shows you how to host two different domains on a single VPS using both Nginx and Apache. You’ll configure Nginx as the front-end reverse proxy, routing each domain to a separate Apache virtual host, so both sites run simultaneously on the same server. You’ll need root access to your VPS and the domain names already pointing to its IP address.
What You’ll Need
- A VPS with a public IP address (Ubuntu/Debian assumed).
- Two domain names (e.g., example.com and example.org) with DNS A records pointing to your VPS IP.
- SSH access with sudo privileges.
- Nginx and Apache installed (Apache listening on a different port, e.g., 8080).
Step-by-Step
- Verify DNS resolution – Run
dig +short example.comanddig +short example.orgfrom your local machine. Both should return your VPS IP. If not, wait for DNS propagation (can take up to 48 hours). - Install Nginx and Apache – If not already installed, run
sudo apt update && sudo apt install nginx apache2. Ensure Apache is configured to listen on port 8080 (edit/etc/apache2/ports.confand changeListen 80toListen 8080). - Create Apache virtual hosts – For each domain, create a config file in
/etc/apache2/sites-available/. Example for example.com (example.com.conf):<VirtualHost *:8080> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com <Directory /var/www/example.com> AllowOverride All Require all granted </Directory> </VirtualHost>Create the document root directory and add a test
index.htmlfile. Repeat for the second domain. - Enable the Apache sites – Run
sudo a2ensite example.com.confandsudo a2ensite example.org.conf. Then restart Apache withsudo systemctl restart apache2. - Configure Nginx as a reverse proxy – Create a config file for each domain in
/etc/nginx/sites-available/. Example for example.com (example.com):server { listen 80; server_name example.com www.example.com; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }Repeat for the second domain, using its own
server_name. - Enable the Nginx sites – Run
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/(and similarly for the second). Test the configuration withsudo nginx -t, then reload Nginx withsudo systemctl reload nginx. - Test both domains – Open a browser and visit
http://example.comandhttp://example.org. Each should show the respective test page. If not, check the troubleshooting section below.
Common Mistakes
- Apache still on port 80 – If Apache listens on 80, it will conflict with Nginx. Change it to 8080 and restart.
- Missing ServerAlias – Forgetting
wwwcan cause the non-www version to fail. Always include both. - DNS not propagated – Using a local hosts file to test before DNS updates can cause confusion. Wait for propagation or use
curl -H "Host: example.com" http://your-vps-ipto test. - Firewall blocking port 8080 – If you have a firewall, ensure port 8080 is open only to localhost (or not at all, since Nginx proxies to it).
Tips
- Use separate log files for each domain in both Apache and Nginx to simplify debugging.
- Enable SSL with Let’s Encrypt for both domains after the basic setup works. Nginx can handle the certificates and proxy HTTPS to Apache.
- Keep your Nginx configs in
/etc/nginx/sites-available/and symlink them; this makes disabling a site easy. - Use
sudo apachectl configtestandsudo nginx -tbefore reloading to catch syntax errors.
Troubleshooting
- Both domains show the same site – Check that each Apache virtual host has a unique
ServerNameand that Nginx proxies to the correct backend. Also ensure the Apache sites are enabled. - 502 Bad Gateway from Nginx – Apache may not be running or listening on 8080. Check with
sudo systemctl status apache2andsudo netstat -tulpn | grep 8080. - Connection refused – Nginx is likely not running or the config has errors. Run
sudo nginx -tand checksudo systemctl status nginx. - One domain works, the other doesn’t – Verify the DNS for the failing domain points to your VPS. Also check the Nginx and Apache configs for typos in the domain names.
- SSL certificate errors – If you added SSL, ensure the certificate covers both the www and non-www versions. Use certbot with the
-dflag for both.
Frequently Asked Questions
Can I use Apache as the front-end and Nginx as the backend?
Yes, but the setup would be reversed. The principle is the same: one server proxies to the other. Nginx is often preferred as the front-end due to its performance with static files and concurrent connections.
Do I need a separate IP for each domain?
No. With virtual hosting, you can host multiple domains on a single IP address. This is the whole point of the setup described.
Can I point more than two domains this way?
Yes, you can add as many domains as your server resources allow. Just repeat the steps for each additional domain, creating a new Apache virtual host and a new Nginx server block.
What if I want to use HTTPS?
Use Let’s Encrypt with certbot. Install certbot and the Nginx plugin, then run sudo certbot --nginx -d example.com -d www.example.com. Certbot will automatically obtain and configure SSL certificates for your domains.
How do I test the setup without changing DNS?
Use the curl command with the Host header: curl -H "Host: example.com" http://your-vps-ip. This sends the request to your VPS but tells it which domain you’re trying to reach.
Is it better to use only Nginx or only Apache?
For simplicity, using only one web server is easier. However, some applications require Apache-specific features (like .htaccess). This dual setup lets you keep those features while benefiting from Nginx’s performance.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.