How to set a domain to ip address?

1. You need to acquire a domain name (or maybe just FQDN) from some DNS provider.

2. Once you have registered the domain name, you will gain access to an administrative panel (like this one shown below), where you will be able (via A records), to redirect the domain name (and all *. or certain sub domains / FQDNs) to your server's IP address.

enter image description here

  • Please note that the provider's administrative panel shall looks different, and the provider will give you exact instructions how to use it.

  • Sometimes the redirection can take up-to 24 hours. You can check if it's successful by the command whois example.com.

  • If the server is behind NAT, you must setup port forwarding.

3. Edit your Virtual Host configuration file and add relevant ServerName and maybe ServerAlias directives. Let's assume the configuration file is 000-default.conf that should look as this:

<VirtualHost *:80>

        ServerName example.com
        ServerAlias www.example.com localhost

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        <Directory /var/www/html>
                # etc ...
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
  • This step can be omitted, but it is absolutely necessary when you have more than one Virtual Hosts.

  • Don't forgot to:

    sudo a2ensite 000-default.conf
    sudo systemctl reload apache2.service
    

4. In addition for local needs:

  • You can bind a FQDN to the loopback interface of the server. For this purpose, edit the file /etc/hosts in a way like this:

    127.0.0.1    localhost example.com www.example.com
    

    It is not possible to enter *.example.com here. You can add an entry also for the IP address of another (local) server's network interface - for example 77.77.77.70.

  • If you want to access the FQDN by another computer through the LAN (or by a private computer through Internet), edit its host file in a way like this:

    77.77.77.70    example.com www.example.com
    

Further reading:

  • Creating additional virtual host Ubuntu Server 16.04.3

  • Connect via ssh to one of the multiple clients on same network


If it is for just local use, you can just put that entry into your hosts file.

On modern Windows, that is usually c:\Windows\System32\Drivers\etc\hosts.

On Linux, the file is /etc/hosts.

For the rest of the world, use one of the freely available DNS providers.

Here is an example, with instructions: FreeDNS