Handling user generated files by NFS

If you have multiple servers for your web application, you may need a Network File System. It is a common use case where you need to keep user generated files. For example, userA has uploaded one file using through server1. Now no one (including userA) cannot access/see the file through server2 because the file is saved in server1. So the problem is to make the files available all over the servers. The best way to solve the problem is having an NFS server. All of your servers will act as NFS client and communicate with NFS server.
#---------------------------#
# Installing NFS Server
#---------------------------#
yum install nfs-utils nfs4-acl-tools portmap

chkconfig nfs on
chkconfig rpcbind on
chkconfig rpcidmapd on
chkconfig nfslock on

# Configure the server:
vim /etc/sysconfig/nfs

RQUOTAD_PORT=875
MOUNTD_PORT=892
STATD_PORT=662
STATD_OUTGOING_PORT=2020

LOCKD_TCPPORT=32803
LOCKD_UDPPORT=32769

# Restart services
sudo /sbin/service rpcbind restart
sudo /sbin/service rpcidmapd restart
sudo /sbin/service nfslock restart
sudo /sbin/service nfs restart

# Write the following line in /etc/fstab to keep it available even after rebooting
/path/to/files     none    bind    0 0

# Write the following line in /etc/exports
/exports  client1_ip(rw,fsid=0,no_subtree_check,sync) client2_ip(rw,fsid=0,no_subtree_check,sync)

# Run the following command
exportfs -av

#-----------------------------------------------------------#
# On nfs clinet install the following packages:
#-----------------------------------------------------------#
yum install nfs-utils # Needed for nfs support
yum install nfs4-acl-tools # For access control

# Mount
mount -t nfs server_ip:/exports/files/app /var/www/files/app
# Add in /etc/fstab to mount automatically after rebooting 
server_ip:/exports/files/app                    /var/www/files/app       nfs    auto        0 0

Share this post

Leave a comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.