If you read my post about creating your own CDN you must have wondered how to share files across servers. I use one server to host my code and another to host my image files or assets. This could be problematic for most simple set ups because you have to upload your files into a different server.

The solution is to use shared folders. You create a folder on your CDN server, and make it accessible through the application server. It's actually very easy.

Software requirement NFS.

You need NFS (network file system). The CDN will have the server application and our code server will have the client application.

Server (CDN)

Install the nfs server program:

sudo apt-get install nfs-kernel-server portmap

Half the work is already complete. All you have to do now is setup the directories you want to share.

Let's say the folder we want to share is called /var/assets. In my case I didn't have to change the ownership but if it fails to share, do change the owner to nobody and the group to nogroup.

sudo chown nobody:nogroup /var/assets

Now you folder is ready to be shared. We can add an entry inside /etc/exports to let the machine know that this folder is to be exported.

sudo vim /etc/exports

You can find your machine IP address using the command ifconfig right after inet addr:<ipaddress>. Let's assume 10.0.0.2 for our CDN server and 10.0.0.1 for our Application server.

Inside the export file add this entry:

/var/assets       10.0.0.1(rw,sync,no_root_squash,no_subtree_check)

Here we are telling our server that a client with ip 10.0.0.1 will have access to this folder.

Save and confirm these entries using the exportfs command.

exportfs -a

Now our server application is ready to broadcast the shared folders to the client. Now let's setup the client machine.

Client (Application)

Install the nfs client program:

sudo apt-get install nfs-common portmap

All we need to do now is create the directories where we want the shared folders to appear. The standard is to have the directory in the /mnt directory since what we are doing is called mounting technically.

Let's create the folders:

mkdir -p /mnt/nfs/var/assets

Now we can mount our shared folder at this location:

mount 10.0.0.2:/var/assets /mnt/nfs/var/assets

That's it! Note that this is the IP of our CDN server. You can run the df command to ensure that it works correctly.

df -h

Filesystem             Size  Used Avail Use% Mounted on
/dev/sda                20G  120M   29G   5% /
udev                   119M  4.0K  102M   1% /dev
tmpfs                   49M  405K   49M   1% /run
none                   6.0M     0  6.0M   0% /run/lock
10.0.0.2:/var/assets    20G  948M   19G   5% /mnt/nfs/var/assets

You can also create a file from the client and check if it is available in the server.

echo "hello world" > /mnt/nfs/var/assets/test.txt

One thing I like to do is create a symlink of the shared folder this way I don't have to type all this path in my application when uploading a file.

ln -s /mnt/nfs/var/assets /var/www/assets

This makes it part of my web application without really being there.

Also if you want to unmount a folder all you need is use the umount command.

sudo umount /mnt/nfs/var/assets

Last but not least, if your client server were to be rebooted for some reason, you would lose the connection. You would have to re-mount the folder. You can solve this by making sure that your shared folder is mounted at start time.

Add this entry inside /etc/fstab

10.0.0.2:/var/assets /mnt/nfs/var/assets   nfs     auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0

That's it now you know you can enjoy uploading files from your we application and serving those files through your CDN.

Ressources: