oiuy 发表于 2014-5-28 08:38:57

LNMP基于fastcgi实现nginx,php,mysql的分离安装部署

安装LNMP是把它们安装到同一台机器上,相对来说丝毫没有挑战,下面把他们剥离到不同的机器上,让各个服务器直接分担原来的压力,也可以增加节点实现负载均衡,如:多增加一台php,让两台机器轮询的编译php,也可以在增加一台nginx,实现dns的轮询负载均衡。
实验环境:

centos6.4 32位,yum可以正常使用,开发包组"Development Tools" "Development Libraries" "X Software Development"已经安装好,如果没有请先安装。SElinux确保已经关闭,iptables先关闭之。

nginx:         172.16.1.1
php(FASTCGI):172.16.1.2
mysql:         172.16.1.3


一.在172.16.1.1编译安装nginx

[*]先安装pcre-devel,nginx的rewrite功能依赖pcre提供的库。


1
# yum -y install pcre-devel




2.为nginx建立用户,实现安全运行,指定uid的原因是为了与php通过nfs共享时权限方便

1
2
# groupadd -r -g 5000 nginx
# useradd -r -g nginx -u 5000 nginx




3.下载并编译安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# wget http://www.nginx.org/download/nginx-1.2.4.tar.gz
# tar xvf nginx-1.2.4.tar.gz
# cd nginx-1.2.4
#./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid\
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre





1
# make && make install





4.为nginx提供SysV init脚本
新建文件/etc/rc.d/init.d/nginx,内容如下:

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
#!/bin/sh
# nginx - this script starts and stops the nginx daemon
# chkconfig:   - 85 15
# description:Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:   /var/run/nginx.pid

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
         value=`echo $opt | cut -d "=" -f 2`
         if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
         fi
       fi
   done
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
      rh_status_q && exit 0
      $1
      ;;
    stop)
      rh_status_q || exit 0
      $1
      ;;
    restart|configtest)
      $1
      ;;
    reload)
      rh_status_q || exit 7
      $1
      ;;
    force-reload)
      force_reload
      ;;
    status)
      rh_status
      ;;
    condrestart|try-restart)
      rh_status_q || exit 0
            ;;
    *)
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
      exit 2
esac




为脚本添加权限,添加服务为自动


1
2
3
4
# chmod +x /etc/rc.d/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
# service nginx start




直接访问172.16.1.1查看是否有nginx的欢迎信息,如果有代表nginx安装一切正常。



二.在172.16.1.3上部署mysql-5.5.28

1.为mysql准备数据目录与用户

1
# mkdir -pv /data/mysql




##其实这里最好为mysql准备一个逻辑卷,为了方便书写我省略了

1
#useradd -r mysql




2.下载安装mysql,这里采用已经编译好的

1
2
3
4
5
6
7
8
9
# wget http://cdn.mysql.com/Downloads/MySQL-5.5/mysql-5.5.28-linux2.6-i686.tar.gz
# tar xvf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local/
# cd /usr/local/
# ln -sv mysql-5.5.24-linux2.6-i686mysql
# cd mysql
# chown -R root:mysql.
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# cd /usr/local/mysql
# cp support-files/my-huge.cnf/etc/my.cnf





修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:

1
thread_concurrency = 2




适当位置添加一行,指定mysql数据文件的存放位置:

1
datadir = /mydata/data




3.为Mysql提供脚本,并添加为系统服务


1
2
3
4
5
# cd /usr/local/mysql
# cp support-files/mysql.server/etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
# service mysqld start   测试启动一切正常mysql安装正常




4.输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:

1
MANPATH/usr/local/mysql/man




5.输出mysql的头文件至系统头文件路径/usr/include:
这可以通过简单的创建链接实现:

1
# ln -sv /usr/local/mysql/include/usr/include/mysql




6.输出mysql的库文件给系统库查找路径:

1
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf




而后让系统重新载入系统库:

1
# ldconfig -v




7.修改PATH环境变量,让系统可以直接使用mysql的相关命令。

1
2
# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
# source /etc/profile.d/mysql.sh





三.172.16.1.2上编译安装php-5.4.8
1.把mysql在这台机器上安装一遍,php要利用一下,否则连接不上mysql,在安装discuz时提示mysql_connect()不支持,如果你有更好办法请给我留言
2.安装扩展包
如果想让编译的php支持mcrypt、mhash扩展,地址:http://www.kuaipan.cn/file/id_33139203151757930.html
libmcrypt-2.5.8-4.el5.centos.i386.rpm
libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm
mhash-0.9.9-1.el5.centos.i386.rpm
mhash-devel-0.9.9-1.el5.centos.i386.rpm
mcrypt-2.6.8-1.el5.i386.rpm
3.下载编译安装php-5.4.8

1
2
3
4
5
# tar xf php-5.4.8.tar.bz2
# cd php-5.4.8
#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm--with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml--with-mhash --with-mcrypt--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
# make
# make intall




4.为php-fpm提供Sysv init脚本,并提供配置文件,并将其添加至服务列表:

1
2
3
4
# 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
# chkconfig php-fpm on




为php提供配置文件:

1
# cp php.ini-production /etc/php.ini




为php-fpm提供配置文件:

1
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf




编辑php-fpm的配置文件:

1
# vim /usr/local/php/etc/php-fpm.conf




配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):

1
2
3
4
5
pid = /usr/local/php/var/run/php-fpm.pid ##注释此项并修改
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8




接下来就可以启动php-fpm了:

1
# service php-fpm start




使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):

1
# ps aux | grep php-fpm





四.整合nginx与php,通过php调用mysql
对172.16.1.1的nginx修改
1.计划把网页放在/www下,建立该目录,并修改权限

1
2
# mkdir /www
# chown nginx:nginx /www




2.编译 /etc/nginx/nginx.conf,注释一些选项 ##编辑前请备份原文件

1
2
3
4
5
6
7
8
9
10
11
location / {
            root   /www;
            indexindex.php index.html index.htm;
      }
location ~ \.php$ {
            root         /www;
            fastcgi_pass   172.16.1.2:9000;
            fastcgi_indexindex.php;
            fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
            include      fastcgi_params;
      }   




3.编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fastcgi_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWARE    nginx;
fastcgi_paramQUERY_STRING       $query_string;
fastcgi_paramREQUEST_METHOD   $request_method;
fastcgi_paramCONTENT_TYPE       $content_type;
fastcgi_paramCONTENT_LENGTH   $content_length;
fastcgi_paramSCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_paramSCRIPT_NAME      $fastcgi_script_name;
fastcgi_paramREQUEST_URI      $request_uri;
fastcgi_paramDOCUMENT_URI       $document_uri;
fastcgi_paramDOCUMENT_ROOT      $document_root;
fastcgi_paramSERVER_PROTOCOL    $server_protocol;
fastcgi_paramREMOTE_ADDR      $remote_addr;
fastcgi_paramREMOTE_PORT      $remote_port;
fastcgi_paramSERVER_ADDR      $server_addr;
fastcgi_paramSERVER_PORT      $server_port;
fastcgi_paramSERVER_NAME      $server_name;




##先不要重启nginx。
在172.16.1.2上修改Php

4.为了以后与nginx共享文件方便建立nginx用户

1
2
# groupadd -r -g 5000 nginx
# useradd -r -g nginx -u 5000 nginx




5.修改php-fpm配置文件 /usr/local/php/etc/php-fpm.conf

1
2
3
4
# vim /usr/local/php/etc/php-fpm.conf
listen = 172.16.1.2:9000#监听物理网卡地址,供其它机器调用
user = nginx      ##php-fpm以nginx用户运行
group = nginx




6.重启php-fpm

1
#service php-fpm restart





五:安装discuz论坛
1.下载解压discuz,更改权限


1
2
3
4
5
6
7
8
9
#wgethttp://cn.wordpress.org/wordpress-3.4.2-zh_CN.tar.gz
#tar xvf wordpress-3.4.2-zh_CN.tar.gz
#mv wordpress-3.4.2-zh_CN /www
#chown -R nginx:nginx /www
在/www建立test.php 作为测试文件
#vim /www/test.php
<?php
phpinfo();
?>





2.启动nfs,编辑配置文件以供nginx挂载

1
2
3
#vi /etc/exports
/www   172.16.1.1(rw)
#service nfs start




3.在172.16.1.1上挂载172.16.1.2的/www

1
#mount -t nfs 172.16.1.2:/www /www




浏览器打开172.16.1.1/test.php看是否能正常调用Phpinfo()函数如果能则继续,如果不能请查找原因
4.安装discuz 浏览器运行172.16.1.1/install,根据提示修改Php.ini的一个选项

1
2
#vi /etc/php.ini
short_open_tag = On




5.在172.16.1.3上为discuz建立数据

1
2
3
4
5
#mysql
>create database discuz;
>grant all on discuz.* to 'discuz'@'172.16.1.2' identified by 'centos';
>flush privileges;
>quit




6.继续安装discuz访问172.16.1.1/install,根据提示输入刚才建立的账号密码mysql的地址,到此discuz应该可以安装成功。

页: [1]
查看完整版本: LNMP基于fastcgi实现nginx,php,mysql的分离安装部署