LNMP是在Linux操作系统上面,搭配开源软件Nginx+Mysql+PHP的网站服务器架构。Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。那么如何架构一个LNMP呢?

下面先说下我所使用的环境和软件:

  1. System:Red Hat Enterprise Linux Server 5.4


  2. Nginx:nginx-1.0.13.tar.bz2                #源码包到http://www.nginx.org/下载


  3. Mysql:mysql-5.5.20-linux2.6-i686.tar.gz   #源码包到http://www.mysql.com/下载


  4. PHP:php-5.3.10.tar.bz2                    #源码包到http://www.php.net/下载

注: 我们实现的是源码编译安装LNMP,那么我们首先得确保已经配置好开发环境,并且还需要注意的是比较新的源码包通常都会依赖比较新的开发工具,。请确保在 编译安装之前已经配置好开发环境。配置开发环境需要安装这几个软件包组:"Development Tools"、"Development  Libraries"、"X Software Development"。其中在编译安装php的时候,它依赖"X Software  Development"包组中的一些软件,为了方便我们将这个软件包组给安装了。在配置好yum源之后,我们可以使用下面命令安装:


  1. #yum groupinstall "Development Tools" "Development Libraries" "X Software Development"

用到的软件包:

  1. 这些软件包已经以附件的形式上传了!

  2. libmcrypt-2.5.8-4.el5.centos.i386.rpm

  3. libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm

  4. mhash-0.9.9-1.el5.centos.i386.rpm

  5. mhash-devel-0.9.9-1.el5.centos.i386.rpm

  6. libevent-2.0.17-2.i386.rpm

  7. libevent-devel-2.0.17-2.i386.rpm

  8. mcrypt-2.6.8-1.el5.i386.rpm


  1. libevent-1.4.14b-stable.tar.gz

  2. libiconv-1.13.1.tar.gz

  3. libmcrypt-2.5.8.tar.gz

  4. mhash-0.9.9.9.tar.bz2

下面就开始让我们来编译安装LNMP吧!

一、编译安装Nginx

1、解决依赖关系

   除了开发环境之外,Nginx的安装还依赖于pcre-devel这个软件包,所以我们要先从光盘中找到这个软件包并事先安装好。我们可以使用yum的方式安装这个软件包!

  1. #yum install pcre-devel

2、编译安装Nginx

首先为nginx添加用户,实现以这个用户的身份来安全的运行nginx服务进程:

  1. #groupadd -r nginx

  2. #useradd -r -g nginx -s /sbin/nologin -M nginx

其次是下载好nginx-1.0.13.tar.gz源码包,然后执行下面的命令:

  1. #tar xf nginx-1.0.13.tar.gz


  2. #cd nginx-1.0.13


  3. #./configure --prefix=/usr/nginx --sbin-path=/usr/sbin/nginx \

  4. --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log \

  5. --http-log-path=/var/log/nginx/access.log \--pid-path=/var/run/nginx/nginx.pid \

  6. --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module \

  7. --with-http_flv_module --with-http_stub_status_module \

  8. --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ \

  9. --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

  10. --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi \

  11. --with-pcre


  12. #make


  13. #make install

3、配置Nginx

   为nginx提供SysV服务脚本/etc/rc.d/init.d/nginx,这样使我们可以使用service命令进行对其进行操作,因为在编译安装nginx的时候,默认是不提供这个服务脚本的。我们需要手动添加:
   #vim /etc/rc.d/init.d/nginx  #添加SysV脚本,添加如下内容

  1. #!/bin/sh

  2. #

  3. # nginx - this script starts and stops the nginx daemon

  4. #

  5. # chkconfig:   - 85 15

  6. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \

  7. #               proxy and IMAP/POP3 proxy server

  8. # processname: nginx

  9. # config:      /etc/nginx/nginx.conf

  10. # config:      /etc/sysconfig/nginx

  11. # pidfile:     /var/run/nginx.pid

  12. # Source function library.

  13. . /etc/rc.d/init.d/functions

  14. # Source networking configuration.

  15. . /etc/sysconfig/network

  16. # Check that networking is up.

  17. [ "$NETWORKING" = "no" ] && exit 0

  18. nginx="/usr/sbin/nginx"

  19. prog=$(basename $nginx)

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

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

  22. lockfile=/var/lock/subsys/nginx

  23. make_dirs() {

  24.   # make required directories

  25.   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

  26.   options=`$nginx -V 2>&1 | grep 'configure arguments:'`

  27.   for opt in $options; do

  28.       if [ `echo $opt | grep '.*-temp-path'` ]; then

  29.           value=`echo $opt | cut -d "=" -f 2`

  30.           if [ ! -d "$value" ]; then

  31.               # echo "creating" $value

  32.               mkdir -p $value && chown -R $user $value

  33.           fi

  34.       fi

  35.   done

  36. }

  37. start() {                                        

  38.    [ -x $nginx ] || exit 5

  39.    [ -f $NGINX_CONF_FILE ] || exit 6

  40.    make_dirs

  41.    echo -n $"Starting $prog: "

  42.    daemon $nginx -c $NGINX_CONF_FILE

  43.    retval=$?

  44.    echo

  45.    [ $retval -eq 0 ] && touch $lockfile

  46.    return $retval

  47. }

  48. stop() {                                        

  49.    echo -n $"Stopping $prog: "

  50.    killproc $prog -QUIT

  51.    retval=$?

  52.    echo

  53.    [ $retval -eq 0 ] && rm -f $lockfile

  54.    return $retval

  55. }

  56. restart() {                                        

  57.    configtest || return $?

  58.    stop

  59.    sleep 1

  60.    start

  61. }

  62. reload() {                                        

  63.    configtest || return $?

  64.    echo -n $"Reloading $prog: "

  65.    killproc $nginx -HUP

  66.    RETVAL=$?

  67.    echo

  68. }

  69. force_reload() {

  70.    restart

  71. }

  72. configtest() {

  73.  $nginx -t -c $NGINX_CONF_FILE

  74. }

  75. rh_status() {

  76.    status $prog

  77. }

  78. rh_status_q() {

  79.    rh_status >/dev/null 2>&1

  80. }

  81. case "$1" in

  82.    start)

  83.        rh_status_q && exit 0

  84.        $1

  85.        ;;

  86.    stop)

  87.        rh_status_q || exit 0

  88.        $1

  89.        ;;

  90.    restart|configtest)

  91.        $1

  92.        ;;

  93.    reload)

  94.        rh_status_q || exit 7

  95.        $1

  96.        ;;

  97.    force-reload)

  98.        force_reload

  99.        ;;

  100.    status)

  101.        rh_status

  102.        ;;

  103.    condrestart|try-restart)

  104.        rh_status_q || exit 0

  105.            ;;

  106.    *)

  107.        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

  108.        exit 2

  109. esac

编辑完成后,保存退出,执行下列命令

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

  2. #chkconfig -add nginx

  3. #chkconfig nginx on                #设置为开机启动

  4. #service nginx start

注:每编译安装好一个软件包后最好对其进行测试,保证其正常,避免等到了编译安装好LNMP后出现错误。

二、安装Mysql

1、为数据库创建文件系统

   我们都知道数据文件是很重要的,为其单独创建一个文件系统是很有必要的,据说XFS文件系统对Mysql数据库的支持最好,不过这里我们使用ext3文件系统。为了便于扩展我们最好为其新建一个逻辑卷,过程如下:

  1. #fdisk /dev/sda            #分区过程不再给出,需要注意分区的类型为8e

  2. #pvcreate /dev/sda5

  3. #vgcreate mysql /dev/sda5

  4. #lvcreate -L 5G -n mydata mysql

  5. #mke2fs -j /dev/mysql/mydata

  6. #mkdir /mysql

  7. #vim /etc/fstab

  8. /dev/mysql/mydata    /mysql    ext3    defaults    0 0

  9. #mount /dev/mysql/mydata /mysql

  10. #mkdir /mysql/data

2、为mysql数据库新建用户

为mysql数据库创建用户,使其以该用户的身份安全运行mysql服务。

  1. #groupadd –r mysql

  2. #groupadd –g mysql –r –s /sbin/nologin –M mysql

  3. #chown –R mysql:mysql /mysql/data

3、安装mysql-5.5.20

   安装mysql有三种方式:rpm包安装、源代码包安装、类似绿色软件的安装。这里我们使用第三种方式安装mysql,直接解压并初始化就可以运行了。另外 安装的mysql版本需要与系统平台对应,我所使用的是32位平台,因此选择的软件包是:mysql-5.5.20-linux2.6- i686.tar.gz。有了软件包后,执行下列命令:

  1. #tar xf mysql-5.5.20-linux2.6-i686.tar.gz –C /usr/local

  2. #cd /usr/local/

  3. #ln -sv mysql-5.5.20-linux2.6-i686 mysql

  4. #cd mysql

  5. #chown -R mysql:mysql .

  6. #scripts/mysql_install_db -user=mysql -datadir=/mysql/data

  7. #chown -R root .

4、配置Mysql

为mysql提供主配置文件:

  1. #cd /usr/local/myql

  2. #cp support-files/my-large.cnf /etc/my.cnf

修改/etc/my.cnf配置文件,将thread_concurrency的值改为我们主机CPU个数的2倍,这样既可以有效的利用CPU,又不至于是CPU的负荷过大。编辑配置文件,如下:

  1. #vim /etc/my.cnf

  2. thread_concurrency = 2

  3. datadir = /mysql/data

为mysql提供SysV服务脚本,执行下列命令:

  1. #cd /usr/local/mysql

  2. #cp support-files/mysql.server /etc/rc.d/init.d/mysqld

  3. #chkocnfig –add mysqld

  4. #chkconfig mysqld on

  5. #service mysqld on

为了使mysql的安装符合系统的使用规范,我们还需要进行如下步骤:

  1. #vim /etc/man.conf        #添加如下内容

  2. MANPATH    /usr/local/mysql/man

  3. #ln –sv /usr/local/mysql/include /usr/include/mysql

  4. #echo ‘/usr/local/mysql/lib’ > /etc/ld.so.conf.d/mysql.conf    

  5. #ldconfig

  6. #vim /etc/profile

  7. PATH=$PATH:/usr/local/mysql/bin

注:在使用mysql数据库前,我们需要为数据库中的root设置密码:

  1. #mysqladmin -uroot -hlocalhost -p password '123456'

三、编译安装php

1、解决依赖关系

   在开始的时候,我们说过编译安装php的时候依赖于"X Software Development"软件包组中的软件包,所以请再确定下是否安装了该软件包组。如果安装过了,就继续下面的步骤吧!

2、编译安装php-5.3.10

如果我们想让编译的php支持mcrypt、mhash扩展和libevent,需要安装下列软件包,当然这些软件包就需要你自己去下载了:

  1. libmcrypt-2.5.8-4.el5.centos.i386.rpm

  2. libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm

  3. mhash-0.9.9-1.el5.centos.i386.rpm

  4. mhash-devel-0.9.9-1.el5.centos.i386.rpm

  5. libevent-2.0.17-2.i386.rpm

  6. libevent-devel-2.0.17-2.i386.rpm

  7. mcrypt-2.6.8-1.el5.i386.rpm


  1. #rpm –Uvh 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 libevent-2.0.17-2.i386.rpm libevent-devel-2.0.17-2.i386.rpm mcrypt-2.6.8-1.el5.i386.rpm

使用升级的方式安装这些软件包,当然我们也可以以源码包的方式进行安装。(最后边给出如何用源码方式安装这些扩展,以及对应的php应该如何编译。有兴趣可以直接看后边的,等编译好,再回过来看如何对php进行配置

安装libconv安装过程如下:

  1. #tar zxvf libiconv-1.13.1.tar.gz

  2. #cd libiconv-1.13.1

  3. #./configure

  4. #make

  5. #make install

然后下载php源码包,然后执行下列命令:

  1. #tar xf php-5.3.10.tar.bz2


  2. #cd php-5.3.10


  3. #./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl \

  4. --enable-fpm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring \

  5. --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir \

  6. --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc/php \

  7. --with-config-file-scan-dir=/etc/php --with-bz2 --with-curl      

  8. #make ZEND_EXTRA_LIBS='-liconv'        #如果安装了libconv需要这样make,没有安装直接make就行了


  9. #make test                #可以省略这一步,检查的时间比较久


  10. #make install

3、配置php

为php提供配置文件

  1. #cp php.ini-procuction /usr/local/php/lib/php.ini

为php-fpm提供SysV脚本:

  1. #cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

  2. #chmod +x /etc/rc.d/init.d/php-fpm

  3. #chkconfig –add php-fpm

  4. #chkconfig php-fpm on

为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      #配置pm.的相关选项为我们所需要的值,并启用pid文件

  2. pm.max_children = 50

  3. pm.start_servers = 5

  4. pm.min_spare_servers = 2

  5. pm.max_spare_servers = 8

  6. pid = /var/run/php-fpm.pid

  7. #service php-fpm start

验证php-fpm是否正常启动成功

  1. #ps aux | grep php-fpm        #此命令输出有中几个php-fpm进程就说明启动成功了

测试php是否正常工作,将/usr/nginx/html目录中的主页面文件更名,并写入如下内容:

  1. #mv index.html index.php


  2. #vim index.php


  3. <?php

  4. phpinfo();

  5. ?>

然后通过浏览器访问该页面,如果正常,会将php的信息给列出来,如图所示:

架设LNMP平台的WEB服务器

然后修改index.php来测试数据库的连通性


  1. #vim index.php


  2. <?php

  3. $link=mysql_connect('localhost','root','');

  4. if ($link)

  5.  echo "successful";

  6. else

  7.  echo "failure";

  8. mysql_close();

  9. ?>

通过浏览器访问该页面,如果成功,会显示successful,反之显示failure。

四、整合nginx和php5

首先编辑/etc/nginx/nginx.conf,启用下面这些选项

  1. location ~ \.php$ {

  2.            root           html;

  3.            fastcgi_pass   127.0.0.1:9000;

  4.            fastcgi_index  index.php;

  5.            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

  6.            include        fastcgi_params;

  7.        }

然后编辑/etc/nginx/fastcgi_params,将其内容更换为如下内容:

  1. fastcgi_param  GATEWAY_INTERFACE    CGI/1.1;

  2. fastcgi_param  SERVER_SOFTWARE      nginx;

  3. fastcgi_param  QUERY_STRING         $query_string;

  4. fastcgi_param  REQUEST_METHOD       $request_method;

  5. fastcgi_param  CONTENT_TYPE         $content_type;

  6. fastcgi_param  CONTENT_LENGTH       $content_length;

  7. fastcgi_param  SCRIPT_FILENAME      $document_root$fastcgi_script_name;

  8. fastcgi_param  SCRIPT_NAME          $fastcgi_script_name;

  9. fastcgi_param  REQUEST_URI          $request_uri;

  10. fastcgi_param  DOCUMENT_URI         $document_uri;

  11. fastcgi_param  DOCUMENT_ROOT        $document_root;

  12. fastcgi_param  SERVER_PROTOCOL      $server_protocol;

  13. fastcgi_param  REMOTE_ADDR          $remote_addr;

  14. fastcgi_param  REMOTE_PORT          $remote_port;

  15. fastcgi_param  SERVER_ADDR          $server_addr;

  16. fastcgi_param  SERVER_PORT          $server_port;

  17. fastcgi_param  SERVER_NAME          $server_name;

最后重新启动nginx

  1. #service nginx restart

五、安装xcache

安装xcache为其提供缓存功能,使nginx的性能更高!

1、首先要下载到xcache的源码包,然后命令如下:


  1. #tar xf xcache-1.3.2.tar.gz

  2. #cd xcache-1.3.2

  3. #/usr/local/php/bin/phpize

  4. #./configure –enable-xcache -with-php-config=/usr/local/php/bin/php-config

  5. #make

  6. #make install

2、编辑php.ini

首先将xcache提供的样例配置导入到php.ini

  1. #cat xcache.ini >> /usr/local/php/lib/php.ini   #xcache.ini在xcache的源码包目录中

然后编辑/usr/local/php/php.ini,找到zend_extension开头的行,修改为如下内容:

  1. zend_extension = /usr/local/lib/php/extensions/non-debug-non-zts-20090626/xcache.so

最后重新启动php-fpm

  1. #service php-fpm restart


附录:
   前边我们使用rpm包的方式安装php所需的扩展,下面就是如何用源码的方式为php添加扩展了!一起来看看吧!
   Php可以支持的扩展有mcrypt、mhash、libevent和libiconv,所以我们首先需要下载到这些所需要的源码包:


  1. libmcrypt :提供了系统自带的crypt的函数,增加了更多的加密函数,提供更高级的加密算法


  2. mhash:做哈希计算的。做单项加密函数的


  3. libiconv:是实现基于一种编码实现国际化的语言的接口


  1. libevent-1.4.14b-stable.tar.gz


  2. libiconv-1.13.1.tar.gz


  3. libmcrypt-2.5.8.tar.gz


  4. mhash-0.9.9.9.tar.bz2

然后,我们挨个对其编译安装,下面的编译安装比较简单,只给出步骤:

Libevent

  1. #tar zxf libevent-1.4.14b-stable.tar.gz


  2. #cd libevent-1.4.14b-stable


  3. #./configure


  4. #make


  5. #make install


  6. #make verify

Libiconv

  1. #tar zxf libiconv-1.13.1.tar.gz


  2. #cd libiconv-1.13.1


  3. #./configure


  4. #make


  5. #make install

Libmcrypt

  1. #tar zxf libmcrypt-2.5.8.tar.gz

  2. #cd libmcrypt-2.5.8

  3. #./configure

  4. #make

  5. #make install

  6. #ldconfig -v

  7. #cd libltdl

  8. #./configure --with-gmetad --enable-gexec --enable-ltdl-install

  9. #make

  10. #make install


Mhash

  1. #tar jxf mhash-0.9.9.9.tar.bz2


  2. #cd mhash-0.9.9.9


  3. #./configure


  4. #make


  5. #make install


将库文件导出

  1. #ln -sv /usr/local/lib/libmcrypt* /usr/lib/


  2. #ln -sv /usr/local/lib/libmhash.* /usr/lib/

那么相应的,php的编译肯能就要做出一些改变了,步骤如下

  1. #tar xf php-5.3.10.tar.bz2


  2. #cd php-5.3.10


  3. #./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl \

  4. --enable-fpm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring \

  5. --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr \

  6. --enable-xml --with-mhash=/usr/local --with-mcrypt=/usr/local --with-config-file-path=/etc/php \

  7. --with-config-file-scan-dir=/etc/php --with-bz2 --with-curl

  8. #其中—with-mhash与—with-mcryt也可以不用加安装路径,但是如果我们自己定义了目录,就需要指出来了。

  9. 这里只是给个提示,源码安装与rpm包安装的区别就在这了,是否需要我们自己指定安装目录了!

  10. #make ZEND_EXTRA_LIBS='-liconv'         #如果安装了libconv需要这样make,没有安装直接make就行了


  11. #make test                              #这一步可以省略,太耗时间了!


  12. #make install

  其实LNMP与LAMP的实现过程有许多相同之处,所以写的两篇博客,会很相似.