Run multiple websites from one server using Apache virtual hosts
By default, Apache serves one website from /var/www/html/. But you might want blog.yourdomain.com, shop.yourdomain.com, and yourdomain.com all on the same machine. Virtual hosts solve this.
A virtual host is a configuration block that tells Apache: "When a request comes in for this domain, serve files from this folder using these settings." Apache can have dozens of virtual hosts, each isolated from the others.
Name-based virtual hosts (what we use) distinguish sites by the domain name in the HTTP request. IP-based uses different IP addresses. Name-based is standard because it requires only one IP.
Each virtual host needs its own DocumentRoot — the folder Apache looks in for that site's files. Keeping them separate prevents one site from accidentally accessing another's files.
Virtual host configs live in /etc/apache2/sites-available/. You create a file there, then enable it with a2ensite. Enabled sites are symlinked to /etc/apache2/sites-enabled/.
Paste this inside:
Paste this inside (change the domain and path):
a2ensite creates a symlink from sites-available to sites-enableda2dissite 000-default disables the default placeholder siteWhen a browser sends a request, it includes the domain name in the Host header. Apache reads this header and matches it against the ServerName in each virtual host. The first match wins. If no match, the first virtual host alphabetically is used as the default.
You should see <h1>Blog</h1>
You should see <h1>Shop</h1>
Make sure DNS A records point each subdomain to your public IP. Then visit:
sudo a2dissite 000-default.conf is required.apache2ctl -S to see the order.