LNMP环境构建
LNMP(Linux+Nginx+Mysql+Php)就是在Linux系统中,通过编译安装Nginx,Mysql,Php来提供一个支持Php动态网页的Web服务器,构建过程如下:
libevent-2.0.16-stable.tar.gz
nginx-1.0.11(下载地址为:http://mirrors.sohu.com
 php-5.3.7 (http://www.php.net/downloads.php
mysql-5.0.81.tar.gz编译安装过程
安装nginx包
1、要解决依赖关系
编译安装nginx事先需要安装开发包组"Development Tools"和 "Development Libraries"。
默认libevent包已经安装。
[root@server ~]# yum list all | grep libevent
This system is not registered with RHN.
RHN support will be disabled.
libevent.i386                            1.1a-3.2.1                  installed 
libevent-devel.i386                      1.1a-3.2.1                  rhel-server
安装libevent源码包:
[root@server ~]# tar -zxvf libevent-2.0.16-stable.tar.gz  -C /usr/src/
[root@server ~]# cd /usr/src/
[root@server src]# cd libevent-2.0.16-stable/
[root@server src]# ./configure
[root@server libevent-2.0.16-stable]# make && make install
[root@server libevent-2.0.16-stable]# cd /usr/local/
[root@server local]# vim /etc/ld.so.conf.d/libevent.conf
/usr/local/lib

同时,还需要专门安装pcre-devel包:
# yum -y install pcre-devel

安装nginx源码包:
首先添加用户nginx,实现以之运行nginx服务进程:
解压nginx包
[root@server ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/src/
[root@server ~]# cd nginx-0.1.11
[root@server nginx-1.0.11]#  groupadd  -r  nginx
[root@server nginx-1.0.11]# useradd -r -g nginx -s /bin/false  -M nginx
接着开始编译和安装:
[root@server nginx-0.1.11]# ./configure \
--prefix=/usr \                              --指定安装路径
  --sbin-path=/usr/sbin/nginx \                --指定sbin安装路径
  --conf-path=/etc/nginx/nginx.conf \          --指定nginx的配置文件路径
  --error-log-path=/var/log/nginx/error.log \  --指定错误日志路径
  --http-log-path=/var/log/nginx/access.log \  --指定访问日志路径
  --pid-path=/var/run/nginx/nginx.pid  \       --指定pid路径
  --lock-path=/var/lock/nginx.lock \           --指定锁文件路径
  --user=nginx \                               --指定运行此服务的用户
  --group=nginx \                              --指定运行此服务的组
  --with-http_ssl_module \                     --编译安装上ssl模块
  --with-http_flv_module \                     --编译安装上flv模块
  --with-http_stub_status_module \             --编译安装上status模块
  --with-http_gzip_static_module \             --编译安装上static模块
  --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/ \     --启动fastcgi功能
  --with-pcre
然后编译安装源码:
[root@server nginx-1.0.11]# make & make install

为nginx提供SysV init脚本
[root@server html]# 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/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
而后为此脚本赋予执行权限:
[root@server html]# chmod +x /etc/rc.d/init.d/nginx
添加到服务启动列表,并设定开机启动,同时启动该服务
[root@server html]# chkconfig --add nginx
[root@server html]# chkconfig nginx on
[root@server html]# service nginx start
查看监听端口:
[root@server html]# netstat -tupln |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      11849/nginx.conf  
测试:

安装mysql源码包:
新建用户以安全方式运行mysql进程
[root@server html]# groupadd -r mysql
[root@server html]# useradd -g mysql -s /sbin/nologin -M mysql
 解压mysql包
[root@server ~]# tar -zxvf mysql-5.0.81.tar.gz -C /usr/local/
编译安装:
[root@server ~]# cd /usr/local/ mysql-5.0.81
[root@server mysql-5.0.81]# ./configure --prefix=/usr/local/mysql
[root@server mysql-5.0.81]# make && make install
改变文件所有者:
[root@server mysql]# chown -R mysql:mysql .
[root@server mysql-5.0.81]# chown -R mysql /usr/local/mysql/var/
为mysql提供主配置文件:
[root@server mysql]# cp support-files/my-large.cnf  /etc/my.cnf
初始化:
[root@server mysql-5.0.81]# /usr/local/mysql/bin/mysql_install_db --user=mysql
后台安全运行:
[root@server mysql-5.0.81]# /usr/local/mysql/bin/mysqld_safe  --user=mysql
[root@server mysql-5.0.81]# vim /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib/mysql
为mysql提供mysql的服务脚本文件
[root@server mysql]# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
赋予脚本执行权限
[root@server mysql-5.0.81]# chmod +x /etc/init.d/mysqld
设置系统全局的环境变量PATH值,方便使用mysql数据库命令
 [root@server mysql-5.0.81]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
将mysql添加至服务列表:
[root@server mysql]# chkconfig --add mysqld
[root@server mysql]# chkconfig mysqld on
[root@server mysql-5.0.81]# service mysqld start启动mysqld服务
安装php源码包:
解压并编译安装php包:
[root@server ~]# tar -jxvf php-5.3.7.tar.bz2 -C /usr/local/src/
[root@server ~]# cd /usr/local/src/php-5.3.7/
[root@server php-5.3.7]# ./configure \
--prefix=/usr/local/phpnginx \
 --with-mysql=/usr/local/mysql \
--enable-fpm\
 --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-config-file-path=/etc/php \
--with-config-file-scan-dir=/etc/php \
--with-bz2 --with-curl
[root@server php-5.3.7]#make
[root@server php-5.3.7]#make test
[root@server php-5.3.7]#make install
为php提供配置文件:
[root@server php-5.3.7]# cp php.ini-production /etc/php/php.ini
为php-fpm提供Sysv init脚本,并将其添加至服务列表
[root@server php-5.3.7]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
给它执行权限:
[root@server php-5.3.7]# chmod +x /etc/rc.d/init.d/php-fpm
[root@server php-5.3.7]# chkconfig --add php-fpm
[root@server php-5.3.7]# chkconfig php-fpm on
启动fastcgi:
为php-fpm提供配置文件:
[root@server php-5.3.7]# cp /usr/local/phpnginx/etc/php-fpm.conf.default /usr/local/phpnginx/etc/php-fpm.conf
编辑php-fpm的配置文件:
[root@server php-5.3.7]# vim /usr/local/phpnginx/etc/php-fpm.conf
166   pm.start_servers = 5
171    pm.min_spare_servers = 2
176    pm.max_spare_servers = 8
25    pid = run/php-fpm.pid
配置pm.的相关选项为自己所需要的值,并启用pid文件(如下最后一行):
pm.max_children = 50                    此项pm的值和虚拟机或者物理机的内存有关,根据自己的内存空间进行适当修改。
pm.start_servers = 5                    此项是初启动程序时开启的进程数   pm.min_spare_servers = 2                此项是最小空闲进程数
pm.max_spare_servers = 8                此项是最大空闲进程数
pid = run/php-fpm.pid           
接下来就可以启动php-fpm了:
[root@server php-5.3.7]# pkill -1 php-fpm 此步是由于提示地址已经使用时候使用其中-1是参数一。
[root@server php-5.3.7]# service php-fpm start

整合nginx和php-5.3.7
编辑如下文件:
[root@server php-5.3.7]# vim /etc/nginx/nginx.conf
启用如下选项
65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 70
 71
 72     include        fastcgi_params;
 73        }
编辑/etc/nginx/fastcgi_params,删除原有内容将其内容更改为如下内容:
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
并在所支持的主页面格式中添加php格式的主页,类似如下:

43         location / {
 44             root   html;
 45             index index.php  index.html index.htm;
 46         }
而后重新载入nginx的配置文件:
[root@server php-5.3.7]# service nginx reload

在/usr/html目录下新建index.php的测试页面,测试php是否能正常工作:
[root@server php-5.3.7]# cd /usr/html/
[root@server html]# vim index.php  

<?php
phpinfo();
?>
 
访问该页面:
 


成功访问,说明nginx已经能提供php服务了。