Create Apache VitualHost for each User - apache

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.

Related

Correct Apache Configuration And Htaccess

I've just reset my Ubuntu 14.04 LAMP server hosted with digital ocean. Could someone tell me the 'proper' way to do server configuration. My goal is to do everything as clean as possible (and hopefully well structured).
I intend on using the server mainly for programming and data analytics, however I do plan on hosting my website in /var/www/html. I also plan on using letsencrypt/certbot to get an easy SSL. With this in mind, these are the main goals I would like to accomplish:
1) Redirect the website to ALWAYS be served through https AND www.
2) Enable HSTS for the entire website.
3) Enable clean url's (remove .php extensions and what not).
Since I would like all of these properties to be used across the entire website, should the configuration be done inside of the /etc/apache2/ folder? Or should it be done inside of .htaccess?
And if it should be done inside of apache2 configuration, which file should I add it to? And finally, how exactly should it be added? (for example vhost 80/443, inside of a mod_something section, etc).
Thank you in advance, I would appreciate and consider any advice about Apache and htaccess!

Using same Directory directive for multiple virtual hosts with 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 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

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.

How to exclude subdomain directory from main website

I've setup a VPS with apache2.
I am using cloudflare for DNS management.
Now, I have my websites's files in "var/www/website" folder. Inside that, there is another folder for forum. like "var/www/website/forum" inside which there are all forum related files.
Now, suppose I have www.website.com pointing to "var/www/website"
and I also have a subdomain forum.website.com pointing to "var/www/website/forum".
What I want to do is make the files inside "var/www/website/forum" accessible via subdomain only. I don't want users to access forum via www.website.com/forum, but I want them to access it only via "forum.website.com"
What you need to do is set up what's called a virtual host. You would put your forum at /var/www/forum and website at /var/www/website.
inside /etc/apache2/sites-available, you'll need to add an additional configuration file for that site called forum.website.com.conf.
You'll then need to create a symbolic link to /etc/apache2/sites-enabled for that file so that apache sees it. From there, you reboot the server and are good to go.
Here's some documentation:
http://httpd.apache.org/docs/2.2/vhosts/
http://httpd.apache.org/docs/2.2/vhosts/examples.html
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts
This may be a bit different depending on the flavor of linux, but should be about the same. Control panels like Cpanel, Plesk and WebMan can make this process a bit easier by abstracting the configuration to a web control panel.
Hope this helps you.

How to setup a domain on a server?

I am running LAMP config on CentOS 5.6
Now, the question is how do I add a domain name to the server? I have already pointed the domain name to the server.
I think the next steps would be something like this.
Create a user on centos
Create a folder for the domain name within that users directory
Create a virtual host on apache pointing the directory to the users directory
Add content to the directory of the domain name specified in the virtual host
Voila! A new domain name created.
Am I missing something from the list? Also, any other methods to do this or just use ssh?
To change domain name, there are a couple config files you need to work with, with root permission. Make sure you backup all original files before making any changes:
"/etc/sysconfig/network" which you should replace the default hostname by your FQDN(Fully Qualified Domain Name), something like this: myHostName.myDomainName.
"/etc/hosts" which you should add your IP address(es) with corresponding hostname and FQDN, separate by whitespace, something like this: 192.168.1.1 myHostName myHostName.myDomainName
"/etc/sysconfig/network-scripts/ifcfg-eth0" which is a config file for your ethernet interface 0, replace default IP address, network, network mask, gateway, etc. to a proper value. And if your server has more than one interface, you should config all of them. They are in the same directory with the above file I mentioned. Look at the name and you should know which one to amend.
Finally, restart your server.
I recommend this book which I am using as a quick reference. For those steps you mentioned, it depends what you want to achieve. But I suggest you open a new thread for them.
You create a user, then you create a domain directory and put your files in there, then you add a virtual domain to apache and restart apache and that should be it.
Obviously dont forget to setup your dns or ask your hosting to set it up for you.