nginx+apache实现动静态页面分离的web环境(一)

  前几天写了nginx+php(FastCGI)环境搭建的文章。搭建完环境压力测试之后发现,访问页面经常出现“502 ”的错误。不知道是php(FastCGI)的配置不合理,还是环境本身就有缺陷。在网上百度了一下,发现nginx结合apache的环境比较好,可以发挥apache处理动态页面的优势,同时利用nginx处理静态页面强大的功能,实现动静页面分离的效果。
   apache可以整合大多数应用,比如jsp,php,cgi,python等等,但是apache过于臃肿以及对静态文件响应过于缓慢让很多使用者感到头疼。nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。它能够支持高达50 000个并发连接数的响应,运行稳定,且内存、CPU等系统资源消耗非常低。nginx的优势在于高性能的处理静态页面。下面是我总结的环境搭建的过程,在这里贴出来,和大家共享一下。
系统、软件包及环境简介:
系统Centos 5.5  、 php 5.2 、mysql 5.1、apache 2.2、nginx 0.8 
1.Php支持库及扩展: libiconv-1.13.1.tar.gz     libevent-2.0.10-stable.tar.gz     ibmcrypt-2.5.8.tar.gz    mhash-0.9.9.9.tar.gz         mcrypt-2.6.8.tar.gz    gd-2.0.35.tar.gz    eaccelerator-0.9.6.1.tar.bz2   ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz  PDO_MYSQL-1.0.2.tgz        suhosin-0.9.32.1.tar.gz   freetds-0.82.tar.gz
2.Mysql所需软件包:  google-perftools-1.7.tar.gz   mysql-5.1.45.tar.gz    
3.Apache 所需软件包: httpd-2.2.17.tar.gz
4.Nginx所需软件包:pcre-8.12.tar.gz     nginx-0.8.54.tar.gz    mod_rpaf-0.6.tar.gz
当然还需要其他的一些安装包,我会在下面的更新中给出。
一、最小化安装Centos系统,删除不必要的程序,并安装相关组件。
yum -y remove httpd mysql php
yum -y install yum-fastestmirror
yum -y update
yum -y install patch make gcc gcc-c++ gcc-g77 flex bison tar libtool libtool-libs kernel-devel autoconf213  libtiff libtiff-devel gettext gettext-devel freetype freetype-devel zlib zlib-devel
libxml2 libxml2-devel file glib2 glib2-devel bzip2 diff* openldap-devel  bzip2-devel vim-minimalunzip
ncurses ncurses-devel curl curl-devel e2fsprogs  e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel 

export PHP_AUTOCONF=/usr/bin/autoconf-2.13
export PHP_AUTOHEADER=/usr/bin/autoheader-2.13
二、安装php 5.2所支持的库
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-2.0.10-stable.tar.gz
tar zxvf libevent-2.0.10-stable.tar.gz
cd libevent-2.0.10-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 ../
安装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
cd  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

三、安装 Mysql:
1)先安装Google的开源TCMalloc库,可以提高MySQL在高并发情况下的性能
cd /usr/local/src
wget -chttp://google-perftools.googlecode.com/files/google-perftools-1.7.tar.gz
tar zxvf  google-perftools-1.7.tar.gz
cd google-perftools-1.7/
./configure
make && make install
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig
cd ../
2)开始安装Mysql

groupadd mysql -g 27
useradd mysql -u 27 -g 27 -c "MySQL Server" -d /var/lib/mysql -m
cd /usr/local/src
tar -zxf mysql-5.1.45.tar.gz
cd mysql-5.1.45
./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-unix-socket-path=/var/lib/mysql/mysql.sock --with-mysqld-user=mysql --enable-assembler --enable-thread-safe-client --with-extra-charsets=all --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innodb_plugin,myisam,myisammrg
make && make install
cd ../
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
/usr/local/mysql/bin/mysql_install_db --user=mysql
chown -R mysql.mysql /var/lib/mysql
chgrp -R mysql /usr/local/mysql/.
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod u+x /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
ln -s /usr/local/mysql/bin/mysql_config /usr/bin/mysql_config
service mysql start
/usr/local/mysql/bin/mysqladmin -u root password wangwang(红色部分为你的数据库的密码)
service mysql restart
3)设置MySQL:
打开“/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服务。
service mysql restart

四、安装Apache:
1)下载Apache并解压缩:
cd /usr/local/src
groupadd www
useradd -g www -s /sbin/nologin -M www
wget -c  http://www.apache.org/dist//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 20000
define DEFAULT_THREAD_LIMIT 64
define MAX_THREAD_LIMIT 20000
以上数值据说改小后,能减低服务器消耗。不过柒月修改后,发现没什么实质变化。
2)安装Apache 2.2
cd /usr/local/src/httpd-2.2.17
./configure --prefix=/usr/local/apache2 --enable-headers --enable-so --enable-rewrite --with-mpm=prefork --disable-userdir --disable-cgid --disable-cgi
make && make install
如果你需要编译apache其他功能,可以自行增加。 不过在这里,我们只是用Apache作为后端并处理伪静态,无需添加过多设置来浪费内存
配置源代码树:
--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 的调用加入到你的系统启动文件中。
五、安装 PHP5.2.x:
1)下载PHP 并解压缩:
安装php支持连接mssql
# tar zxvf freetds-0.82.tar.gz
# cd freetds-0.82
//--with-tdsver=8.0是指安装tds 8.0版本(如果没有加这个参数,则按照默认编译为5.0。5.0连接数据库的端口是4000,而不是SQLServer的1433)
# ./configure --prefix=/usr/local/freetds --with-tdsver=8.0
# make
# make install
#vi /etc/ld.so.conf 设置系统动态库配置文件,加入以下:
/usr/local/freetds/lib
保存退出。
#ldconfig 重新加载动态库列表ld.so.conf
#生成两个文件,后面PHP编译时需要
#touch /usr/local/freetds/include/tds.h
#touch /usr/local/freetds/lib/libtds.a
cd /usr/local/src
wget -c  http://us.php.net/distributions/php-5.2.17.tar.gz
tar zxvf php-5.2.17.tar.gz
cd php-5.2.17
./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 --disable-cli --disable-cgi --disable-debug  --with-mssql=/usr/local/freetds
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /etc/php.ini
#vi  /etc/php.ini
mssql.charset = "GBK"   //修改MSSQL默认编码,把前面的“;”去掉。
2)安装PHP扩展:
2.1)安装eaccelerator加速软件
eaccelerator是php的加速软件,使用后php的执行效率会有很大幅度的提升。
cd /usr/local/src
wget  http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2
tar -jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1
/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
2.2)安装Zend(如果程序没要求,尽量不要装Zend,容易和eA产生冲突):
cd /usr/local/src
wgethttp://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"
2.3)安装PDO_MYSQL
cd /usr/local/src
wget -chttp://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"
2.4)安装 suhosin:
cd /usr/local/src
wget -chttp://download.suhosin.org/suhosin-0.9.32.1.tar.gz
tar -zxvf suhosin-0.9.32.1.tar.gz
cd suhosin-0.9.32.1
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
继续修改 php.ini 文件
vi /usr/local/php/php.ini
查找代码:
extension="pdo_mysql.so"
在以上代码下面加上:
extension="suhosin.so"

六、Apache 整合 PHP 以及相关优化:
1)Apache设置:
mkdir /home/www
编辑 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 改为你自己的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
这样以后Apache 启动、关闭、重启只需要输入以下命令:
service httpd start/stop/restart
2)整合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
DocumentRoot "/home/www"
ServerName  192.168.1.100                     //修改为你主机的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 是否成功显示了?
七、优化MySQL
通过Tcmalloc 优化mysql性能
# vi /etc/init.d/mysql
添加一行
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
重新启动mysql
service mysql restart
检测是否成功,在SSH中输入:
lsof -n|grep tcmalloc
出现相关进程即可

八、安装Nginx:
1)安装所需组件:
cd /usr/local/src

tar -zxvf pcre-8.12.tar.gz
cd pcre-8.12/
./configure
make && make install
cd ../
rm -rf pcre-8.12
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/
2)安装 Nginx :
cd /usr/local/src
wget -c http://nginx.org/download/nginx-0.8.54.tar.gz
tar zxvf nginx-0.8.54.tar.gz
cd nginx-0.8.54
./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' --with-ipv6
make && make install
编辑 nginx.conf 文件:
vi /usr/local/nginx/conf/nginx.conf
输入以下内容:

#运行用户
user  www;

#启动进程
worker_processes  1;
worker_rlimit_nofile  65535;

#工作模式及连接数上限   
events {
 worker_connections  65535;
 use epoll;
}
error_log  /usr/local/nginx/logs/error.log info;

#设定http服务器,利用它的反向代理功能提供负载均衡支持  
http {
 include    mime.types;

 #设定mime类型  
 default_type  application/octet-stream;
 #charset  gb2312;

#设定请求缓冲   
 server_names_hash_max_size 2048;
 server_names_hash_bucket_size 256;
 client_header_buffer_size 256k;
 client_max_body_size    100m;
 large_client_header_buffers 4 256k;
 sendfile on;
 tcp_nopush on;
 server_tokens off;
 tcp_nodelay on;
 proxy_send_timeout   300;
 proxy_read_timeout   300;
 proxy_buffer_size    4k;
 proxy_buffers     16 32k;
 proxy_busy_buffers_size 64k;
 proxy_temp_file_write_size 64k;
 proxy_connect_timeout 30s;
 keepalive_timeout  10;

#开启gzip模块
 gzip on;
 gzip_http_version 1.0;
 gzip_min_length  1100;
 gzip_comp_level  3;
 gzip_buffers  4 32k;
 gzip_types    text/plain text/xml text/css application/x-javascript application/xml application/xml+rss text/javascript application/atom+xml;
 ignore_invalid_headers on;
 client_header_timeout  3m;
 client_body_timeout 3m;
 send_timeout     3m;
 connection_pool_size  256;
 request_pool_size  32k;
 output_buffers   4 64k;
 postpone_output  1460;
 open_file_cache max=1000 inactive=300s;
 open_file_cache_valid    600s;
 open_file_cache_min_uses 2;
 open_file_cache_errors   off;
 include "/usr/local/nginx/conf/vhosts/*.conf";

#设定虚拟主机
 server {
 listen 80;
 server_name 192.168.1.100;
 root /home/www/;                     #设定网站的资源存放路径  
 index   index.html index.htm;    #设定访问的默认首页地址
 access_log off;
 if (-d $request_filename)
 {
 rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
 }

#所有php的页面均交由apache处理
 location ~ \.(php)?$ {
 proxy_set_header  Host $host;
 proxy_set_header  X-Real-IP  $remote_addr;
 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_pass http://192.168.1.100:81;
    }
  #  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;
 location ~* \.(ftpquota|htaccess|asp|aspx|jsp|asa|mdb)$ {
 deny  all;
 }

#设定查看Nginx状态的地址

location /NginxStatus {
stub_status on;

access_log off;

 #定义访问日志的写入格式,如果需要关闭nginx的访问日志就把一下的日志内容注释掉。       

    log_format  wwwlog  '$remote_addr - $remote_user [$time_local] "$request" '                

       '$status $body_bytes_sent "$http_referer" '               

       '"$http_user_agent" $http_x_forwarded_for';      

    access_log  /home/www/nginx.log  wwwlog;        #设定访问日志的存放路径    

   }  
 }
}
3)设置Nginx的启动,停止方式为:service  nginx  start/stop/status/restart

一般的启动方式为:/usr/local/nginx/sbin/nginx/

重启方式为:/usr/local/nginx/sbin/nginx/  -s  reload

编辑 /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 777 /etc/init.d/nginx
chmod a+x /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
九、为apache安装rpaf模块,该模块用于apache做后端时获取访客真实的IP(建议在LNAMP最后安装。。。之前安装有时会出错)
1)使用apxs安装模块.这里要使用此前apache编译安装后的apxs
cd /usr/local/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)vi  /usr/local/apache2/conf/httpd.conf       // 编辑/usr/local/apache2/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 192.168.1.100 [your_ips]
RPAFsethostname On
RPAFheader X-Forwarded-For
上面出现的[your_ips]请修改为你本机所监听web服务的ip.多个IP用空格空开.
Apache、Nginx、MySQL 启动/重启/关闭命令:
service mysql start/stop/restart
service httpd start/stop/restart
service nginx start/stop/restart

转载于:https://my.oschina.net/liting/blog/405972

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值