Httpd Apache multiple virtual host - enable https on specific domain - apache

I have 3 sites (site1, site 2, site3) run on same IP server. Now, I want to add ssl and redirect http to https for site1 only. Site2 and site 3 still run in http. This is my httpd configs:
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
ServerName site1.com
Redirect / https://site1.com
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/html/site1
ServerName site1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/site2
ServerName site2.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/site2
ServerName site3.com
</VirtualHost>
OS : CentOS 6.9 - Apache : 2.2.15
When I restart the apache, I get FAILED result without message. How can I setup the server as my expectation ?
Thanks !

Related

Multiple Virtual host with Port 443 pointing to wrong directory

I have created two virtual host files on ubuntu apache2
www.example1.com.conf
www.example2.com.conf
which is working fine with port 80. without https.
I have added following code for 443
VirtualHost *:443>
ServerAdmin info#example1
ServerName example1.com
DocumentRoot /var/www/www.example1.com/
#SSLEngine on
#SSLCertificateFile /etc/ssl/example1.crt
#SSLCertificateKeyFile /etc/ssl/example1.key
#SSLCertificateChainFile /etc/ssl/SectigoRSADomainValidationSecureServerCA.crt
</VirtualHost>
and
<VirtualHost *:443>
ServerAdmin info#example2
ServerName example2.com
DocumentRoot /var/www/www.example2.com/
</VirtualHost>
Now, when i am opening https://www.example2.com/, i am seeing content of example1.com.
but if i do http://www.example2.com/ then content is ok.
Can you please suggest what will be the issue ?

how to redirect browser to two different site with apache virtualHost?

I have a valid IP, e.g. x.x.x.x, and a domain, e.g. site.com which point to x.x.x.x.
First, I need to redirect any request from port 80 to 443 (security issue).
Second, for every request sent to my IP, I want apache to show "Hello it's
working" (/var/www/index.html) and everyone request to my domain to show the real site.
I tried this, but it did not work:
<VirtualHost x.x.x.x:433>
ServerName x.x.x.x
ServerAlias x.x.x.x
DocumentRoot /var/www/
DirectoryIndex index.html
Options -Indexes
</VirtualHost>
<VirtualHost site.com:443>
...
</VirtualHost>
for redirection:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.yourdomain.com
Redirect / https://www.yourdomain.com
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.yourdomain.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
for domain separation:
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot "/www/domain"
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot "/www/otherdomain"
</VirtualHost>

Virtual Host With SSL Support on OS X Mavericks

I have a few virtual hosts setup on my local dev machine running Apache/2.2.24 on OS X 10.9 (Mavericks).
My http-vhosts.conf file (that is configured to load through httpd.conf looks like this:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/me/Sites/testsite.com
ServerName testsite.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/me/Sites/testsite2.com
ServerName testsite2.dev
</VirtualHost>
I have also configured my /etc/vhosts file to contain this line:
127.0.0.1 testsite2.dev
I want to be able to use the testsite2.dev over SSL (https). I have tried multiple configurations of the vhosts config file with no luck.
With this current configuration, going to http://testsite2.dev pulls up the page I expect while https://testsite2.dev points to the apache home page at /Library/WebServer/Documents/index.html.en
I have tried the following configuration, and multiple others, that do not work:
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/me/Sites/testsite.com
ServerName testsite.dev
</VirtualHost>
<VirtualHost *:80 *:443>
DocumentRoot "/Users/me/Sites/testsite2.com
ServerName testsite2.dev
</VirtualHost>
Is it possible to have a virtual host listen on port 80 and port 443 on a local machine?
I figured out how to do this. I was simply missing a few directives to show where my certificate and key are located. Here's what I added:
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /private/etc/apache2/ssl/server.crt
SSLCertificateKeyFile /private/etc/apache2/ssl/server.key
DocumentRoot "/Users/me/Sites/testsite2.com"
ServerName testsite2.dev
</VirtualHost>
Shown in context, my http-vhosts.conf file looked like this:
Listen *:443
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/me/Sites/testsite.com
ServerName testsite.dev
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /private/etc/apache2/ssl/server.crt
SSLCertificateKeyFile /private/etc/apache2/ssl/server.key
DocumentRoot "/Users/me/Sites/testsite2.com"
ServerName testsite2.dev
</VirtualHost>
If you don't have a certificate and key installed, you can create your own by following a tutorial like this one:
http://www.cfdad.com/2012/12/12/creating-a-self-signed-ssl-cert-for-mac-osx-mountain-lion-apache/
It should work on both Mountain Lion and Mavericks.

Apache virtual host for browsing with IP address

I'm using virtual host. but I want that when user browses IP address, my server will serve resource in local host. The content of my virtual host config:
NameVirtualHost *:80
# for main web
<VirtualHost *:80>
DocumentRoot /Library/WebServer/Documents
ServerName localhost
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot /Library/WebServer/Documents
</VirtualHost>
<VirtualHost 192.168.1.6:80>
DocumentRoot /Library/WebServer/Documents
</VirtualHost>
#for other webs
...
When user type 192.168.1.6 in the browser, the resource in /Library/WebServer/Documents will be return. But I don't want to designate 192.168.1.6 explicitly, I want something like ..*.*
I changed
...
<VirtualHost 192.168.1.6:80>
DocumentRoot /Library/WebServer/Documents
</VirtualHost>
...
to
...
<VirtualHost *.*.*.*:80>
DocumentRoot /Library/WebServer/Documents
</VirtualHost>
...
but it don't work.
What should I change? Is this even possible?
I am personally using this one for IP Vhost
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin contact#steelbrain.com.pk
DocumentRoot /var/www/ipbased/
ServerName 192.168.1.1
ServerAlias 192.168.1.1 192.168.1.2
</VirtualHost>

Apache default VirtualHost

How can I set a default VirtualHost in Apache?
Preferably, I want the default host not to be the same as the IP address host. Now I have something like this:
NameVirtualHost *
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someroot/
ServerAlias example.com *.example.com
</VirtualHost *>
If a domain is forwarded to my server, but isn't in this vhost.conf file, the files from /someOtherDir/ are loaded, as expected. But I want to be able to use a different root for the IP address itself and domains which aren't added to the vhost.conf file (yet). Is this possible?
I found the answer: I remembered that Apache uses the first block if no other matching block is found, so I've added a block without a serveralias at the top of the blocks:
NameVirtualHost *
<VirtualHost *>
DocumentRoot /defaultdir/
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someroot/
ServerAlias example.com *.example.com
</VirtualHost>
If you are using Debian style virtual host configuration (sites-available/sites-enabled), one way to set a Default VirtualHost is to include the specific configuration file first in httpd.conf or apache.conf (or what ever is your main configuration file).
# To set default VirtualHost, include it before anything else.
IncludeOptional sites-enabled/my.example.com.conf
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
# Load virtual host config files from "/etc/httpd/sites-enabled/".
IncludeOptional sites-enabled/*.conf
The other answers here didn't work for me, but I found a pretty simple solution that did work.
I made the default one the last one listed, and I gave it ServerAlias *.
For example:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.secondwebsite.example
ServerAlias secondwebsite.example *.secondwebsite.example
DocumentRoot /home/secondwebsite/web
</VirtualHost>
<VirtualHost *:80>
ServerName www.defaultwebsite.example
ServerAlias *
DocumentRoot /home/defaultwebsite/web
</VirtualHost>
If the visitor didn't explicitly choose to go to something ending in secondwebsite.example, they get the default website.
Actually, I'm using Virtual host configuration (sites-available / sites-enabled) on EC2 Linux AMI with Apache/2.4.39 (Amazon). So, I have 1 EC2 instance to serve many sites (domains).
Considering that you already have Virtual Host installed and working. In my folder /etc/httpd/sites-available, I have some files with domain names (suffix .conf), for example: example.com.conf. Create a new file like that.
sudo nano /etc/httpd/sites-available/example.com.conf
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html/domain
</VirtualHost>
For each file.conf in sites-available, I create a symbolic link:
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
This is the default configuration, so, if access directly by IP of Server, you will be redirect to DocumentRoot of the first file (.conf) in sites-available folder, sorted by filename.
To have a default DocumentRoot folder when access by IP, you have to create a file named 0a.conf, then Apache will serve this site because this new file will be the first in sites-available folder.
You must create a symbolic link:
sudo ln -s /etc/httpd/sites-available/0a.conf /etc/httpd/sites-enabled/0a.conf
To check serving order, use it:
sudo apachectl -S
Now, restart Apache, and check out it.
Obligatory - none of the previous answers worked for me. I inherited a strange combination of IP address-based virtual hosts and * vhosts (not assigned/catch all IP addresses) based virtual hosts in this Apache configuration messed up by ISPConfig.
I wanted Apache to serve not configured hosts with the same page.
I had: not configured hosts went to the first vhost after 000-default.conf. No matter I had *:80 catch all defined as the first vhost, instead of default Apache would load first defined site:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
Although it's not completely valid configuration, what finally worked was adding an IP address-based virtualhost without ServerName/ServerAlias defined:
<VirtualHost 192.168.10.10:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost 192.168.10.10:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
SSLEngine On
...
</VirtualHost>
$ apachectl -S outputs IP address-based vhosts first, and * based vhosts later, and finally my default site is loaded before real site:
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:50
192.168.10.10:80 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
port 80 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:7)
...
46.23.86.103:443 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
port 443 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
port 443 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:182)
...
*:80 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
Word of notice - in a configuration like this, * vhosts won't work, so you need to apply IP addresses to all vhosts.
An alternative setting is to have the default virtual host at the end of the config file rather than the beginning. This way, all alternative virtual hosts will be checked before being matched by the default virtual host.
Example:
NameVirtualHost *:80
Listen 80
...
<VirtualHost *:80>
ServerName host1
DocumentRoot /someDir
</VirtualHost>
<VirtualHost *:80>
ServerName host2
DocumentRoot /someOtherDir
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /defaultDir
</VirtualHost>
I had the same issue. I could fix it by adding the following in httpd.conf itself before the IncludeOptional directives for virtual hosts. Now localhost and the IP 192.168.x.x both points to the default test page of Apache. All other virtual hosts are working as expected.
<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>
Reference: https://httpd.apache.org/docs/2.4/vhosts/name-based.html#defaultvhost
Only supported and correct answer is:
<VirtualHost _default_:*>
DocumentRoot "/www/default"
</VirtualHost>
or my own version to return 403:
<VirtualHost _default_:*>
<Location />
Require all denied
</Location>
</VirtualHost>
The NameVirtualHost option would be a good option.
The solution is:
# apache2.conf
# #warning this is specific to apache 2.2
NameVirtualHost *:80
Listen 80
# ...
# aaaa.example.conf
<VirtualHost *:80>
ServerName aaaa.example
DocumentRoot /defaultDir
</VirtualHost>
# host1.example.conf
<VirtualHost *:80>
ServerName host1.example
DocumentRoot /someDir
</VirtualHost>
# host2.example.conf
<VirtualHost *:80>
ServerName host2.example
DocumentRoot /someOtherDir
</VirtualHost>
In my case, to work, I created a VirtualHost (n.e. VirtualHost per CNAME) called aaaa.example since I have different files for different VirtualHosts and knowing that Apache reads them in alphabetical order.