Give each website its own folder and its own Apache virtual host file, turn it on with a2ensite and reload Apache. Then add free SSL with Certbot.
example.com and example.net with your own domains. Each domain needs an A record that points to your server's IP address.1. Create a folder for each website
sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/example.net/public_html
Give the folders to the user you upload files with, so you don't need sudo for every file. $USER is the user you're logged in as:
sudo chown -R $USER:$USER /var/www/example.com/public_html /var/www/example.net/public_html
Apache only needs to read the files. New folders get 755 and new files 644 by default, which is what it needs: no chmod required.
2. Add a test page
A small page per site shows you later which site answers:
echo '<h1>example.com works</h1>' > /var/www/example.com/public_html/index.html
echo '<h1>example.net works</h1>' > /var/www/example.net/public_html/index.html
3. Create a virtual host file for each website
Open a new file for the first site:
sudo nano /etc/apache2/sites-available/example.com.conf
Paste this, then save with Ctrl+O, Enter and close with Ctrl+X:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
- ServerName and ServerAlias: the addresses this site answers to.
- DocumentRoot: the folder with the site's files.
- AllowOverride All: lets the site use
.htaccessfiles, which WordPress and most PHP apps need. - ErrorLog and CustomLog: separate logs per site in
/var/log/apache2/.
Create /etc/apache2/sites-available/example.net.conf the same way, with example.net in every line.
4. Turn the sites on
sudo a2ensite example.com.conf
sudo a2ensite example.net.conf
sudo a2enmod rewrite
a2ensite switches each site on. a2enmod rewrite turns on the module that .htaccess redirects and pretty links use. Optional: switch off Apache's default page so it doesn't answer for unknown names:
sudo a2dissite 000-default.conf
Check the settings and reload Apache:
sudo apache2ctl configtest
sudo systemctl reload apache2
configtest must end with Syntax OK. List the sites Apache now serves:
sudo apache2ctl -S
Each domain should appear with its own .conf file.
5. Test the sites
When the DNS of both domains points to the server, open http://example.com and http://example.net. Each shows its own test page.
To test before DNS is ready, ask the server directly with the domain name as the host:
curl -H "Host: example.com" http://203.0.113.10
curl -H "Host: example.net" http://203.0.113.10
Or point the domains to the server on your own computer only, by adding a line to your hosts file: C:\Windows\System32\drivers\etc\hosts on Windows (edit it as administrator), /etc/hosts on macOS and Linux:
203.0.113.10 example.com www.example.com example.net www.example.net
Remove the line again when you're done testing, or you won't see real DNS changes later.
6. Add free SSL with Let's Encrypt
Certbot gets free certificates from Let's Encrypt, adds the HTTPS settings to your virtual hosts and renews the certificates automatically. Port 443 must be open (sudo ufw allow 'Apache Full') and the domains must already point to the server.
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
sudo certbot --apache -d example.net -d www.example.net
Certbot asks for an email address and for you to accept the terms the first time. Then test the automatic renewal:
sudo certbot renew --dry-run
It should report that all simulated renewals succeeded. sudo certbot certificates lists your certificates and their expiry dates.
Add another website later
Repeat steps 1 to 6 for the new domain: folder, test page, .conf file, a2ensite, configtest, reload, Certbot.
Common problems
Every domain shows the same site
Apache uses the first site in the list when no ServerName or ServerAlias matches. Check the spelling in your .conf files, check with sudo apache2ctl -S that the site is on, and reload Apache.
"403 Forbidden"
Apache can't read the folder, or there's no index.html or index.php in it. Check the folder permissions (ls -ld /var/www/example.com/public_html) and the path in DocumentRoot.
.htaccess rules are ignored
The site's <Directory> block needs AllowOverride All, and the rewrite module must be on (sudo a2enmod rewrite, then reload).
Certbot fails with a connection or "unauthorized" error
Let's Encrypt couldn't reach your site over HTTP. Check that the domain's A record points to this server (dig +short example.com) and that port 80 is open. If you only just changed DNS, wait until the change has spread and try again.
Related articles
Still stuck? Open a support ticket and we'll help.