Last week I set up my first domain name and attached it to a website, and it wasn't as hard as I thought. In this post I hope to enlighten you the steps you need to take in order to point a domain name to a web server. I will also show you how to configure the nginx software to display a webpage when someone types your domain name. As a bonus I describe how to set up TLS on your website using a certificate from Let's Encrypt.
I assume you have already bought the domain and are now trying to figure out how to use it.
You need to add an A record to the domain with a target of your IP address.
- Go to your domain registrar's control panel and navigate to the DNS records page.
Add a new record leaving
Host(or the name of the first field in your control panel) blank, leave
Time to Live(second field name) unchanged, and
Target(third field name) to your IP address.
This lets users access mydomainname.com, sending requests to the server on your IP address.
Optionally, you can add a new record with the same values as the first one, but with
www, this will allow users to access www.mydomainname.com as well.
Changes to DNS records take up to 24 hours to propagate to root name servers, but I've observed changes taking effect after only a few hours.
I usually use nginx as my web server software, so I will show you how to configure it.
- First you must install nginx:
apt-get install nginx. This also enables and starts the nginx service on your server, but if it didn't start for some reason, you can start it manually with
systemctl start nginx.
- Create a new WWW folder for your website in
/var/www/, such as
/var/www/mydomainname.com. This folder will store your website HTML/CSS/JS files. Also make an
html/subfolder in there:
mkdir -p /var/www/mydomainname.com/htmlcreates them both at once.
Copy the default index.html file to your newly created folder:
cp /var/www/html/index.nginx-debian.html /var/www/mydomainname.com/html/
index.nginx-debian.htmlis recognized as an index page, so don't worry whether it will be loaded as the homepage, it will.
Copy the default nginx configuration file to a new file for your own website:
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/mydomainname.com
Open the newly copied configuration file, and remove default_server from the listen directive, so replace
listen 80 default_server;with
listen [::]:80 default_server;with
server_name mydomainname.com www.mydomainname.com;. The www domain name is optional and enables serving for the www subdomain.
Right now we have a website configuration but it's not enabled yet. We can enable it by linking the configuration file to the
sites-enabledsubfolder like this:
ln -s /etc/nginx/sites-available/mydomainname.com /etc/nginx/sites-enabled/
Restart nginx for the changes to take effect:
systemctl restart nginx.
Now you can go to your website and it will display the default nginx landing page. But currently it only works with HTTP. Let's see how to make it work with HTTPS.
You can get TLS certificates for free, courtesy of Let's Encrypt. These certificates expire every 3 months.
The easiest way to get started with Let's Encrypt is head over to the Certbot website, and selecting your server operating system and hosting software. It will generate instructions to install the certificate specifically for your configuration. But, let's review the instructions for nginx running on Ubuntu here:
- Install certbot and the nginx plugin for certbot:
apt-get install certbot python3-certbot-nginx.
- In your server, run
certbot --nginx. This starts the interactive configuration for creating the certificate.
- When prompted, supply your email address and agree to the license terms, and choose whether you want to receive newsletters from Electronic Frontier Foundation (EFF).
- Certbot scans your nginx configuration for site names hosted on it. It presents a list of these names, and asks you to select the ones you want to enable HTTPS, essentially install the certificate, for.
- It puts a file on your web server, and then tries to download it from the internet, to verify that you own the domain in question. This prevents you from registering a site you don't own.
After you complete all of these steps, you will get a success message that tells you when your certificate will expire. You can renew your certificate at any time, non-interactively, with