转:http://blog.csdn.net/xymyeah/article/details/6939544

一、一般来说nginx 配置文件中对优化比较有作用的为以下几项:

1. worker_processes 8;

nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu计为8)。

2. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

为每个进程分配cpu,上例中将8 个进程分配到8 个cpu,当然可以写多个,或者将一
个进程分配到多个cpu。

3. worker_rlimit_nofile 65535;

这个指令是指当一个nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文
件数(ulimit -n)与nginx 进程数相除,但是nginx 分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。

现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。

这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。

查看linux系统文件描述符的方法:

[root@web001 ~]# sysctl -a | grep fs.file

fs.file-max = 789972

fs.file-nr = 510 0 789972

4. use epoll;
使用epoll 的I/O 模型

(

补充说明:

与apache相类,nginx针对不同的操作系统,有不同的事件模型

A)标准事件模型
Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
B)高效事件模型
Kqueue:使用于 FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X. 使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
Epoll: 使用于Linux内核2.6版本及以后的系统。

/dev/poll:使用于 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。

Eventport:使用于 Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装安全补丁。

)

5. worker_connections 65535;

每个进程允许的最多连接数, 理论上每台nginx 服务器的最大连接数为worker_processes*worker_connections。

6. keepalive_timeout 60;

keepalive 超时时间。

7. client_header_buffer_size 4k;

客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。

分页大小可以用命令getconf PAGESIZE 取得。

[root@web001 ~]# getconf PAGESIZE

4096

但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。

8. open_file_cache max=65535 inactive=60s;

这个将为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。

9. open_file_cache_valid 80s;

这个是指多长时间检查一次缓存的有效信息。

10. open_file_cache_min_uses 1;

open_file_cache 指令中的inactive 参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。

二、关于内核参数的优化:

net.ipv4.tcp_max_tw_buckets = 6000

timewait 的数量,默认是180000。

net.ipv4.ip_local_port_range = 1024 65000

允许系统打开的端口范围。

net.ipv4.tcp_tw_recycle = 1

启用timewait 快速回收。

net.ipv4.tcp_tw_reuse = 1

开启重用。允许将TIME-WAIT sockets 重新用于新的TCP 连接。

net.ipv4.tcp_syncookies = 1

开启SYN Cookies,当出现SYN 等待队列溢出时,启用cookies 来处理。

net.core.somaxconn = 262144

web 应用中listen 函数的backlog 默认会给我们内核参数的net.core.somaxconn 限制到128,而nginx 定义的NGX_LISTEN_BACKLOG 默认为511,所以有必要调整这个值。

net.core.netdev_max_backlog = 262144

每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。

net.ipv4.tcp_max_orphans = 262144

系统中最多有多少个TCP 套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤儿连接将即刻被复位并打印出警告信息。这个限制仅仅是为了防止简单的DoS 攻击,不能过分依靠它或者人为地减小这个值,更应该增加这个值(如果增加了内存之后)。

net.ipv4.tcp_max_syn_backlog = 262144

记录的那些尚未收到客户端确认信息的连接请求的最大值。对于有128M 内存的系统而言,缺省值是1024,小内存的系统则是128。

net.ipv4.tcp_timestamps = 0

时间戳可以避免序列号的卷绕。一个1Gbps 的链路肯定会遇到以前用过的序列号。时间戳能够让内核接受这种“异常”的数据包。这里需要将其关掉。

net.ipv4.tcp_synack_retries = 1

为了打开对端的连接,内核需要发送一个SYN 并附带一个回应前面一个SYN 的ACK。也就是所谓三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送SYN+ACK 包的数量。

net.ipv4.tcp_syn_retries = 1

在内核放弃建立连接之前发送SYN 包的数量。

net.ipv4.tcp_fin_timeout = 1

如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2 状态的时间。对端可以出错并永远不关闭连接,甚至意外当机。缺省值是60 秒。2.2 内核的通常值是180 秒,3你可以按这个设置,但要记住的是,即使你的机器是一个轻载的WEB 服务器,也有因为大量的死套接字而内存溢出的风险,FIN- WAIT-2 的危险性比FIN-WAIT-1 要小,因为它最多只能吃掉1.5K 内存,但是它们的生存期长些。

net.ipv4.tcp_keepalive_time = 30

当keepalive 起用的时候,TCP 发送keepalive 消息的频度。缺省是2 小时。

三、下面贴一个完整的内核优化设置:

vi /etc/sysctl.conf CentOS5.5中可以将所有内容清空直接替换为如下内容:

net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000

使配置立即生效可使用如下命令:
/sbin/sysctl -p

四、下面是关于系统连接数的优化

linux 默认值 open files 和 max user processes 为 1024

#ulimit -n

1024

#ulimit –u

1024

问题描述: 说明 server 只允许同时打开 1024 个文件,处理 1024 个用户进程

使用ulimit -a 可以查看当前系统的所有限制值,使用ulimit -n 可以查看当前的最大打开文件数。

新装的linux 默认只有1024 ,当作负载较大的服务器时,很容易遇到error: too many open files 。因此,需要将其改大。

解决方法:

使用 ulimit –n 65535 可即时修改,但重启后就无效了。(注ulimit -SHn 65535 等效 ulimit -n 65535 ,-S 指soft ,-H 指hard)

有如下三种修改方式:

1. 在/etc/rc.local 中增加一行 ulimit -SHn 65535
2. 在/etc/profile 中增加一行 ulimit -SHn 65535
3. 在/etc/security/limits.conf 最后增加:

* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535

具体使用哪种,在 CentOS 中使用第1 种方式无效果,使用第3 种方式有效果,而在Debian 中使用第2 种有效果

# ulimit -n

65535

# ulimit -u

65535

备注:ulimit 命令本身就有分软硬设置,加-H 就是硬,加-S 就是软默认显示的是软限制

soft 限制指的是当前系统生效的设置值。 hard 限制值可以被普通用户降低。但是不能增加。 soft 限制不能设置的比 hard 限制更高。 只有 root 用户才能够增加 hard 限制值。

五、下面是一个简单的nginx 配置文件:

user www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000
01000000;
error_log /www/log/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 204800;
events
{
use epoll;
worker_connections 204800;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
keys_zone=TEST:10m
inactive=5m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;
fastcgi_busy_buffers_size 8k;
fastcgi_temp_file_write_size 8k;
fastcgi_cache TEST;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
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;
server
{
listen 8080;
server_name backup.aiju.com;
index index.php index.htm;
root /www/html/;
location /status
{
stub_status on;
}
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
expires 30d;
}
log_format access ‘$remote_addr — $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;
access_log /www/log/access.log access;
}
}

六、关于FastCGI 的几个指令:

fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10minactive=5m;

这个指令为FastCGI 缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间。

fastcgi_connect_timeout 300;

指定连接到后端FastCGI 的超时时间。

fastcgi_send_timeout 300;

向FastCGI 传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI 传送请求的超时时间。

fastcgi_read_timeout 300;

接收FastCGI 应答的超时时间,这个值是指已经完成两次握手后接收FastCGI 应答的超时时间。

fastcgi_buffer_size 4k;

指定读取FastCGI 应答第一部分需要用多大的缓冲区,一般第一部分应答不会超过1k,由于页面大小为4k,所以这里设置为4k。

fastcgi_buffers 8 4k;

指定本地需要用多少和多大的缓冲区来缓冲FastCGI 的应答。

fastcgi_busy_buffers_size 8k;

这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers 的两倍。

fastcgi_temp_file_write_size 8k;

在写入fastcgi_temp_path 时将用多大的数据块,默认值是fastcgi_buffers 的两倍。

fastcgi_cache TEST

开启FastCGI 缓存并且为其制定一个名称。个人感觉开启缓存非常有用,可以有效降低CPU 负载,并且防止502 错误。

fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;

为指定的应答代码指定缓存时间,如上例中将200,302 应答缓存一小时,301 应答缓存1 天,其他为1 分钟。

fastcgi_cache_min_uses 1;

缓存在fastcgi_cache_path 指令inactive 参数值时间内的最少使用次数,如上例,如果在5 分钟内某文件1 次也没有被使用,那么这个文件将被移除。

fastcgi_cache_use_stale error timeout invalid_header http_500;

不知道这个参数的作用,猜想应该是让nginx 知道哪些类型的缓存是没用的。以上为nginx 中FastCGI 相关参数,另外,FastCGI 自身也有一些配置需要进行优化,如果你使用php-fpm 来管理FastCGI,可以修改配置文件中的以下值:

60

同时处理的并发请求数,即它将开启最多60 个子线程来处理并发连接。

102400

最多打开文件数。

204800

每个进程在重置之前能够执行的最多请求数。

转:http://www.myhack58.com/Article/sort099/sort0102/2011/29472.htm

一、更改yum源为网易的源加快速度

vi /etc/yum.repos.d/CentOS-Base.repo
更改内容如下

# CentOS-Base.repo
#
# This file uses a new mirrorlist system developed by Lance Davis for CentOS.
# The mirror system uses the connecting IP address of the client and the

# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#packages used/produced in the build but not released
[addons]
name=CentOS-$releasever - Addons
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=addons
#baseurl=http://mirror.centos.org/centos/$releasever/addons/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/addons/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
baseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

二、update yum

yum -y update

三、利用CentOS Linux系统自带的yum命令安装、升级所需的程序库

LANG=C
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

四、安装php和mysql

yum -y install php mysql mysql-server mysql-devel php-mysql php-cgi php-mbstring php-gd php-fastcgi

五、安装nginx
由于centos没有默认的nginx软件包,需要启用REHL的附件包

rpm -Uvh http://download.Fedora.RedHat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
yum -y install nginx

设置开机启动

chkconfig nginx on

六、安装spawn-fcgi来运行php-cgi

yum install spawn-fcgi

七、下载spawn-fcgi 的启动脚本

wget http://bash.cyberciti.biz/dl/419.sh.zip
unzip 419.sh.zip
mv 419.sh /etc/init.d/php_cgi
chmod +x /etc/init.d/php_cgi

启动php_cgi

/etc/init.d/php_cgi start

查看进程

netstat -tulpn | grep :9000

若出现如下代表一切正常

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4352/php-cgi

八、配置nginx(详细配置见nginx.conf详细说明)

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

九、查看phpinfo
编写脚本

phpinfo();

十、安装phpmyadmin
修改/var/lib/php/session的权限和nginx和php_cgi一致

chown -R www.www /var/lib/php/session

转:http://apps.hi.baidu.com/share/detail/14819035

如果你打算使用Apache httpd Web服务器,请看这里。

一、系统约定

软件源代码包存放位置 /usr/local/data-original

源码包编译安装位置(prefix) /usr/local/software_name

脚本以及维护程序存放位置 /usr/local/sbin

MySQL 数据库位置 /var/lib/mysql(可按情况设置)

网站根目录 /home/www/wwwroot(可按情况设置)

虚拟主机日志根目录 /home/www/logs(可按情况设置)

运行账户 www:www

二、系统环境部署及调整

1、检查系统是否正常

# more /var/log/messages (检查有无系统级错误信息)

# dmesg (检查硬件设备是否有错误信息)

# ifconfig(检查网卡设置是否正确)

# ping www.163.com (检查网络是否正常)

# cat /proc/cpuinfo (检查CPU频率是否正常)

# top (按1检测CPU核数是否正常,内存大小是否正常)

2、关闭不需要的服务

# ntsysv

以下仅列出需要启动的服务,未列出的服务一律推荐关闭:

atd

crond

irqbalance

microcode_ctl

network

sendmail

sshd

syslog

关闭SElinux:修改/etc/selinux/config文件中的SELINUX= 为 disabled

3、更换yum国内源

# cd /etc/yum.repos.d

# mv CentOS-Base.repo CentOS-Base.repo.save

# wget http://centos.ustc.edu.cn/CentOS-Base.repo.5

# mv CentOS-Base.repo.5 CentOS-Base.repo

# yum clean all

4、服务器时间检查和设置

#data (检查时间是否正确,是否是中国时间CST)

#cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime (如果时区不对,则执行,时间正常的跳过)

#yum -y install ntp (安装ntp对时工具)

#chkconfig ntpd on (让对时服务开机启动)

5、使用 yum 对系统进行更新并且安装必要软件包

#yum update –y

  1. #yum -y install make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel

复制代码

6. 重新启动系统

# init 6

三、编译安装L.A.M.P环境

1、下载软件(截止到09年10月的最新版本)

# cd /usr/local/data-original

#wget http://sysoev.ru/nginx/nginx-0.7.63.tar.gz

#wget http://download.scientificlinux.net/nginx

#wget http://download.scientificlinux.net/php-fpm.conf

#wget http://download.scientificlinux.net/nginx.conf

#wget http://download.scientificlinux.net/fcgi.conf

#wget http://download.scientificlinux.net/php-5.2.10.tar.gz

#wget http://download.scientificlinux.net/php-5.2.10-fpm-0.5.13.diff.gz

#wget http://download.scientificlinux. ... glibc23-i386.tar.gz (32位系统)

#wget http://download.scientificlinux. ... ibc23-x86_64.tar.gz (64位系统)

#wget http://download.scientificlinux. ... i686-glibc23.tar.gz (32位系统)

#wget http://download.scientificlinux. ... 6_64-glibc23.tar.gz (64位系统)

2、安装MySQL

cd /usr/local/data-original

tar zxvf mysql-5.1.39-linux-i686-glibc23.tar.gz

mv mysql-5.1.39-linux-i686-glibc23 /usr/local/

ln -s /usr/local/mysql-5.1.39-linux-i686-glibc23/ /usr/local/mysql

groupadd mysql

useradd -g mysql mysql

chown -R mysql:mysql /usr/local/mysql

chown -R mysql:mysql /usr/local/mysql-5.1.39-linux-i686-glibc23/

cd /usr/local/mysql

./scripts/mysql_install_db --user=mysql

cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld

chmod 755 /etc/rc.d/init.d/mysqld

chkconfig --add mysqld

chkconfig --level 3 mysqld on

cp ./support-files/my-huge.cnf /etc/my.cnf

mv /usr/local/mysql/data /var/lib/mysql

chown -R mysql:mysql /var/lib/mysql

编辑/etc/my.cnf

在 [mysqld] 段增加

  1. datadir = /var/lib/mysql
  2. skip-innodb
  3. wait-timeout = 10
  4. max_connections = 512
  5. max_connect_errors = 10000000

复制代码

在 [mysqld] 段修改

  1. max_allowed_packet = 16M
  2. thread_cache_size = CPU个数*2

复制代码

将 log-bin 注释

service mysqld start

bin/mysqladmin -u root password 'password_for_root'

其中引号内的password_for_root是要设置的root密码

3、安装Nginx

cd /usr/local/data-original/

tar zxvf nginx-0.7.63.tar.gz

cd nginx-0.7.63

./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_random_index_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module

make

make install

cp /usr/local/data-original/nginx /etc/init.d/nginx

chmod 755 /etc/init.d/nginx

chkconfig --add nginx

chkconfig nginx on

4、安装PHP和Zend

cd /usr/local/data-original

tar zxvf php-5.2.10.tar.gz

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

cd php-5.2.10

./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/etc/cgi --enable-mbstring --enable-ftp --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --enable-magic-quotes --with-mysql=/usr/local/mysql --with-pear --enable-sockets --with-ttf --with-freetype-dir=/usr --enable-gd-native-ttf --with-zlib --enable-sysvsem --enable-sysvshm --with-libxml-dir=/usr --enable-force-cgi-redirect --enable-fastcgi --with-xmlrpc --enable-zip --enable-fpm

make

make install

mkdir -p /usr/local/etc/cgi/

cp php.ini-dist /usr/local/etc/cgi/php.ini

mv -f /usr/local/data-original/php-fpm.conf /usr/local/php5/etc/php-fpm.conf

groupadd www

useradd -g www www

echo 'ulimit -SHn 65535' >> /etc/rc.local

echo '/usr/local/php5/sbin/php-fpm start' >> /etc/rc.local

cd /usr/local/data-original

tar zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz

cd ZendOptimizer-3.3.3-linux-glibc23-i386

./install

(注意第一个要填的路径是Zend安装路径,第二个是php.ini所在的路径,即/usr/local/etc/cgi)

(不要选重启apache)

5、启动Nginx和php

mv -f /usr/local/data-original/fcgi.conf /usr/local/nginx/conf/

cp -f /usr/local/data-original/nginx.conf /usr/local/nginx/conf/nginx.conf

mkdir -p /home/www/wwwroot

ulimit -SHn 65535

/usr/local/php5/sbin/php-fpm start

service nginx start

在/home/www/wwwroot放入一个index.php,内容为

打开浏览器访问,即可看到phpinfo页面

6、设置系统防火墙

编辑/usr/local/sbin/fw.sh

复制以下内容进去

  1. #!/bin/bash
  2. # Stop iptables service first
  3. service iptables stop
  4. # Load FTP Kernel modules
  5. /sbin/modprobe ip_conntrack_ftp
  6. /sbin/modprobe ip_nat_ftp
  7. # Inital chains default policy
  8. /sbin/iptables -F -t filter
  9. /sbin/iptables -P INPUT DROP
  10. /sbin/iptables -P OUTPUT ACCEPT
  11. # Enable Native Network Transfer
  12. /sbin/iptables -A INPUT -i lo -j ACCEPT
  13. # Accept Established Connections
  14. /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  15. # ICMP Control
  16. /sbin/iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
  17. # WWW Service
  18. /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  19. # FTP Service
  20. /sbin/iptables -A INPUT -p tcp --dport 21 -j ACCEPT
  21. # SSH Service
  22. /sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT

复制代码

退出编辑,执行以下命令

# chmod 755 /usr/local/sbin/fw.sh
# echo '/usr/local/sbin/fw.sh' >> /etc/rc.local
# /usr/local/sbin/fw.sh

PS:这篇优化总结不错,适合于小内存的VPS服务器。

CentOS 是免费的,并且有着 RHEL 的稳定,因此深受各大 hosting 服务商支持,几乎所有 Linux VPS 都支持 CentOS。

一般来说如果 VPS 配置较高我会选 CentOS,配置低的话就选 Debian,当然这是个人偏好,大多数 Linux VPS 服务商也会提供 Gentoo,不过每次安装程序,升级都要编译会消耗很多资源,耗时,而且性能没有明显提高,不推荐给配置低的 VPS。

VPS 服务商一般给的操作系统版本都是最小安装版本,或者优化过的版本。每个VPS服务商提供的版本都可能不同,安装 CentOS 的系统最低要求至少 64MB 内存(纯文字界面),1GB 硬盘空间。

安装和升级系统

1、登录 VPS 安装 CentOS 5。

2、安装完毕后马上升级整个系统。

yum update

有了一个干净的系统以后,剩下来就是加强和优化 Linux。

删除不必要的软件包,服务,用户,文件等

3、删除不需要的软件包。

yum remove Deployment_Guide-en-US finger cups-libs cups
bluez-libs desktop-file-utils ppp rp-pppoe wireless-tools irda-utils
nfs-utils nfs-utils-lib rdate fetchmail eject ksh mkbootdisk mtools
syslinux tcsh startup-notification talk apmd rmt dump setserial portmap yp-tools
ypbind

rpm -qa (列出所有安装了的包)
rpm -e package (删除某个包)
rpm -qi package (查询某个包)
rpm -qf command (根据程序查询包的名字)
rpm -ql package (查询某个包所有的安装文件)

4、删除一些不安全的软件包,并且用相应安全的软件替代,如: ssh/sftp/scp 替代 telnet, rsh, ftp, rcp
注意系统需要一个默认的 MAT,删除 Sendmail MAT 之前必须先安装一个,如: Postfix。

yum remove telnet rsh ftp rcp
yum install postfix
yum remove sendmail
/sbin/chkconfig postfix off

5、停掉并且删除一些不需要的 xinetd 服务。

/sbin/service xinetd stop; /sbin/chkconfig xinetd off
rm -rf /etc/xinetd.d

6、禁止一些 /etc/init.d/ 下面不需要的服务,更多信息请参考 “Understanding your (Red Hat Enterprise Linux) daemons, by Len DiMaggio”。

/sbin/chkconfig --list

for a in acpid anacron apmd atd autofs bluetooth cpuspeed cups gpm
hidd ip6tables irqbalance messagebus microcode_ctl netfs nfs nfslock
pcscd portmap readahead_early readahead_later rpcgssd rpcidmapd
sendmail smartd xinetd yum-updatesd; do /sbin/chkconfig $a off; done

7、重启系统后,检查一下正在运行中的服务,看看是不是都是必须的。

netstat -an | grep LISTEN
netstat -atunp

8、为了安全起见,删除一些不需要的用户。

cp /etc/passwd /etc/passwd.sav
cp /etc/group /etc/group.sav
for a in adm lp sync news uucp operator games gopher mailnull nscd rpc;
do /usr/sbin/userdel $a -f; done
for a in lp news uucp games gopher users floopy nscd rpc rpcuser nfsnobody;
do /usr/sbin/groupdel $a -f; done

加固和优化系统

9、打开防火墙。

system-config-securitylevel-tui

10、检查和禁止全局可写的 SUID 文件。

find / -perm +4000 -user root -type f -print
find / -perm +2000 -group root -type f -print
chmod u-s /full/path/to/filename
chmod g-s /full/path/to/filename

11、只允许 root 在一个 terminal 上登录,如: tty1。

vi /etc/securetty

12、避免其他用户按 Ctrl+Alt+Del 重启。

vi /etc/inittab

注释掉
#ca::ctrlaltdel:/sbin/shutdown -t3 -r now

13、/etc/security/console.apps/ 下面有 root 用户登录 console 后可以运行的程序,全部删除。
rm -f /etc/security/console.apps/*

14、删除一些登录信息。

vi /etc/issue (warning at login prompt)
vi /etc/motd (warning after successful login)

15、只运行一个 virtual terminal,如果是 VPS 的话,自己不可能物理登录终端,可以全部禁止掉。

vi /etc/inittab
# Run gettys in standard runlevels
#1:2345:respawn:/sbin/mingetty tty1
#2:2345:respawn:/sbin/mingetty tty2
...

16、加固 SSH 安全。

vi /etc/ssh/sshd_config
Port 2222
Protocol 2
PermitRootLogin no
PermitEmptyPasswords no
X11Forwarding no
UsePAM no
UseDNS no
AllowUsers vpsee
Banner /etc/issue

17、安装 Bastille 软件包帮助加固。

rpm -Uvh perl-Curses-1.15-1.el5.rf.i386.rpm
rpm -ivh Bastille-3.0.9-1.0.noarch.rpm
/usr/sbin/bastille -c

18、优化 Linux 内核。

vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0

定制 Linux 内核

19、定制,编译,安装 Linux 内核。

yum install rpm-build ncurses ncurses-devel
rpm -ivh kernel-2.6.18-8.1.1.el5.data-original.rpm
cd /usr/data-original/redhat/SPECS
rpmbuild -bp --target i686 kernel-2.6.spec
cd /usr/data-original/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i686
sed -i 's/EXTRAVERSION = -prep/EXTRAVERSION = -8.1.1.custom.el5/' Makefile
make menuconfig
make rpm
cd /usr/data-original/redhat/RPMS/i686
rpm -ivh kernel-2.6.18prep-1.rpm
/sbin/mkinitrd /boot/initrd-2.6.18-prep.img 2.6.18-prep (2.6.18-prep -> /lib/modules)
vi /boot/grub/menu.1st

20、修改 iptables,只允许 ssh,http 和 https 端口打开。

/sbin/iptables -F
/sbin/iptables -A INPUT -i lo -j ACCEPT
/sbin/iptables -A INPUT -i ! lo -d 127.0.0.0/8 -j REJECT
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/iptables -A OUTPUT -j ACCEPT
/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -A INPUT -p tcp --dport 443 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
/sbin/iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
/sbin/iptables -A INPUT -j REJECT
/sbin/iptables -A FORWARD -j REJECT

然后查看一下 iptables:

iptables -L

更多信息

10 Realistic Steps to a Faster Web Site
Understanding your (Red Hat Enterprise Linux) daemons, by Len DiMaggio
Linux Server Security (2nd Edition), by Michael D. Bauer, O’Reilly
Hardening Linux, by James Turnbull, Apress
RHEL 5.0 Deployment Guide, by RedHat
RHEL 5.0 Installation Guide, by RedHat

转自:http://www.vpsee.com/2009/06/128mb-vps-optimize-centos5/

smartmontools介绍

smartmontools是一款开源的磁盘控制,监视工具,可以运行在 Linux,Unix,BSD,Solaris,Mac OS,OS/2,Cygwin和Windows上,同时它还可以从启动光盘或启动软盘运行,支持ATA/ATAPI/SATA-3(到-8)位的硬盘和 SCSI硬盘,另外还支持磁带设备,它的老家在smartmontools.sourceforge.net,实际上它是一个软件包,包括了两个实用程 序:smartctl和smatd。它监控的硬盘必须具有S.M.A.R.T特性,目前所有硬盘都有这个特性,但默认情况下通常没有开启这个功能,有两种 方法来开启这个特性:1)通过BIOS设置选项2)通过smartctl命令。利用它可以测试硬盘的健康状况,并在发生故障前进行预警。

准备工作

在开始测试或监控之前,先检查一下目标硬盘是否支持S.M.A.R.T,以root登陆(Windows下以系统管理员身份登陆),运行下面的命令:

#smartctl -i -d ata /dev/sda

这里的参数-i指出显示设备信息,-d指出设备类型,这里指定的设备类型为ata,当然,如果是SCSI硬盘,就指定scsi了,最后的/dev/sda就是设备了,这个命令返回:

smartctl version 5.37 ‘i686-pc-linux-gnu‘ Copyright (C) 2002-6
Bruce Allen
Home page is http://smartmontools.sourceforge.net/=== START OF INFORMATION SECTION ===
Model Family: Western Digital Caviar SE (Serial ATA) family
Device Model: WDC WD800JD-00MSA1
Serial Number: WD-WMAM9S474555
Firmware Version: 10.01E01
User Capacity: 80,026,361,856 bytes
Device is: In smartctl database ‘for details use: -P show‘
ATA Version is: 7
ATA Standard is: Exact ATA specification draft version not
indicated
Local Time is: Thu Feb 7 13:09:37 2008 PST
SMART support is: Available - device has SMART capability.
SMART support is: Disabled

从返回的信息中,可以看到硬盘的生产厂家,型号,序列号,容量,是否支持SMART,目前SMART开启没有。结果的最后两行就是我们需要的信息,从这里可以看出,这块硬盘是支持SMART技术的,但目前还没有开启它。

引用:http://genmicha.cn/chinese-troditional-simplified.htm

前几天浏览来源访问,发现有一位使用台湾Google的朋友,搜索到本blog,而且使用Google的在线简繁翻译的功能,但是效果不好。所以搜索了一下WordPress有没有相关的组件,就发现了下面这个。现在已经加到博客的右上角,可以简繁切换了。

-------------------------------------------------------------------------

大家基本上都是用中文写博客,我们也知道国内用的是简体中文,而港台澳和其他海外的华人用的是正体中文,也就是我们所说的繁体字。严格地说简体中文是繁体中文的阉割版,因为它是在繁体中文的基础上简化而来的。细心一点分析我们的统计就会发现,我们的读者不仅仅限于国内朋友,还有很多来自港台澳和国外的华人朋友也通过各种渠道来到我们的博客。简体字经过几十年的发展,已经深入到国内的每一个行业。但是很明显,简体字对于这些朋友来说很陌生,比较形象的说法是“缺胳膊少腿”。

上网的时候我们会发现很多网站都有繁体版本,通过导航栏上的一个按钮就可以把网页上的文字从简体中文变成繁体中文。很多主机商也提供类似于“简繁通”之类的产品,当然,是需要收费的。那么,怎么让我们的博客不用花钱也能支持简繁转换呢?

我们看到国内的很多博客用户都在自己的主页上添加了Google的翻译挂件,通过简单的设置让Google来为我们把网页从简体中文翻译成繁体中文或英语。而我介绍的这个简繁转换,是通过js的方式实现的。下面我就以WordPress为例说明一下:

前几天看到迅雷动漫主页上的幻灯片很有趣,抓下来研究一下。
全部代码下载
如有不妥请告知。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>动漫频道 - 迅雷看看</TITLE>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<style>
* {
PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 0px
}
HTML {
BACKGROUND: #ffffff
}
BODY {
FONT: 12px/20px Arial, Verdana, Lucida, Helvetica, simsun, sans-serif; COLOR: #313131
}
A {
COLOR: #016a9f; TEXT-DECORATION: none
}
A:hover {
TEXT-DECORATION: underline
}
img {border:0px;}
.kk-catalog .rbgx .content {
HEIGHT: 363px
}
.kk-catalog .kanguo .content {
HEIGHT: 363px
}
.kk-catalog .qqdy .content {
HEIGHT: 310px
}
.kk-catalog .rank .content {
HEIGHT: 310px
}
.kk-index .flash-box {
PADDING-BOTTOM: 5px; PADDING-LEFT: 9px; WIDTH: 450px; PADDING-RIGHT: 9px; MARGIN-BOTTOM: 8px; BACKGROUND: url(../img/flashBg.png) no-repeat 0px 0px; HEIGHT: 251px; OVERFLOW: hidden; PADDING-TOP: 5px
}
.kk-index .flash-box H2 {
LINE-HEIGHT: 20px; WIDTH: 448px; MARGIN-BOTTOM: 1px; HEIGHT: 20px; COLOR: #193b5f; FONT-SIZE: 13px
}
.kk-index .flash-box .big-pic {
WIDTH: 366px; FLOAT: left; HEIGHT: 188px; OVERFLOW: hidden; MARGIN-RIGHT: 2px
}
.kk-index .flash-box .big-pic IMG {
BORDER-BOTTOM: #fff 1px solid; BORDER-LEFT: #fff 1px solid; WIDTH: 364px; DISPLAY: block; HEIGHT: 186px; BORDER-TOP: #fff 1px solid; BORDER-RIGHT: #fff 1px solid
}
.kk-index .flash-box .pic-list {
POSITION: relative; WIDTH: 81px; FLOAT: left; HEIGHT: 188px
}
.kk-index .flash-box .pic-list .pre {
POSITION: absolute; WIDTH: 81px; BACKGROUND: url(http://misc.web.xunlei.com/www_5_1/img/sprite.png) no-repeat; HEIGHT: 11px; TOP: 0px; LEFT: 0px
}
.kk-index .flash-box .pic-list .next {
POSITION: absolute; WIDTH: 81px; BACKGROUND: url(http://misc.web.xunlei.com/www_5_1/img/sprite.png) no-repeat; HEIGHT: 11px; TOP: 0px; LEFT: 0px
}

linux下有sync, windows下也有类似的软件,就是cwRsync,此软件分为服务器端/客户端。
目前能找到的最新的版本是4.0.1。下载地址:cwRsync下载

主服务器:A 192.168.0.10

从服务器:B 192.168.0.20

 

1、主服务器A安装

主服务器A安装cwRsyncServer_4.0.1_Installer.zip,server 包括了 client 的功能。

安装完后,会在 系统服务 中增加 RsyncServer 和 ssh 服务,进入服务管理,设置为自动启动。安装成功后,要备份的文件目录必须加上 SvcwRsync 这个用户可读权限。

2、主服务器A配置

进入cwRsync安装目录,配置 rsyncd.conf 如下。

程序代码:

port = 9999 #默认端口是873,做了端口限制的要开启cwRsync所使用的端口。

use chroot = false

strict modes = false

hosts allow = *

log file = rsyncd.log #LOG

pid file = rsyncd.pid

# Module definitions

# Remember cygwin naming conventions : c:\work becomes /cygwin/c/work

#

[web]

path = /cygdrive/d/web/test #注意格式,这说明是D盘WEB目录下的test目录

read only = true #只读

list = no

auth users = username  #指定用户名, 如果没有这行,则表明是匿名

secrets file=/cygdrive/d/rsyncd.secrets  这里指定了认证文件目录,名字叫 rsyncd.secrets,其内容是txt编辑为 username:123456 前面是用户名,后面是密码

transfer logging = no #是否记录详细的传输情况

rsyncd.conf相关解释:

use chroot = no        # 不使用chroot

max connections = 4    # 最大连接数为4

pid file = /cygdrive/d/rsyncd.pid

lock file = /cygdrive/d/rsync.lock

log file = /cygdrive/d/log/rsyncd.log    # 日志记录文件

[web]            # 这里是认证的模块名 client端需要根据此名字进行同步

path = /cygdrive/d/web/test    # 需要做镜像的目录

comment = BACKUP CLIENT IS SOLARIS 8 E250

ignore errors           # 可以忽略一些无关的IO错误

read only = yes         # 只读

list = no               # 不允许列文件

hosts allow=192.168.0.20         #允许连接IP,不限制则填写 *

auth users = username      # 认证的用户名,如果没有这行,则表明是匿名

secrets file = /cygdrive/d/rsyncd.secrets    # 认证文件名

关于session的问题集锦解决方案

1.
错误提示
Warning: Cannot send session cookie - headers already sent
Warning: Cannot send session cache limiter - headers already sent
分析及解决办法
这一类问题,的原因是你在程序中使用session_start()时,之前已经有实际的html内容输出了。或许你说,我没有啊,我只不过是echo或print一条消息了。很抱歉,你的echo或print语句所产生的输出,就是实际的html内容输出。解决此类问题的办法是,将你的session_start()调到程序的第一行。

2.
错误提示
Warning: open(F:/689\php\sessiondata\sess_66a39376b873f4daecf239891edc98b5, O_RDWR) failed
分析及解决方法
出现这样的错误语句一般是因为你的php.ini中关于session.save_path一项没有设置好,解决的方法是将session.save_path和session.cookie_path 设置置为
session_save_path = c:\temp
session.cookie_path = c:\temp
然后在c:\目录下建立一个temp目录,即可

3.
错误提示
Warning: Trying to destroy uninitialized session in
分析及解决方法
出类这样的提示,一般情况都是你直接调session_destroy()函数造成的。很多的朋友认为session_destroy()函数可以独立的运行,其实不然。解决的方法是在你调session_destroy()函数之前,要用session_start()开启session的功能。

4.问题:怎么获得当前session的id值呢?
最简单的方法是:
echo SID;
你会发现的。

5.问题:我的程序,在调用header函数之前没有任何的输出,虽然我include了一个config.php文件,但在config.php文件中也没有任何的输出,为什么session还是会报出与问题1同样的错误呢,是不是因为我在header之前用了session_start()的缘故呢?
答:或许你确实认真的检查了你的php程序,在引用header()之前确实也没有任何的输出,并且在你的include文件中也没有任何的输出!但是你是否用光标键在?>这个PHP代码结束语句后移动检查呢?那么你会发现在?>这个后面,有一个空行或几个空格,你删除了这几个空行或空格,那么问题就解决了。
注:此问题,会出PHP4.1.2中,更高版本,没有测试过。

6.问:用session做登录主页面后,其它页面怎么用session限制登录。。。
答:最简单的方法是
session_start();
if(!session_registered('login') ││ $login != true) {
echo "你没有登陆";
exit;
}

7.问:我用session_register()注册了session变量,可是当我用header或用javascript的重定向语句,那么在一下页面中,我却访问不到session所注册的变量值。请问如何解决?
问题的程序片段:
<?
session_start();
$ok = 'love you';
session_register('ok');
header("location : next.php");
?>

next.php
<?
session_start();
echo $ok;
?>

解决的方法:
当你用header函数或window.location这样的功能后,你上一个页面所注册的session变量,就会容易的丢失,关于这个问题的原因,至今仍没有一个详细的回答。
不过有解决的方法。如下所示
header("Location: next.php" ."?" . SID);
在跳转到下一页面的时候,将session的当前id做为一个参数,传到后一个页面。

8.session如何传数组
session_register('data');
$data=array(1,2,3,4);

方法是先注册后赋值

9.问题9:我是不是可以用像$HTTP_GET_VARS['**']方式来访问session值呢?

回答:可以,你可以使用如下global数组来访问session,以加强网页的安全性
$HTTP_SESSION_VARS
$_SESSION
例程:
<?php
session_start();
$username = 'stangly.wrong';
session_register('username');

echo $HTTP_SESSION_VARS['username'];
echo '<br>';
echo $_SESSION['username'];
?>
请参照此例程修改符合您自己的程序。

问题10:session_unregister() 和 session_destroy() 有何区别?
session_unregister()函数主要作用是注消当前的一个session变量。不过要注意的是,如果你用$HTTP_SESSION_VARS或$_SESSION在当前页面中引用过session变量,那么你可能需要和unset()配合 来注消session变量。
而session_destroy()是清除当前的session环境。意思就是说,当你用session_destroy()函数后,那么你就不可能再用session_is_registered()来检测session的变量了。但是需要注意的是他不能清除global中的session或使用了session cookie的中的session.所以在用session_destroy之前,最好不要用$HTTP_SESSION_VARS $_SESSION来访问session.(译自于php.net)

例程:
if(isset($_COOKIE[session_name()])) {
session_start();
session_destroy();
unset($_COOKIE[session_name()]);
}

转载。

公司数据库服务器mysql经常重启后就再也启不来了,搜了好久才找到这个方法。

2009年06月12日 星期五 10:02

我用yum安装完mysql后,service mysqld start 就报这个错,在网上搜,大多数都是说用netstat查看一下,说是mysql已经启动了,但我这根本就没有启动,有的说是权限问题,我把/etc/init.d/mysqld的权限改成777了,还是启不动,后来搜到了好几页,才找到真正的解决办法,在这里我记录一下,以便大家少走弯路。

解决办法:/usr/bin/mysql_install_db