1

I'm doing a Laravel project and I need to set up a virtual host example.dev in my system. For that I've created a copy of 000-default.conf and named it as example.dev.conf and placed in /etc/apache2/sites-available. The contents of the file is given below (comments removed):

<VirtualHost *:80>
    <Directory /var/www/html/example/public/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    ServerName example.dev
    ServerAlias www.example.dev

    ServerAdmin [email protected]
    DocumentRoot /var/www/html/example/public

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

Then I've enabled the site using a2ensite command and disabled 000-default.conf using a2dissite command. Then it will open my project when I try localhost (/var/www/html) in my browser and not example.dev. The virtual host example.dev always produce "The site can't be reached" error in browsers.

Any suggestions?

pa4080
  • 29,831
Anoop S
  • 23
  • I've also added the site to the /etc/hosts file:

    172.168.0.1 www.example.dev (tried without www too).

    – Anoop S Apr 14 '18 at 12:29
  • have you tried 127.0.0.1 www.example.dev instead? – Mojtaba Zali Apr 14 '18 at 12:39
  • @MD9 Haven't tried that. Can I use localhost after that? – Anoop S Apr 14 '18 at 12:43
  • @AnoopS I guess the IP address should be 192.168.0.1 instead of 172.168.0.1 – Florian Diesch Apr 14 '18 at 12:43
  • @MD9 Tried that, same result. – Anoop S Apr 14 '18 at 12:49
  • @FlorianDiesch Tried that, same result. But I suppose we can give any ip in that place, right? That will redirect us to the host we provided in there. That's how it works, no? – Anoop S Apr 14 '18 at 12:50
  • 1
    You should choice domain extension different from .dev. Last year Google bought .dev, .foo and some other domain extensions. Now the redirection to https:// of these is hard coded in some browsers. Few days ago I read here a nice answer about that, but I can't find it. The proposed duplication should be also helpful. – pa4080 Apr 14 '18 at 12:50
  • @pa4080 changed from dev to example.local. It also produces same results. – Anoop S Apr 14 '18 at 13:01
  • @AnoopS it has to be your web server's IP address Your browser will try to connect to that IP address if you visit www.example.dev, and then your web server will try to find a matching VirtualHost for www.example.dev. – Florian Diesch Apr 14 '18 at 13:01
  • The line in /etc/hosts should be 127.0.0.1 localhost example-dev.org www.example-dev.org, etc... You can use and your LAN IP, but when the network interface is down it shouldn't work. Then use ServerName example-dev.org and ServerAlias www.example-dev.org. – pa4080 Apr 14 '18 at 13:18
  • Please review this answer: https://askubuntu.com/a/941305/566421 – pa4080 Apr 14 '18 at 13:23
  • @pa4080 It worked when changed the line in /etc/hosts to:

    127.0.0.1 localhost example.local www.example.local

    Now I can access both my localhost and the virtual host. Thank you for your support mate. :)

    – Anoop S Apr 14 '18 at 13:39
  • I'm glad to help, @AnoopS! I've retracted my close vote. – pa4080 Apr 14 '18 at 13:50

2 Answers2

3

First choice domain extension different from .dev. Last year Google bought .dev, .foo and some other domain extensions. Now the redirection to https:// of these is hard coded in some browsers. More details could be find in this helpful answer.

Then edit your /etc/hosts file in this way (more details could be fund in this answer):

127.0.0.1   localhost example.local www.example.local

Then edit your <virtual-host>.conf file in the following way:

<VirtualHost *:80>

    ServerName example.local
    ServerAlias www.example.local
    ServerAdmin [email protected]

    DocumentRoot /var/www/html/example/public
    <Directory /var/www/html/example/public/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

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

</VirtualHost>

Now, reload Apache's configuration and try to access http://example.local.

pa4080
  • 29,831
1

I had the same problem as I used to name all my test sites .dev -- changing all of my /etc/apache2/sites-available/ config files (and local hosts file) as per pa4080's answer fixed it for me.

Use a2dissite to remove the old configs (before you delete the files) and a2ensite to enable the new.

Make sure to reload apache with service apache2 reload as well.

sigil
  • 11