拓扑图

扩展实验:LAMP+NFS_第1张图片

Http server1 172.20.10

http server2 172.20.1.11

mariadb\NFS 172.20.1.12

效果:客户机在使用IP分别访问server1 和 server2,使用相同的账号、密码,看到帖子的内容完全一样,上传的附件在两台server中完全一致。

实施过程:

在http server 1、2中安装Apache、php,两台机的过程一样,则不重复列出过程

172.20.1.10安装Apache和php

编译安装Apache2.4版本,事先安装一些开发包组

[root@localhost linux software]# yum groupinstall -y "Development Tools" "Server Platform Development"

安装顺序:apr à apr-util à httpd2.4

apr-1.5.0.tar.bz2

apr-util-1.5.3.tar.bz2

[root@localhost linux software]# tar -xf apr-1.5.0.tar.bz2

[root@localhost linux software]# cd apr-1.5.0

[root@localhost apr-1.5.0]# ./configure --prefix=/usr/local/apr

[root@localhost apr-1.5.0]# make && make install

[root@localhost software]# tar xf apr-util-1.5.3.tar.bz2

[root@localhost software]# cd apr-util-1.5.3

[root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[root@localhost software]# yum install -y pcre-devel zlib-devel openssl-devel

[root@localhost software]# tar xf httpd-2.4.20.tar.bz2

[root@localhost software]# tar xf httpd-2.4.20.tar.bz2

[root@localhost software]# cd httpd-2.4.20

[root@localhost httpd-2.4.20]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-module=most --enable-mpms-shared=all --with-mpm=prefork

[root@localhost httpd-2.4.20]# make && make install

[root@localhost ~]# vim /etc/profile.d/httpd.sh

export PATH=/usr/local/apache/bin:$PATH

[root@localhost ~]# source /etc/profile.d/httpd.sh

[root@localhost ~]# cp /etc/rc.d/init.d/httpd /etc/rc.d/init.d/httpd24 #如果有的话复制,没有的话就下载或者从别的地方copy一个即可

[root@localhost ~]# vim /etc/rc.d/init.d/httpd24

主要修改:

apachectl=/usr/local/apache/bin/apachectl

httpd=${HTTPD-/usr/local/apache/bin/httpd}

pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}

另外在/etc/httpd24/httpd.conf中也要修改

User apache

Group apache

DocumentRoot 后面会再次提到,根据NFS共享路径来修改

安装PHP

[root@localhost software]# tar -xf php-5.4.26.tar.bz2

[root@localhost software]# cd php-5.4.26

[root@localhost php-5.4.26]# yum install -y libxml2-devel libcrypt-devel bzip2-devel libmcrypt-devel #如果仍有报错,则根据错误安装依赖的软件即可

[root@localhost php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-apxs2=/usr/local/apache/bin/apxs --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-bz2 --with-config-file-path=/etc/php/php.ini --with-config-file-scan-dir=/etc/php.d/

[root@localhost php-5.4.26]# make && make install

[root@localhost php-5.4.26]# cp php.ini-production /etc/php/php.ini

[root@localhost php-5.4.26]# vim /etc/httpd24/httpd.conf

AddType application/x-httpd-php .php #增加

AddType application/x-httpd-php-source .phps #增加

DirectoryIndex index.php index.html #增加index.php

MariaDB(172.20.1.12)

[root@localhost ~]# tar xf mariadb-5.5.40-linux-x86_64.tar.gz

[root@localhost ~]# mv mariadb-5.5.40-linux-x86_64 /usr/local/mysql

[root@localhost ~]# id mysql

id: mysql: No such user

[root@localhost ~]# groupadd -r mysql

[root@localhost ~]# useradd -r -g mysql mysql

[root@localhost ~]# chown -R root:mysql /usr/local/mysql/*

[root@localhost ~]# mkdir -pv /data/sqldata

mkdir: created directory `/data'

mkdir: created directory `/data/sqldata'

[root@localhost ~]# chown -R mysql:mysql /data/sqldata/

[root@localhost ~]# mkdir /etc/mysql

[root@localhost ~]# cd /usr/local/mysql/

[root@localhost mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf

[root@localhost mysql]# cp scripts/mysql_install_db . #执行报错缺少文件,复制到上一目录

[root@localhost mysql]# ./mysql_install_db --user=mysql --datadir=/data/sqldata/

[root@localhost mysql]# vim /etc/mysql/my.cnf #修改配置文件[mysqld]模块下

thread_concurrency = 4 #CPU核数*2

datadir = /data/sqldata #增加

innodb_file_per_table = on #增加

[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld

[root@localhost mysql]# chkconfig --add mysqld

[root@localhost mysql]# chkconfig mysqld on

[root@localhost mysql]# service mysqld start

[root@localhost mysql]# vim /etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:$PATH

[root@localhost mysql]# source /etc/profile.d/mysql.sh

启动并进入mysql,创建一个给discuz使用的库,并授权一个远程用户

MariaDB [(none)]> CREATE DATABASE discuz;

Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> GRANT ALL ON discuz.* TO 'bbsuser'@'%' IDENTIFIED BY '123456';

Query OK, 0 rows affected (0.00 sec)

NFS(172.20.1.12)

[root@localhost ~]# mkdir /share

[root@localhost ~]# cp Discuz_X3.2_SC_GBK.zip /share/

[root@localhost ~]# id apache

uid=48(apache) gid=48(apache) groups=48(apache) #正好三台机器上都存在apache用户,并且其UID GID都是一样的(UID GID很重要,必须要一致)

[root@localhost ~]# setfacl -R -m u:apache:rwx /share

[root@localhost ~]# setfacl -R -m g:apache:rwx /share

[root@localhost ~]# getfacl /share/

getfacl: Removing leading '/' from absolute path names

# file: share/

# owner: root

# group: root

user::rwx

user:apache:rwx

group::r-x

group:apache:rwx

mask::rwx

other::r-x

[root@localhost ~]# vim /etc/exports

/share 172.20.1.0/24(rw,all_squash,anonuid=48,anongid=48)

[root@localhost ~]# service nfs start

在172.20.1.10上手动挂载测试一下,172.20.1.11上所有操作跟1.10上一样即可

[root@localhost ~]# mkdir -pv /var/www/webroot

mkdir: created directory `/var/www/webroot'

[root@localhost ~]# mount -t nfs 172.20.1.12:/share /var/www/webroot/

[root@localhost ~]# mount

/dev/sda5 on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw)

/dev/sda1 on /boot type ext4 (rw)

/dev/sda2 on /home type ext4 (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

172.20.1.12:/share on /var/www/webroot type nfs (rw,vers=4,addr=172.20.1.12,clientaddr=172.20.1.10)

解压出nfs共享里的discuz安装包

[root@localhost webroot]# unzip Discuz_X3.2_SC_GBK.zip

[root@localhost webroot]# ll

total 12120

-rw-rwxr-- 1 root root 12395897 Aug 12 2016 Discuz_X3.2_SC_GBK.zip

drwxr-xr-x 2 apache apache 4096 May 31 2016 readme

drwxr-xr-x 12 apache apache 4096 May 31 2016 upload

drwxr-xr-x 4 apache apache 4096 May 31 2016 utility

##虽然使用的root,但是属主和属组都是apache的身份

[root@localhost webroot]# mv upload/* .

将upload下的文件放到DocumentRoot设定的路径下

[root@localhost webroot]# vim /etc/fstab

172.20.1.12:/share /var/www/webroot nfs defaults,_netdev 0 0

#让nfs共享开机能够自动挂载

[root@localhost webroot]# vim /etc/httpd24/httpd.conf

DocumentRoot "/var/www/webroot "

options Indexes FollowSymLinks #根据实际情况修改,试验情况下无所谓了

AllowOverride None

Require all granted

#注释或者修改原来的DocumentRoot路径

网页打开安装discuz

http://172.20.1.10/install

扩展实验:LAMP+NFS_第2张图片

扩展实验:LAMP+NFS_第3张图片

扩展实验:LAMP+NFS_第4张图片

扩展实验:LAMP+NFS_第5张图片

扩展实验:LAMP+NFS_第6张图片

到另外一台机器上验证

扩展实验:LAMP+NFS_第7张图片

上传的附件以及图片完全一致

172.20.1.10与172.20.1.11两台机的操作完全一致,重点在与nfs服务器的共享目录权限,匿名id与启动Apache服务用户的id一致,即rw,all_squash,anonuid=48,anongid=48,Nfs共享出来的目录可以修改其属主属组或者setfacl赋予权限(uid/gid 48)。