Apache Virtual Hosts on different domain names AND ports - apache

I use a Ubuntu 16.04 server VM on which I have to configure the following domains:
maindomain.com:80
otherport.com:8080
Each domain pointing to the VM's IP, but to a different directory obviously.
I managed to get bind9 to make these domains point to the VM's IP when the VM is the DNS server, and I configured Apache to get the following results:
Good:
maindomain.com:80 returns maindomain's index
otherport.com:8080 returns otherport's index
Bad:
maindomain.com:8080 returns otherport's index
otherport.com:80 returns maindomain's index
If I put both on port 80, each is separated, but if I do different ports it seems that Apache just cares about the port.
How could I block the access to maindomain.com:8080 and otherport.com:80?
maindomain.com.conf file:
<VirtualHost maindomain.com:80>
ServerAdmin webmaster#localhost
ServerName maindomain.com
ServerAlias www.maindomain.com maindomain.com
DocumentRoot "/var/www/html/maindomain"
<Directory /var/www/html/maindomain>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
otherport.com.conf file:
<VirtualHost otherport.com:80>
ServerAdmin webmaster#localhost
ServerName otherport.com
ServerAlias www.otherport.com otherport.com
DocumentRoot "/var/www/html/otherport"
<Directory /var/www/html/otherport>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I managed to get it done, but I think it's more of a dirty hack than an actual solution. I made two more virtual hosts like so :
maindomain.com.trap.conf
<VirtualHost *:8080>
ServerAdmin webmaster#localhost
ServerName maindomain.com
ServerAlias www.maindomain.com maindomain.com
DocumentRoot "/var/www/html/maindomain"
<Directory /var/www/html/maindomain>
Require all denied
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The other one having the names and port switched.
By the way, I left <VirtualHost *:80> and <VirtualHost *:8080> in the first two .conf files I mentioned in my first post.

Related

Order to declare VirtualHost for domain and subdomains, Apache2, Ubuntu

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).

Apache Virtual Host Config Multiple Domains One File

Is it possible in the apache config file to use regular expressions in the ServerName and ServerAlias field to have 2 domains use the exact same config file vs. creating 2 for each domain. I was trying something like this, but was getting an error from Apache:
<VirtualHost *:80>
ServerAdmin admin#test.com
ServerName [test.com|test.net]
ServerAlias [www.test.com|www.test.net]
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Apache2: 2 virtual hosts with subdomains not working

I have set up one conf file in apache with 2 virtual hosts:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName test.domain.com
WSGIScriptAlias / /var/django/test/test/wsgi.py
<Directory /var/django/test/test>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *.80>
ServerAdmin webmaster#localhost
ServerName domain.com
#ServerAlias *.domain.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
But unfortunately only the first one is being called no matter what domain I put into the browser.
My expectation is that only test.domain.com will open my Django project and all other subdomains use the standard website.
What did I do wrong?
Regards
Kev
The error is here:
after replacing the dot wih a ":" the it worked perfekt.

Host two different sites in apache

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>

How to enable multiple domains and or subdomains with apache httpd server

I'm trying to enable multiple domains in my environment Development, but am not succeeding the way I tried below, at which point I'm wrong?
I installed httpd and changed DocumentRoot in httpd.conf to:
C:/Webserver/www
*I changed the Windows hosts file to(in Italics would like to access):
If I switch to 127.0.0.1 api.rotadorock the address resolves to www/ but the right is www/rotadorock/api.
127.0.0.1 localhost
127.0.0.1 webserver
127.0.0.1/rotadorock/ecommerce rotadorock
127.0.0.1/rotadorock/api api.rotadorock
127.0.0.1/rotadorock/ecommerce ecommerce.rotadorock
127.0.0.1/rotadorock/mobile mobile.rotadorock
127.0.0.1/rotadorock/sistema sistema.rotadorock
127.0.0.1/rotadorock/social social.rotadorock
*Update(windows hosts file)
I removed the hosts file changes I mentioned above, because as #Volker Birk said are not necessary. But even so, still can not access as desire (api.rotadorock/ or localhost/api.rotadorock/ and should point to C:/Webserver/www/rotadorock/api). What could be wrong?
And finally changed httpd-vhost.conf for:
NameVirtualHost webserver:80
<Directory "C:/Webserver/www">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerName localhost
DocumentRoot "c:/Webserver/www"
ServerAlias localhost
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerName webserver
DocumentRoot "c:/Webserver/www"
ServerAlias webserver
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost rotadorock:80>
ServerName rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/ecommerce"
ServerAlias rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost api.rotadorock:80>
ServerName api.rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/api"
ServerAlias api.rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost ecommerce.rotadorock:80>
ServerName ecommerce.rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/ecommerce"
ServerAlias ecommerce.rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost mobile.rotadorock:80>
ServerName mobile.rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/mobile"
ServerAlias mobile.rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost sistema.rotadorock:80>
ServerName sistema.rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/sistema"
ServerAlias sistema.rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
<VirtualHost social.rotadorock:80>
ServerName social.rotadorock
DocumentRoot "c:/Webserver/www/rotadorock/social"
ServerAlias social.rotadorock
ErrorLog "logs/httpd-error.log"
CustomLog "logs/httpd-access.log" common
</VirtualHost>
You don't need the hosts file. Have a look into the documentation:
http://httpd.apache.org/docs/2.2/en/vhosts/name-based.html
Finally solved the problem. And I could just using the Windows hosts file and httpd-vhosts.conf httpd together.
Let me give an example of what was done to enable multiple subdomains accessing locally.
On Windows hosts file to add, for each domain and subdomain you want something like this:
127.0.0.1 api.rotadorock #my subdomain
127.0.0.1 rotadorock #my domain
And then the httpd-vhosts apache httpd:
# Accessing the API
<VirtualHost 127.0.0.1>
DocumentRoot "C:/Webserver/www/rotadorock/api"
ServerName api.rotadorock
ServerAlias ​​api.rotadorock
<Directory "C:/Webserver/www/rotadorock/api">
All Options Includes Indexes
</ Directory>
</VirtualHost>
# Accessing the domain
<VirtualHost 127.0.0.1>
DocumentRoot "C:/Webserver/www/rotadorock/"
ServerName rotadorock
ServerAlias ​​*.rotadorock
<Directory "C:/Webserver/www/rotadorock/">
All Options Includes Indexes
</Directory>
</VirtualHost>
And then that way I could access api.rotadorock/ and rotadorock/ locally. I tried all ways without the hosts file. But just gotta use it. If someone can explain to me how it should have done so it would not need to use the hosts I would be grateful.