Chapter 9: Domain Names, DNS and Subdomains
9.8 Configure the web server for the domain
DNS brings the visitor to your IP. The web server must now recognise the name.
# Nginx: /etc/nginx/conf.d/example.conf (Ubuntu: sites-available/example + symlink)
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example;
index index.html;
location / { try_files $uri $uri/ =404; }
}
# Apache: /etc/httpd/conf.d/example.conf (Ubuntu: sites-available/example.conf + a2ensite)
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example
<Directory /var/www/example>
Require all granted
</Directory>
</VirtualHost>
sudo mkdir -p /var/www/example && echo '<h1>example.com is live!</h1>' | sudo tee /var/www/example/index.html
sudo nginx -t && sudo service nginx reload # Nginx
sudo apachectl configtest && sudo service httpd reload # Apache AL2023/CentOS
sudo apache2ctl configtest && sudo service apache2 reload # Apache Ubuntu
curl -I http://example.com
If you had put the public IP in server_name in Chapter 7, replace it with the domain names now.