43524 发表于 2015-9-30 08:46:13

LAMP+NFS(编译php-fpm模块与apache2.4结合)

搭建环境:Centos 6.6 + apache2.4 + mariadb5.5 + php-fpm5.5 + nfs

准备好3台虚拟机,配置好主机名和IP地址,关闭防火墙和selinux
nfs启动顺序:先启动rpc—>在启动nfs
nfs权限参数
rw:读写ro:只读sync:同步模式,内存中数据时时写入磁盘async:不同步,把内存中数据定期写入磁盘中no_root_squash:加上这个选项后,root用户就会对共享的目录拥有至高的权限控制,就像是对本机的目录操作一样。不安全,不建议使用root_squash:把root通过网络访问时,换成nfsnobody用户,即限制了root权限all_squash:不管使用NFS的用户是谁,他的身份都会被限定成为nfsnobody用户身份anonuid/anongid:要和root_squash以及 all_squash一同使用,用于指定使用NFS的用户限定后的uid和gid,前提是服务端和客户端/etc/passwd中都存在这个uid和gid共享文件权限为:服务权限与文件系统权限的交集
安装、配置nfs服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# mkdir -p /data/{mydata,www} #为web,mysql创建共享目录
# ls /data/
mydatawww
# groupadd -g 110 apache
# useradd -u 110 -M -s /sbin/nologin apache
# chmod 777 /data/www/
# groupadd -g 111 msyql
# useradd -u 111 -M -s /sbin/nologin mysql
# chmod 777 /data/mydata/
# yum groupinstall-y "NFS file server"
# vi /etc/exports
/data/www 192.168.0.11(rw,root_squash)
/data/mydata 192.168.0.20(rw,no_root_squash)
# service rpcbind restart
Stopping rpcbind:                                          
Starting rpcbind:                                          
# service nfs start
Starting NFS quotas:                                       
Starting NFS mountd:                                       
Starting NFS daemon:                                       
Starting RPC idmapd:                                       




安装、配置mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# mkdir -p /data/mydata
# showmount -e 192.168.0.30
Export list for 192.168.0.30:
/data/mydata 192.168.0.20
/data/www    192.168.0.11
# mount -t nfs 192.168.0.30:/data/mydata /data/mydata/
# cd /data/mydata/
# ll
total 0
# touch aa
# ll
total 0
-rw-r--r-- 1 root root 0 Sep 29 22:23 aa
# cd ~
# groupadd -g 111 msyql
# useradd -u 111 -M -s /sbin/nologin mysql #创建mysql账户
# chown -R mysql:mysql /data/mydata
# tar -xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local/
# cd /usr/local
# chown -R mysql:mysql mariadb-5.5.36-linux-x86_64/
# ln -sv mariadb-5.5.36-linux-x86_64/ mysql
# cd mysql
# scripts/mysql_install_db --datadir=/data/mydata/ --user=mysql
# mkdir /etc/mysql
# cp support-files/my-large.cnf /etc/mysql/my.cnf
# vi /etc/mysql/my.cnf #在mysqld下面添加下面几项

...
datadir = /data/mydata
innodb_file_per_table =on
skip_name_resolve = on
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# ln -sv /usr/local/mysql/include /usr/include/mysql
# echo'/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
# ldconfig
# chkconfig --add mysqld
# service mysqld start
# bin/mysql_secure_installation #数据库安全初始化,设置root密码、权限和删除匿名用户
# /usr/local/mysql/bin/mysql -uroot -p #验证能否登录
Enter password:
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.36-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database         |
+--------------------+
| information_schema|
| mysql          |
| performance_schema|
+--------------------+
3 rows in set (0.03 sec)
MariaDB [(none)]> grant all on *.* to test@"192.168.%.%" identified by 'chinoe-147';
##在nfs服务器上可以看到mysql元数据
# cd /data/mydata/
# ls
aria_log.00000001aria_log_controlibdata1ib_logfile0ib_logfile1mysqlmysql-bin.000001mysql-bin.000002mysql-bin.000003mysql-bin.indexmysql.pidperformance_schema




安装、配置apache+php-fpm
apache依赖包 apr-util-1.5.3.tar.bz2 、apr-1.5.0.tar.bz2、pcre-devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# groupadd -g 110 apache
# useradd -u 110 -M -s /sbin/nologin apache
# yum install -y pcre-devel
# tar -xf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr
# make && make install
# cd ..
# tar -xfapr-util-1.5.3.tar.bz2
# cd apr-util-1.5.3
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && makeinstall
# cd ..
# tar -xvf httpd-2.4.10.tar.bz2
# cd httpd-2.4.10
# ./configure
--prefix=/usr/local/apache --sysconfdir=/etc/httpd --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-modules=most
--enable-mpms-shared=all --with-mpm=event
# make && make install
###然后需要自己编写httpd服务启动脚本,我这里是直接修改rpm安装httpd的脚本
#!/bin/bash
#
# httpd      Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.It is used to serve \
#      HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
      . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl #修改为相关路径
httpd=${HTTPD-/usr/local/apache/bin/httpd} #修改为相关路径
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid} #修改为相关路径
lockfile=${LOCKFILE-/var/lock/subsys/httpd} #修改为相关路径
RETVAL=0

start() {
      echo -n $"Starting $prog: "
      LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
      RETVAL=$?
      echo
      [ $RETVAL = 0 ] && touch ${lockfile}
      return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
      RETVAL=$?
      echo $"not reloading due to configuration syntax error"
      failure $"not reloading $httpd due to configuration syntax error"
    else
      killproc -p ${pidfile} $httpd -HUP
      RETVAL=$?
    fi
    echo
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
      status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
    stop
    start
fi
;;
reload)
      reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac

exit $RETVAL
# chmod +x /etc/init.d/httpd
# chkconfig --add httpd
# vi /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
# ./etc/profile.d/httpd.sh #重读一下配置文件
编译安装apache在服务启动之前必须指定pid文件路径
在/etc/httpd/httpd.conf的全局配置中添加pid文件
ServerRoot "/usr/local/apache"
PidFile "/var/run/httpd.pid" 必须与服务启动脚本httpd中的pidfile路径一样
# service httpd start
# curl -I 127.0.0.1
HTTP/1.1 200 OK
Date: Tue, 29 Sep 2015 12:57:18 GMT
Server: Apache/2.4.10 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html
#apache已经安装完毕,测试正常




php-fpm以独立模块与apache结合
依赖包:bzip2-devel libmcrypt-devel libxml2-devel php-mysql
mysql与php不在同一台物理机时,还需要安装mysql-devel包和mysql-libs包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# mkdir /etc/php5{,.d}
# mkdir /usr/local/php5
# yum -y groupinstall "Desktop Platform Development"
# yum -y install bzip2-devel libmcrypt-devel libxml2-devel php-mysql
# yum install -y mysql-devel mysql-libs
# tar -xf php-5.5.29.tar.xz
# cd php-5.5.29
# ./configure --prefix=/usr/local/php5/ --with-mysql --with-openssl --with-mysqli --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml--enable-sockets --enable-fpm --with-mcrypt--with-config-file-path=/etc/php5/ --with-config-file-scan-dir=/etc/php5.d --with-bz2
# make -j 4 && make install
# cp php.ini-production /etc/php5/php.ini
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
###修改php-fpm参数
# vim /usr/local/php5/etc/php-fpm.conf
user = apache
group = apache
listen = 0.0.0.0:9000
在pm = dynamic模块下面修改参数
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php5/var/run/php-fpm.pid
# service php-fpm start
# ss -tanlp |grep 9000
LISTEN   0      128               127.0.0.1:9000                     *:*      users:(("php-fpm",114598,7),("php-fpm",114599,0),("php-fpm",114600,0),("php-fpm",114601,0),("php-fpm",114602,0),("php-fpm",114603,0))
# vi /etc/httpd/httpd.conf
User apache #修改进程用户
Group apache
##注释掉中心主机,使用虚拟主机
#DocumentRoot "/usr/local/apache/htdocs"
#<Directory "/usr/local/apache/htdocs">
#    Options Indexes FollowSymLinks
#    AllowOverride None
#    Require all granted
#</Directory>
在相关的LoadModule下添加
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
在AddType下添加
AddType application/x-httpd-php .php
找到DirectoryIndex index.html 修改为:
DirectoryIndexindex.php index.html
#结尾加上虚拟主机
<virtualhost *:80>
    servername www.test.com
    documentroot /data/www
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/www/$1
    <directory "/data/www">
      options none
      allowoverride none
      <requireall>
            require all granted
      </requireall>
    </directory>
</virtualhost>
# mkdir -p /data/www
# mount -t nfs 192.168.0.30:/data/www /data/www/
# vi index.php
<?php
    phpinfo()
?>






1
2
3
4
5
6
7
8
9
# vi index.php #测试数据库
<?php
$link = mysql_connect('192.l68.0.20','test','chinoe-147');
    if ($link)
echo "Success...";
else
    echo "Failure...";
mysql_close();
?>




再测试数据库是否能链接上

已经success

页: [1]
查看完整版本: LAMP+NFS(编译php-fpm模块与apache2.4结合)