ScaleScaleScaleScale

Tips / Nginx


How to Install NFS Server on CentOS 6

nfs

NFS means Network File System, and it is a great way to share a filesystem across your network and use it mounted on as many computers/servers as you want. The result: you can access a remote local network storage to read, write, and of course even serve your Nginx requests.

On this tutorial I will show you How to install NFS server on CentOS 6, you will also lear how to mount nfs on the remote clients and finally how to serve traffic from NFS using Nginx.

Requirements:

  • 2 servers, one to act as master NFS server and the other to act as client/slave.
  • 100MBit private network (1Gbps is better) between both servers.
  • Nginx installed (if you want to serve files from NFS server).
  • CentOS Linux on each box, if you need a VPS/Dedicated server you can take a look at this two great hosting providers:
    • A Small Orange
    • A2 Hosting

Install NFS server on CentOS 6

yum update -y
yum groupinstall 'Development Tools' -y

On the NFS Server (master) run this commands:

yum install nfs-utils nfs-utils-lib -y

Add the NFS server to boot automatically:

chkconfig nfs on 
chkconfig rpcbind on
service rpcbind start
service nfs start

Export the Shared Directory

Now the next step is to configure which directory will be shared across the client (slaves) servers. It could be /var/www/yoursite.com /home/user/documents, or whatever you need. Once you have the shared directory in mind, then add it to /etc/exports file, where you can specify the directory to share and its options.

On this example, let’s suppose we want to share /var/www/yoursite.com and that your CLIENT NFS server is using 192.168.1.110 IP.

nano -w /etc/exports

Add the following lines to the bottom of the file, sharing the directory with the client:

/var/www/yoursite.com     192.168.1.110(rw,sync,no_root_squash,no_subtree_check)

As you see, we specified the shared directory to be /var/www/yoursite.com, the IP allowed to connect to the NFS server is 192.168.1.110 and the last line is showing a few sharing options, let’s explore each one of them:

rw: allow read and write from the client server.
sync: will sync only once changes have been done to the nfs server local directory.
no_root_squash: allows root user to connect to the shared directory.
no_subtree_check: disabling this will speed up transfers, avoid volume check.

Tcpwrappers and Firewall configuration

If you are using tcpwrappers, then add this lines to your /etc/hosts.allow file

nano -w /etc/hosts.allow

Add this inside:

# NFS
portmap : 192.168.1.110
lockc : 192.168.1.110
mountd : 192.168.1.110
rquotad : 192.168.1.110
statd : 192.168.1.110

If you are using iptables firewall, remember to add 192.168.1.110 IP to the whitelist to allow any communication, or run this iptables allow command:

iptables -A INPUT -s 192.168.1.110 -j ACCEPT

On the NFS clients (slaves) run this commands:

yum install nfs-utils nfs-utils-lib

Mount the NFS Shared Directory

Create the shared directory:

mkdir -p /var/www/yoursite.com

Then mount it manually:

mount XX.XX.XX.XX:/var/www/yoursite.com /var/www/yoursite.com

Replace “192.168.1.109” with the private IP of your master server.

Test the NFS mount

df -ah | grep yoursite.com

You should see something like this:

192.168.1.109:/var/www/yoursite.com 108G 23G 80G 23% /var/www/yoursite.com

Now let’s add the mount point to the /etc/fstab file so it can boot automatically after any reboot:

192.168.1.109:/var/www/yoursite.com /var/www/yoursite.com nfs auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0

Test it:

mount -a
df -ah

If you see it has been mounted again, then your /etc/fstab entry is working as expected.

Finally, you can use /var/www/yoursite.com to be served using Nginx, as any other local directory. This is a full example of a simple virtual host working with NFS and PHP-FPM daemon:

nano -w /etc/nginx/conf.d/yoursite.comf

Add this to that file:

### yoursite.com

server {
access_log off;
log_not_found off;
error_log  /etc/nginx/logs/yoursite.com-error_log warn;

        listen       80;
        server_name  yoursite.com www.yoursite.com;


       location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|wml|swf)$ {
        root   /var/www/yoursite.com;
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";

        }

        location / {
            root   /var/www/yoursite.com;
            index  index.php index.html index.htm;
        }


# PHP parsing using php-fpm
        location ~ .php$ {
            root           /var/www/yoursite.com;
            try_files $uri =404;
            fastcgi_pass   unix:/tmp/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 256 4k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;

        }
}

As you see, installing a NFS server on CentOS 6 is not that hard, and it’s even more easy to use it with Nginx web server.

Popular search terms:

  • how to install nfs server on centos
  • centos nfs mount
  • install nfs on centos 6
  • nfs server centos
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • miax

    Ok but what with /var/lib/mysql?