views:

30

answers:

2

I have a Virtual Private server with Dreamhost. I'm trying to create a shared folder that all of my domains can access. In the folder I'd like to put PHP classes, and even static files like javascripts.

I've created a directory on the same level as my domain folders. I'd like to call a file via something like this... /home/username/shared/file.php. This isn't working however, and I'm hoping some magic (like .htaccess maybe) will make this work.

EDIT: Oops, OK, I am able to include PHP files using the method above. How can I include static files like Javascript, CSS, images, etc from the same directory?

This works:

<?php include('/home/username/shared/file.php'); ?>

This doesn't work:

<link rel="stylesheet" type="text/css" href="/home/username/shared/reset.css" media="screen" />

Alternatively, I realize that I could just place my static files inside of a domain, and simply point to them, but I'd like to know how to make this other configuration work.

A: 

You should be happy that <link rel="stylesheet" type="text/css" href="/home/username/shared/reset.css" media="screen" /> did not work. If it would, you would have a great security hole.

The correct way is to make a symlibk to that shared directory in your document root, or using Aliases.

The Symlink Method

Run the following in a shell:

ln -s /home/username/shared /home/target/

It'll create a symlink named /home/target/shared, pointing to /home/username/shared. You might need add a Options +FollowSymLinks directive to your server config (or a .htaccess file)

The Alias Method

This method requires a change in your server configuration (e.g. httpd.conf). Add an Alias directive to it:

Alias /shared /home/username/target

Note: this will only be used for requests through your server (e.g. http://example/shared/reset.css), and not for PHP files. Within PHP files, you have to use the full path (/home/username/shared/file.php)


Which method you choose is up to you. The latter is easier to configure as you do not have to worry about permissions / ownership. The former could be used if you really need to use /home/target/shared/file.php.

Lekensteyn
A: 

Including static files like images, js, css are not parsed by the server, but the client. So when the client sees /home/user/shared/reset.css, he actually tries to load this like: www.domain.com/home/user/shared/reset.css

The easiest thing you can do is just create a folder somewhere on the hosting and then link it to a folder in the domain folders.

So for example you have this structure:

/home/
   user/
      domain1/
      domain2/
      domain3/

You could easily create a folder for static content under /home/user, e.g. /home/user/static_files and then for every domain, just link the folder once through the console (ssh):

ln -s /home/user/static_files /home/user/domain1/static_files
ln -s /home/user/static_files /home/user/domain2/static_files
ln -s /home/user/static_files /home/user/domain3/static_files

The lines above will create the so called symbolic link. This means that it will create just shortcuts in the domain folders and when you edit files in the main folder (/home/user/static_files), the changes will be available in the domain folders.

Now just adjust your code to point like this:

<img src="/static_files/image.png" />
<script src="/static_files/somescript.js"></script>
<link rel="stylesheet" type="text/css" src="/static_files/reset.css" />

This will just look for the folder "static_files" under the root of your domain.

bisko