ANAT Apache folders convention

I’ll start by getting right to the point. This is how I structure the folders for Apache.

Assumptions :

  • Using Ubuntu (or a similar distro)
  • Single main user, but can have multiple (virtual) FTP users
  • You have full root access (in this case using Sudo)
  • You aren’t a security nazi

The easiest way of explaining this is to give an example.

Example : anat.org.au

  • /var/www/anat/
  • /var/www/anat/www/ — Primary site root
  • /var/www/anat/list/ — Used as a placeholder for mailing lists
  • /var/www/anat/staging/www/ — Used as a testing site for new concepts.
  • /var/www/anat/stats/ — Optionally used to contain webalizer or other stats (usually of the main www site). Can also be used to re-direct to Google.com/analytics, or even a Cacti, MRTG, or Naigos style SNMP graphing app.

As you can see the format is :

/var/www/$DOMAIN/$SUBDOMAIN/$SUB_SUBDOMAIN/

Log Files

To complicate things slightly more, I also put the apache log files into their own folder, so it can be easily symlinked into an FTP chroot.

Lets use anat.org.au as an example.

www.anat.org.au as is usually listed last as it has a number of extra wildcard alias’s such as *.anat.org.au and it would have the following Apache Sites log entry :

CustomLog /var/log/apache2/anat/www.anat.log vhost_combinedErrorLog /var/log/apache2/anat/error.log

list.anat.org.au would have the following :

CustomLog /var/log/apache2/anat/list.anat.log

staging.anat.org.au would have the following :

CustomLog /var/log/apache2/anat/staging.anat.log combined

History

When I first got started into running a LAMP (Linux Apache MySQL and PHP) server, I had barely the faintest of ideas of just how much they can expand and get added to. Nor did I know how bad it can be to make bad or incorrect assumptions.

The primary assumption I made was that my small server would be running one, and only one domain. The issue was that I put all the files for that domain into /var/www/ I then started adding sub-domains and would add them as random folders (so they would be accessible via www.TheSiteInQuestion.com/subdomain/ as well as subdomain.TheSiteInQuestion.com/ What was even worse was when I started hosting other domains, I had no where to put them, so threw them into /var/www/domain/domainNames/ so now not only were my subdomains available from the main site (and could cause interference with my main site, but other peoples websites could be accessed! Very bad (don’t, DON’T do this).

Long story short, that was a very bad way of doing things, and migrating all those folders and changing the apache site configurations took a lot of work.

All of this happened long before I started working at ANAT, however we recently did a migration from a shared hosting account which existed before I started on the job, to a full VPS, and had to migrate the files into something more sane than /home/username/public_html/FoldersThatAreDomainsOrSubdomains/

I should mention that in general most sites I host have a very active main (www) site with almost nothing happening on the subdomains, so I generally have a single error log that all vhost entries point to, however if you are expecting a lot of traffic on a subdomain (like w00t.anat.org.au) then it should have it’s own error.w00t.anat.log file.

Tailing/Monitoring

Because of the sparse nature of the log files it can be hard to try tailing them all to see changes.

The old way of monitoring changes to the log files usually looks like the following :

tail -f /var/www/apache2/*.log

However that doesn’t work when the log files are kept in their own folders. The new command to alias would look like

tail -f /var/www/apache2/*/*.log

Although that only gets the Apache log files, personally I use the alias below quite often to work out what’s wrong (e.g some times it turns out to be a MySQL error or auth issue, etc..)

alias logs='sudo find /var/log/ -name "*.log" -type f -exec sudo tail -f {} +'

The alias command above should be placed in your ~/.bashrc or ~/.bash_aliases file. It works by running find on all the folders in /var/log, filtering the list to ensure they are a file (-type f = file, -type d = directory), then ensuring the file ends in .log. For each found file it runs tail -f. The {} = the filename. The main downside is that this command won’t tail files which are log files but don’t end in .log, such as syslog, dmesg, and ones named *.err or *.info. The main reason you can’t just send all files to tail is that most log-rotate settings will leave a large number of zipped old log files, and tailing a log file cause cause things to go screwy. If you do want, you could add a reverse filter to the find command so it selects all files except those ending in .gz, .gzip, .tar, .tar.gz, .tar.gzip, .zip, .1 (usually an old log file that’s been rotated but not compressed), .2 etc..

If people are interested I have a basic bash script file which I use to create a new website. This includes apache config, folders, file permissions, scaffolding the ftp folders and symlinks, and more.

If you have an even better convention, then please let us know!

About admin

Australian Network for Art and Technology [ANAT] e: [email protected] | ph: +61 8 8231 9037 www.anat.org.au | www.filter.org.au | www.synapse.net.au Twitter: __ANAT | Facebook: http://bit.ly/bF9fXl The Australian Network for Art and Technology (ANAT) is supported by the Visual Arts and Craft Strategy, an initiative of the Australian, State and Territory Governments; the Australian Government through the Australia Council, its arts funding and advisory body, and the South Australian Government through Arts SA.
This entry was posted in Articles, Need to know. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *