Lets install NFS so servers can share files.

I have some web servers that need to share some content files. We’ve tried Rsync and Unison but they are failing us, big time. We are opt-ing to setup an NFS share located on a central server.

First step, install NFS and needed packages on each server involved. We use Debian, so I get to do a simple:

apt-get -f install nfs-common

A netstat -ntlp should show some ports in use:

tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 21486/portmap
 tcp 0 0 0.0.0.0:37715 0.0.0.0:* LISTEN 21681/rpc.statd
 tcp 0 0 0.0.0.0:47907 0.0.0.0:* LISTEN 24930/rpc.mountd

We need to setup matching user and group on the NFS server so we do not need to worry so much about permissions:

adduser --home /www/sites/work --uid 1002 --ingroup vhosts --force-badname  work

Lets edit /etc/exports and add some info:

/www/sites/www.hats.com/files/html/media 192.168.0.1(rw) 192.168.0.2(rw)

Lets lock down NFS/Portmap with /etc/hosts.deny and /etc/hosts.allow. Lets start with hosts.deny. Add these lines:

portmap:ALL
lockd:ALL
mountd:ALL
rquotad:ALL
statd:ALL

Now, lets open up those to our servers that need to connect to the NFS share. Add these to /etc/hosts.allow:

portmap: 192.168.0.1 , 192.168.0.2
lockd: 192.168.0.1 , 192.168.0.2
mountd: 192.168.0.1 , .192.168.0.2
rquotad: 192.168.0.1 , 192.168.0.2
statd: 192.168.0.1 , 192.168.0.2

Lets give NFS/protmap a check:

rpcinfo -p localhost
 program vers proto port
 100000 2 tcp 111 portmapper
 100021 1 udp 39415 nlockmgr
 100021 3 udp 39415 nlockmgr
 100021 4 udp 39415 nlockmgr
 100021 1 tcp 47646 nlockmgr
 100021 3 tcp 47646 nlockmgr
 100021 4 tcp 47646 nlockmgr
 100003 2 tcp 2049 nfs
 100003 3 tcp 2049 nfs
 100003 4 tcp 2049 nfs
 100003 2 udp 2049 nfs
 100003 3 udp 2049 nfs
 100003 4 udp 2049 nfs
 100005 1 udp 58197 mountd
 100005 1 tcp 47907 mountd
 100005 2 udp 58197 mountd
 100005 2 tcp 47907 mountd
 100005 3 udp 58197 mountd
 100005 3 tcp 47907 mountd
 100000 2 udp 111 portmapper
 100024 1 udp 50436 status
 100024 1 tcp 51904 status

Yay! It is running. Lets create the rest of the path, we are preserving the paths the exist on the web servers:

mdkir /www/sites/work/files
mkdir /www/sites/work/files/html/
mkfir /www/sites/work/files/html/media

And fix up permissions:

chown work:vhosts -R /www/sites/work/

Lets jump to one of the web servers and give mounting the NFS share a try (note: I already opened access between servers through the firewalls):

mkdir /mnt/nfs-share
mount 192.168.0.3:/www/sites/work/files/html/media /mnt/nfs-share
ls -lah /mnt/nfs-share
drwxr-xr-x 5 www.hats.com vhosts 4.0K May 14 21:48 .
drwxr-xr-x 3 root root 4.0K May 14 21:40 ..
-rwxr-xr-x 1 www.hats.com vhosts 135K May 14 21:48 testfileondb01

Go a head and cp or rsync or copy the files over to the NFS share:

cp -Rv /www/sites/work/files/html/media/* /mnt/nfs-share/

Lets set this up to mount automagicaly but adding this line to /etc/fstab on each web server:

192.168.0.3:/www/sites/work/files/html/media/ /mnt/nfs-share nfs rw,async 0 0

Then we will create a symlink from the web directory to the NFS share mount:

ln -s /mnt/nfs-share/ /www/sites/work/files/html/media

If you are using Apache, then do not forget to check the apache config files:

Options  +FollowSymLinks

Or how ever you want to do it.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.