Web Server/Apache/Nginx & 技术 28 Jul 2008 06:33 pm

nginx+php(FCGI)+xcache+mysql on as4

参考
http://blog.s135.com/read.php/351.htm

http://31543.blog.51cto.com/21543/83964

linux为redhat as4 全新安装,不带默认万维网服务,带开发工具和系统工具,加上mrtg、net-snmp-utils、sysstat

mkdir src
cd src
vi lemp_down_list
<coolcode>
http://www.zlib.net/zlib-1.2.3.tar.gz
http://download.savannah.gnu.org/releases/freetype/freetype-2.3.5.tar.gz
ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng-1.2.31.tar.gz
ftp://xmlsoft.org/libxml2/libxml2-2.6.32.tar.gz
ftp://xmlsoft.org/libxml2/libxslt-1.1.24.tar.gz
ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz
http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz
http://fontconfig.org/release/fontconfig-2.6.0.tar.gz
http://www.libgd.org/releases/gd-2.0.35.tar.bz2
http://ftp.gnu.org/pub/gnu/gettext/gettext-0.17.tar.gz
http://mirror.optus.net/sourceforge/m/mc/mcrypt/libmcrypt-2.5.8.tar.gz
http://mirror.x10.com/mirror/mysql/Downloads/MySQL-5.1/mysql-5.1.26-rc.tar.gz
http://www.monkey.org/~provos/libevent-1.4.5-stable.tar.gz
http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
http://www.php.net/get/php-5.2.6.tar.gz/from/this/mirror
http://php-fpm.anight.org/downloads/head/php-5.2.6-fpm-0.5.8.diff.gz
http://pecl.php.net/get/memcache-2.2.3.tgz
http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
</coolcode>

wget -i lemp_down_list 下载

A1、安装zlib
tar xzvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure –prefix=/usr/local/zlib
make
make install
A2、安装freetype
tar xzvf freetype-2.3.5.tar.gz
cd freetype-2.3.5
./configure –prefix=/usr/local/freetype
make
make install
A3、安装libpng
tar xzvf libpng-1.2.29.tar.gz
tar xzvf libpng-1.2.31.tar.gz
cd libpng-1.2.29
cd libpng-1.3.31
cp scripts/makefile.std makefile
make test
make install

A4、# tar -zxf libxml2-2.6.32.tar.gz
# cd libxml2-2.6.32
# ./configure –prefix=/usr/local/libxml
# make; make install
echo ‘/usr/local/libxml/lib’  >> /etc/ld.so.conf
ldconfig -v

A5、安装 libxslt
# tar -zxf libxslt-1.1.24.tar.gz
# cd libxslt-1.1.24
# ./configure –prefix=/usr/local/libxslt –with-libxml-prefix=/usr/local/libxml
# make; make install

A6、安装jpeg
# mkdir -p /usr/local/jpeg6/{,bin,lib,include,man/man1,man1}
# tar xzvf jpegsrc.v6b.tar.gz
# cd jpeg-6b
# ./configure –prefix=/usr/local/jpeg6 –enable-shared –enable-static
# make

如果安装提示没有libtool,先安装libtool
然后进入jpeg-6b的源码目录,然后执行以下步骤,
# make install

在64位系统安装时使用以下命令
# CFLAGS=”-O3 -fPIC” ./configure –prefix=/usr/local/jpeg6 –enable-shared –enable-static
# make
# make install
# make install-lib

如果安装PHP5,必需安装libxml2

A7 安装iconv
tar zxvf libiconv-1.12.tar.gc
cd libiconv-1.12
./configure –prefix=/usr/local/libiconv
make
make install
ln -s /usr/local/libiconv/lib/* /usr/lib
ldconfig -v

A8安装fontconfig
tar xzvf fontconfig-2.5.0.tar.gz
tar xzvf fontconfig-2.6.0.tar.gz
cd fontconfig-2.5.0
cd fontconfig-2.6.0
./configure –prefix=/usr/local/fontconfig –disable-docs –sysconfdir=/etc  –mandir=/usr/share/man –with-freetype-config=/usr/local/freetype/bin/freetype-config
错误:
/usr/local/fontconfig/bin/fc-cache: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory
解决:
ln -s /usr/local/lib/libionv.so.2 /usr/lib/libiconv.so.2

错误:
checking for LIBXML2… configure: error: The pkg-config script could not be found or is too old.  Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.
解决:
wget http://pkgconfig.freedesktop.org/releases/pkg-config-0.23.tar.gz
tar zxvf pkg-config-0.23.tar.gz
cd pkg-config-0.23
./configure
make
make install

错误:
fcxml.c: In function `FcConfigMessage’:
fcxml.c:484: error: `va_start’ used in function with fixed args
解决:
export LIBXML2_CFLAGS=-I/usr/local/libxml/include/libxml2
export LIBXML2_LIBS=/usr/local/libxml/lib/libxml2.so
编译参数最后加 –enable-libxml2

A9、安装GD
tar xzvf gd-2.0.35.tar.gz
cd gd-2.0.35
 ./configure –prefix=/usr/local/gd2 –with-jpeg=/usr/local/jpeg6/ –with-png –with-zlib –with-freetype=/usr/local/freetype/ –with-fontconfig=/usr/local/fontconfig
make
make install
错误:
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.
解决:
cd ..
tar zxvf gettext-0.17.tar.gz
cd gettext-0.17
./configure –prefix=/usr/local/gettext –disable-java –disable-native-java
make
make install
/bin/sed: can’t read /lib/libattr.la: No such file or directory
ln -s /usr/lib/* /lib/
再安装gettext
再安装gd

X86 64位LINUX下安装GD的注意事项

错误提示:
/usr/bin/ld: /usr/local/lib/libjpeg.a(jcapimin.o): relocation R_X86_64_32 against `a local symbol’ can not be used when making a shared object; recompile with -fPIC

进入Jpeg目录
CFLAGS=”-O3 -fPIC” ./configure
make
make install-lib

编译前需指定为64位编译模式,否则会出现以下错误:
/usr/bin/ld: /usr/local/lib/libz.a(crc32.o): relocation R_X86_64_32against `a local symbol’ can not be used when making a shared object;recompile with -fPIC
/usr/local/lib/libz.a: could not read symbols: Bad value
解决办法 : 重新安装 zlib-1.2.3.tar.gz

tar -zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure

vi Makefile
找到 CFLAGS=-O3 -DUSE_MMAP
在后面加入-fPIC,即变成CFLAGS=-O3 -DUSE_MMAP -fPIC
接下面步骤

make
make install

nfig.so -L/usr/lib64 /usr/local/freetype/lib/libfreetype.so -lpng -lz -lm  -Wl,–rpath -Wl,/usr/local/freetype/lib -Wl,-soname -Wl,libgd.so.2 -o .libs/libgd.so.2.0.0
/usr/bin/ld: /usr/local/lib/libpng.a(png.o): relocation R_X86_64_32 against `a local symbol’ can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libpng.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
重新安装libpng;删除解压文件,重新解压
cp scripts/makefile.linux makefile
vi Makefile
找到 CFLAGS=后加上-fPIC
make test
make install

再重新安接gd;
vi Makefile
找到 CFLAGS=后加上-fPIC

A10 安装 libmcrypt
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure –prefix=/usr/local/libmcrypt
make
make install

mysql安装
http://dev.mysql.com/doc/refman/5.1/en/index.html
http://dev.mysql.com/doc/refman/5.1/en/installing-source.html

<coolcode>
/usr/sbin/groupadd website # 管理组
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql  -d /dev/null -s /sbin/nologin
tar zxvf mysql-5.1.26-rc.tar.gz
cd mysql-5.1.26-rc/
CFLAGS=”-O3″ CXX=gcc CXXFLAGS=”-O3 -felide-constructors -fno-exceptions -fno-rtti” ./configure –prefix=/opt/mysql –localstatedir=/opt/mysql/var –sysconfdir=/opt/mysql –without-debug –with-unix-socket-path=/opt/mysql/mysql.sock –with-big-tables –with-charset=gbk –with-collation=gbk_chinese_ci –with-client-ldflags=-all-static –with-mysqld-ldflags=-all-static –enable-assembler –with-extra-charsets=gbk,gb2312,utf8 –with-pthread –enable-thread-safe-client –with-innodb
#–with-innodb时,mysql的data存放于/opt/mysql/var 下,无些参数时为/opt/mysql/data下
默认为/var/lib/mysql (在/etc/my.cnf中)

错误:
/usr/bin/ld: cannot find -lncurses
collect2: ld returned 1 exit status
解决:
# yum -y install ncurses-devel

make
make install
#512M内存和web共用
cp support-files/my-medium.cnf /opt/mysql/my.cnf
#2G内存和web共用
#cp support-files/my-large.cnf /opt/mysql/my.cnf
#删除默认的my.cnf
rm /etc/my.cnf

/opt/mysql/bin/mysql_install_db –defaults-file=/opt/mysql/my.cnf –basedir=/opt/mysql –datadir=/opt/mysql/var –user=mysql –pid-file=/opt/mysql/var/mysql.pid –skip-locking –socket=/opt/mysql/mysql.sock
chmod +w /opt/mysql
chown -R mysql:mysql /opt/mysql

cd ..
chgrp website /opt/mysql/my.cnf
chmod 0664 /opt/mysql/my.cnf
</coolcode>
自动运行mysql
http://dev.mysql.com/doc/refman/5.1/en/automatic-start.html

<coolcode>用lemp控制
cp support-files/mysql.server /opt/mysql/bin/
chmod 755 /opt/mysql/bin/mysql.server自动启动
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig –add mysql
#某些linux上
#chkconfig –level 345 mysql on
#开启服务
/etc/init.d/mysql start
#关闭服务
#/etc/init.d/mysql stop  

</coolcode>

 手动开启服务

/bin/sh /opt/mysql/bin/mysqld_safe –user=mysql &

手动关闭服务
<coolcode>
/opt/mysql/bin/mysqladmin -uroot -p shutdown
Enter password:
</coolcode>

mysql修改密码
<coolcode>
#123456为密码
/opt/mysql/bin/mysqladmin -uroot  password 123456
 #测试密码
/opt/mysql/bin/mysql -uroot -p
Enter password:
</coolcode>

编辑my.cnf
<coolcode>
vi /opt/mysql/my.cnf
</coolcode>
关闭log_bin
将log-bin=mysql-bin注释掉,需要热备份或主从服务器的可以保留,开启后会占很多空间
开启innodb
将innodb开头的注释去掉,除了innodb_log_arch_dir参数,开启它将无法启动msyql

修改完成后重启mysql服务

 

http://www.monkey.org/~provos/libevent/
http://www.danga.com/memcached/download.bml
wget http://www.monkey.org/~provos/libevent-1.4.5-stable.tar.gz
wget http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
安装libevent
# tar zxvf libevent-1.4.5-stable.tar.gz
# cd libevent-1.4.5
# ./configure –prefix=/usr
# make
# make install

安装memcached
tar zxvf memcached-1.2.5.tar.gz
cd memcached-1.2.5
./configure –prefix=/opt/memcached –with-libevent=/usr
make
make install

1.启动Memcache的服务器端:
# /opt/memcached/bin/memcached -d -m 100 -u root -l 127.0.0.1-p 12000 -c 256 -P /tmp/memcached.pid

/opt/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

==========================
error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

#cp /usr/lib/libevent* /usr/lib64/ -R 
==========================

echo /usr/local/libevent/lib >> /etc/ld.so.conf
ldconfig

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是100MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.54.96,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,

2.如果要结束Memcache进程,执行:

# kill `cat /tmp/memcached.pid`

也可以启动多个守护进程,不过端口不能重复。

图形介面监控memcached
 http://www.ooso.net/index.php/archives/462

php 补丁安装
<coolcode>
tar zxvf php-5.2.6.tar.gz
gzip -cd php-5.2.6-fpm-0.5.8.diff.gz | patch -d php-5.2.6 -p1

php安装
<coolcode>
cd php-5.2.6
./configure –prefix=/opt/php –with-config-file-path=/opt/php/etc –with-mysql=/opt/mysql –with-mysqli=/opt/mysql/bin/mysql_config –with-iconv-dir=/usr/local/libiconv –with-freetype-dir=/usr/local/freetype/ –with-jpeg-dir=/usr/local/jpeg6/ –with-png-dir –with-zlib=/usr/local/zlib/ –with-libxml-dir=/usr/local/libxml/ –enable-xml –enable-zend-multibyte –disable-debug –disable-ipv6 –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fastcgi –enable-fpm –enable-force-cgi-redirect –enable-mbstring –with-mcrypt=/usr/local/libmcrypt/ –with-gd –enable-gd-native-ttf

错误:
checking for xml2-config path…
configure: error: xml2-config not found.
解决:
编译参数最后加上
–with-xml-config=/usr/local/libxml/bin/xml2-config

错误:
configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/
解决:
yum install curl-devel

sed -i ’s#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#’ Makefile
make
make install
cp php.ini-dist /opt/php/etc/php.ini
chgrp website /opt/php/etc/php.ini
chmod 0664 /opt/php/etc/php.ini

chgrp website /opt/php/etc/php-fpm.conf
chmod 0664 /opt/php/etc/php-fpm.conf
cd ../
</coolcode>

编译安装PHP5扩展模块
<coolcode>
tar zxvf memcache-2.2.3.tgz
cd memcache-2.2.3/
/opt/php/bin/phpize

错误:
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF
environment variable is set correctly and then rerun this script.
解决:
# wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
# tar -zvxf m4-1.4.9.tar.gz
# cd m4-1.4.9/
# ./configure && make && make install
# cd ../
# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.61.tar.gz
# tar -zvxf autoconf-2.61.tar.gz
# cd autoconf-2.61/
# ./configure && make && make install

记得要用2.61 的autoconf-2.62会有错误,郁闷死了
<coolcode>
Zend Extension Api No:   220060519
configure.in:77: warning: AC_CACHE_VAL(lt_prog_compiler_static_works, …): suspicious cache-id, must contain _cv_ to be cached
../../lib/autoconf/general.m4:1973: AC_CACHE_VAL is expanded from…
../../lib/autoconf/general.m4:1993: AC_CACHE_CHECK is expanded from…
</coolcode>

./configure –with-php-config=/opt/php/bin/php-config
make
make install
cd ../

tar zxvf xcache-1.2.2.tar.gz
cd xcache-1.2.2/
/opt/php/bin/phpize
./configure –with-php-config=/opt/php/bin/php-config –enable-xcache  –enable-xcache-coverager  –enable-inline-optimization –disable-debug
make
make install
cd ../
</coolcode>

 5、修改php.ini文件
  手工修改:查找/opt/php/etc/php.ini中的extension_dir = “./”
  修改为extension_dir = “/opt/php/lib/php/extensions/no-debug-non-zts-20060613/”
  并在此行后增加以下几行,然后保存:
  extension = “memcache.so”

php.ini中
cgi.fix_pathinfo=1; 这样php-cgi方能正常使用SCRIPT_FILENAME这个变量,默认就是1

在php.ini尾部增加
<coolcode>

[xcache-common]
zend_extension = /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so

[xcache.admin]
xcache.admin.user = “xcache”
; xcache.admin.pass = md5($yourpasswd)
;如何生成md5密码: echo -n “password”| md5sum
xcache.admin.pass = “0563ff87afd961147cc3b89fec551a87″  #password is c1gstudio

[xcache]
xcache.cacher = On
xcache.shm_scheme = “mmap”
xcache.size = 32M
; cpu number (cat /proc/cpuinfo |grep -c processor)
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 2M
; cpu number (cat /proc/cpuinfo |grep -c processor)
xcache.var_count = 2
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.readonly_protection = Off
xcache.mmap_path = “/dev/zero”
</coolcode>

让session使用memcached
session.save_handler = memcache
session.save_path = “tcp://127.0.0.1:12000″ 
创建用户组
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -g website www -d /dev/null -s /sbin/nologin
mkdir -p /opt/htdocs/www/nginx
chmod -R 0775 /opt/htdocs/
chown -R www:website /opt/htdocs/

创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
在/opt/php/etc/目录中创建php-fpm.conf文件:
rm -f /opt/php/etc/php-fpm.conf
vi /opt/php/etc/php-fpm.conf

<coolcode>
<?xml version=”1.0″ ?>
<configuration>

        All relative paths in this config are relative to php’s install prefix

        <section name=”global_options”>

                Pid file
                <value name=”pid_file”>/opt/php/logs/php-fpm.pid</value>

                Error log file
                <value name=”error_log”>/opt/php/logs/php-fpm.log</value>

                Log level
                <value name=”log_level”>notice</value>

                When this amount of php processes exited with SIGSEGV or SIGBUS …
                <value name=”emergency_restart_threshold”>10</value>

                … in a less than this interval of time, a graceful restart will be initiated.
                Useful to work around accidental curruptions in accelerator’s shared memory.
                <value name=”emergency_restart_interval”>1m</value>

                Time limit on waiting child’s reaction on signals from master
                <value name=”process_control_timeout”>5s</value>

                Set to ‘no’ to debug fpm
                <value name=”daemonize”>yes</value>

        </section>

        <workers>

                <section name=”pool”>

                        Name of pool. Used in logs and stats.
                        <value name=”name”>default</value>

                        Address to accept fastcgi requests on.
                        Valid syntax is ‘ip.ad.re.ss:port’ or just ‘port’ or ‘/path/to/unix/socket’
                        <value name=”listen_address”>127.0.0.1:9000</value>

                        <value name=”listen_options”>

                                Set listen(2) backlog
                                <value name=”backlog”>-1</value>

                                Set permissions for unix socket, if one used.
                                In Linux read/write permissions must be set in order to allow connections from web server.
                                Many BSD-derrived systems allow connections regardless of permissions.
                                <value name=”owner”></value>
                                <value name=”group”></value>
                                <value name=”mode”>0666</value>
                        </value>

                        Additional php.ini defines, specific to this pool of workers.
                        <value name=”php_defines”>
                                <value name=”sendmail_path”>/usr/sbin/sendmail -t -i</value>
                                <value name=”display_errors”>0</value>
                        </value>

                        Unix user of processes
                        <value name=”user”>www</value>

                        Unix group of processes
                        <value name=”group”>website</value>

                        Process manager settings
                        <value name=”pm”>

                                Sets style of controling worker process count.
                                Valid values are ’static’ and ‘apache-like’
                                <value name=”style”>static</value>

                                Sets the limit on the number of simultaneous requests that will be served.
                                Equivalent to Apache MaxClients directive.
                                Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
                                Used with any pm_style.
                                <value name=”max_children”>20</value>

                                Settings group for ‘apache-like’ pm style
                                <value name=”apache_like”>

                                        Sets the number of server processes created on startup.
                                        Used only when ‘apache-like’ pm_style is selected
                                        <value name=”StartServers”>20</value>

                                        Sets the desired minimum number of idle server processes.
                                        Used only when ‘apache-like’ pm_style is selected
                                        <value name=”MinSpareServers”>5</value>

                                        Sets the desired maximum number of idle server processes.
                                        Used only when ‘apache-like’ pm_style is selected
                                        <value name=”MaxSpareServers”>250</value>

                                </value>

                        </value>

                        Time limit on waiting execution of single request
                        Should be used when ‘max_execution_time’ ini option does not terminate execution for some reason
                        <value name=”request_execution_timeout”>31s</value>

                        Set open file desc rlimit
                        <value name=”rlimit_files”>51200</value>

                        Set max core size rlimit
                        <value name=”rlimit_core”>0</value>

                        Chroot to this directory at the start
                        <value name=”chroot”></value>

                        Chdir to this directory at the start
                        <value name=”chdir”></value>

                        Redirect workers’ stdout and stderr into main error log.
                        If not set, they will be redirected to /dev/null, according to FastCGI specs
                        <value name=”catch_workers_output”>yes</value>

                        How much requests each process should execute before respawn.
                        Useful to work around memory leaks in 3rd party libraries.
                        For endless request processing please specify 0
                        Equivalent to PHP_FCGI_MAX_REQUESTS
                        <value name=”max_requests”>51200</value>

                        Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
                        Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
                        Makes sense only with AF_INET listening socket.
                        <value name=”allowed_clients”>127.0.0.1</value>

                        Pass environment variables like LD_LIBRARY_PATH
                        All $VARIABLEs are taken from current environment
                        <value name=”environment”>
                                <value name=”HOSTNAME”>$HOSTNAME</value>
                                <value name=”PATH”>/usr/local/bin:/usr/bin:/bin</value>
                                <value name=”TMP”>/tmp</value>
                                <value name=”TMPDIR”>/tmp</value>
                                <value name=”TEMP”>/tmp</value>
                                <value name=”OSTYPE”>$OSTYPE</value>
                                <value name=”MACHTYPE”>$MACHTYPE</value>
                                <value name=”MALLOC_CHECK_”>2</value>
                        </value>

                </section>

        </workers>

</configuration>

</coolcode>

<coolcode>
ulimit -SHn 51200
/opt/php/sbin/php-fpm start
#/opt/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload。
</coolcode>
和原始配置文件的差别

<coolcode>
用户组->www
max_children=64
MaxApareServers=250
rlimit_files=51200
max_requests=51200
去掉注释
</coolcode>

安装Nginx
安装Nginx所需的pcre库
<coolcode>
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7/
./configure –enable-utf8 –enable-unicode-properties
make && make install
cd ../
</coolcode>

安装Nginx

tar zxvf nginx-0.6.32.tar.gz
cd nginx-0.6.32/

关闭debug模式来减少nginx大小
http://bianbian.org/technology/271.html
<coolcode>
 du /opt/nginx/sbin/nginx
#未做优化时的大小
1712    /opt/nginx/sbin/nginx
#优化后的大小
404     /opt/nginx/sbin/nginx
vi auto/cc/gcc
    # 最后几行sheft+g
    #注释这行
    #CFLAGS=”$CFLAGS -g”
</coolcode>
伪装header
<coolcode>
   vi src/core/nginx.h
   #define NGINX_VERSION      “1.0″
   #define NGINX_VER          “C1GWS/” NGINX_VERSION
</coolcode>
<coolcode>

./configure –user=www –group=website –prefix=/opt/nginx –with-http_stub_status_module –with-http_ssl_module
make && make install
cd ../

</coolcode>

修改权限
<coolcode>
chown -R www:website /opt/nginx/logs/
chmod -R 0775 /opt/nginx/logs/

chown -R www:website /opt/nginx/conf/
chmod -R 0775 /opt/nginx/conf/
</coolcode>

创建Nginx配置文件

rm -f /opt/nginx/conf/nginx.conf
vi /opt/nginx/conf/nginx.conf

 <coolcode>

user  www website;

worker_processes 4; #cpu*2

error_log  /opt/nginx/logs/nginx_error.log  crit;

pid        /dev/shm/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
     use epoll;

     worker_connections 51200;
}

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

      log_format  access  ’$remote_addr - $remote_user [$time_local] “$request” ‘
                                   ’$status $body_bytes_sent “$http_referer” ‘
                                   ’”$http_user_agent” $http_x_forwarded_for’;

     charset  utf-8;
     
     server_names_hash_bucket_size 128;
     
     sendfile on;
     tcp_nopush     on;

     keepalive_timeout 60;

     tcp_nodelay on;

     fastcgi_intercept_errors on; # 解决 no input file specified
     fastcgi_connect_timeout 60;
     fastcgi_send_timeout 180;
     fastcgi_read_timeout 180;
     fastcgi_buffer_size 128k;
     fastcgi_buffers 4 128k;
     fastcgi_busy_buffers_size 128k;
     fastcgi_temp_file_write_size 128k;
     fastcgi_temp_path /dev/shm;

     gzip on;
     gzip_min_length  1k;
     gzip_buffers     4 8k;
     gzip_http_version 1.1;
     gzip_types       text/plain application/x-javascript text/css text/html application/xml;

     server
     {
             listen       80;
             server_name  devwww.c1gstudio.com;
             index index.html index.htm index.php;
             root  /opt/htdocs/www;
                            
             location ~ .*\.php?$
             {
                  include fcgi.conf;      
                  #fastcgi_pass  unix:/tmp/php-cgi.sock;
                  fastcgi_pass  127.0.0.1:9000;
                  fastcgi_index index.php;
             }

             access_log  /opt/nginx/logs/access.log  access;
     }

     server
     {
             listen       80;
             server_name  devstatus.c1gstudio.com;
             index index.html index.htm index.php;
             root  /opt/htdocs/status;

            error_page 404 /404.html;
            #error_page 500 502 503 504 /50x.html;

             location ~ .*\.php?$
             {
                  include fcgi.conf;
                  #fastcgi_pass  unix:/tmp/php-cgi.sock;
                  fastcgi_pass  127.0.0.1:9000;
                  fastcgi_index index.php;
             }

            location /nginx {
                 stub_status on;
                 access_log  off;
                 auth_basic “NginxStatus”;
                 auth_basic_user_file htpasswd;
                 #用apache的htpasswd生成访问密码
                 #/opt/apache2/bin/htpasswd -c /opt/nginx/conf/htpasswd c1gstduio admin
           }
            # alias
            #localtion /alias/ {
                 #alias /spool/w3/images/;
            #}

            #防盗链

            location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip)$ {
                valid_referers none blocked *.yingjiesheng.com *.yingjiesheng.net localhost;
                if ($invalid_referer) {
                     rewrite ^/ http://www.c1gstudio.com/logo.gif;
                      return 403;
                }
            }
            # add expires header for static content
            location ~* ^.+\.(jpg|jpeg|gif|png|css|js|ico|html)$ {
                 access_log   off;
                 root /opt/htdocs/www;
                 expires 2h;
                 break;
            }
            #禁止访问
            location ~/\.ht {
                 deny all;
            }
            location /(themes|templates_c)/ {
                deny all;
            }

             access_log  /opt/nginx/logs/status.log  access;
     }

     server
     {
             listen  80;
             server_name  status.blog.s135.com;

             location / {
                  stub_status on;
                  access_log   off;
             }

            #awstats
            location ~ ^/cgi-bin/.*\.pl$ {
                auth_basic “Restricted”;
                auth_basic_user_file htpasswd;
                gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
                include awstats.conf;
            }

     }
}
</coolcode>

nginx的日志滚动

mkdir 0775 /opt/shell
chgrp website /opt/shell
vi /opt/shell/nginx_log.sh
<coolcode>
#!/bin/sh
log_dir=/opt/nginx/logs
yesterday=`date +%Y%m%d`
lastday=`date +%Y%m%d -d ‘-1 month’`
/bin/rm ${log_dir}/access.${lastday}.log
/bin/rm ${log_dir}/nginx_error.${lastday}.log
/bin/mv ${log_dir}/access.log ${log_dir}/access.${yesterday}.log
/bin/mv ${log_dir}/nginx_error.log ${log_dir}/nginx_error.${yesterday}.log
kill -USR1 `cat /opt/nginx/nginx.pid`
/bin/gzip ${log_dir}/access.${yesterday}.log &
/bin/gzip ${log_dir}/nginx_error.${yesterday}.log &

</coolcode>

在crontab里每日23:59时
59 23 * * * /bin/sh /opt/shell/nginx_log.sh > /dev/null 2>&1
http://www.bullog.cn/blogs/shunz/archives/157311.aspx

让nginx支持FastCGI
http://wiki.codemongers.com/NginxSimpleCGI
http://wiki.codemongers.com/NginxSimpleCGI?action=recall&rev=10

cpan安装
#perl -MCPAN -e ‘install FCGI’

一路回车

或使用原码安装
#wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.67.tar.gz
#tar zxvf FCGI-0.67.tar.gz
#cd FCGI-0.67
#perl Makefile.PL
#make
#make install

#mkdir -p /var/run/nginx/
将链接网页中的perl代码保存成perl-fcgi.pl并运行
chmod o+x /opt/shell/perl-fcgi.pl
perl /opt/shell/perl-fcgi.pl
修改nginx配置文件
/opt/nginx/sbin/nginx -t
kill -HUP 进程号 # 重载配置文件
kill -15 进程号 # 关闭

pear升级
使用新装的pear代替系统的pear
#which pear
/usr/bin/pear
#pear config-show
Configuration (channel pear.php.net):
=====================================
Auto-discover new Channels     auto_discover    <not set>
Default Channel                default_channel  pear.php.net
HTTP Proxy Server Address      http_proxy       <not set>
PEAR server [DEPRECATED]       master_server    pear.php.net
Default Channel Mirror         preferred_mirror pear.php.net
Remote Configuration File      remote_config    <not set>
PEAR executables directory     bin_dir          /usr/bin
PEAR documentation directory   doc_dir          /usr/share/pear/doc
PHP extension directory        ext_dir          /usr/lib/php4
PEAR directory                 php_dir          /usr/share/pear
PEAR Installer cache directory cache_dir        /tmp/pear/cache
PEAR data directory            data_dir         /usr/share/pear/data
PHP CLI/CGI binary             php_bin          /usr/bin/php
PEAR test directory            test_dir         /usr/share/pear/test
Cache TimeToLive               cache_ttl        3600
Preferred Package State        preferred_state  stable
Unix file mask                 umask            22
Debug Log Level                verbose          1
PEAR password (for             password         <not set>
maintainers)
Signature Handling Program     sig_bin          /usr/bin/gpg
Signature Key Directory        sig_keydir       /etc/pearkeys
Signature Key Id               sig_keyid        <not set>
Package Signature Type         sig_type         gpg
PEAR username (for             username         <not set>
maintainers)
User Configuration File        Filename         /root/.pearrc
System Configuration File      Filename         /etc/pear.conf
#/opt/php/bin/pear config-show
Configuration (channel pear.php.net):
=====================================
Auto-discover new Channels     auto_discover    <not set>
Default Channel                default_channel  pear.php.net
HTTP Proxy Server Address      http_proxy       <not set>
PEAR server [DEPRECATED]       master_server    pear.php.net
Default Channel Mirror         preferred_mirror pear.php.net
Remote Configuration File      remote_config    <not set>
PEAR executables directory     bin_dir          /opt/php/bin
PEAR documentation directory   doc_dir          /opt/php/lib/php/doc
PHP extension directory        ext_dir          /opt/php/lib/php/extensions/no-debug-non-zts-20060613
PEAR directory                 php_dir          /opt/php/lib/php
PEAR Installer cache directory cache_dir        /tmp/pear/cache
PEAR data directory            data_dir         /opt/php/lib/php/data
PHP CLI/CGI binary             php_bin          /opt/php/bin/php
PEAR test directory            test_dir         /opt/php/lib/php/test
Cache TimeToLive               cache_ttl        3600
Preferred Package State        preferred_state  stable
Unix file mask                 umask            22
Debug Log Level                verbose          1
                               cfg_dir          <not set>
                               download_dir     <not set>
                               php_ini          <not set>
                               temp_dir         <not set>
                               www_dir          <not set>
PEAR password (for             password         <not set>
maintainers)
Signature Handling Program     sig_bin          /usr/bin/gpg
Signature Key Directory        sig_keydir       /opt/php/etc/pearkeys
Signature Key Id               sig_keyid        <not set>
Package Signature Type         sig_type         gpg
PEAR username (for             username         <not set>
maintainers)
User Configuration File        Filename         /root/.pearrc
System Configuration File      Filename         /opt/php/etc/pear.conf

 

#mv /usr/bin/pear /usr/bin/pear.del
#ln -s /opt/php/bin/pear /usr/bin/pear
#pear list
Installed packages, channel pear.php.net:
=========================================
Package          Version State
Archive_Tar      1.3.2   stable
Console_Getopt   1.2.3   stable
PEAR             1.7.1   stable
Structures_Graph 1.0.2   stable
#pear upgrade pear
#pear install Benchmark Cache_Lite DB HTTP Mail Mail_Mime Net_SMTP Net_Socket Pager XML_Parser XML_RPC
#pear list
Installed packages, channel pear.php.net:
=========================================
Package          Version State
Archive_Tar      1.3.2   stable
Benchmark        1.2.7   stable
Cache_Lite       1.7.4   stable
Console_Getopt   1.2.3   stable
DB               1.7.13  stable
Mail             1.1.14  stable
Mail_Mime        1.5.2   stable
Mail_mimeDecode  1.5.0   stable
Net_SMTP         1.3.1   stable
Net_Socket       1.0.9   stable
PEAR             1.7.2   stable
Pager            2.4.7   stable
Structures_Graph 1.0.2   stable
XML_Parser       1.3.0   stable
XML_RPC          1.5.1   stable

awstats安装
linux awstats 安装
清除日志记录可以删除conf中DirData=”/var/lib/awstats” 下的文件
分析
perl /opt/awstats/wwwroot/cgi-bin/awstats.pl -config=sina -update
查看
http://localhost/awstats/awstats.pl?config=sina
自动运行
10 8 * * * (cd /opt/awstats/wwwroot/cgi-bin/; ./awstats.pl -update -config=sina )

apache与nginx+php cgi性能比较
http://www.guogoul.com/2008/07/08/nginx_2/

快捷控制脚本
lemp脚本

/opt/lemp
Usage: /opt/lemp <action>
 
        start           Start LEMP (nginx, MySQL, phpfpm, tomcat)
        startnginx      Start only nginx
        startmysql      Start only MySQL
        starttomcat     Start only tomcat
        startphpfpm     Start only phpfpm
        startmemcached  Start only memcached
 
        stop            Stop LEMP (nginx, MySQL, phpfpm, tomcat)
        stopnginx       Stop only nginx
        stopmysql       Stop only MySQL
        stoptomcat      Stop only tomcat
        stopphpfpm      Stop only phpfpm
        stopmemcached   Stop only memcached
 
        reload          Reload LEMP (nginx, MySQL, phpfpm )
        reloadnginx     Reload only nginx
        reloadmysql     Reload only MySQL
        reloadphpfpm    Reload only phpfpm
 
        restart      Stop and start LEMP

防arp
http://bbs.linuxtone.org/thread-41-1-1.html

update 2008/08/14
解决php 404错误为 no input file specified
在nginx.conf中增加
fastcgi_intercept_errors on;

优化
#ulimit -SHn 51200
#vi /etc/rc.local
尾部增加
ulimit -SHn 51200

#vi /etc/sysctl.conf
尾部增加
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
#/sbin/sysctl -p

PHP FastCGI进程数多少合适?

netstat -anpo | grep “php-cgi” | wc -l
  如果实际使用的“FastCGI进程数”接近预设的“FastCGI进程数”,那么,说明“FastCGI进程数”不够用,需要增大。

One Response to “nginx+php(FCGI)+xcache+mysql on as4”

  1. on 03 Oct 2008 at 3:23 pm 1.nes游戏网 said …

    好文章啊
    nes游戏网 http://www.nesnes.cn
    我喜欢啊216

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply