I have a server with multiple websites hosted and distinguishable using name-based virtual hosting of apache.
How do I set it so that a specific website is hosted when the ip of my server is entered in the address bar?
I have a server with multiple websites hosted and distinguishable using name-based virtual hosting of apache.
How do I set it so that a specific website is hosted when the ip of my server is entered in the address bar?
What you want to use is the _default_
VirtualHost
.
<VirtualHost _default_:80>
DocumentRoot /www/default80
# ...
</VirtualHost>
It's described here. Basically if nothing else match the request the _default_
host will be used.
EDIT
This could also be written as:
<VirtualHost *>
DocumentRoot /www/default
# ...
</VirtualHost>
Is is important that this is the first VirtualHost
in the configuration since Apache will start matching them from top to bottom, selecting the one that fit the best based on ServerName
and ServerAlias
.
This post might also be of interest: Apache default VirtualHost
just find the Include sites-enabled/
line in your apache2.conf
file and add the path to the conf file you want to be site default above it. from:
Include sites-enabled/
to
Include sites-enabled/mydefault.conf
Include sites-enabled/
When you first install apache2, there is a site configuration file named 000-default.conf
. This is going to be the default because it is very likely to appear first in the list of files under /etc/apache2/sites-enabled
.
To have your own file as the default, you can either replace the file under /etc/apache2/sites-available/000-default.conf
with your own, or replace the link like so:
sudo rm /etc/apache2/sites-enabled/000-default.conf
sudo ln -s ../sites-available/my-site-setup.conf /etc/apache2/sites-enabled/000-default.conf
Then restart apache2 (or just reload).
The _default_
as mentioned by the other answer is for defining a virtual host which can be found with the default IP address. It's not the default virtual host.
<VirtualHost _default_:80>
...
is equivalent to
<VirtualHost *:80>
...
The *
is a globing pattern which matches any IP addresses.
Note:
Replacing the 000-default.conf
file is okay, but in most cases the installation package is going to view that as a modified file and manage it in some weird way which is why I think it's cleaner to only change the soft link.
Keep it clean, don't delete or edit anything in /etc/apache2/sites-available/
.
Create all new site configurations in /etc/apache2/sites-available/
. Copy whatever site configuration you want enabled to /etc/apache2/sites-enabled/
. Only make sure /etc/apache2/sites-enabled/
has only one configuration file.
Sample format for new apache site configurations in Ubuntu 20.04 LTS is
<VirtualHost *:80>
ServerName http://localhost
ServerAdmin admin@mysite.com
DocumentRoot /var/www/html/mysiteroot/public
<Directory /var/www/html/mysiteroot>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Notice that 000-default.conf
is by default in both directories mentioned above and should only be replaced by your new configuration in /etc/apache2/sites-enabled/
so that it can be restored anytime you need it.
Restart Apache2 service after you make any configuration changes.