Domain and subdomain wildcards on Apache as dynamic subfolders - apache

I'm creating website builder like weebly and wix format. I would like to dynamic subfolders. For example:
domain.com /public_html/sites/domain.com/
sub1.root.com /public_html/sites/sub1/
test.com /public_html/sites/test.com/
sub2.root.com /public_html/sites/sub2/
I can do only static with following and forced me to add one by one whole websites.
<VirtualHost *:80>
DocumentRoot "/public_html/sites/domain.com"
ServerName domain.com
ServerAlias *.domain.com
</VirtualHost>
How can i do dynamic both domains that client owner and subdomains that i'll give user.

For dynamic domain name configuration based on the names of folders you create in
/public_html/sites/, you can use the ServerAlias and VirtualDocumentRoot directives.
Add something like this in your apache virtualhost configuration file:
<VirtualHost *:80>
...
ServerAlias *
VirtualDocumentRoot /public_html/sites/%0
...
</VirtualHost>
To understand why %0 is used, consider the domain m.rate.movies.net split into parts
Index: %1 %2 %3 %4
Domain: m rate movies net
Negative Index: %-4 %-3 %-2 %-1
The one I chose to use, %0 represents the entire domain name - m.rate.movies.net. This allows you to support domain names that can have varying number of parts separated by dots. This makes sub-directories created inside /public_html/sites/ be the document root for any domain name pointed to your server's IP.
/public_html/sites/domain.com --> http://domain.com/
/public_html/sites/blog.writers.org --> http://blog.writers.org/
/public_html/sites/m.rate.movies.net --> http://m.rate.movies.net/
If you used something like VirtualDocumentRoot /public_html/sites/%1
You will have to create the sub-directories this way
/public_html/sites/domain --> http://domain.com/
/public_html/sites/blog --> http://blog.writers.org/
/public_html/sites/m --> http://m.rate.movies.net/
You can also use a combination: VirtualDocumentRoot /public_html/sites/%-2\.%-1
/public_html/sites/domain.com --> http://domain.com/
/public_html/sites/writers.org --> http://blog.writers.org/
/public_html/sites/movies.net --> http://m.rate.movies.net/
Yet another way: VirtualDocumentRoot /public_html/sites/%-1/%-2/
/public_html/sites/com/domain --> http://domain.com/
/public_html/sites/org/writers --> http://blog.writers.org/
/public_html/sites/net/movies --> http://m.rate.movies.net/
With these setup, you won't have to edit the virtual host configuration every time you add a new domain. You won't be able to use separate log files for each domain, but for that, you can use a custom log format that includes the host name.

Related

Request to apache2 server always redirects to /var/www (Index of /) site

I am currently trying to setup an virtual hosts following this tutorial on DigitalOcean.
The dummy-site I am trying to serve is under /var/www/example/html/index.html. I have not registered an official domain but setup /etc/hosts/ to resolve example.com to the IP address of my server.
I created another conf file called example.conf under /etc/apache2/sites-available and enabled it with sudo a2ensite example.conf and disabled the 000-default.conf.
Now whenever I go to example.com in my browser I get served:
.
This is the same page I would get when directly going to the IP address of my server. Only when I got directly to example.com/example/html I get served the correct index.html.
My current config looks like this:
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And my /etc/hosts file on my laptop like this:
#<ip-address> <hostname.domain.org> <hostname>
<server-ip> example.com
There are some other folders inside /var/www/ as the company I rented the server from had some maintenance sites preinstalled, but that shouldn't matter in this situation. (See edit, this did actually matter).
It feels like I am missing something obvious here, but I can't find a solution for my problem.
Edit:
The obvious thing I was missing, was that 2 additional sites where enabled by default as well.
One had the following contents:
# 10_froxlor_ipandport_<ip-address>.conf
# Created 28.11.2019 21:05
# Do NOT manually edit this file, all changes will be deleted after the next domain change at the panel.
<VirtualHost <ip-address>:80>
DocumentRoot "/var/www/"
ServerName <predefined Server name>
</VirtualHost>
After disabling all the other sites, the request to example.com actually went to the right index.html.
I figure, that the above enabled site actually matched the request coming from my browser and showed the www root directory.
The obvious thing I was missing, was that 2 additional sites where enabled by default as well.
One had the following contents:
# 10_froxlor_ipandport_<ip-address>.conf
# Created 28.11.2019 21:05
# Do NOT manually edit this file, all changes will be deleted after the next domain change at the panel.
<VirtualHost <ip-address>:80>
DocumentRoot "/var/www/"
ServerName <predefined Server name>
</VirtualHost>
After disabling all the other sites, the request to example.com actually went to the right index.html.
I figure, that the above enabled site actually matched the request coming from my browser and showed the www root directory.

Include external file in apache conf

I am running apache 2.4 on my web servers and I am always trying to find ways to streamline. Is it possible to include a conf file for every Joomla website and another one for every Wordpress site that I host? I put the Joomla .htaccess configuration (https://docs.joomla.org/Special:MyLanguage/Preconfigured_htaccess) inside of the domainname.com.conf file and specify Allowaccess none for performance reasons.
It would be great to have a single file for different versions of Joomla, Wordpress or other apps that require Apache configurations instead of needing to edit dozens of conf files when the app requirements change.
I found the include directive from Apache, but not sure if it would work on an individual vhost. http://httpd.apache.org/docs/2.4/mod/core.html#include
The Include does work for individual vhosts.
The documentation you linked (https://httpd.apache.org/docs/2.4/mod/core.html#include) states this in the header block of the section:
Context: server config, virtual host, directory
That means that the "Include" directive can be used, amongst other places, in the virtual host section of the configuration.
See here for a definition of the contexts: https://httpd.apache.org/docs/2.4/mod/directive-dict.html#Context
So you could do this:
<VirtualHost *:80>
ServerName joomla1.example.com
Include "conf/joomla.conf"
</VirtualHost>
<VirtualHost *:80>
ServerName joomla2.example.com
Include "conf/joomla.conf"
</VirtualHost>
<VirtualHost *:80>
ServerName wordpress1.example.com
Include "conf/wp.conf"
</VirtualHost>
<VirtualHost *:80>
ServerName wordpress2.example.com
Include "conf/wp.conf"
</VirtualHost>
joomla.conf and wp.conf would contain the directives that are common to either Joomla or Wordpress.

access to a server apache that has virtualhost

I have a cloud server with lampp installed on. I had configured a virtual host here like that:
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/folder/"
ServerName www.xxx.com
</VirtualHost>
and everything work as i expect, if i go to www.xxx.com i see my 'folder' site.
Now i need to work to another site present on the same server, but it doesn't allready have a domain, so i had imagine (by reading the apache's configuration file explanation)that i have to do it in this way:
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/folder/"
ServerName www.xxx.com
</VirtualHost>
<VirtualHost xx.xxx.xx.xxx:80/test>
DocumentRoot "/opt/lampp/htdocs/test/"
</VirtualHost>
But it doesn't work, if i do http://xx.xxx.xx.xxx:80 i reach the 'folder' site while if i do http://xx.xxx.xx.xxx:80/test rather the reach the 'test' site i still reach www.xxx.com, why? How could i reach this objective?
The virtual host defined first (top most) acts as default host. That one is used to respond to any incoming requests that are not matched by a specific host name in the request.
You want to try this setup:
# some fallback host for testing and development
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/_default"
</VirtualHost>
# a virtual host with a specific host name
<VirtualHost xx.xxx.xx.xxx:80>
DocumentRoot "/opt/lampp/htdocs/example.com"
ServerName example.com
ServerAlias www.example.com
</VirtualHost>
(here xx.xxx.xx.xxx stands for the systems public and routable IPV4 address)
In your file system you have this hierarchy:
/opt/lampp/htdocs/
_default/
test1/
test2/
example.com/
This way requests to http://example.com or http://www.example.com are mapped to the folder /opt/lampp/htdocs/example.com, requests to URLs with any other host name to the default folder /opt/lampp/htdocs/_default in which you now can create as many sub folders as you like for different applications.
Another approach would be to use other host names below your existing domain name for internal tests, so something like test1.example.com or similar. That way you do not have to use raw IP addresses with their routing risk.

Running multiple domains on same server

I have a Ubuntu server running Apache and have 3 sites under /var/www/website/abc, /var/www/website/xyz and /var/www/website/lmn. I have 3 domains (www.abc.com, www.xyz.com, www.lmn.com) mapped to same machine (mapped same ip to 3 different domains on godaddy).
So I googled around and found this link - virtual host setup and made abc.com.conf in /etc/apache2/sites-available/ and correspondingly for other sites. Enabled the sites and then restarted apache but same site(/var/www/website/abc) appears on all 3 domains. I rechecked the paths but they seem to be correct. I can't figure out what is wrong. How can I route them to their corresponding sites?
It would be helpful in the future if you share your code (in this case the apache config files) to figure out what's wrong. In any case, this is roughly how the files can look (they don't have to look like this, there are other ways it can be configured).
First check /etc/apache2/apache2.conf and make sure you see the following code:
IncludeOptional sites-enabled/*.conf
The apache2.conf file is the primary configuration file. That line above includes all of the configuration files in the site-enabled folder. If you use a Red Hat derived OS you'll notice that the configuration file structure is different (Debian derivatives like Ubuntu like to split everything up into tons of configuration files, Red Hat derivatives keep it together)
Make sure that each of the files in the sites-enabled folder includes lines that look like this.
For abc.com.conf:
<VirtualHost *:80>
ServerName www.abc.com
DocumentRoot /var/www/website/abc
</VirtualHost>
If you also want "abc.com" to point to this virtual host enter "ServerAlias abc.com" underneath the ServerName line. What you're doing here is creating a VirtualHost block for any ip address (*) on port 80 (:80). You could replace the * in the opening VirtualHost line with your external ip address if you want to make sure that the VirtualHost is only matched to a particular ip (this is only potentially needed if there are multiple external ips pointing to your webserver). The ServerName line tells apache to match this VirtualHost whenever the Host HTTP header is www.abc.com. ServerAlias can be used to specify additional Hosts to match. Remember that www.abc.com and abc.com are treated as different Hosts. The DocumentRoot line sets the directory from which files are served.
Similarly for xyz.com.conf:
<VirtualHost *:80>
ServerName www.xyz.com
DocumentRoot /var/www/website/xyz
</VirtualHost>
If you also want "xyz.com" to point to this virtual host enter "ServerAlias xyz.com" underneath the ServerName line.
And finally for lmn.com.conf:
<VirtualHost *:80>
ServerName www.lmn.com
DocumentRoot /var/www/website/lmn
</VirtualHost>
If you also want "lmn.com" to point to this virtual host enter "ServerAlias lmn.com" underneath the ServerName line.

Apache Non-Vhost Redirect

I want to force all server requests not matching one of my configured vhosts to redirect to my company's home page?
Currently my primary DocumentRoot is set to my home site directory, so non-vhost request do serve home page content; however, the domain name does not change. How can I force this?
Also, my primary ServerName is commented out by default. Is setting this recommended? And if so, why?
The first virtual host found in the apache config will be the default host if there is no ServerName match. So add a default host first that redirects to your companies home page:
<VirtualHost *:80>
ServerName myserver
Redirect 301 / https://www.example.com/
</VirtualHost>
If you have more than one IP address and different listeners x.x.x.x:80 then it can be a bit more complex as apache actually looks for a match on the combination of listener and servername. But it's not hard to resolve.
If the primary servername is commented out then apache will try to figure this out at start up. It's usually not an issue when using VirtualHost directives with ServerName local to each vhost.