How to add domain on Apache 2.4 | Debian / Ubuntu

Enter your domain name here:

This blog post will be parametrized for your case

1. Configuration directory

After you installed LAMP, you are ready to set your application online. To do that, you need to set VirtualHost configuration. Apache2 VirtualHost configuation files are in /etc/apache2/sites-enabled/. A good practice is to put them on /etc/apache2/sites-available/ and then symlink to sites-enabled, so you can remove domains by simply removing symlinks, without deleting the file.

So, go to your sites-available directory:

cd /etc/apache2/sites-available/

2. Create a VirtualHost config file

Create your domain config file, let’s assume, your website files are under /var/www/html/{domain}/

sudo nano {domain}.conf

Then, paste the code:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName {domain}
        ServerAlias *.{domain}
        DocumentRoot /var/www/html/{domain}/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/html/{domain}/>
                Options -Indexes
                Options FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Press ctrl+o to save, and ctrl+x to exit.

Then, create a symlink to sites-enabled directory:

sudo ln -s /etc/apache2/sites-available/{domain}.conf /etc/apache2/sites-enabled/{domain}.conf

3. Check configuration and reload

Run this code to test your Apache2. Coding Cat’s advice is to run this code everytime you make changes to Apache configuration. It may save you a lot of trouble.

sudo apachectl configtest

To reload Apache2 without impact of current website visitors, simply run:

sudo service apache2 reload

4. Done! Let’s check if it works

Open your browser, and go to http://{domain}/

If you get DNS errors, check if your domain points at your server:

dig {domain}

If not, you need to configure DNS records at your domain provider.

 

If you see 404 error, check if you have index file under /var/www/html/{domain}/ directory, like index.html or index.php, and make sure you did not receive any errors at step 3.

 

If you see other errors or a blank page, check out error.log file:

tail -f /var/log/apache2/error.log