Using same Directory directive for multiple virtual hosts with custom conf files - apache

We have a SaaS product and we give websites for customers. The customers can request to have their own domain pointed to their website. At the moment we use httpd.conf file to add a VirtualHost entry pointing to the same document directory. Afterwards the database will load the website by matching the URL. We have CentOS installed.
I think this is quite risky because if there is a mistake in httpd.conf file their is potential the whole product might not work. This has actually happen. Also, we use WHM and cPanel, so when we add a mod or does a server tweak the entire httpd.conf gets rewritten having us to replace/add existing virtual host entries to the file.
I will need a solution where i can add VirtualHost entries in a separate conf file or multiple conf files and load them via httpd.conf file which should automatically pick the newly added file/files without having to restart the server.
Can someone point me in the right direction where i can achieve the above.
Cheers!

You dont need to restart, just reload

Related

Pointing a domain to apache server without virtual hosts or custom conf files

We have a SaaS product and we give websites for customers. The customers can request to have their own domain pointed to their website. At the moment we use httpd.conf file to add a VirtualHost entry pointing to the same document directory. Afterwards the database will load the website by matching the URL. We realized there are couple of problems with this approach and this is not scalable at all.
If there is a mistake in httpd.conf file their is potential the whole product might not work. This has actually happen. Also, we use WHM and cPanel, so when we add a mod or does a tweak in the live server, the entire httpd.conf gets rewritten having us to replace/add existing virtual host entries to the file.
Strangely in our QA server, i did not have to add any virtualhost entries and as soon as a new domain is pointed, the website is picked up from the database. I realized the virtualhost entries were needed in the live server only. Both have Apache 2.4 CentOS installed. Is there a reason behind this? Im sure this has something to do with a configuration change.
Hope someone can point me in the right direction where i can achieve the same in our live server environment.
Cheers!
In Apache VirtualHosts add versatility but you are not forced to create any.
Apache will listen to the interfaces you tell it to (with the Listen directive) and when receiving requests if you haven't defined any virtualhosts the default server config will answer all requests.
The moment you add a virtualhost, that will be used to answer all requests, and if you add more virtualhosts the servername in them is examined to determine where to deliver requests depending on the incoming host header.
There really isn't anything more to it really.

Why do Apache config files `httpd.conf.work.[hash]` exist and how are they used?

In the same folder as the active Apache httpd.conf file are a set of httpd.conf.work.[hash] files. Where [hash] is an 8 character hash consisting of a-z A-Z 0-9 and _
The data they contain is essentially an `httpd.conf' file which suggests one is generated each time the main config file is updated and working.
I can see from a search that in some cases it stops a WHM CentOS install from updating after using the include config options.
Are they Cent OS specific? Can they be deleted? Why do the cause the include error above when they aren't referenced in httpd.conf file?
As far as I know, every time you do change the apache configuration, cPanel/WHM scripts automatically store the previous configuration as httpd.conf.bak. This happens when you modify your apache configuration from WHM (and not by manually editing the httpd.conf file).
I can't provide you a proper answer about the hash files but as a suggestion you could ask that on cPanel forums. I am pretty sure that you will receive the desired answer from any of the cPanel/WHM staff guys.

Remove ServerAlias from httpd.conf

I am using WHM/cPanel to manage web hosting and wondering how to remove the "cpanel, whm, webmail" ServerAlias. For example, I DO NOT want anyone to do this {HTTP_HOST}/cpanel in order to get to their cPanel.
These rules are clearly defined in the httd.conf file which is located in
/usr/local/apache/conf
I have already tried to manually modify them and I got the following message:
# DO NOT EDIT. AUTOMATICALLY GENERATED. IF YOU NEED TO MAKE A CHANGE
PLEASE USE THE INCLUDE FILES.
Nothing really happened even though the file was successfully saved. Users are still able to use these unwanted aliases. What should I do to effectively make the change?
Have you restarted Apache?
service httpd graceful
.. or you can do it through WHM.

Trouble setting up a subdomain on a VPS under Ubuntu and Apache

I'm a complete newbie when it comes to servers, so I need some help. Basically, I want to have a subdomain on my VPS. I'm not too concerned where the files reside, though of course I would like them separated if possible.
I found a guide to doing this that basically sums the procedure up in five steps:
Create directories to host the content of a new virtual site.
Make a copy of the /etc/apache2/sites-available/defaults file with a site appropriate name.
Change the two /var/www/ entries to the actual directory of the new site.
Add the ServerName line with the real domain name you will be hosting.
Use a2ensite to enable the new site, and finally reload Apache2 so it knows about the new site
I tried all of this, but I can't get it to work. I didn't get any errors at any point during this process, but when I enter the address with the subdomain into my browser, I get a "Server not found" error.
The company that hosts my VPS has an admin page where I can set up subdomains. Stupid question: do I have to do reconfigure the DNS records or something while doing this, or is it likely that the problem is caused by something else?
Additional note: I followed a guide on howtoforge.com when setting up the server, which in hindsight may not have been the brightest move on my part. It is possible that I made a mistake there that is somehow affecting me now?
Assuming you've setup the subdomain correctly in your VPS' admin page, the only thing left to check here is that you've actually created the subdomain in your DNS administration page (have you?). For example, I use afraid.org to manage my DNS records and to add a subdomain, it's essentially:
Type: A
Subdomain: sparky
Domain: example.com
Destination: 120.34.2.3 (this would be your VPS' IP address)
In general, you add a (sub)domain by:
sparky.example.com A 120.34.2.3
Please keep in mind that you want to add an A record (this is important!).

Create Apache VitualHost for each User

I'm trying to setup a server at www.domain.com that will allow me to create hosting accounts for each user I add.
Basically my goal is to create a user and map a name-based vhost to their home dir, so:
"joe.domain.com" would point to "/home/joe"
I've tried setting up vhosts in httpd.conf but I'm not having any luck and wondering if there's a way to either 1. script this process or 2. setup some sort of variable in a generic vhost entry that will automatically create this entry.
Without knowing the details of your setup, I can tell you that:
Yes, you can script the process.
Apache configuration files are just text files, so it's very easy to generate the programatically.
Yes, you can configure some sort of generic virtual host entry.
The Dynamically configured mass virtual hosting documentation from Apache addresses this case quite nicely.