网站的成功与否,与其稳定的性能有很大的关系。高配硬件对于很多草根站长而言,实在是望价止步。就拿一款512M的VPS来 说,Apache固然强大又稳定,但动辄200M+的内存,实在不是首选,而Nginx、Lighttpd这类的程序,内存消耗低、速度快,但又不是很稳 定,一旦在线人数过多或动态连接数过高,Nginx就歇菜了,直接丢个502错误。

不过,程序是人写的,方案也是可以由我们而改进的。既然Nginx处理静态文件强悍,Apache处理动态稳定,那我们干脆可以将Apache替代fcgi与Nginx结合。将Nginx作为前端处理静态,Apache后置处理动态,简称LANMP或LNAMP。

网上已经有LNAMP一键安装包了,不过不是很理想,网上很多类似的教程我没一次测试成功过,很纠结。于是我参考了一些资料,做了一个适合大部分新手都可以使用并快速修改的方案,以此来解决502瓶颈!

 

此方案的程序要求:

Centos 5+

PHP 5.2.x

MySQL 5.1.x

Nginx 0.8.x

硬件要求:

CPU:无,尽可能两核心+

内存:256M+ 内存偏小的话,建议直接用Nginx吧,这个方案是以稳定为主,不是用来拼命缩减内存消耗的

1,重装Centos系统,并删除不比要的程序:

yum -y remove httpd mysql php

2,安装相关组件:

yum -y install yum-fastestmirror
yum -y update
yum -y install patch make gcc gcc-c++ gcc-g77 flex bison tar
yum -y install libtool libtool-libs kernel-devel autoconf213
yum -y install libjpeg libjpeg-devel libpng libpng-devel
yum -y install libtiff libtiff-devel gettext gettext-devel
yum -y install freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel
yum -y install file glib2 glib2-devel bzip2 diff* openldap-devel
yum -y install bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs
yum -y install e2fsprogs-devel krb5 krb5-devel libidn libidn-devel
yum -y install openssl openssl-devel vim-minimal unzip
export PHP_AUTOCONF=/usr/bin/autoconf-2.13
export PHP_AUTOHEADER=/usr/bin/autoheader-2.13

安装PHP 5.2.x 所需的支持库

cd /usr/local/src

wget -c http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../

wget -c http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
tar zxvf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable/
./configure --prefix=/usr
make
make install
cd ../

wget -c http://ncu.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../

wget -c http://nchc.dl.sourceforge.net/sourceforge/mhash/mhash-0.9.9.9.tar.gz
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
cd ../

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

wget -c http://nchc.dl.sourceforge.net/sourceforge/mcrypt/mcrypt-2.6.8.tar.gz
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
./configure
make
make install
cd ../

3,安装 Mysql 5.1.x

先安装Google的开源TCMalloc库,可以提高MySQL在高并发情况下的性能

wget -c http://google-perftools.googlecode.com/files/google-perftools-1.6.tar.gz
tar zxvf google-perftools-1.6.tar.gz
cd google-perftools-1.6/
./configure
make && make install
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig
cd ../

安装之后,开始安装Mysql 5.1.x

wget -c http://mysql.proserve.nl/Downloads/MySQL-5.1/mysql-5.1.53.tar.gz
tar -zxvf mysql-5.1.53.tar.gz
cd mysql-5.1.53
./configure --prefix=/usr/local/mysql --with-extra-charsets=all --enable-thread-safe-client --enable-assembler --with-charset=utf8 --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --without-debug --with-mysqld-ldflags=-ltcmalloc_minimal
make && make install
cd ../
groupadd mysql
useradd -g mysql mysql
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
/usr/local/mysql/bin/mysql_install_db --user=mysql
chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql/.
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig --level 345 mysql on
echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
echo "/usr/local/lib" >>/etc/ld.so.conf
ldconfig
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
service mysql start
/usr/local/mysql/bin/mysqladmin -u root password 11111111 //修改红色部分为你的密码
service mysql restart

打开“/etc/my.cnf”文件,修改以下设置,如果没有,可手动添加。

  • 关闭远程连接,即3306端口。这是MySQL的默认监听端口。由于此处MySQL只服务于本地脚本,所以不需要远程连接。尽管 MySQL内建的安全机制很严格,但监听一个TCP端口仍然是危险的行为,因为如果MySQL程序本身有问题,那么未授权的访问完全可以绕过MySQL的 内建安全机制。(你必须确定,你是否真的不需要远程连接mysql,一般数据库地址为localhost的,即使本地数据库,不需要外链)

skip-networking

保存后退出。

修改完my.cnf后,还需要对mysql的用户名、帐号、及默认数据库进行调整

首先先登录mysql,在终端窗口输入

/usr/local/mysql/bin/mysql -u root –p

然后会提示输入密码,输入正确密码后,会出现mysql>提示符。

输入以下命令:

mysql>use mysql;
mysql>select Host,User,Password,Select_priv,Grant_priv from user;
mysql>delete from user where user='';                 (删除user用户)
mysql>delete from user where password='';              (删除user用户)
mysql>delete from user where host='';                    (删除user用户)
mysql>drop database test;            (删除默认的test数据库)
mysql>flush privileges;              (刷新mysql的缓存,让以上设置立即生效)
mysql>quit;

为了使以上优化和安全设置生效,请重启Mysql服务或Linux。

service mysql restart

4,安装GD图形支持库:

cd /usr/local/src
wget http://www.libgd.org/releases/gd-2.0.35.tar.gz
tar xzvf gd-2.0.35.tar.gz
cd gd-2.0.35
./configure --prefix=/usr/local/gd --mandir=/usr/share/man --with-jpeg --with-png --with-freetype --with-zlib --with-fontconfig
make            //如果GD报错:configure.ac:64: warning: macro `AM_ICONV' not found in library 你就make clean一下,然后再make
make install

5,安装Apache:

cd /usr/local/src
groupadd www
useradd -g www -s /sbin/nologin -M www

wget http://apache.freelamp.com//httpd/httpd-2.2.17.tar.gz
tar zxvf httpd-2.2.17.tar.gz
cd httpd-2.2.17

Apache默认最大连接数和最大客户端数为40000,如果你的服务器要求更大,可以编译Apache安装文件中的:

server/mpm/worker/worker.c

找到下面几行,并改成如下的数值,其目的是在源码中修改apache可支持的最大线程数和最大客户端数目。

define DEFAULT_SERVER_LIMIT 32
define MAX_SERVER_LIMIT 40000
define DEFAULT_THREAD_LIMIT 64
define MAX_THREAD_LIMIT 40000

以上数值据说改小后,能减低服务器消耗。不过柒月修改后,发现没什么实质变化。

5.1,依次安装apr和apr-util

cd srclib/apr
./configure --prefix=/usr/local/apr
make && make install
cd ../apr-util
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ --with-mysql=/usr/local/mysql
make && make install

5.2,安装Apache 2.2

cd /usr/local/src/httpd-2.2.17
./configure --prefix=/usr/local/apache2 --with-mysql=/usr/local/mysql --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --enable-so --enable-rewrite --with-mpm=prefork --disable-cgid --disable-cgi
make && make install

如果你需要编译apache其他功能,可以自行增加。不过在这里,我们只是用Apache作为后端并处理伪静态,无需添加过多设置来浪费内存

注解:

./configure //配置源代码树
--prefix=/usr/local/apache2 //体系无关文件的顶级安装目录PREFIX ,也就Apache的安装目录。
--enable-module=so //打开 so 模块,so 模块是用来提 DSO 支持的 apache 核心模块
--enable-mods-shared=all //编译全部的模板,对于不需要我们可以在httpd.conf去掉。
--enable-cache //支持缓存
--enable-file-cache //支持文件缓存
--enable-mem-cache //支持记忆缓存
--enable-disk-cache //支持磁盘缓存
--enable-static-support //支持静态连接(默认为动态连接)
--enable-static-htpasswd //使用静态连接编译 htpasswd - 管理用于基本认证的用户文件
--enable-static-htdigest //使用静态连接编译 htdigest - 管理用于摘要认证的用户文件
--enable-static-rotatelogs //使用静态连接编译 rotatelogs - 滚动 Apache 日志的管道日志程序
--enable-static-logresolve //使用静态连接编译 logresolve - 解析 Apache 日志中的IP地址为主机名
--enable-static-htdbm //使用静态连接编译 htdbm - 操作 DBM 密码数据库
--enable-static-ab //使用静态连接编译 ab - Apache HTTP 服务器性能测试工具
--enable-static-checkgid //使用静态连接编译 checkgid
--disable-cgid //禁止用一个外部 CGI 守护进程执行CGI脚本
--disable-cgi //禁止编译 CGI 版本的 PHP
--with-mpm=worker // 让apache以worker方式运行
--enable-ssl // 编译 ssl模块。

启动Apache(建议先不要启动,等我们全部设置完毕后,和Nginx启动)

/usr/local/apache2/bin/apachectl start

查看apache是否启动

ps aux|grep httpd

将apache设置成开机自启动:

echo '/usr/local/apache2/bin/apachectl start ' >> /etc/rc.local //将 apachectl 的调用加入到你的系统启动文件中。

6,安装 PHP5.2.x

cd /usr/local/src
wget -c http://us.php.net/distributions/php-5.2.14.tar.gz
wget http://download.suhosin.org/suhosin-patch-5.2.14-0.9.7.patch.gz
tar zxvf php-5.2.14.tar.gz
gunzip suhosin-patch-5.2.14-0.9.7.patch.gz
cd php-5.2.14
patch -p 1 -i ../suhosin-patch-5.2.14-0.9.7.patch
./buildconf --force
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/etc --with-zlib --with-libxml-dir --with-gd=/usr/local/gd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-ttf --with-iconv --with-openssl --with-mcrypt --enable-sockets --enable-bcmath --enable-calendar --enable-exif --enable-libxml --enable-magic-quotes --enable-mbstring --with-bz2 --with-curl --with-xmlrpc --with-gettext --enable-suhosin --disable-cli --disable-cgi --disable-debug
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /etc/php.ini

6.1,安装PHP扩展:

6.1.1,安装eaccelerator加速软件

eaccelerator是php的加速软件,使用后php的执行效率会有很大幅度的提升。

cd /usr/local/src
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
tar -jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make && make install

编译安装后我们会看到屏幕提示的eaccelerator.so所在的目录,php5.2.x系列是在 /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/,记住这个路径,待会要用到

修改php.ini 文件,将以下内容输入(详细设置):

[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

注意:这部分内容务必放在[zend]之前,不然可能会出现不可预期的服务器问题。

然后建立文件夹并设置权限:

mkdir /tmp/eaccelerator
chmod 777 /tmp/eaccelerator

6.1.2,安装Zend(如果程序没要求,尽量不要装Zend,容易和eA产生冲突):

cd /usr/local/src
wget http://downloads.zend.com/optimizer/3.3.9/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
tar -zxvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
cp ZendOptimizer-3.3.9-linux-glibc23-i386/data/5_2_x_comp/ZendOptimizer.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

修改php.ini 文件:

vi /usr/local/php/php.ini

将以下代码放入php.ini文件最底部

[Zend Optimizer]
zend_optimizer.optimization_level=1
zend_optimizer.encoder_loader=0
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/ZendOptimizer.so"

6.1.3,安装PDO_MYSQL

cd /usr/local/src
wget -c http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make
make install

继续修改 php.ini 文件

vi /usr/local/php/php.ini

查找代码:

extension_dir = "./"

替换为:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension="pdo_mysql.so"

7,Apache 整合 PHP 以及相关优化:

编辑 httpd.conf :

vi /usr/local/apache2/conf/httpd.conf

查找代码:

DocumentRoot "/usr/local/apache2/htdocs"

修改为:

DocumentRoot "/home/www"

查找代码:

<Directory "/usr/local/apache2/htdocs">

修改为:

<Directory "/home/www">

找到以下代码并根据提示修改

<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all       //这句改为Allow from all
</Directory>

找到这一段并修改,以使Apache支持rewrite(伪静态):

# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#  Options FileInfo AuthConfig Limit
#
AllowOverride None

更改为

AllowOverride All

将以下代码注释掉,禁止目录列表:

Options Indexes FollowSymLinks

找到以下代码,修改用户为 www

<IfModule !mpm_netware_module>
<IfModule !mpm_winnt_module>

User daemon             //改为www
Group daemon            //改为www

</IfModule>
</IfModule>

设置 ServerAdmin you@example.com 改为你自己的mail地址

查找:Listen 80 改为 Listen 81

分别找到以下四段代码,将之前的注释#去除:

Include conf/extra/httpd-mpm.conf

Include conf/extra/httpd-info.conf

Include conf/extra/httpd-vhosts.conf

Include conf/extra/httpd-default.conf

编辑 Include conf/extra/httpd-mpm.conf 找到如下选项,并改成对应的数值

<IfModule mpm_prefork_module>
StartServers         5
MinSpareServers      5
MaxSpareServers     10
MaxClients         150
MaxRequestsPerChild  1000
</IfModule>

编辑 conf/extra/httpd-default.conf

Timeout 60  #与nginx的保持一至
KeepAlive On
MaxKeepAliveRequests 1000
KeepAliveTimeout 5

复制以下文件:

cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd

然后 vi /etc/rc.d/init.d/httpd 添加(#!/bin/sh下面)

chkconfig: 2345 10 90
description: Activates/Deactivates Apache Web Server

最后,运行chkconfig把Apache添加到系统的启动服务组里面:

chkconfig --add httpd
chkconfig httpd on

这样以后Apache 启动、关闭、重启只需要输入以下命令:

service httpd start/stop/restart

整合Apache与php

编辑 /usr/local/apache2/conf/httpd.conf

找到AddType application/x-gzip .gz .tgz在其下加以下内容

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

查找:(设置WEB默认文件)

DirectoryIndex index.html

改成:

DirectoryIndex index.php index.htm index.html index.html.var

保存退出

以上设置完毕后,全部保存退出,然后建立一个虚拟主机做测试:

编辑 Include conf/extra/httpd-vhosts.conf

<VirtualHost *:81>
ServerAdmin webmaster@example.com
DocumentRoot "/home/www"
ServerName 你的IP地址或域名
ErrorLog "logs/IP-error_log"
CustomLog "logs/IP-access_log" common
</VirtualHost>

保存退出,并重启Apache

service httpd restart 或 /usr/local/apache2/bin/apachectl restart

然后建立一个 index.php文件,放入 /home/www 目录,php文件内容为:

<?php
phpinfo();
?>

然后在浏览器中输入:IP:81 是否成功显示了?

8,优化MySQL

通过Tcmalloc 优化mysql性能

# vi /etc/init.d/mysqld

添加一行

export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

重新启动mysql

service mysql restart

检测是否成功,在SSH中输入:

lsof -n|grep tcmalloc

出现相关进程即可

9,安装Nginx:

9.1,安装所需组件:

cd /usr/local/src
wget http://ncu.dl.sourceforge.net/sourceforge/pcre/pcre-8.10.tar.gz
tar zxvf pcre-8.10.tar.gz
cd pcre-8.10
./configure
make && make install

cd ..
rm -rf pcre-8.10
rm -rf /usr/bin/pcre-config
cp -a /usr/local/bin/pcre-config /usr/bin/
rm -rf /usr/lib/libpcre.a
cp -a /usr/local/lib/libpcre.a /usr/lib/

9.2,安装 Nginx 0.8.x:

cd /usr/local/src
wget -c http://nginx.org/download/nginx-0.8.53.tar.gz
tar zxvf nginx-0.8.53.tar.gz
cd nginx-0.8.53
./configure --user=www --group=www --prefix=/usr/local/nginx --pid-path=/usr/local/nginx/logs/nginx.pid --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --with-http_stub_status_module --with-http_ssl_module --http-client-body-temp-path=/tmp/nginx_client --http-proxy-temp-path=/tmp/nginx_proxy --http-fastcgi-temp-path=/tmp/nginx_fastcgi --with-http_gzip_static_module --with-google_perftools_module --with-ld-opt='-ltcmalloc_minimal'

make && make install
cd ../

编辑 nginx.conf 文件:

vi /usr/local/nginx/conf/nginx.conf

输入以下内容:

user www;
worker_processes 4;
pid logs/nginx.pid;

google_perftools_profiles /var/tmp/tcmalloc;

worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}

http{

include      mime.types;
default_type application/octet-stream;

access_log off;
error_log /dev/null;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;

sendfile on;
tcp_nopush    on;

keepalive_timeout 120;
#fastcgi_connect_timeout 300;
#fastcgi_send_timeout 300;
#fastcgi_read_timeout 300;
#fastcgi_buffer_size 64k;
#fastcgi_buffers 4 64k;
#fastcgi_busy_buffers_size 128k;
#fastcgi_temp_file_write_size 128k;
tcp_nodelay on;

gzip on;
gzip_min_length 1k;
gzip_buffers    4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types      text/plain application/x-javascript text/css application/xml;
gzip_vary on;

include vhost/*.conf;

}

在 /usr/local/nginx/conf 目录 新建proxy.conf 文件,并输入以下内容:

proxy_redirect         off;
proxy_set_header       Host $host;
proxy_set_header       X-Real-IP $remote_addr;
proxy_set_header       X-Forwarded-For  $proxy_add_x_forwarded_for;
client_max_body_size   50m;
client_body_buffer_size 256k;
proxy_connect_timeout  30;
proxy_send_timeout     30;
proxy_read_timeout     60;

proxy_buffer_size      4k;
proxy_buffers          4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;

#Nginx cache
client_body_temp_path client_body 1 2;
proxy_temp_path proxy_temp 1 2;

#client_body_temp_path     /tmpfs/client_body_temp 1 2;
#proxy_temp_path           /tmpfs/proxy_temp 1 2;
#fastcgi_temp_path         /tmpfs/fastcgi_temp 1 2;

建立 Nginx虚拟主机目录,把nginx加入到系统服务:

mkdir -p /usr/local/nginx/conf/vhost
chown www -R /home/www/
chown www /usr/local/nginx/conf/
chmod 777 /etc/init.d/nginx
chkconfig --level 345 nginx on
service mysql start
/etc/init.d/nginx start

编辑 /etc/rc.d/init.d/nginx 文件,覆盖为以下代码:

#! /bin/sh

ulimit -n 65535

# Description: Startup script for nginx

# chkconfig: 2345 55 25

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DESC="nginx daemon"

NAME=nginx

DAEMON=/usr/local/nginx/sbin/$NAME

CONFIGFILE=/usr/local/nginx/conf/nginx.conf

PIDFILE=/usr/local/nginx/logs/$NAME.pid

SCRIPTNAME=/etc/init.d/$NAME

set -e

[ -x "$DAEMON" ] || exit 0

do_start() {

$DAEMON -c $CONFIGFILE || echo -n "nginx already running"

}

do_stop() {

kill -QUIT `cat $PIDFILE` || echo -n "nginx not running"

}

do_reload() {

kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"

}

case "$1" in

start)

echo -n "Starting $DESC: $NAME"

do_start

echo "."

/etc/init.d/httpd start

;;

stop)

echo -n "Stopping $DESC: $NAME"

do_stop

echo "."

/etc/init.d/httpd stop

;;

reload)

echo -n "Reloading $DESC configuration..."

do_reload

echo "."

/etc/init.d/httpd restart

;;

restart)

echo -n "Restarting $DESC: $NAME"

do_stop

sleep 1

do_start

echo "."

/etc/init.d/httpd restart

;;

*)

echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2

exit 3

;;

esac

exit 0

#! /bin/shulimit -n 65535# Description: Startup script for nginx# chkconfig: 2345 55 25
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDESC="nginx daemon"NAME=nginxDAEMON=/usr/local/nginx/sbin/$NAMECONFIGFILE=/usr/local/nginx/conf/nginx.confPIDFILE=/usr/local/nginx/logs/$NAME.pidSCRIPTNAME=/etc/init.d/$NAME
set -e[ -x "$DAEMON" ] || exit 0
do_start() { $DAEMON -c $CONFIGFILE || echo -n "nginx already running"}
do_stop() { kill -QUIT `cat $PIDFILE` || echo -n "nginx not running"}
do_reload() { kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"}
case "$1" in start) echo -n "Starting $DESC: $NAME" do_start echo "." /etc/init.d/httpd start ;; stop) echo -n "Stopping $DESC: $NAME" do_stop echo "." /etc/init.d/httpd stop ;; reload) echo -n "Reloading $DESC configuration..." do_reload echo "." /etc/init.d/httpd restart ;; restart) echo -n "Restarting $DESC: $NAME" do_stop sleep 1 do_start echo "." /etc/init.d/httpd restart ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2 exit 3 ;;esac
exit 0

在ssh 中輸入:

chmod a+x /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

在Nginx虚拟主机目录 /usr/local/nginx/conf/vhost 建立一个虚拟主机:

新建文件:Default.conf 输入以下代码:

server
{
listen 80;
server_name    你的IP或域名;
index index.html index.htm index.php;
root   /home/www/;

location /nginx {
stub_status on;
auth_basic "NginxStatus";
# auth_basic_user_file conf/htpasswd;
#密码由apache的htpasswd工具来产生
access_log off;
}

location / {
location ~ .*\.(php|php5)?$ {
index index.php;
root /home/www/;
proxy_pass   http://127.0.0.1:81;
}

include proxy.conf;

if ( !-e $request_filename) {
proxy_pass http://127.0.0.1:81;
}

location ~* \.(jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
root /home/www/;
expires   30d;
break;
}
}
location ~* \.(js|css)$ {
if (-f $request_filename) {
root /home/www/;
expires   1d;
break;
}
}
}

error_page  500 502 503 504 /50x.html;
location = /50x.html {
root  html;
}

#如果需要记录把下面的注释去掉
# log_format access '$http_x_forwarded_for - $remote_user [$time_local] "$request"'
#    '$status $body_bytes_sent "$http_referer"'
#    '"$http_user_agent" $remote_addr';
# access_log   logs/IP_access.log   access;

}

以上全部保存完毕后,启动 nginx:

service nginx start

10,加速 Nginx 与 PHP

10.1,加速nginx:

mkdir /tmpfs

编辑/etc/fstab,加入:

tmpfs  /tmpfs   tmpfs  size=1024m,mode=1777  0 0

编辑/usr/local/nginx/conf/proxy.conf,将缓存目录均指向/tmpfs

client_body_temp_path     /tmpfs/client_body_temp 1 2;
proxy_temp_path           /tmpfs/proxy_temp 1 2;
fastcgi_temp_path         /tmpfs/fastcgi_temp 1 2;

此分区可视情况适当调整大小,此处设置为1G

2,加速php:

mkdir /tmpfs/eaccelerator
chmod 777 /tmpfs/eaccelerator

编辑php.ini

vi /etc/php.ini

eaccelerator.cache_dir="/tmp/eaccelerator"

改为:

eaccelerator.cache_dir="/tmpfs/eaccelerator"

注:/tmpfs重启后就没有了,eaccelerator需要手工创建。我们可以写一个创建eaccelerator目录的脚本:

新建并编辑文件:

vi /usr/local/eacc.sh

输入以下内容:

#!/bin/sh
/bin/mkdir /tmpfs/eaccelerator
/bin/chmod 777 /tmpfs/eaccelerator
/etc/init.d/httpd restart

然后在SSH中输入:

chmod a+x /usr/local/eacc.sh
echo "/usr/local/eacc.sh">>/etc/rc.local

为apache安装rpaf模块,该模块用于apache做后端时获取访客真实的IP.

1.使用apxs安装模块.这里要使用此前apache编译安装后的apxs

 

cd /usrlocal/src

wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz

tar -zxf mod_rpaf-0.6.tar.gz

cd mod_rpaf-0.6

/usr/local/apache2/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

2.编辑/usr/local/apache/conf/httpd.conf,添加模块参数,查找LoadModule php5_module modules/libphp5.so,在下方添加:

LoadModule rpaf_module modules/mod_rpaf-2.0.so
#Mod_rpaf settings
RPAFenable On
RPAFproxy_ips 127.0.0.1 [your_ips]
RPAFsethostname On
RPAFheader X-Forwarded-For

上面出现的[your_ips]请修改为你本机所监听web服务的ip.多个IP用空格空开.

到此,柒月自己定制的 LNAMP 就全部完成了,需要注意几点:

  1. 这个版本是未经任何优化的,可以根据自己需要去实行相应的优化;

  2. Apache + Nginx 有点麻烦的地方就是每次建立一个虚拟主机,都需要两边设置。不过为了其稳定性,还是麻烦下吧;

希望这个能给有需要的人带来帮助。