kjut 发表于 2014-10-15 08:42:06

搭建完全分离式LNMP平台的简单案例

案例拓扑图

安装配置nginx服务器
编译安装nginx时,需要事先安装 开发包组"Development Tools"和"Server Platform Development",同时还需专门安装pcre-devel包。

1
2
3
# yum -y groupinstall "Development Tools"
# yum -y groupinstall "Server Platform Development"
# yum -y install pcre-devel




首先添加nginx用户组和nginx用户


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




创建编译安装是所需要的目录

1
# mkdir -pv /var/tmp/nginx/client




编译安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# tar xf nginx-1.4.7.tar.gz
# cd nginx-1.4.7
# ./configure
--prefix=/usr/local/nginx
--sbin-path=/usr/local/nginx/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
# make && make install




为nginx提供SysV init脚本

1
# vim /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
125
126
#!/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/local/nginx/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=([^ ]*).*//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
# chmod +x /etc/rc.d/init.d/nginx




将nginx服务添加至服务管理列表,并让其开机自动启动

1
2
# chkconfig --add nginx
# chkconfig nginx on




编辑配置文件/etc/nginx/nginx.conf,在server段内添加如下内容

1
2
3
4
5
6
location ~ .php$ {
    fastcgi_pass   10.170.2.90:9000;
    fastcgi_indexindex.php;
    fastcgi_paramSCRIPT_FILENAME/var/www/html$fastcgi_script_name;
    include      fastcgi_params;
}




启动nginx服务

1
# vim /etc/init.d/nginx start




测试nginx是否工作起来,在浏览器中键入10.170.2.80,可以得到如下页面


安装PHP服务器
编译安装php

1
2
3
4
# tar xf php-5.4.26.tar.bz2
# cd php-5.4.26
# ./configure --prefix=/usr/local/php5 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --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 --with-config-file-scan-dir=/etc/php.d --with-bz2
# make && make install




为php提供配置文件

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




配置php-fpm

1
2
3
4
5
# 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
# cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf




编辑配置文件/usr/local/php5/etc/php-fpm.conf,将以下选项修改为相对应的值

1
2
3
4
5
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




在/var/www/html目录下提供测试页面index.php,其内容为

1
2
3
4
5
6
7
8
9
10
11
hello,nginx
      $link = mysql_connect('10.170.2.36','testuser','******');
      if ($link)
                echo "Success...";
      else
                echo "Failure...";

      mysql_close();
      phpinfo();
?>




启动php-fpm服务

1
# /etc/init.d/php-fpm start




测试nginx服务器与php服务器是否能够建立通信,在浏览器中键入10.170.2.80/index.php,可以得到如下页面

页面中显示Failure...,是因为后端的数据库还没有进行相应的配置

安装MariaDB服务器
编译安装mariadb-5.5.36
1
2
3
4
5
6
7
8
# tar xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mariadb-5.5.36-linux-x86_64/ mysql
# mkdir -pv /mysql/data
# groupadd -r mysql
# useradd -g mysql -s /sbin/nologin -M -d /mysql/data -r mysql
# chown -R mysql:mysql /mysql
# chown -R mysql:mysql /mysql/data




为数据库提供配置文件:

1
2
3
4
5
6
# cd mysql
# mkdir /etc/mysql
# chown -R root.mysql ./*
# cp support-files/my-large.cnf /etc/mysql/my.cnf
修改文件/etc/mysql/my.cnf文件内容,在thread_concurrency = 8行下添加一行:
datadir = /mysql/data




为数据库提供SysV启动脚本,并设置为开机启动:

1
2
3
# cp support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on




初始化数据库并启动数据库:
1
2
3
4
5
6
7
# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
# source /etc/profile.d/mysql.sh
# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
# ldconfig
# ln -sv /usr/local/mysql/include/ /usr/include/mysql
# scripts/mysql_install_db --user=mysql --datadir=/mysql/data/
# /etc/init.d/mysqld start




创建数据库并授权:
1
2
3
MariaDB [(none)]> CREATE DATABASE testdb;
MariaDB [(none)]> GRANT ALL ON testdb.* TO dscuser@'10.170.2.%' IDENTIFIED BY '******';
MariaDB [(none)]> FLUSH PRIVILEGES;




整体测试LNMP平台
在浏览器中键入10.170.2.80/index.php,可以得到如下页面

这次可以看到页面中显示Success...信息

页: [1]
查看完整版本: 搭建完全分离式LNMP平台的简单案例