LNMP介绍
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
 Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。[1]
  Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。[2]
  Mysql是一个小型关系型数据库管理系统。[3]
  Php是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言。[3][4]
这四种软件均为免费 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。
  作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。
  作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。
Nginx对静态页面的支持相当出色,轻量且免费。Nginx不支持CGI,但是支持更灵活的FastCGI。PHP5.2及之前的版本比较多的是使用PHP-FPM来管理PHP FastCGI进程。PHP-FPM使用给PHP源码打补丁后编译的方式让新手多少有些难上手,但从PHP 5.3.2开始内置PHP-FPM,只需编译PHP时启用PHP-FPM。
    Nginx 安装非常的简单,配置文件 非常简洁(还能够支持perl语法),Bugs非常少的服务器: Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在 不间断服务的情况下进行软件版本的升级。软件,组合到一起,成为一个免费、高效的网站服务系统PHP,是英文超级文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛的运用。
 

二、源码安装Nginx:
1、解决依赖关系
把自己的安装光盘装入,yum源指向自已的光盘,进行开发包组的安装。
编译安装nginx需要事先需要安装开发包组"Development Tools"和 "Development Libraries"。同时,还需要专门安装pcre-devel包:
# yum -y install pcre-devel  // 安装 pcre,系统自带的 pcre 版本过低,不能满足我们的需求。 pcre 是一个正则表达式相关的包,要想 Nginx 使用 Rewrite,那么就需要正则的支持。
2、下载及安装
下载地址:
http://mirrors.sohu.com/找到最新版本的nginx源码,下载到本地。
安装:
首先添加用户nginx,实现以之运行nginx服务进程:
# groupadd -r nginx
# useradd -r -g nginx -s /bin/false -M nginx   //-r系统用户 -M不创建家目录
接着开始编译和安装:
# ./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  \pid路径
  --lock-path=/var/lock/nginx.lock \锁文件存放位置
  --user=nginx \指定服务以nginx用户运行
  --group=nginx \指定服务以nginx组运行

  --with-http_ssl_module \起用ssl模块
  --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
3、为nginx提供SysV init脚本:
新建文件/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
而后为此脚本赋予执行权限:
# chmod +x /etc/rc.d/init.d/nginx
添加至服务管理列表,并让其开机自动启动:
# chkconfig --add nginx
# chkconfig nginx on
而后就可以启动服务并测试了:
# service nginx start
ps aux | grep nginx查看是否启动
二、安装mysql-5.5.20
1、准备数据存放的文件系统
新建一个逻辑卷,并将其挂载至特定目录即可。
注:先分个区,把数据库文件存到那里,文件系统为类型为8e,即逻辑卷,这样以后数据就可以方便备份
fdisk /dev/sda
.....
partprobe /dev/sda5
pvcreate /dev/sda5
vgcreate myvg /dev/sda5
lvcreate -L 3G -n mysql myvg
mke2fs -j /dev/myvg/mysql
开机自动挂载在/mydata下,
vim /etc/fstab
/dev/myvg/mysql    /mydata      ext3    default 0 0
mount -a
mount    //查看

这里假设其逻辑卷的挂载目录为/mydata,创建/mydata/data目录做为mysql数据的存放目录。
2、新建用户以安全方式运行进程:
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
# chown -R mysql:mysql /mydata/data
3、安装并初始化mysql-5.5.20
首先下载平台对应的mysql版本至本地,这里是32位平台,因此,选择的为mysql-5.5.19-linux2.6-i686.tar.gz,其下载位置为http://mirrors.sohu.com/
# tar xf mysql-5.5.20-linux2.6-i686.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mysql-5.5.20-linux2.6-i686  mysql
# cd mysql
# chown -R mysql:mysql  .
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# chown -R root  .
4、为mysql提供主配置文件:
# cd /usr/local/mysql
# cp support-files/my-large.cnf  /etc/my.cnf
并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
thread_concurrency = 2
另外还需要添加如下行指定mysql数据文件的存放位置:
datadir = /mydata/data

5、为mysql提供sysv服务脚本:
# cd /usr/local/mysql
# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
添加至服务列表:
# chkconfig --add mysqld
# chkconfig mysqld on
而后就可以启动服务测试使用了。

为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:
6、输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:
MANPATH  /usr/local/mysql/man
7、输出mysql的头文件至系统头文件路径/usr/include:
这可以通过简单的创建链接实现:
# ln -sv /usr/local/mysql/include  /usr/include/mysql
8、输出mysql的库文件给系统库查找路径:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而后让系统重新载入系统库:
# ldconfig
9、修改PATH环境变量,让系统可以直接使用mysql的相关命令。具体实现过程这里不再给出。

三、编译安装php-5.3.10
1、解决依赖关系:
请配置好yum源(可以是本地系统光盘)后执行如下命令:
# yum -y groupinstall "X Software Development"
如果想让编译的php支持mcrypt、mcrypt、mhash扩展和libevent,此处还需要在http://mirrors.sohu.com/中下载几个rpm包并安装之:
php-fpm里有前四个
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
最好使用升级的方式安装上面的rpm包,命令格式如下:
# rpm -Uvh         //--nodeps
2、编译安装php-5.3.10
首先下载源码包至本地目录,下载位置http://mirrors.sohu.com/
# tar xf php-5.3.10.tar.bz2
# cd php-5.3.10
#  ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --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-mcrypt  --with-config-file-path=/etc/php --with-config-file-scan-dir=/etc/php --with-bz2 --with-curl
说明:如果前面第1步解决依赖关系时安装mcrypt相关的两个rpm包,此./configure命令还可以带上--with-mcrypt选项以让php支持mycrpt扩展。
# make
# make test
# make intall
为php提供配置文件:
# cp php.ini-production /etc/php/php.ini
为php-fpm提供Sysv init脚本,并将其添加至服务列表:
# cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on
为php-fpm提供配置文件:
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm的配置文件:
# vim /usr/local/php/etc/php-fpm.conf
配置pm.的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /var/run/php-fpm.pid
接下来就可以启动php-fpm了:
# service php-fpm start
使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
# ps aux | grep php-fpm
php-fpm脚本可在php目录下 find ./ -name "init*"

四、整合nginx和php5
1、编辑/etc/nginx/nginx.conf,启用如下选项:
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
2、编辑/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格式的主页,类似如下:
location / {
            root   html;
            index  index.php index.html index.htm;
        }
       
而后重新载入nginx的配置文件:
# service nginx reload
3、在/usr/html新建index.php的测试页面,测试php是否能正常工作:
# cat > /usr/html/index.php << EOF
<?php
phpinfo();
?>
接着就可以通过浏览器访问此测试页面了。

五、安装xcache,为php加速:
1、安装
首先下载,可到http://mirrors.sohu.com/中找到xcache-1.3.2.tar.gz
然后:
# tar xf xcache-1.3.2.tar.gz
# cd xcache-1.3.2
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
安装结束时,会出现类似如下行:
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
2、编辑php.ini,整合php和xcache:
首先将xcache提供的样例配置导入php.ini
# cat xcache.ini >> /usr/local/php/lib/php.ini
说明:xcache.ini文件在xcache的源码目录中。
接下来编辑/usr/local/php/lib/php.ini,找到zend_extension开头的行,修改为如下行:
zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xcache.so
注意:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。
3、重新启动php-fpm
# service php-fpm restart
 
 
六、nginx.conf配置文件解释
#nginx工作的进程数量
worker_processes 2;

# [ debug | info | notice | warn | error | crit ]   错误日志的位置
error_log  /var/htdocs/logs/nginx_error.log  crit;

#进程号保存文件
pid /usr/local/nginx/nginx.pid;
#最大文件描述符 ?有待继续整理.
worker_rlimit_nofile 51200;
events
{
     # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
     use epoll;  #使用epoll高性能方式
     worker_connections 51200; #每个进程最大连接数(最大连接=连接数x进程数)
}
  #默认编码
     charset  gb2312,utf-8;
   
     server_names_hash_bucket_size 128;
     #开启高效文件传输模式
     sendfile on;
#打开gzip压缩
     gzip on;
     #最小压缩文件大小
     gzip_min_length  1k;
     #压缩缓冲区
     gzip_buffers     4 8k;
     #压缩版本(默认1.1,前端为squid2.5使用1.0)
     gzip_http_version 1.1;

七、
1、创建虚拟主机
 mkdir /www/html
vim /etc/nginx/nginx.conf
添加如下内容
server {
listen 80;
server_name www.wl.com;
location /{
root /www/html; 绝对路径
index index.html index.htm;
}
echo "www.wl.com" > /www/html/index.php
2、基于别名访问
别名就是不在网站路径下,但却可以在url中输入别名进行访问
如:网站主目录在/www/html
现在想通过输入http://ip地址/bbs访问/www/bbs下的内容
location /bbs/ {
alias /www/bbs/;
index home.html //定义主页面
}
3、基于ip的访问控制
location / {
  deny    192.168.1.1;
  allow   192.168.1.0/24;
  allow   10.1.0.0/16;
  deny    all;
}
4、location的符号区分
location
syntax: location [=|~|~*|^~|@] /uri/ { ... }
= 精确匹配,定文件
~ 正则表达式区分大小写
~*正则表达式不区分大小写
^~不使用正则表达式 ,似类=
5、开启Nginx状态监控的功能:
在某个虚拟主机里
location /status {
  stub_status on;
  access_log off;
}
在地址览输入http://虚拟机ip地址/status查看
6、基于身份的认证
在你需要身份认证的虚拟机里面输入下面两行:
auth_basic "Restricted";  /认证类型
auth_basic_user_file /etc/nginx/.htpasswd; /认证的文件存放位置
创建认证文件方法:
htpasswd -cm /etc/nginx/.htpasswd jerry
7、压力测试
下载工具 复制光盘webbench到本地
解压缩,然后
直接make install
webbench -c 1000 -t 30 http://虚拟主机ip地址/index.html