安装nginx

useradd -r nginx

tar xf tengine-2.0.1.tar.gz -C /usr/local/src

./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

make && make install

配置配置文件语法着色

1、下载nginx.vim

下载页面:http://www.vim.org/scripts/script.php?script_id=1886

2、安装nginx.vim

将nginx.vim放置于~/.vim/syntax/目录,

3、配置 nginx.vim

而后在~/.vim/filetype.vim中添加如下行:
au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif

其中“/etc/nginx”为nginx配置文件的目录。

添加一个service脚本
#!/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@localhost nginx]# vim /etc/init.d/nginx
[root@localhost nginx]# chmod +x /etc/init.d/nginx
[root@localhost nginx]# chkconfig --add nginx
[root@localhost nginx]# service nginx start

配置文件
用于配置错误日志,可用于main、http、server及location上下文中;语法格式为:
error_log file | stderr [ debug | info | notice | warn | error | crit | alert | emerg ]
要禁用错误日志,不能使用“error_log off;”,而要使用类似如下选项:
error_log /dev/null crit;


通过sched_setaffinity()将worker绑定至CPU上,只能用于main上下文。语法格式为:
worker_cpu_affinity cpumask ...
例如:
worker_processes     4;
worker_cpu_affinity 0001 0010 0100 1000;

2.1.4 worker_priority
为worker进程设定优先级(指定nice值),此参数只能用于main上下文中,默认为0;语法格式为:
worker_priority number

2.1.6 worker_rlimit_nofile
设定worker进程所能够打开的文件描述符个数的最大值。语法格式:
worker_rlimit_nofile number

配置反向代理
lnmmp_nginx
配置Nginx状态页:
lnmmp_nginx_02 添加密码
[root@localhost nginx]# htpasswd -c -m /etc/nginx/.pass xiaoyu

配置压缩
lnmmp_nginx_03
简单防盗链
lnmmp_nginx_04


检查语法启动服务
[root@localhost nginx]# nginx -t
the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
[root@localhost nginx]# service nginx reload
the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
Reloading nginx:                                           [  OK  ]


安装php

yum -y install libcurl-devel bzip2-devel  libxml2-devel mhash-devel libmcrypt-devel

./configure --prefix=/usr/local/php --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm   --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd 

 --enable-bcmath  --with-gd  --with-gettext
# make
# make test
# make intall

为php提供配置文件:
# cp php.ini-production /etc/php.ini

为php-fpm提供Sysv init脚本,并将其添加至服务列表:
# 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

为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
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid
listen = /tmp/php-fcgi.sock

接下来就可以启动php-fpm了:
# service php-fpm start

提供一个测试页
# echo "<?php phpinfo(); ?>" >index.php

安装mysql
# useradd -r mysql
# mkdir /data/mysql/
# chowm mysql.mysql /data/mysql -R
# tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mysql-5.5.24-linux2.6-i686  mysql
# cd mysql

# chown -R mysql:mysql  .
# scripts/mysql_install_db --user=mysql --datadir=/data/mysql/
# chown -R root  .

为mysql提供主配置文件:

# cd /usr/local/mysql
# cp support-files/my-large.cnf  /etc/my.cnf

并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
thread_concurrency = 2

另外还需要添加如下行指定mysql数据文件的存放位置:
datadir = /data/mysql

提供启动脚本
# cd /usr/local/mysql
# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld

添加至服务列表:
# chkconfig --add mysqld
# chkconfig mysqld on

安装memcached
Memcached是一款开源、高性能、分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对Database的访问来加速web应用程序。它是一个基于内存的“键值对”存储,用于存储数据库调用、API调用或页面引用结果的直接数据,如字符串、对象等。

memcached是以LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件。现在
已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。

Memcached是一款开发工具,它既不是一个代码加速器,也不是数据库中间件。其设计哲学思想主要反映在如下方面:

1. 简单key/value存储:服务器不关心数据本身的意义及结构,只要是可序列化数据即可。存储项由“键、过期时间、可选的标志及数据”四个部分组成;
2. 功能的实现一半依赖于客户端,一半基于服务器端:客户负责发送存储项至服务器端、从服务端获取数据以及无法连接至服务器时采用相应的动作;服务端负责接收、存储数据,并负责数据项的超时过期;
3. 各服务器间彼此无视:不在服务器间进行数据同步;
4. O(1)的执行效率
5. 清理超期数据:默认情况下,Memcached是一个LRU缓存,同时,它按事先预订的时长清理超期数据;但事实上,memcached不会删除任何已缓存数据,只是在其过期之后不再为客户所见;而且,memcached也不会真正按期限清理缓存,而仅是当get命令到达时检查其时长;

Memcached提供了为数不多的几个命令来完成与服务器端的交互,这些命令基于memcached的协议实现。

存储类命令:set, add, replace, append, prepend
获取数据类命令:get, delete, incr/decr
统计类命令:stats, stats items, stats slabs, stats sizes
清理命令: flush_all

安装memcached
# yum -y install memcached
启动服务
# service memcached start
查看监听端口
# ss -anlp|grep memcached
查看状态
# memcached-tool 127.0.0.1:11211 stats


安装PHP的memcache扩展

# tar xf memcache-2.2.5.tgz
# cd memcache-2.2.5
/usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
# make && make install

上述安装完后会有类似以下的提示:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

编辑/usr/local/php/lib/php.ini,在“动态模块”相关的位置添加如下一行来载入memcache扩展:
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so


而后对memcached功能进行测试,在网站目录中建立测试页面test.php,添加如下内容:
<?php
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211)  or die("Could not connect");

$version = $mem->getVersion();
echo "Server's version: ".$version."<br/>\n";

$mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");
echo "Store data in the cache (data will expire in 600 seconds)<br/>\n";

$get_result = $mem->get('hellokey');
echo "$get_result is from memcached server.";        
?>

如果有输出“Hello World is from memcached.”等信息,则表明memcache已经能够正常工作。

lnmmp_nginx_05