Setting up an NFS server and its clients on CentOS
NFS is quite handy when you have several servers and need shared access to the same storage from each of them.
NFS lets you expose folders and files over the network to other servers.
Let’s look at an example:
Say we have 2 servers:
web01, ip: 10.0.0.10
web02, ip: 10.0.0.20
Each hosts 2 sites. One runs on WordPress, the other on Joomla.
I need the following folders to be identical on both servers, with the same content:
- /var/www/html/wordpress-site.com/wp-content/uploads
- /var/www/html/joomla-site.com/cache
- /var/www/html/joomla-site.com/media
I’ll set up sharing these folders from web01 to web02.
Let’s get started. Connect to web01 and install the packages we need:
yum install nfs-utils nfs-utils-lib
Enable nfs on boot and start it:
chkconfig nfs on
/etc/init.d/nfs start
Start rpcbind if you get this error:
Starting NFS daemon: rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
Even better, enable it on boot too:
chkconfig rpcbind on
/etc/init.d/rpcbind start
Open /etc/exports in your favorite editor and add these lines:
/var/www/html/wp-site.com/wp-content/uploads 10.0.0.20(rw,sync,no_root_squash,no_subtree_check)
/var/www/html/joomla-site.com/cache 10.0.0.20(rw,sync,no_root_squash,no_subtree_check)
/var/www/html/joomla-site.com/media 10.0.0.20(rw,sync,no_root_squash,no_subtree_check)
Example for a whole subnet:
/var/www 10.10.0.0/16(rw,sync,no_root_squash,no_subtree_check)
I won’t go into detail on the options — it’s all documented here.
Save and close the file. To apply the changes, run:
exportfs -a
Move to the second server, web02, and install the packages there too:
yum install nfs-utils nfs-utils-lib
Enable nfs on boot and start it:
chkconfig nfs on
/etc/init.d/nfs start
Next, edit fstab so our folders get mounted at server startup.
Open /etc/fstab in your favorite editor:
10.0.0.10:/var/www/html/wp-site.com/wp-content /var/www/html/wp-site.com/wp-content nfs rw,sync,hard,intr 0 0
10.0.0.10:/var/www/html/joomla-site.com/cache /var/www/html/joomla-site.com/cache nfs rw,sync,hard,intr 0 0
10.0.0.10:/var/www/html/joomla-site.com/media /var/www/html/joomla-site.com/media nfs rw,sync,hard,intr 0 0
Save. Close. Run:
mount -a
If the folders already exist, that’s fine. You can rename them just in case, or clear them out. Or do nothing at all — the mounted folders will just sit on top of the old ones.
To verify, create some test files in the folders on web01 and check whether they show up on web02.