I was configuring a couple of domains that share the same SSL certificate, that the content will be in different folders and obviously that everything will be on the same server.
My domains are:
example.com (For the Website)
api.example.com (For the Web Service)
I got the SSL Certificate with Let's Encrypt (free) .
The certificate was generated for both domains, therefore it could be placed in the same directory.
/etc/apache2/ssl/example.com/
Later I made the respective configurations in the Apache sites directory.
/etc/apache2/sites-available/
Also, perform the activation of the sites:
a2ensite example.com
a2ensite api.example.com
The structure of the website is:
-> /var/www/
-> website/
-> api/
These are the settings:
/etc/apache2/sites-available/example.com
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/website/
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/example.com/error.log
<Directory "/var/www/website/">
Options FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/website/
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/example.com/error_ssl.log
SSLEngine on
SSLCertificateKeyFile /etc/apache2/ssl/example.com/privkey.pem
SSLCertificateFile /etc/apache2/ssl/example.com/cert.pem
SSLCertificateChainFile /etc/apache2/ssl/example.com/chain.pem
<Directory "/var/www/website/">
Options FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
/etc/apache2/sites-available/api.example.com
<VirtualHost *:80>
ServerName api.example.com
DocumentRoot /var/www/api/
#RewriteEngine On
#RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
<Directory "/var/www/api/">
Options FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerName api.example.com
DocumentRoot /var/www/api/
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/error.log
SSLEngine on
SSLCertificateKeyFile /etc/apache2/ssl/example.com/privkey.pem
SSLCertificateFile /etc/apache2/ssl/example.com/cert.pem
SSLCertificateChainFile /etc/apache2/ssl/example.com/chain.pem
<Directory "/var/www/api/">
Options FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
The drawback is:
When I access the site example.com
in the browser, the content that it has is opened api.example.com
.
This gives rise to my question:
I share my solution.
After a bit of searching about this requirement I found this Apache parameter.
Which gives us to know a term regarding the Apache.
SNI
Server Name Indication
According to an internet reference, I quote:
This means that we must enable
SNI
our server to serve multiple subdomains using the same SSL Certificate.How do we do it?
The first thing to do is modify the following file:
And in it we add the following:
Afterwards, the file is edited:
And in it we add the following instruction:
The instruction
NameVirtualHost *:443
is more necessary to add, since what is below it, is usually the default in the configuration ofports.conf
.Finally, we save and restart Apache.
After this, the subdomains allow their use independently and using the same SSL Certificate.
Update
The directive
ServerName
indicates one of the server names that can be used in thevirtualhost
, but we can use another directive, which can be useful if there are multiple domains that use the same content.Leaving our configuration like this.
Allowing the same configuration to
example.com
be used by other sites.