Build an nfs server in Ubuntu

Source: Internet
Author: User
Tags fully qualified domain name
The reason is that two servers need to share a hard disk space during work, so we are going to build an nfs server, the following describes how to install and use php to access Shared partitions: NFSNFS (NetwordFileSystem), a network file system, is a way to share disk files between Unix systems, it supports a file system protocol that allows applications to access data on the server disk over the network on the client. (Ps: I understand NFS. It is a C/S architecture. The SERVER shares the directory with CL.

Cause
Because the two servers need to share a hard disk space at work, we are preparing to build an nfs server. The following describes the installation steps and using php to access the shared partition.

Introduction NFS
Network File System (NFS) is a way to share disk files between Unix systems, it supports a file system protocol that allows applications to access data on the server disk over the network on the client. (Ps: I understand NFS. It is a C/S architecture. The SERVER shares the directory with the CLIENT)

Principle
Based on SUN's Remote Procedure Call (Remote Procedure Call), RPC defines a system-independent method for inter-process communication. therefore, the NFS server can also be viewed as an RPC server.
Because NFS is an RPC service program, you must map the port before using it-set it through portmap. for example, when an NFS client initiates an NFS service request, it must first obtain a port ). therefore, it first obtains the port number through portmap.

Server Configuration
Install server software

Sudo apt-get install nfs-kernel-server

After nfs-kernel-server is installed, the system automatically installs portmap and nfs-common (client software for testing) for us)
Configure portmap

Sudo vim/etc/exports

Comment out a configuration statement as follows:


# OPTIONS = "-I 127.0.0.1"
Restart portmap (ps: real bug, portmap does not support direct restart)

Sudo service portmap stop
Sudo service portmap start

Configure the shared directory

Sudo vim/etc/exports

A typical shared directory configuration code is as follows:

/Home/xiaoyi * (rw, sync, no_subtree_check)

/Home: the file directory to be shared on the server (you can specify it as needed)


*: A computer that allows access. The following information can be used:
Single Machine => A complete IP address, a fully qualified domain name (can be resolved by the server), and a Host Name (can be resolved by the server)
The host name specified by the wildcard. Wildcards are not allowed in the IP address. * indicates that all hosts that can access the server are allowed to connect to the shared directory.
IP Address: IP address 192.168.1.1/24 + Subnet Mask

(): Sharing options
Rw: the client has read and write permissions (ro: the code has only read permissions)
Sync: synchronize data written to memory and Hard Disk
No_subtree_check: The subdirectory permission is not checked.
Root_squash: if the client uses the root permission to access the shared directory, it will be considered as an anonymous user (permission reduction)
No_root_squash: if the client uses the root permission to access the shared directory, the permission is still root

After the configuration is complete, restart the nfs-server service.

Sudo/etc/init. d/nfs-kernel-server restart

Client Configuration
Install client software

Sudo apt-get install nfs-common

Similarly, apt will automatically help you install portmap

Configure portmap

Sudo vim/etc/default/portmap

Comment out a command as follows:

# OPTIONS = "-I 127.0.0.1"

Restart portmap

Sudo service portmap stop
Sudo service portmap start

Hanging in the shared directory on the server to the local

Sudo mount server_ip:/share_nfs_directory client_nfs_directory

Unmount directory

Sudo umonut client_nfs_directory

Operate NFS mount partitions in PHP

// 1. Test the Directory Creation
$ Temp_dir = "/srv/www/nfs/image ";
If (mkdir ($ temp_dir ))
{
Echo "PHP has successfully mounted the nfs partition directory! \ N ";
} Else
{
Echo "PHP failed to mount the partition directory for nfs! \ N ";
}

// 2. Test File Creation
$ Temp_file = $ temp_dir. "/ceshi.txt ";

If (! $ Fp = fopen ($ temp_file, 'A '))
{
Echo "An error occurred while creating the file! \ N ";
}

$ Content = "hello world! \ N ";
If (fwrite ($ fp, $ content ))
{
Echo "www-data user written successfully! \ N ";
} Else
{
Echo "Write failed! \ N ";
}
Fclose ($ fp );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.