How to setup a VirtualHost on Fedora

If you are developing more projects at the same time on your localhost machine, it is very practical to use the virtual hosts on your Apache web server. How to run Apache web server on Fedora you can read in another post on my blog.

For example we will develop projects on domains www.projectA.com and www.projectB.com. So let's set it up.

All additional configurations of Apache web server are loaded from alphabetically sorted files with .conf extension in /etc/httpd/conf.d/ directory. So create the first one. You can use your favourite text editor of course (vim is not required ;)).
$ sudo vim /etc/httpd/conf.d/01-projectA.conf

with this content:
<VirtualHost *:80>
        ServerAdmin your@adminemail.com
        ServerName projecta.dev
        ServerAlias www.projecta.dev
        DocumentRoot /var/www/projecta.com/public/

        ErrorLog /var/log/httpd/error-projecta.log
        CustomLog /var/log/httpd/access-projecta.log combined

        <Directory /var/www/projecta.com/>
                Options Indexes FollowSymLinks
                AllowOverride All
        </Directory>

</VirtualHost>

I used .dev tld (which is very popular by web developers) in configuration above. Then is easy to work with url rewrites and domains.

For the second project just create very similar file: /etc/httpd/conf.d/02-projectB.conf
<VirtualHost *:80>
        ServerAdmin your@adminemail.com
        ServerName projectb.dev
        ServerAlias www.projectb.dev
        DocumentRoot /var/www/projectb.com/public/

        ErrorLog /var/log/httpd/error-projectb.log
        CustomLog /var/log/httpd/access-projectb.log combined

        <Directory /var/www/projectb.com/>
                Options Indexes FollowSymLinks
                AllowOverride All
        </Directory>

</VirtualHost>

Of course we need to create directories and index files specified above, for example: /var/www/projecta.com/public/index.html and /var/www/projectb.com/public/index.html.

As the next step we need to add these lines into our /etc/hosts file. This file is responsible for right routing specific domains to correct ip addresses.
127.0.1.1       projecta.dev www.projecta.dev
127.0.2.1       projectb.dev www.projectb.dev

edit /etc/hosts file

And the last step is restart the Apache webserver:
$ sudo systemctl restart httpd

Then we can test our configuration by enter the urls: http://projecta.dev and http://projectb.dev
Test urls in browser: http://projecta.dev and http://projectb.dev

Comments