So I created a very simple site containing only index.html with the text 'welcome'. the virtual host of this site is as follows:
ServerAdmin [email protected]
ServerName name.com
ServerAlias www.name.com
DocumentRoot /var/www/name.com/public_html
This works just fine. Then I make another site which I want to be www1.name.com
with the text www1
.
I do practicly the same, I make a folder /var/www1/name.com/public_html
with index.html
in it
I copy the previous file and change it
ServerAlias www1.name.com
DocumentRoot /var/www1/name.com/public_html
but when going to www1.name.com, I get the text 'Welcome' and not 'www1'. Can anyone please help me with this?
And yes I always do a2ensite/a2dissite
and service apache2 restart
Assuming the
VirtualHost
tags are fine. It's probably because theServerAlias
isn't used if there is noServerName
present. Try it again after changingServerAlias
toServerName
in your secondVirtualHost
.A good reminder is to set
ServerName
for the first name and useServerAlias
for any additional aliases.Do note though if you are going to use another directory (outside of /var/www) that the permissions are set properly. Also if
SELinux
is used, the appropriate context needs to be set for that directory. But that's properly not an issue unless you enableSELinux
yourself.Clarification of few basic points:
Within Ubuntu,
/var/www
is the default directory where the user's web content must be stored. This is defined into the global Apache's configuration file:/etc/apache2/apache2.conf
, by the directives:There is no direct link between the VirtualHost's name and its
DocumentRoot
directory name.The directive
ServerName
defines the general server's name or the name of certain VirtualHost when it is placed into its definition - between the<VirtualHost>
tags.With a view of the effective VHs management, usually, the definition of each individual VirtualHost is placed in a separate configuration file.
The directive
ServerName
is required for each VirtualHost.The directive
ServerAlias
is not required. We can think about it as place, where we can type one or more synonyms ofServerName
.According to these points, my suggestion is next:
Create two separate directories located in
/var/www
, as the example from the question they could be:Create configuration file for the first VirtualHost:
Its content should be something as:
Create configuration file for the second VirtualHost:
Its content should be something as:
Disable all existing VirtualHost's configurations, if it is needed:
sudo a2dissite "*.conf"
.Enable the new configuration files:
Restart Apache:
Ubuntu 14.04:
sudo service apache2 restart
Ubuntu 16.04:
sudo systemctl restart apache2.service
Put some index.html pages into
public_html/
folders and try to access the two domains (http://www...
andhttp://www1...
) through the browser.