作为一个轻量级的HTTP服务器,Nginx与Apache相比,小巧而精致:在性能上,它占用很少的系统资源,能支持更多的并发连接,达到更高的访问效率;在功能上,Nginx是优秀的代理服务器和负载均衡服务器;在安装配置上,Nginx安装简单、配置灵活。LNMP环境是指Linux下搭建Nginx+MySQL+PHP.

Linux下Web服务器架构之

源码构建LNMP环境

1.构建的linux环境:

[root@junjie ~]# uname –a   #查看Linux的环境

Linux junjie 2.6.25.19 #1 SMP Mon Feb 20 17:25:04 CST 2012 i686 i686 i386 GNU/Linux

2.准备工作:

准备一:关闭firewall和SELinux(略)

准备二:构建本地yum服务器(略)

准备三:下载以下软件(我存放在linux的/root/lamp下)

libevent-2.0.18-stable.tar.gz
mysql-5.0.95.tar.gz
nginx-1.1.18.tar.gz
php-5.4.0.tar.bz2
libmcrypt-2.5.8.tar.bz2
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.bz2

下载地址:

nginx http://nginx.org/

准备四:构建编译组环境

使用# yum grouplist all |less查看已安装的组环境(红色必选

[root@junjie ~]# yum grouplist all |less

Legacy Software Development

X Software Development

Development Libraries

Development Tools

发现这里没有安装Development Tools,使用下面命令安装

[root@www ~]# yum groupinstall "Development Tools"

#安装依赖包:

#nginx会有几个依赖包,我们首先安装依赖包,若安装过程中报错,可以跳过,不会影#响nginx的正常运行:

[root@junjie ~]# yum -y install zlib-devel pcre-devel openssl-devel

源码构建LNMP环境

1.源码安装nginx:

#这里我将下载的软件存放到/root/lnmp目录下
[root@junjie ~]# cd /root/lnmp/
[root@junjie lnmp]# ls
libevent-2.0.18-stable.tar.gz
mysql-5.0.95.tar.gz
nginx-1.1.18.tar.gz
php-5.4.0.tar.bz2
#查看libevent的版本,发现版本过低,现在安装新的libevent

[root@junjie lnmp]# ldconfig -v |grep libevent                     

      libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2

#解压libevent的程序文件,使用tar –zxvf,并使用-C指定解压目录,参数,z代表
#gzip(也就是后面的.gz文件)x代表解压,v表示显示详细信息,-f使用档案文件或设备
#(必选参数)(说明:一般一些额外的软件安装时经常将其放到/usr/src/目录下)

[root@junjie lnmp]# tar -zxvf libevent-2.0.18-stable.tar.gz -C /usr/src/

[root@junjie lnmp]# cd /usr/src/
[root@junjie src]# ll
#预编译(执行源码包下的configure),编译(make),编译安装(make install)

[root@junjie src]# cd libevent-2.0.18-stable/

[root@junjie libevent-2.0.18-stable]# ls

[root@junjie libevent-2.0.18-stable]# ./configure
[root@junjie libevent-2.0.18-stable]# make
[root@junjie libevent-2.0.18-stable]# make install

[root@junjie libevent-2.0.18-stable]# vim /etc/ld.so.conf.d/libevent.conf

/usr/local/lib

[root@junjie libevent-2.0.18-stable]# ldconfig -v |grep libevent

       libevent_pthreads-2.0.so.5 -> libevent_pthreads.so

       libevent-2.0.so.5 -> libevent.so

       libevent_extra-2.0.so.5 -> libevent_extra.so

       libevent_openssl-2.0.so.5 -> libevent_openssl.so

       libevent_core-2.0.so.5 -> libevent_core.so

       libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2

安装nginx:

#首先解压源码包:

[root@junjie libevent-2.0.18-stable]# cd /root/lnmp

[root@junjie lnmp]# tar -zxvf nginx-1.1.18.tar.gz -C /usr/src/

#进入源码包目录:

[root@junjie lnmp]# cd /usr/src/nginx-1.1.18/

[root@junjie nginx-1.1.18]# ll
#添加nginx系统组和用户

[root@junjie nginx-1.1.18]# groupadd -r nginx

[root@junjie nginx-1.1.18]# useradd -r -g nginx -s /bin/false -M nginx

#进行预编译(配置):(各项配置说明略)
[root@junjie nginx-1.1.18]# ./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
#预编译完成后就可以进行编译和安装:
[root@junjie nginx-1.1.18]# make
[root@junjie nginx-1.1.18]# make install
#启动测试,发现失败,按下面方法创建目录,再次启动,查看运行端口信息,#nginx占用TCP的80端口

##pkill -9 nignx      #关闭nginx

##/usr/sbin/nginx  #启动nginx

[root@junjie nginx-1.1.18]# nginx

nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)

[root@junjie nginx-1.1.18]# mkdir -pv /var/tmp/nginx/client

mkdir: created directory `/var/tmp/nginx'

mkdir: created directory `/var/tmp/nginx/client'

[root@junjie nginx-1.1.18]# nginx

[root@junjie nginx-1.1.18]# netstat -tupln |grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      16561/nginx: master

#为nginx提供一个启动服务的脚本(请下载附件

[root@junjie nginx-1.1.18]# vim /etc/rc.d/init.d/nginx   (请下载附件)

[root@junjie nginx-1.1.18]# chmod +x /etc/rc.d/init.d/nginx

#设置nginx开机启动

[root@junjie nginx-1.1.18]# chkconfig --add nginx

[root@junjie nginx-1.1.18]# chkconfig nginx on

[root@junjie nginx-1.1.18]# chkconfig --list nginx

nginx                0:off      1:off      2:on       3:on       4:on       5:on       6:off
#启动nginx服务,查看nginx端口

[root@junjie nginx-1.1.18]# service nginx stop

Stopping nginx:                                            [ OK ]

[root@junjie nginx-1.1.18]# service nginx start

Starting nginx:                                            [ OK ]

[root@junjie nginx-1.1.18]# service nginx restart

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

Stopping nginx:                                            [ OK ]

Starting nginx:                                            [ OK ]

#配置nginx网页文件,编辑网页

[root@junjie nginx-1.1.18]# ls /usr/html/

50x.html index.html

[root@junjie nginx-1.1.18]# vim /usr/html/index.html

5 <body bgcolor="white" text="black">

 6 <center><h1>Welcome to my nginx!</h1></center>

 7 --xjzhujunjie
 8 --2012/04/08
 9 </body>
#用浏览器可以访问--客户端测试显示如下信息--正常

2.源码安装mysql:

[root@junjie ~]#cd /root/lnmp/
[root@junjie lnmp]# ls
libevent-2.0.18-stable.tar.gz
mysql-5.0.95.tar.gz
nginx-1.1.18.tar.gz
php-5.4.0.tar.bz2

#解压mysql的主程序文件,使用tar –zxvf,并使用-C指定解压目录 

[root@junjie lnmp]# tar -zxvf mysql-5.0.95.tar.gz -C /usr/src/

#切换至/usr/src/的目录下,并创建软连接,便于访问mysql文件
[root@junjie lnmp]# cd /usr/src/                             
[root@junjie src]# ll

[root@junjie src]# ln -s mysql-5.0.95/ mysql

[root@junjie src]# ll
[root@junjie src]# cd mysql
#安装mysql之前先做一些准备工作,安装依赖包:显示已经安装了

[root@junjie mysql]# yum -y install ncurses-devel

#创建MySQL用户,-M不创建home目录,-s指定shell为不登录

[root@junjie mysql]# useradd -M -s /sbin/nologin mysql

#进行预编译(配置):(各项配置说明略)
[root@junjie mysql]# ./configure \
> --prefix=/usr/local/mysql \
> --without-debug \
> --with-extra-charsets=utf8,gbk \
> --enable-assembler \
> --with-mysqld-ldflags=-all-static \
> --with-client-ldflags=-all-static \
> --with-unix-socket-path=/tmp/mysql.sock \
> --with-ssl
#预编译完成后就可以进行编译和安装:
[root@junjie mysql]#make
[root@junjie mysql]#make install
#安装完成后复制配置文件和启动脚本,再给启动脚本执行权限:

[root@junjie mysql]# cp support-files/my-medium.cnf /etc/my.cnf

[root@junjie mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[root@junjie mysql]# chmod +x /etc/init.d/mysqld

#为所有的二进制可执行文件和动态链接库文件做一个软连接

[root@junjie mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/

[root@junjie mysql]# ln -s /usr/local/mysql/lib/mysql/lib* /usr/lib/

#初始化数据库,更改MySQL安装目录和MySQL的数据库目录的属主和属组

[root@junjie mysql]# mysql_install_db --user=mysql

[root@junjie mysql]# chown -R root.mysql /usr/local/mysql/

[root@junjie mysql]# chown -R mysql.mysql /usr/local/mysql/var/

#启动mysql,查看MySQL是否启动成功,MySQL占用TCP的3306端口

[root@junjie mysql]# service mysqld start

Starting MySQL..                                           [ OK ]

[root@junjie mysql]# netstat -tupln |grep 3306

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      14200/mysqld       

#通过mysql命令来连接mysql,测试mysql是否正常安装,可以看出已安装:
[root@junjie mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.95-log Source distribution
 

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 

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

 

mysql> show databases;

+--------------------+
| Database           |
+--------------------+

| information_schema |

| mysql              |
| test               |
+--------------------+

3 rows in set (0.00 sec)

 
mysql> quit
Bye
[root@junjie mysql]#

3.源码安装PHP依赖程序包:

#安装PHP前首先要安装几个源码包依赖:libmcrypt mhash mcrypt

#这里我将下载的包放到/root/php/目录下,安装过程中若出现错误,可以直接#跳过,这里不会影响后续过程
[root@junjie ~]# cd /root/php/
[root@junjie php]# ls
libmcrypt-2.5.8.tar.bz2
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.bz2
#安装libmcrypt依赖包:

[root@junjie php]# tar -jxvf libmcrypt-2.5.8.tar.bz2 -C /usr/src/

[root@junjie php]# cd /usr/src/libmcrypt-2.5.8/

[root@junjie libmcrypt-2.5.8]# ./configure
[root@junjie libmcrypt-2.5.8]# make

[root@junjie libmcrypt-2.5.8]# make install

[root@junjie ~]# cd /root/php/
[root@junjie php]# ls
libmcrypt-2.5.8.tar.bz2
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.bz2
#安装mhash依赖包:

[root@junjie php]# tar -jxvf mhash-0.9.9.9.tar.bz2 -C /usr/src/

[root@junjie php]# cd /usr/src/mhash-0.9.9.9/

[root@junjie mhash-0.9.9.9]# ./configure

[root@junjie mhash-0.9.9.9]# make

[root@junjie mhash-0.9.9.9]# make install

[root@junjie mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.* /usr/lib/

[root@junjie mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt* /usr/lib

[root@junjie ~]# cd /root/php/
[root@junjie php]# ls
libmcrypt-2.5.8.tar.bz2
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.bz2
#这两个包安装完成后要把动态链接库做一个软连接到/usr/lib,以为接下来的#mcrypt依赖于这两个包,安装mcrypt包:

[root@junjie php]# tar -zxvf mcrypt-2.6.8.tar.gz -C /usr/src/

[root@junjie php]# cd /usr/src/mcrypt-2.6.8/

[root@junjie mcrypt-2.6.8]# ./configure
[root@junjie mcrypt-2.6.8]# make

[root@junjie mcrypt-2.6.8]# make install

#安装其它依赖包,发现很多已经安装,不影响后续过程:

[root@junjie mcrypt-2.6.8]#yum –y install libxml2-devel curl-devel libpng-devel openldap-devel

4.源码安装PHP:

#使用nginx调用php的时候使用fpm的方式,在php 5.4中加入了对php-fpm
#的支持,所以就不需要打补丁了.以下显示安装PHP过程:

[root@junjie mcrypt-2.6.8]# cd /root/lnmp/

[root@junjie lnmp]# ls
libevent-2.0.18-stable.tar.gz
mysql-5.0.95.tar.gz
nginx-1.1.18.tar.gz
php-5.4.0.tar.bz2

#解压mysql的主程序文件,使用tar –jxvf,并使用-C指定解压目录:

[root@junjie lnmp]# tar -jxvf php-5.4.0.tar.bz2 -C /usr/src/

[root@junjie lnmp]# cd /usr/src/php-5.4.0/

[root@junjie php-5.4.0]# ls
#进行预编译(配置):(各项配置说明略)

[root@junjie php-5.4.0]#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql/ --with-zlib --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --with-curlwrappers --enable-fpm --enable-fastcgi --with-mcrypt --with-gd --with-openssl --with-mhash --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc -enable-zip --enable-soap

#预编译完成后就可以进行编译和安装:
[root@junjie php-5.4.0]# make && make install

5.使能nginx调度phphemysql:

#置php和nginx能运行php网站, 首先为php创建配置文件:

[root@junjie php-5.4.0]# cp php.ini-production /usr/local/php/php.ini

[root@junjie php-5.4.0]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@junjie php-5.4.0]# ln -s /usr/local/php/bin/php /usr/bin/

#配置php-fpm,编辑php-fpm.conf,找到listen那一行,修改成如下内容:

[root@junjie php-5.4.0]# vim /usr/local/php/etc/php-fpm.conf

143 listen = /var/run/php-fpm/php-fpm.sock

#创建php-fpm目录,启动php-fpm

[root@junjie php-5.4.0]# mkdir /var/run/php-fpm

[root@junjie html]# cd /usr/local/php/sbin/       
[root@junjie sbin]# ls
php-fpm
[root@junjie sbin]# ./php-fpm
#然后配置nginx,编辑nginx配置文件

[root@junjie sbin]# cd /usr/src/php-5.4.0/

[root@junjie php-5.4.0]# vim /etc/nginx/nginx.conf

#修改44行内容,增加index.php,添加index.php的首页文件选项

44             index index.php index.html index.htm;

#添加46—52

46         location ~ \.php$ {
 47                 fastcgi_pass        unix:/var/run/php-fpm/php-fpm.sock;
 48                 fastcgi_index       index.php;

 49                 fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

 50                 include fastcgi_params;
 51                 include fastcgi.conf;
 52         }

#修改完毕后保存退出重启nginx:

[root@junjie php-5.4.0]# service nginx restart

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

Stopping nginx:                                            [ OK ]

Starting nginx:                                            [ OK ]

6.测试LNMP环境:

#增加网页测试文件:编辑nginx的配置文件使识别index.php文件
[root@junjie php-5.4.0]# cd /usr/html/
[root@junjie html]# ll

#测试nginx与PHP的连接:#测试成功

[root@junjie html]# vim index.php
 --xjzhujunjie
 --2012/04/08
 phpinfo();
 ?>

#测试nginx与mysql的连接:#测试成功

[root@junjie html]# vim /usr/html/index.php

 --xjzhujunjie
 --2012/04/08
<?php
$link=mysql_connect('127.0.0.1','root','');
if($link)

echo "scuess";

else

echo "fail";

?>

#停止mysql服务,nginx调用mysql失败
[root@junjie html]# service mysqld stop

Shutting down MySQL..                                      [ OK ]

#测试成功

《完》

至此源码构建LNMP环境成功!!!

 

附件:nginx启动脚本: 

 

#nginx启动脚本

#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