Ubuntu – Linking to items outside of the web root

apache-2.4mod-aliasPHPUbuntu

Here is how the directories are laid out on my server (I inherited this and cannot change at the moment) –

/var/www/<stuff for the web>

/home/project/src/static/<some CSS, some JavaScript, etc.>

In the grand scheme of not wanting to have duplicate files in different directories I tried to include some files like this –

<link rel="stylesheet" href="/home/project/src/static/css/<file>.css" type="text/css" />
<script src="/home/project/src/static/lib/<file>.js" type="text/javascript"></script>

I get a 404 error for each file I have tried to include this way. I have modified the path to the 'home' directory, attempting to traverse up from /var/www/ to /home/ by adding .. to the beginning of the path string, but no joy.

Then, after confirming that mod_alias was installed, I set about adding a path alias directive to my config file:

Alias /home/project/src/static/ /home/project/src/static/
<Directory /home/project/src/static/>
    Order allow,deny
    Allow from all
</Directory> 

Restarted Apache, but still no luck. I tried this with and without the trailing slash.

One other thing that I have confirmed is that open_basedir is not set in the php.ini as I understand having it set can produce problems when trying to access files outside of the web root.

What can I do to make these files findable from the web root outside of copying the directory and placing it in the web root folder?

Best Answer

Two options... one is just use Alias properly... the first path is relative to the web directory, like if you had http://yoursite/static in the browser, you would use:

Alias /static/ /home/project/src/static/

Or if you don't like that and would rather solve it by changing the file system heirarchy, use a bind mount.

mkdir /var/www/static/

And then in fstab:

/home/project/src/static/ /var/www/static/ bind defaults 0 0

And to apply changes:

mount -a

or a one time mount:

mount --bind /home/project/src/static/ /var/www/static/

A bind mount is not always the best or simplest choice... funny things can happen, eg. run rm -rf /var/www/* and your /home/project/src/static/* will get deleted too.

Related Topic