What is the recommended directory to store website content?
Solution 1
There is no "best" directory. And while people might argue that this question is subjective, or that the actual placement of files does not matter—and they're right about the latter—there are standardized recommendations on where to put what in Unix-like systems.
The Filesystem Hierarchy Standard defines this and offers you the following:
/var
– a place to put data that changes during normal operation, like logs, etc./var/www
is the default directory to place web content for Apache, but its usage is not standardized at all and just the "usual" place you'd put it because people don't change the default settings very often.-
/srv
– this directory should contain data that is served by the system. This is usually the place you want. The FHS explains:This main purpose of specifying this is so that users may find the location of the data files for particular service, and so that services which require a single tree for readonly data, writable data and scripts (such as cgi scripts) can be reasonably placed. Data that is only of interest to a specific user should go in that users’ home directory. (…)
One method for structuring data under
/srv
is by protocol, eg.ftp
,rsync
,www
, andcvs
So, simply create a
/srv/www
directory and use this. You can create subfolders for every virtual host you might want to serve with your machine. -
/home
contains files that really should just belong to one user. Apache for example allows userdirs, so you can access a user's web files throughhttp://example.com/~username
, and they're served from thepublic_html
directory in the user's home.If you use a server that is shared among multiple people, and you want to allow everybody to host their own scripts, this is where they should go. Remember to make the directories writable by the user they belong to only.
In essence /srv/www
and /var/www
are directories you should create subdirectories in for any web project you might want to host. You can then define different permissions on these directories to allow certain users or user groups to write to them. If you have projects for one user at a time, use /home
.
Solution 2
Well you can put files anywhere long as things can access them properly, however cluttered filesystems are a headache if someone comes in later.
/srv
is most logical plus if you follow Filesystem Hierarchy Standard it would go here.
If you do multiple domains you can do /srv/domain1
/srv/domain2
etc etc then subfolder out inside there /ftp
/www
/tftp
/logs
/etc.etc.etc
To me that feels a very solid structure to build upon and easily control
But as an administrator you can do as clean or messy as you wish.
Solution 3
Ok easy quick answer.
If your web files on the system will only be accessed by ONE user on the linux system. Use the home directory of the user (~/
).
If your web files on the system will be accessed by MULTIPLE users on the linux system. Use /srv/
.
This is exactly what http://refspecs.linuxfoundation.org/FHS_2.3/fhs-2.3.html#SRVDATAFORSERVICESPROVIDEDBYSYSTEM states.
Here is the quote:
/srv contains site-specific data which is served by this system.
This main purpose of specifying this is so that users may find the location of the data files for particular service, and so that services which require a single tree for readonly data, writable data and scripts (such as cgi scripts) can be reasonably placed. Data that is only of interest to a specific user should go in that users' home directory.
Bonus: www? ftp? Organize by protocol? Huh?
As stated here in http://refspecs.linuxfoundation.org/FHS_2.3/fhs-2.3.html#SRVDATAFORSERVICESPROVIDEDBYSYSTEM
- If your website is only accessed by ONE user on the system and ONLY via the browser (http protocol) then:
~/http/your-website-directory/
- If your website is only accessed by ONE user on the system and NOT ONLY via the browser but multiple protocols (i.g. http AND tcp AND ...) then:
~/your-website-directory/
- If your website is accessed by MULTIPLE users on the system and ONLY via the browser (http protocol) then:
/srv/http/your-website-directory/
- If your website is accessed by MULTIPLE users on the system and NOT ONLY via the browser but multiple protocols (i.g. http AND ftp AND ...) then:
/srv/your-website-directory/
Huh why not www? This is legacy from the Apache time. www doesn't specify which protocol is being used. Debian still uses this as of today while for example Arch linux uses /srv/http.
Solution 4
Apache web server have default website under /var/www/
but it is suggesting to put other websites under /srv/
I noticed this on Ubuntu Server 14.04 LTS. Its default apache2.conf
file contains commented block:
#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
Related videos on Youtube
Comments
-
Pattle over 1 year
When I first started programming for the web and I wanted to create a new project I was always taught to create a directory in
/var/www/
. However in a lot of tutorials I read people tend to create a directory in/home/username/
.I don't like the idea of putting it in
/home/username/
.Is there a correct place or if not what are the advantages/disadvantages of placing it in certain areas on the file system?
-
nerdwaller over 10 yearsThat's preference ("best" implies that, maybe you should change this to be most secure, etc.) for development mostly. If you are serving them from there too, then you'd probably want something secluded to keep some sort of distance between your personal files and web-share stuff. But there are a million configurations. I stick with
/var/www
and usually have that on another drive (just preference). -
Ramhound over 10 yearsThere is no "best" directory. Its entirely your perference because the phyiscal location of the files does not matter.
-
-
choroba over 10 yearsThe
http://example.com/~username
usually does not point to/home/username/
, but to/home/username/public_html/
. -
slhck over 10 yearsYeah, thanks, that should have been added for clarity. Done.
-
sitilge over 7 yearsAfter several years of using
/var/www
it is time for change! -
sitilge over 7 yearsAlso,
www
sounds more like a subdomain to me. -
Jools about 5 yearsJust to add... The use of /var/www is a common practice for single site serving instances and as said the default Apache location, whereas using /home/usr/ was/is a common practice for reseller servers or multi-site hosting drawn from the concept of users==clients. Both are just common practices and shlck's answer is a better use of the filesystem's intended purposes.
-
Sly Gryphon about 3 yearsThe FHS specifically mentions "eg. ftp, rsync, www, ...". I would debate whether HTTP and HTTPS count as separate protocols, or just the same protocol with and without TLS. They are both Hypertext Transfer Protocol, just one is Hypertext Transfer Protocol Secure. Usually the same service would serve the content once, using both protocols (with port 80 to get an automatic Let's Encrypt certificate to use for 443). Otherwise there would be multiple methods of FTP, with and without TLS, and any other protocol that has both with and without TLS versions.
-
Karl Morrison about 3 years@SlyGryphon You bring up a valid point actually. Now when I think about it I think just using the protocol and leaving out TLS would make life easier for everyone.
-
Karl Morrison about 3 years@SlyGryphon Removed https.
-
Eduardo Lucio over 2 yearsUp for FHS - Filesystem Hierarchy Standard pathname.com/fhs . =D
-
MestreLion over 2 years