Reputation: 11108
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?
Upvotes: 16
Views: 54500
Reputation: 515
This can be achieved by setting the server name in the Vhost configuration file to the local server IP address at ServerName and also pointing to the right folder holding the required files to be served at DocumentRoot. As always, restart Apache for the new configs to be activated. This assumes that the IP address in mind was the local one.
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/var/www/base_folder/dummy_default_folder"
ServerName 192.168.X.XX #the key entry, your local server IP address
ServerAlias example.server.com
ErrorLog "/var/log/httpd/example.error_log"
CustomLog "/var/log/httpd/example.access_log" common
</VirtualHost>
This is tested to work in Apache/2.4.37 (rocky)
Upvotes: 0
Reputation: 1
Many servers do not have a monolithic configuration file, but several host-specific configuration files for various virtual hosts organized as follows:
/etc/apache2/
sites_available (actual configuration files)
sites_enabled (symlinks to files in sites_available)
In order to make a particular virtual host configuration load first when accessed by the shared IP address, rename the symlink (only) to something which will be first when sorted, such as 00-default originally was:
SITES-AVAILABLE EXAMPLES:
lrwxrwxrwx 1 root root 41 Jul 27 2019 00_yourdomain.com.conf -> ../sites-available/yourdomain.com.conf
lrwxrwxrwx 1 root root 45 Jul 27 2019 00_yourdomain.com.ssl.conf -> ../sites-available/yourdomain.com.ssl.conf
*** This is a "clean", easily interpretable, simple method, which (importantly) does not involve the apache2.config file!
Upvotes: 0
Reputation: 2843
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 [email protected]
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.
Upvotes: 2
Reputation: 20828
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.
Upvotes: 1
Reputation: 71
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/
Upvotes: 7
Reputation: 2623
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
Upvotes: 19