Host two different sites in apache - 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>

Related

How to avoid repeating virtualhost properties in apache?

I want my sites served via SSL to have a bunch of the same properies in my apache2 config. Unfortunately, I can't find anywhere that demos how to do that. Here's my config at the moment for two separate servers accessed by www.mydomain.com and blog.mydomain.com:
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /var/www/html
ServerName www.mydomain.com
# Repeated stuff
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.com/privkey.pem
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/wordpress
ServerName blog.mydomain.com
# Repeated stuff
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.com/privkey.pem
</VirtualHost>
</IfModule>
As you can see, both virtualhosts have a lot of the same properties under #Repeated stuff. How can I specify these properties once and then have ONLY these virtualhosts inherit those properties? E.g. can virtualhosts be nested?
mod_macro gave me just what I wanted. On ubuntu it can simply be enabled with sudo a2enmod macro. My apache2 SSL config now looks something like this:
<IfModule mod_ssl.c>
<Macro SSLStuff>
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.mydomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.mydomain.com/privkey.pem
</Macro>
<VirtualHost *:443>
Use SSLStuff
DocumentRoot /var/www/html
ServerName mydomain.com
ServerAlias www.mydomain.com
</VirtualHost>
<VirtualHost *:443>
Use SSLStuff
DocumentRoot /var/www/wordpress
ServerName blog.mydomain.com
</VirtualHost>
<VirtualHost *:443>
Use SSLStuff
ServerName mynodeapp.mydomain.com
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
</VirtualHost>
</IfModule>

Ubuntu 18 Point two domains to same folder

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

Apache virtual host from GET url

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

Apache Virtual Hosts on different domain names AND ports

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.

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.