Apache: Central Virtual Host configuration per domain - apache

I'm having some problems configuring multiple domains in my apache instance.
I have 2 domains: a.com and b.com
I only want to configure the cert files once, so i have a configuration for a.com and b.com. But if i have something like this:
<VirtualHost *:443>
ServerName a.com
ServerAlias www.a.com *.a.com
SSLEngine On
SSLCertificateFile /path/fullchain-a.pem
SSLCertificateKeyFile /path/privkey-a.pem
</VirtualHost>
<VirtualHost *:443>
ServerName b.com
ServerAlias www.b.com *.b.com
SSLEngine On
SSLCertificateFile /path/fullchain-b.pem
SSLCertificateKeyFile /path/privkey-b.pem
</VirtualHost>
it wont allow me to define subdomains in seperate configuration files. Because of the whole wildcard stuff, it just doesn't care about the following vhosts.
Is there a way to configure the certificates centrally so all subdomain vhosts will automatically default to them, without having to specify the whole path in each and every subdomain config?
So i just need to do this:
<VirtualHost *:443>
ServerName subdomain.b.com
DocumentRoot /var/www/html-b
</VirtualHost>
<VirtualHost *:443>
ServerName subdomain.a.com
DocumentRoot /var/www/html-a
</VirtualHost>
<VirtualHost *:443>
ServerName test.a.com
ServerAlias test.b.com
DocumentRoot /var/www/shared-html
</VirtualHost>
or do i need to specify the certificate paths for each subdomain?
I tried using multiple default vhosts, but it would always default to the fist one.

Related

Name-based VirtualHosts by subfolder

I'm trying to configure Apache with three different VirtualHosts, such that a specific VirtualHost will be used when someone requests either the corresponding subdomain (e.g. foo.example.com) or the corresponding subfolder (e.g. example.com/foo).
I thought the following httpd.conf would do the trick, but the ServerAlias directives are simply being ignored:
<VirtualHost *:80 *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/http
</VirtualHost>
<VirtualHost *:80 *:443>
ServerName foo.example.com
ServerAlias example.com/foo
DocumentRoot /usr/share/web
</VirtualHost>
<VirtualHost *:80 *:443>
ServerName bar.example.com
ServerAlias example.com/bar
DocumentRoot /var/www/html
</VirtualHost>
When bar.example.com is requested the third VirtualHost is used, as intended. However, in the case of example.com/bar the first VirtualHost takes precedence despite the ServerAlias. Similarly, requesting example.com/foo matches the first VirtualHost, not the second.
How can I fix this configuration to produce the desired behavior?
ServerAlias takes a hostname, not a hostname and a path. This mechanism doesn't do what you want it to do. Just create an Alias or Redirect in the virtual host being accessed.

Apache VirtualHost multiple domain multiple SSL

Hi I have the following use case, I have an application (let's call it foobar) on a remote server /var/www/foobar and I have the following Apache VirtualHost conf
<VirtualHost *:80>
DocumentRoot /var/www
# This is to redirect http traffic to https
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
SSLEngine On
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/private/example.com.key
SSLCertificateChainFile /etc/ssl/certs/example.com.bundle.crt
ServerName example.com:443
DocumentRoot /var/www/foobar
</VirtualHost>
And its working all fine. Now suppose I have another domain example2.com and I want it to point to the same foobar application. My current thinking is create another VirtualHost below, something like this
<VirtualHost *:443>
SSLEngine On
SSLCertificateFile /etc/ssl/certs/example2.com.crt
SSLCertificateKeyFile /etc/ssl/private/example2.com.key
SSLCertificateChainFile /etc/ssl/certs/example2.com.bundle.crt
ServerName example2.com:443
DocumentRoot /var/www/foobar
</VirtualHost>
But I was wondering is this the correct way of doing stuff like this? I need both domains to be "independent" so I didn't make a permanent redirect from example2.com to example.com
You can do something like below,You can use the server alias for this, Also I don't see anything wrong in having 2 virtual hosts as well.
<VirtualHost *:443>
ServerName example1.com
ServerAlias example2.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/testlink
</VirtualHost>
If you are getting unable to get a certificate error, you can create a certificate with a wildcard CN. e.g : *.comthis will match both example1.com and example2.com.

Using VirtualHosts to host multiple domains

I have searched far and wide for this answer, and can't find a working solution. So here goes,
I have 2 VirtualHosts set up on my server, with each serving a separate domain name. However, when I visit the first domain on the list, it then serves the DocumentRoot of the second domain. I even have them both listening on different ports. In my DNS under each domain I've got them leading to the IP of the server.
Here is my Apache .conf file:
ServerName 137.117.33.226
<VirtualHost *:443>
ServerName joshstroup.me
ServerAlias www.joshstroup.me
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/joshstroup.me.cert
SSLCertificateKeyFile /etc/apache2/ssl/joshstroup.me.key
</VirtualHost>
<VirtualHost *:80>
ServerName respice.xyz
ServerAlias www.respice.xyz
DocumentRoot /var/www/respice
</VirtualHost>

How to configure dynamic subdomains for Apache2 on Ubuntu?

I need all url mydomain.com, a.mydomain.com, b.mydomain.com, whatever.mydomain.com....
point to the same DocumentRoot, the subdomain is dynamic(maybe have more than hundreds)
Now I have the following lines in 000-default.conf:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias *.mydomain.com
The mydomain.com is work, but all subdomain is not found.
Can someone help me? thanks so much.....
For example:
A user register a new account, the new account is "obama" then the url would be "obama.mydoamin.com". The subdomain can be entry when the account create immediately.
Wildcard sub-domains are possible using Apache virtual hosts.
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName xyz1.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/wildcard
ServerName other.example.com
ServerAlias *.example.com
</VirtualHost>
The first entry will become the default if you don't get an explicit match. So if you had xyz.otherexample.com point to it, it would be caught be xyz1.example.com. You need to turn on the name based virtual hosts with the first entry.
For further details you can also refers to apache documentation apache Doc

Wildcard SSL with Multiple Domains

I have a CentOS/Apache+OpenSSL server. I host two domain names with wildcard sub domains (application logic surfaces the correct site), e.g.
https://*.testing1.com
https://*.testing2.com
It works great over HTTP:-
<VirtualHost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin webmaster#testing1.com
ServerName testing1.com
ServerName testing2.com
ServerAlias *.testing1.com *.testing2.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /home/app/public_html/public
</VirtualHost>
I've purchased two Wildcard SSL certificated for both testing1.com and testing2.com, but I'm unsure how to set it up in this structure:-
<VirtualHost *.testing1.com:443>
SSLEngine On
SSLCertificateFile /etc/httpd/ssl/*.testing1.com.crt
SSLCertificateKeyFile /etc/httpd/ssl/*.testing1.com.key
SSLCACertificateFile /etc/httpd/ssl/geotrust.cer
ServerAdmin john#testing1.com
ServerName testing1.com
ServerAlias *.testing1.com
DirectoryIndex index.html index.php
DocumentRoot /home/app/public_html/public
</VirtualHost>
<VirtualHost *.testing2.com:443>
SSLEngine On
SSLCertificateFile /etc/httpd/ssl/*.testing2.com.crt
SSLCertificateKeyFile /etc/httpd/ssl/*.testing2.com.key
SSLCACertificateFile /etc/httpd/ssl/geotrust.cer
ServerAdmin john#testing2.com
ServerName testing2.com
ServerAlias *.testing2.com
DirectoryIndex index.html index.php
DocumentRoot /home/app/public_html/public
</VirtualHost>
The above for the SSL doesn't work, with the *.testing1.com definition, nor with just testing1.com.
I will also need to repeat this for testing2.com
Name-based virtualhosts and SSL wil only work if all the virtualhosts are within the same domain and you have a wildcard SSL certificate for that domain.
But you have 2 different domains.
In this case it will only work if you give each SSL-enabled virtualhost it's own IPaddress.
So you should use IP-based virtualhosts, not Name-based.
Explanation:
The ServerName which is requested, is contained in the HTTP request headers, but before that the SSL encryption must be already setup. So the ServerName is only available after the encryption has been setup. Therefore Apache can never know which SSL certificate te serve up and wil just use the first one available on that particular IPaddress.
With the single dedicated IP we can configure domain-based wildcard SSL in centos + apache2.2 server.
Hope the configurations below will help you guys!!
NameVirtualHost IP:80
NameVirtualHost IP:443
Domain 1
<VirtualHost IP:80>
ServerName abc.domain1.com
DocumentRoot /var/www/html/domain1
</VirtualHost>
<VirtualHost IP:443>
ServerName *.domain1.com
DocumentRoot /var/www/html/domain1
SSLEngine On
SSLCipherSuite ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLCertificateFile /var/www/html/domain1/cert/5465456.crt
SSLCertificateKeyFile /var/www/html/domain1/cert/domain1.com.key
SSLCertificateChainFile /var/www/html/domain1/cert/g2-g1.crt
</VirtualHost>
Domain 2
<VirtualHost IP:80>
ServerName abc.domain2.com
DocumentRoot /var/www/html/domain2
</VirtualHost>
<VirtualHost IP:443>
ServerName abc.domain2.com
DocumentRoot /var/www/html/domain2
SSLEngine On
SSLCipherSuite ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLCertificateFile /var/www/html/domain2/cert/5465456.crt
SSLCertificateKeyFile /var/www/html/domain2/cert/domain1.com.key
SSLCertificateChainFile /var/www/html/domain2/cert/g2-g1.crt
</VirtualHost>