I've a server installed with Ubuntu 18.04 and on that server I've created new host for a new sub-domain:
api.example.com
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName example.com
ServerAlias api.example.com
DocumentRoot /var/www/searchcore
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now I would like now to add another sub-domain "newapi.example.com" and I would like it to point to the same folder of the first domain (/var/www/searchcore).
Any idea how to do it?
Thanks
The ServerAlias directive accepts one or more names, so you can simply append the name of your new sub-domain to the existing ServerAlias directive like this:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName example.com
ServerAlias api.example.com newapi.example.com
DocumentRoot /var/www/searchcore
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Alternatively, you can also have multiple ServerAlias directives:
...
ServerAlias api.example.com
ServerAlias new.example.com
...
You might want to check out the docs here: https://httpd.apache.org/docs/2.4/mod/core.html#serveralias
Related
I declared in my DNS, my domain, subdomain1 and subdomain2, and everything works.
Then I create my directories like this :
/var/www/domain.com/public_html/index.html /var/www/subdomain1.domain.com/public_html/index.html /var/www/subdomain2.domain.com/public_html/index.html
Then I create : /etc/apache2/sites-available/domain.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.subdomain1.domain.com
ServerAlias www.subdomain1.domain.com
ServerAdmin webmaster#domain.com
DocumentRoot /var/www/subdomain1.domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.subdomain2.domain.com
ServerAlias www.subdomain2.domain.com
ServerAdmin webmaster#domain.com
DocumentRoot /var/www/subdomain2.domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias www.domain.com
ServerAdmin webmaster#domain.com
DocumentRoot /var/www/domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I disable the default configuration: sudo a2dissite 000-default.conf
Then I activate each of the websites: sudo a2ensite domain.conf
I restart Apache 2: sudo systemctl restart apache2.service
The problem is that all links point to the first declared VirtualHost, even when I separate the VirtualHost declarations in separate .conf files, it will always be the first VirtualHost in the directory that will be opened for all DNS domains.
Apache will try to match the requested domain to one of the VirtualHost it knows about. When it cannot find a match, it will use the first one it read, top to bottom in the configurations. Hence here, your domains are not being recognized by Apache, and you get the first one all the time.
I modified your configuration a little bit:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.subdomain1.example.com
ServerAlias subdomain1.example.com
ServerAdmin webmaster#example.com
DocumentRoot /var/www/subdomain1.example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/sub1_error.log
CustomLog ${APACHE_LOG_DIR}/sub1_access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.subdomain2.example.com
ServerAlias subdomain2.example.com
ServerAdmin webmaster#example.com
DocumentRoot /var/www/subdomain2.example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/sub2_error.log
CustomLog ${APACHE_LOG_DIR}/sub2_access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster#example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>
Details:
NameVirtualHost is only required if you run Apache 2.2. >2.4, it is automatic, no need to put it, it will work anyway.
ServerAlias is useful to define another domain name that applies to this VirtualHost. If you define ServerAlias == ServerName, it is useless.
Therefore, the ServerAlias are now configured with the domain names, without "www."
Split your log files. If you have 3 VH pointing to the same logs, it will be a real mess to debug. It makes it easier when split.
So with this configuration...
http://www.subdomain1.example.com and http://subdomain1.example.com will go to the 1st VH.
Similar for subdomain2
And http://www.example.com and http://example.com will use the third one.
This is based on your question and the comment where you say you tried example.com. In your configuration, example.com was not listed anywhere (i.e. www.example.com != example.com).
So I recently updated my server from ubuntu 16.04 to 18.04 (full reinstall)
Now I'm having the following issue:
When using https no site will embed images or meta tags
https://i.imgur.com/ILFYZ0b
^ same problem with facebook-messenger
Here is my /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerAdmin merlijn#melijn.com
ServerName melijn.com
ServerAlias www.melijn.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName melijn.com
ServerAlias www.melijn.com
ServerAdmin admin#melijn.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/ssl-error.log
CustomLog ${APACHE_LOG_DIR}/ssl-acces.log combined
SSLEngine on
SSLCertificateFile /home/merijn/Certs/melijn.com.crt
SSLCertificateKeyFile /home/merijn/Certs/melijn.com.key
</VirtualHost>
You can visit the sites fine btw:
https://melijn.com
http://melijn.com
I hope there is a solution :)
Ok so the problem was the following:
I had an incomplete certificate and I needed to add a .ca-bundle file in my configuration.
So I added this line:
SSLCertificateChainFile /path/to/file.ca-bundle
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Above is the example of https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts which I've followed to setup some sites. But what I wanna do is basically:
www.example.com?site=mynewsite.com
With htaccess: www.example.com/mynewsite.com
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com/mynewsite.com
ServerAlias www.example.com/mynewsite.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Sadly above doesn't work.
Reason why I wanna do this is because I want to generate a website using database which will be triggered based of URL.
How may I do?
You could try the obscure ServerPath directive, which maps a request to a virtualhost based on the first component of the URL path.
I have one Linux server with Apache installed. I configured two sites in it using VirtualHost.
I configured the two VirtualHosts for two different domain names. The configuration looks like this:
<VirtualHost 12.123.123.123>
ServerAdmin info#example-one.com
ServerName example-one.com
ServerAlias www.example-one.com
DocumentRoot /var/www/html/example-one
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost 12.123.123.123>
ServerAdmin info#example-two.com
ServerName example-two.com
ServerAlias www.example-two.com
DocumentRoot /var/www/html/example-two
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
When I visit my site example-one.com, then I see the correct website.
But when I visit example-two.com, then I see the website of example-one.com.
What am I doing wrong? I'm trying to host those two different websites under the same Apache server.
Do you have NameVirtualHost directive somewhere in your httpd config file?
This should work. Be aware that the first VirtualHost block is the default in case the http request does not match any other VirtualHost block.
For reference https://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin info#example-one.com
ServerName example-one.com
ServerAlias www.example-one.com
DocumentRoot /var/www/html/example-one
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin info#example-two.com
ServerName example-two.com
ServerAlias www.example-two.com
DocumentRoot /var/www/html/example-two
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I've got a site set up on localhost that I'm actively developing, and I'd like to set up a subdomain on localhost to make my life 10* easier.
I added this to C:\xampp\apache\conf\extra\httpd-vhosts.conf:
<VirtualHost i1.localhost:80>
ServerAdmin dummy#localhost
DocumentRoot "C:/xampp/htdocs/i1/"
ServerName i1.localhost
ServerAlias www.i1.localhost
ErrorLog "logs/dummy-host2.localhost-error.log"
CustomLog "logs/dummy-host2.localhost-access.log" combined
</VirtualHost>
Apache stats up fine, but when I navigate to http://localhost/ I'm seeing content from the i1 subdomain. http://i1.localhost/ works fine, however.
Then I tried doing this:
<VirtualHost localhost:80>
ServerAdmin dummy#localhost
DocumentRoot "C:/xampp/htdocs/"
ServerName localhost
ServerAlias www.localhost
ErrorLog "logs/dummy-host2.localhost-error.log"
CustomLog "logs/dummy-host2.localhost-access.log" combined
</VirtualHost>
<VirtualHost i1.localhost:80>
ServerAdmin dummy#localhost
DocumentRoot "C:/xampp/htdocs/i1/"
ServerName i1.localhost
ServerAlias www.i1.localhost
ErrorLog "logs/dummy-host2.localhost-error.log"
CustomLog "logs/dummy-host2.localhost-access.log" combined
</VirtualHost>
But that worked the opposite. On both localhost and i1.localhost I'm seeing content from C:/xampp/htdocs/.
Anyone got an idea what's going wrong?
Cheers.
Apache usually does not like a vhosts document root inside another vhost, try:
DocumentRoot "C:/xampp/htdocs/"
and
DocumentRoot "C:/xampp/i1/"