3月 12

nginx 0.8 安装 配置

【整理人:张子萌 最近一次修改2010-12】

nginx的官方网站:http://nginx.org/

1.下载nginx源码
nginx-0.8.34.tar.gz

如果希望在Nginx中使用正则表达式使配置更灵活,需要安装PCRE。
查看是否安装pcre
# rpm -qa pcre*

如果希望在是用ssl加密部分请安装openssl。

如果希望压缩传送需要安装gzip

如果使用nginx使用的缓存,需要清理指定url需要下载并同时编译。
http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz

推荐使用升级工具安装,升级工具使用可以参照:
http://simon-zzm.blog.163.com/blog/static/88809522201025102237958/

2. 编译安装nginx
首先创建账户,指定用户与组的id均为700
# groupadd -g 700 nginx
# useradd -g700 -u700 nginx
解压、安装nginx,对于不需要的模块可以不编译。
最后一行为清理指定url模块,可以选择性编译
# tar zxvf ngx_cache_purge-1.0.tar.gz
# tar zxvf nginx-0.8.34.tar.gz
# cd nginx-0.8.34
# ./configure –prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-pcre
–with-select_module
–with-poll_module
–with-http_stub_status_module
–with-http_ssl_module
–with-http_realip_module
–with-http_gzip_static_module
–add-module=/usr/local/src/ngx_cache_purge-1.0
# make && make install

3. nginx配置文件

主要配置文件:/usr/local/nginx/conf/nginx.conf
# 程序运行用户
user nginx nginx;
# 启动进程数
worker_processes 8;

#给每个进程分配cpu,例:将8个进程分配到8个cpu,也可以写多个,或将一个进程分配到多个cpu

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

# 全局错误日志和程序PID文件
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;

# 工作模式及连接数上限
# 工作模式有:select(标准模式),poll(标准模式),kqueue(高效模式,适用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
# poll(高效模式。适用Linux 2.6+,SuSE 8.2,Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
events {
use poll;
worker_connections 1024;
}

# 设定http服务器,反向代理功能提供负载均衡支持
http {
#设定mime类型
include mime.types;
default_type application/octet-stream;

# 隐藏nginx版本,防止攻击
server_tokens off;
# 设定日志格式
#log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
# ‘$status $body_bytes_sent “$http_referer” ‘
# ‘”$http_user_agent” “$http_x_forwarded_for”‘;
#设定access log
#access_log logs/access.log main;
#linux下强大的静态文件发送功能,一定要开启
sendfile on;
tcp_nopush on;

keepalive_timeout 60;
tcp_nodelay on;

# 定义一个叫“mysvr”的记录区,总容量为 10M
# 和下面location中的limit_conn一起限制单个IP的并发连接数为10
# limit_zone只能是用在http中,limit_conn可以使用在http、sever、local中
limit_zone mysvr $binary_remote_addr 10m;

#设定负载均衡的服务器列表
upstream mysvr {
ip_hash #如果不需要可以删除直接采用轮训方法负载
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.0.1:80 weight=5;
server 192.168.0.2:80 weight=1;
server 192.168.0.3:80 weight=6;
server 192.168.0.5:80;
server 192.168.0.6:80;

#down 表示单前的server暂时不参与负载
#weight 默认为1.weight越大,负载的权重就越大。
#max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
#fail_timeout:max_fails次失败后,暂停的时间。
#backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
}
# 设定缓存临时目录,临时目录要与proxy_cache_path中目录一样
proxy_temp_path /usr/local/nginx_test/proxy_temp;
# proxy_cache_path中参数如下
# levels指定该缓存空间有两层hash目录,第一层目录是1个字母,第二层为2个字母
# keys_zone为这个空间起个名字,比如 cache_one,在server模块中proxy_cache使用
# 200m 内存缓存空间大小为200MB
# inactive 1天没有被访问的内容自动清除;
# max_size使用硬盘缓存大小30G
# clean_time指定一分钟清理一次缓存。
proxy_cache_path /usr/local/nginx_test/proxy_temp/ levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

#设定虚拟主机,默认为监听80端口
server {
listen 80;
server_name 192.168.0.7 mysvr.c
om www.mysvr.com;
#charset gb2312;
#charset utf8;
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;

#对 “/” 启用负载均衡
location / {
proxy_pass http://mysvr;
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 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 90; #与后端连接超时时间
proxy_send_timeout 90; #后端服务器回传超时时间
proxy_read_timeout 90; #连成功后,后端服务器响应超时时间。
proxy_buffer_size 4k; #代理服务器保存用户头信息的缓存大小
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k; #高负荷缓村大小。大小计算方法proxy_buffers*2
proxy_temp_file_write_size 64k; #设定缓存文件大小,大于这个值经直接从upstream获得。
limit_conn mysvr 10; #每个IP只能有10个并发
limit_rate 100k; #每个并发只能有100K的速度
index index.html index.htm; #选则文件后缀解析都优先顺序
expires 30d; #客户端缓存时间 30天

}

#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus

3月 10

linux 下用 cronolog 切割 apache日誌

cronolog安装可见http://simon-zzm.blog.163.com/blog/static/88809522201022112040647/

apache安装位置:/usr/local/apache/

编辑配置文件httpd.conf

LogFormat “%v %h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” mylog
LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” combined
LogFormat “%h %l %u %t “%r” %>s %b” common


LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i” %I %O” combinedio

CustomLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log” combined

如果使用虚拟机可以使用以下配置

httpd-vhosts.conf

ErrorLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log”
CustomLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log” common

3月 08

apache+php+mysql+cronolog+Zend Optimizer整理

【收集整理:张子萌 2010-03-12】

操作系统centos

概要说明:
在centos操作系统下安装,apache+php+mysql+cronolog+Zend Optimizer。网上大部分文档会写明操作系统
和安装各个包的版本。本文档中只是建议版本,因为有些软件版本找起来不是很容易,可以使用其它
版本代替。

1 首先安装apache
# ./configure –prefix=/usr/local/apache –with-included-apr \
–enable-so –enable-deflate=shared –enable-expires=shared \
–enable-rewrite=shared –enable-static-support –disable-userdir \
–enable-rewrite

#make && make install

问题1:
checking for chosen layout… apr
checking for gcc… no
checking for cc… no
checking for cl.exe… no
configure: error: in `/usr/local/src/httpd-2.2.11/srclib/apr’:
configure: error: no acceptable C compiler found in $PATH
See `config.log’ for more details.
configure failed for srclib/apr
编译失败,说明gcc安装不完全
# yum install binutils*
# yum install gcc*
# yum install automake*

问题2
configure: error: mod_deflate has been requested but can not be built due to prerequisite failures
安装zlib-devel*

2.安装mysql

mysql安装一般分为两种:一种为源代码编译安装,一种为直接使用二进制安装。
(本人推荐使用二进制方式安装)

2.1 使用源码编译(简化步骤,主要来源网上整理,个人未测试)。
# tar zxvf mysql-5.0.45.tar.gz
# cd mysql-5.0.45
# ./configure \
–prefix=/usr/local/mysql \
–sysconfdir=/etc \
–enable-thread-safe-client \
–enable-local-infile \
–enable-static \
–enable-assembler \
–enable-thread-safe-client \
–with-charset=utf8 \ //修改默认字符
–with-extra-charset=all \
–with-low-memory \
–with-comment=Source \
–with-client-ldflags=-all-static \
–with-server-suffix=-Comsenz \
–with-mysqld-user=mysql \
–without-debug \
–without-isam \
–without-innodb \
–without-ndb-debug \
–with-big-tables \
–with-apxs=/usr/local/apache/bin/apxs \
–localstatedir=/var/lib/mysql
# make && make install

2.2使用二进制文件安装

2.2.1 创建用户和用户组

# groupadd mysql

# useradd -g mysql mysql

2.2.2 解压二进制文件

# tar zxvf mysql-5.1.44-linux-i686-glibc23.tar.gz

将解压后的文件mv到/usr/local下并改名为mysql

拷贝配置和启动文件

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

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

2.2.3 初始化mysql库

/usr/local/mysql/scripts/mysql_install_db –user=mysql

可能遇到的问题:
FATAL ERROR: Could not find ./bin/my_print_defaults
If you are using a binary release, you must run this script from
within the directory the archive extracted into. If you compiled
MySQL yourself you must run ‘make install’ first.

需要在启动时多加两个参数,如下:
/usr/local/mysql/scripts/mysql_install_db –user=mysql \
–basedir=/usr/local/mysql –datadir=/usr/local/mysql/data

启动数据库查看是否启动成功

# /etc/init.d/mysql start

mysql的基本路径和数据存放路径可以在/etc/init.d/mysql 中修改。如果没有修改则使用脚本默认地址

basedir=/usr/local/mysql ,datadir=/usr/local/mysql/data。

2.2.4 设置权限

# chown -R mysql:mysql /usr/local/mysql(所属mysql用户)

2.2.5 启动服务器并测试

启动服务
# /usr/local/mysql/bin/mysqld_safe –user=mysql &

登录服务
# /usr/local/mysql/bin/mysql -u mysql

如果出现:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 210
Server version: 5.1.44-log MySQL Community Server (GPL)

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buff

mysql>
出现’mysql>’表示安装成功。

2.2.6 远程连接mysql服务器

测试连接远程服务器,如果远程服务器不能连接请检查以下内容:

如果使用防火墙请开通了3306端口

还无法远程连接mysql服务器的话就做如下操作:

登陆mysql,使用update把mysql.user中的root的host从localhost修改成%

mysql> UPDATE mysql.user SET Host=’%’ WHERE Host=’localhost’ ;

mysql> GRANT ALL PRIVILEGES ON *.* TO ;

mysql> flush privileges;

测试远程登录可否成功。

2.2.7 修改密码

登录mysql服务器

mysql> update user set password=password(‘newpassword) where user=’root’;

或者

2.2.8 添加服务随服务器启动

# chkconfig –add mysql

# chkconfig –level 35 mysql on

3.安装环境需要以下包,可以通过源码包安装,也可以使用centos系统自带的yum工具安装(本人推
荐使用yum工具安装,但是部分包需要下载。源码安装注意安装顺序)。

3.1 使用源码安装,请下载以下包。
freetype-2.3.5.tar.gz
libiconv-1.11.tar.gz
libmcrypt-2.5.7.tar.gz
libpng-1.2.10.tar.bz2
jpegsrc.v6b.tar.gz
gd-2.0.35.tar.gz (建议源码包安装)
mhash-0.9.9.9.tar.bz2 (建议源码包安装)
httpd-2.2.11.tar.gz (建议源码包安装)
mysql-5.1.44-linux-i686-glibc23.tar.gz (建议源码包安装)
php-5.3.1.tar.bz2 (建议源码包安装)

3.1.1 安装 freetype
# tar -zxvf freetype-2.3.5.tar.gz
# cd freetype-2.2.1
# ./configure
# make && make install

3.1.2 安装:libpng
# tar jxvf libpng-1.2.10.tar.bz2
# cd libpng-1.2.20/
# ./configure
# make && make install

3.1.3 安装libiconv
# tar zxvf libiconv-1.11.tar.gz
# cd libiconv-1.11/
# ./configure
# make && make install

3.1.4 安装:jpeg6
建立目录
# mkdir /usr/local/jpeg6
# mkdir /usr/local/jpeg6/bin
# mkdir /usr/local/jpeg6/lib
# mkdir /usr/local/jpeg6/include
# mkdir /usr/local/jpeg6/man
# mkdir /usr/local/jpeg6/man/man1
# tar -zxvf jpegsrc.v6b.tar.gz
# cd jpeg-6b
# ./configure –prefix=/usr/local/jpeg6/ –enable-shared –enable-static
# make && make install
注意:这里./configure一定要带–enable-shared参数,不然不会生成共享库

3.1.5 安装 gd
# tar -zxvf gd-2.0.35.tar.gz
# cd gd-2.0.35
# ./configure
# make && make install

注意:查看图片安装程序时候正常

问题1
configure.ac:64: error: possibly undefined macro: AM_ICONV
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
make: *** [configure] Error 1
安装yum install gettext*

3.1.6 安装 libxml2
# tar zxvf libxml2-2.6.30.tar.gz
# cd libxml2-2.6.30/
# ./configure –prefix=/usr/local/libxml2
# make && make install

问题1
/bin/rm: cannot remove `libtoolT’: No such file or directory
安装libtool包

问题2
configure: error: C++ preprocessor /lib/cpp fails sanity check
See `config.log’ for more details.
安装gcc编译器
#yum install gcc-c++

3.1.7 安装:libmcrypt
# tar zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8/
# ./configure –prefix=/usr/local/libmcrypt
# make && make install
# /sbin/ldconfig
# cd libltdl/
# ./configure –enable-ltdl-install
# make && make install

3.1.8 安装mhash
# bunzip2 mhash-0.9.9.9.tar.bz2
# tar xvf mhash-0.9.9.9.tar
# cd mhash-0.9.9.9/
# ./configure
# make && make install

3.1.9 安装mcrypt
# tar zxvf mcrypt-2.6.6.tar.gz
# cd mcrypt-2.6.6/
# ./configure
# make && make install

3.2 使用yum安装
# yum install flex
# yum install libpng*
# yum install libjpeg*
# yum install freetype*
# yum install Fontconfig*
# yum install xpm*
# yum install libxml2*
# yum install gettext*
# yum install libxml2
# yum install libtool*
# yum install gcc*
# yum install gd*

3.3 编译安装PHP

3.3.1.安装fpm,php的一个补丁。FastCGI FastCGI的缩写,本补丁提供高速的脚本语言间通迅的接口。

#wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.3-fpm-0.5.13.diff.gz

# gzip -cd php-5.2.3-fpm-0.5.13.diff.gz | patch -d php-5.2.3 -p1

3.3.2 开始编译

我将fpm和apache的支持模块都编译进去

# cd /usr/local/src
# tar xjvf php-5.2.3.tar.bz2
# cd php-5.2.3
# ./configure \
–prefix=/usr/local/php \
–with-apxs2=/usr/local/apache/bin/apxs \
–with-config-file-path=/usr/local/php/etc \
–with-mysql=/usr/local/mysql/ \
–with-libxml-dir=/usr/local/libxml2 \
–with-jpeg-dir \
–with-png-dir \
–with-bz2 \
–without-iconv \
–with-freetype-dir \
–enable-fpm \
–with-mcrypt \
–enable-soap \
–enable-gd-native-ttf \
–enable-ftp \
–enable-mbstring \
–enable-exif \
–disable-cgi \
–with-gd\
–with-openssl=/usr/local/openssl
注意:如果不用加密连接,最后一行可以不用,如需使用请安装openssl。
enable-fpm是FastCGI Process Manager,是一个php的补丁,管理
php的进程整合,并且在nginx和apache连接可以节省系统资源。
–enable-memcache memcache为集中缓存服务,需要安装memchach,只是提高读取,压力不大也可以不要。 libxml-dir如是yum安装也可以不用。
# make
# make install
# mkdir /usr/local/php/etc
# cp php.ini-dist /usr/local/php/etc/php.ini 或拷贝php.ini-development、php.ini-production

问题1
checking lex output file root… ./configure: line 2554: lex: command not found
# yum install flex*

问题2

configure: error: Try adding –with-zlib-dir=

首先确认是否安装zlib-devel,如果有安装,则在编译时添加–with-zlib-dir=/usr/lib

问题3

configure: error: mysql configure failed.

检查mysql的版本是否与操作系统匹配。在64位系统上使用32位数据库会有此问题。如果是使用rpm包安装,可以将

lib部分做软连接到/usr/local/mysql.

问题4

make是出现错误undefined reference to libiconv_open

编辑Makefile,找到 EXTRA_LIBS = ….. -lcrypt行,在最后加上 -liconv,保存后重新编译。

或在 make 编译时 使用make ZEND_EXTRA_LIBS=’-liconv’ 进行编译即可

注:在当前版本php中没有–enable-memory-limit这个参数。

4. 安装Zend Optimizer
# cd /usr/local/src
# tar xzvf ZendOptimizer-3.2.8-linux-glibc21-i386.tar.gz
# ./ZendOptimizer-3.2.8-linux-glibc21-i386/install.sh

注意:安装Zend Optimizer过程的最后不要选择重启Apache。

5. 整合Apache与PHP
5.1 编辑配置apache配置
# vi /usr/local/apache/conf/httpd.conf
找到:
AddType application/x-gzip .gz .tgz
在该行下面添加
AddType application/x-httpd-php .php

找到:

DirectoryIndex index.html

将该行改为

DirectoryIndex index.html index.htm index.php

找到:
# Include conf/extra/httpd-mpm.conf
# Include conf/extra/httpd-info.conf
# Include conf/extra/httpd-vhosts.conf
# Include conf/extra/httpd-default.conf
去掉前面的“#”号,取消注释。
注意:以上 4 个扩展配置文件中的设置请按照相关原则进行合理配置!

修改完成后保存退出。
# /usr/local/apache2/bin/apachectl restart

5.2 验证apache与php整合是否成功
apache所指向的根目录下创建phpinfo.php页面。
# vi phpinfo.php

通过浏览器:http://x.x.x.x/phpinfo.php
查看确认整合和成功,并确认安装的各种模块成功。

5.3 确认 PHP 能够正常工作后,在 php.ini 中进行设置提升 PHP 安全性。
编辑 php.ini
找到:
disable_functions =
设置为:

passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_open,proc_get_status,error_log,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,leak,popepassthru,stream_socket_server

找到

expose_php = On

修改为:

expose_php = Off

6. 安装cronolog切割日志

6.1 安装cronolog
# tar zxvf cronolog-1.6.2.tar.gz
# ./configure
# make && make install

6.2 配置
# vi /usr/local/apache/conf/httpd.conf
找到
CustomLog “logs/access_log” common
修改为
CustomLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access_log.%Y%m%d” common

6.3 重启apache确认日志分割成功

3月 02

linux 下用 cronolog 切割 tomcat catalina日誌

http://cronolog.org/下载cronolog软件。

cronolog-1.6.2.tar.gz

上传到服务器,操作如下:

# tar zxvf cronolog-1.6.2.tar.gz

# cd cronolog-1.6.2

# ./configure

# make && make install

如果安裝正常,cronolog安装完毕。开始修改tomcat。

编辑bin/catalina.sh文件。

首先找到“touch “$CATALINA_BASE”/logs/catalina.out”并注释掉,并编辑如下

elif [ “$1” = “start” ] ; then

shift
#touch “$CATALINA_BASE”/logs/catalina.out
if [ “$1” = “-security” ] ; then
echo “Using Security Manager”
shift
“$_RUNJAVA” “$LOGGING_CONFIG” $JAVA_OPTS $CATALINA_OPTS
-Djava.endorsed.dirs=”$JAVA_ENDORSED_DIRS” -classpath “$CLASSPATH”
-Djava.security.manager
-Djava.security.policy==”$CATALINA_BASE”/conf/catalina.policy
-Dcatalina.base=”$CATALINA_BASE”
-Dcatalina.home=”$CATALINA_HOME”
-Djava.io.tmpdir=”$CATALINA_TMPDIR”
org.apache.catalina.startup.Bootstrap “$@” start 2>&1
| /usr/local/sbin/cronolog “$CATALINA_BASE”/logs/catalina.%Y-%m-%d.out >>/dev/null &
# org.apache.catalina.startup.Bootstrap “$@” start
# >> “$CATALINA_BASE”/logs/catalina.out 2>&1 &

if [ ! -z “$CATALINA_PID” ]; then
echo $! > $CATALINA_PID
fi
else
“$_RUNJAVA” “$LOGGING_CONFIG” $JAVA_OPTS $CATALINA_OPTS
-Djava.endorsed.dirs=”$JAVA_ENDORSED_DIRS” -classpath “$CLASSPATH”
-Dcatalina.base=”$CATALINA_BASE”
-Dcatalina.home=”$CATALINA_HOME”
-Djava.io.tmpdir=”$CATALINA_TMPDIR”
org.apache.catalina.startup.Bootstrap “$@” start 2>&1
| /usr/local/sbin/cronolog “$CATALINA_BASE”/logs/catalina.%Y-%m-%d.out >>/dev/null &
# org.apache.catalina.startup.Bootstrap “$@” start
# >> “$CATALINA_BASE”/logs/catalina.out 2>&1 &

if [ ! -z “$CATALINA_PID” ]; then
echo $! > $CATALINA_PID
fi
fi

重新启动tomcat,日志分割格式如下:

catalina.yyyy-mm-dd.out

cronolog 安装后所在位置,系统版本不同可能位置不一样,以 which 或whereis命令查看到的位置为准。