写在前面:如果此文有幸被某位朋友看见并发现有错的地方,希望批评指正。如有不明白的地方,愿可一起探讨。



案例拓扑图


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

安装配置nginx服务器


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

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

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

# groupadd -r nginx
# useradd -g nginx -r nginx

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

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

编译安装nginx

# 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脚本

# vim /etc/rc.d/init.d/nginx
#!/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=\([^ ]*\).*/\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

为此脚本赋予执行权限

# chmod +x /etc/rc.d/init.d/nginx

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

# chkconfig --add nginx
# chkconfig nginx on

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

location ~ \.php$ {
    fastcgi_pass   10.170.2.90:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
    include        fastcgi_params;
}

启动nginx服务

# vim /etc/init.d/nginx start

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

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


安装PHP服务器


编译安装php

# 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提供配置文件

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

配置php-fpm

# 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,将以下选项修改为相对应的值

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,其内容为

<h1>hello,nginx</h1>
<?php
        $link = mysql_connect('10.170.2.36','testuser','******');
        if ($link)
                echo "Success...";
        else
                echo "Failure...";

        mysql_close();
        phpinfo();
?>

启动php-fpm服务

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

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

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

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


安装MariaDB服务器


编译安装mariadb-5.5.36

# 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

为数据库提供配置文件:

# 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启动脚本,并设置为开机启动:

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

初始化数据库并启动数据库:

# 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

创建数据库并授权:

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,可以得到如下页面

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

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