5

Although I've used MAMP and AMPPS before on OSX, simply for testing purposes, this question is about actual production websites on Apache servers.

If I had the domain name www.example.com - if this was on an Apache installed as a sudo apt-get on Linux (maybe Debian or Ubuntu) and not via a GUI like MAMP or AMPPs wouldn't it be under /var/www/public_html/www.example.com or would it be /var/www/public_html/example

Is /var/www/public_html/yoururlhere the most common type of setup on Linux servers?

However, could it also be /var/www/www.example.com or /var/www/example ?

If I was to have a subdomain, for example, http://canada.example.com, would it be:

/var/www/public_html/example/canada

or

/var/www/public_html/www.example.com/canada

For a URL like http://www.example.com/~site1 would it be /var/www/public_html/example/site1 or would it require some form of mod_rewrite?

In general, what's the most conventional way of doing things like this for URLs that use Apache on Linux hosts, especially the major webhosting companies?

  • I recommend that you research jailing each domain and having them run as a user for each site. e.g /home/site1/public_html. – Simon Hayter Jun 24 '18 at 21:18
2

wouldn't it be under /var/www/public_html/www.example.com or would it be /var/www/public_html/example

This is a completely personal call as it has (in all generic cases) absolutely no technical consequences, both for the anchor path you choose, and how you configure things in it.

So it is up to the administrator to decide what to do. Some considerations that will need to be taken into account to choose optimal scheme:

  • how permissions are handled? one user has access to all websites, or one per website? How are they accessed: ssh, sftp, ftp, etc.
  • how are the websites run? all under the same httpd or apache user account, or you have a separate system account per website?
  • how backups are handled? Is there a need to do some fine grained (only one website) ones instead of everything together
  • volumes of data and quotas: at some point all websites on the same host will compete for I/O access and CPU, so at some point if you want to move one to another host, what kind of copy you will need to do.

And just what "reads" more naturally and the ability for example to use autocomplete on the website name if it exists as is as a directory on the filesystem.

In "mass" virtual hosting you might even find other schemes:

  • reversing labels in a domain (because basically domain names are read right to left): /var/www/public_html/com/example/canada, /var/www/public_html/com/example/www etc.

  • to avoid too many files/directories in a given directory (because filesystems do not handle this case well), do some kind of hashing, based often on the previous point because otherwise you will frequently start with www so that will not discriminate enough: /srv/web/example/f/i/www.first.example, /srv/web/example/s/e/www.second.example, /srv/web/example/t/h/www.third.example, etc. (for the websites www.first.example, www.second.example, www.third.example, etc.)

I am sure you can imagine other schemes. If the websites are managed by some kind of panel, the panel may itself come with its own way to organize things on the filesystem.

For a URL like http://www.example.com/~site1

Typically, a standard Apache under Unix uses ~ as a very specific element of the path, and this is handled by mod_userdir to typically map the previous URL to /home/site1/public_html if site1 is a Unix user on the box. But this all depends on the Apache configuration...

See https://httpd.apache.org/docs/2.4/mod/mod_userdir.html

1

Websites can be served from pretty much any directory. I’ve seen /var/www, /srv/www, and /home/username among others. According to this thread the srv directory is for “data served from this system” so that seems logical. It’s what I normally use these days, but I think /var/www is still the most common.

In terms of hosting multiple sites, the standard procedure is to use a separate directory for each site, often with a ‘public’ subdirectory in each. They can be named as the domain, or something else that helps you distinguish them. So for example /var/www/example.com/public and /var/www/sub.example.com/public

Depending on what sites you are hosting, you may want to use separate directories for logs. You can either use multiple directories such as /var/log/apache2/example.com or have a logs directory next to your public directory, ie /var/www/example.com/logs

  • I should have thought of that, I'm used to shared hosts, but am trying to learn how to run my own website as if I was the webhost, using an old computer as a server for Linux. – avenas8808 Jun 28 '18 at 16:41
1

This can be a bit tricky a question to answer. I am glad to see Ubuntu in the question. However, what is missing is whether hosting will be used or even a control panel. This is where it gets tricky. Let me explain.

Years ago just about when Apache 2.0 was released a structural change was made that did not always carry through. Prior, all site configurations were placed in httpd.conf which was the configuration for Apache itself. This was not necessarily bad, however, a positive change was made in configuration intended to be more secure and simpler to manage. There were, and still is, two problems with this change. One, Apache did not make the builds for the various OS installs and two, not all control panels followed best practices at the time nor did they change.

For example, Ubuntu did impliment the update the configuration schema while Redhat did not. On the control panel side, Webmin/Virtualmin did update the configuration schema while cPanel did not. See where I am going?

For hosting, cPanel is used universally. This is unfortunate since we often clean up the mess here. For most who hosts their sites find security issues and operational issues that should never exist.

Since I only use Ubuntu, I can tell you that the Apache install is good and will remain so if you also use Webmin or Virtualmin. However, if you choose to use cPanel, the Apache configuration will be as poor as it can be. If you choose not to use a control panel, you should be fine.

Here is what happens.

Redhat installs and cPanel installs will create a web space in /var/httpd/html or in /var/www/public_html. Somethimes this directory will be on a partition that is small following old traditions when sites were small. This is not always the case, however, I have seen it enough times where a site operator runs out of disk space quickly and easily.

Proper installs will create a default site under the /home/ directory or under the /var/www/ directory with individual configuration files per site under /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ directories. The default configuration file is 000-default.conf which should largely be left alone. The reason for this is to create a catch-all site which handles all IP based requests and requests for domains that do not exist on the server. The 000-default.conf file can be copied to create a new site. Typically, the file name format would be example.com.conf and created in the sites-available directory.

As for the web space, this should match the configuration file such as /home/example.com/ or /var/www/example.com. I do not like using the /var/www/ directory for individual sites. Often, you get into permissions conflicts. Save /var/www/html for the default site if that is what you have. Otherwise, if you are familiar with setting permissions, I suggest using the /home/ directory which will not have permission conflicts.

Of course you will need to sudo a2ensite example.com.conf to properly enable the site and then restart Apache with sudo service apache2 restart.

So what is wrong with using /var/www/? Apache runs with specific permissions which would have to exist throughout the entire tree. If, for example, we follow what cPanel does, your first site is created under /var/www/public_html/ which in of itself is not bad. However, the next site is created under /var/www/public_html/example/ which makes it available as a directory of the first site created as www.firstsite.com/example/. Bad. Very bad. In addition, all sites are run under one user, Apaches default user which can be a variety of names including root. It is not common that root is used. It really should be a non-privilaged user.

When you segment sites into wholely separate directories in a directory other than /var/www/, each site can be run under different users added to the group Apache uses. This is sandboxing as mentioned in the comments. Leaving the default site alone allows you to further sandbox bad requests including intentional attacks.

For example configurations, I have a few in this answer. Virtualhost config: routing and wildcard usage These are non-controversial and should work well assuming you did not goof something up.

  • Thank you. I've currently got Debian installed on a server, not publically accessible, but may replace it with Ubuntu. It's mainly for a few Wordpress installs and a CMS. – avenas8808 Jun 28 '18 at 16:40

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.