一些优化

#####################################################system 优化

1增大系统连接数:ulimit -SHn 65535,如果不生效vi /etc/security/limits.conf文件最后加上

* soft nofile 65535

* hard nofile 65535

解决方法:

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

有如下三种修改方式:

在/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 种有效果

3 发现存在大量 TIME_WAIT 状态的连接

问题描述: 发现大量 time_wait 状态的连接 , 有时甚至达到 7000 以上

netstat n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"\t",state[key]}'

查找较多 time_wait 连接

netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20

发现是 DB 服务器的 mysql 和 memcache 的未释放导致。

建议:在 PHP程序开发时,建议大家尽量 mysql_close,减少 msyql消耗。

解决方法: 对于大量使用 tcp 连接的应用来说,也需要对 /etc/sysctl.conf 中的参数进行相应优化 :vim /etc/sysctl.conf

编辑文件,加入以下内容:

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_fin_timeout = 30

然后执行 /sbin/sysctl -p 让参数生效。优化后:发现大量的 TIME_WAIT 已不存在, mysql 进程的占用率很快就降下来的。

################################php-fpm 优化

2根据服务器内存情况,可以把PHP FastCGI子进程数调到100或以上,在4G内存的服务器上200就可以

服务器上内存为8G,我把PHP FastCGI子进程数调整到300

vi /usr/local/webserver/php/etc/php-fpm.conf

将max_children修改为300

增加 PHP-FPM 打开文件描述符的限制:

# vi /path/to/php-fpm.conf

找到“1024”

把1024 更改为 4096 或者更高.

<value name="process_control_timeout">5s</value>

<value name="style">static</value>

<value name="max_children">128</value>

<value name="rlimit_files">65535</value>

<value name="max_requests">102400</value>

##########################################################################

3.调高linux内核打开文件数量,可以使用这些命令(必须是root帐号)

echo 'ulimit -HSn 65536' >> /etc/profile

echo 'ulimit -HSn 65536' >> /etc/rc.local

source /etc/profile

###################################################php.ini 优化

调试php的时候开起display_errors = On   默认是off  ;生产环境建议关闭

disable_functions =passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepas

sthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_erro

r,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_g

etppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_s

etpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

把不用的函数禁用,会比较安全

open_basedir = /data/www/  #将用户可操作的文件限制在某目录

safe_mode = on #打开php的安全模式,开启重要文件的权限控制

safe_mode_exec_dir = /data/www/ #执行某些程序目录设置。不要执行任何程序,那么就可以指向我们网页目录

disable_functions = chdir,getcwd,scandir,chgrp,chmod,chown #禁用函数

expose_php = Off #关闭PHP版本信息在http头中的泄漏

display_errors = Off #禁止错误

关闭error_reporting

log_errors = On

post_max_size = 8M

upload_max_filesize = 10M

apc.enabled = 1

apc.cache_by_default = on

apc.shm_segments = 1

apc.shm_size = 32

apc.ttl = 600

apc.user_ttl = 600

apc.num_files_hint = 0

apc.write_lock = On

###########################################mysql 优化

[client]

#password       = your_password

default-character-set = utf8

port            = 33306

socket          = /data/mysql/mysql.sock

# Here follows entries for some specific programs

# The MySQL server

[mysqld]

port            = 33306

socket          = /data/mysql/mysql.sock

skip-external-locking

key_buffer_size = 16M

max_allowed_packet = 32M

table_open_cache = 64

sort_buffer_size = 512K

net_buffer_length = 8K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

open_files_limit    = 10240

max_connections = 3000

max_connect_errors = 6000

table_cache = 1024

back_log = 600

max_allowed_packet = 32M

sort_buffer_size = 2M

join_buffer_size = 2M

thread_cache_size = 64

thread_concurrency = 16

query_cache_size = 32M

query_cache_limit = 2M

query_cache_min_res_unit = 2k

#default-storage-engine = MyISAM

#default_table_type = MyISAM

thread_stack = 192K

transaction_isolation = READ-COMMITTED

tmp_table_size = 256M

max_heap_table_size = 256M

binlog_cache_size = 8M

max_binlog_cache_size = 8M

max_binlog_size = 512M

expire_logs_days = 7

key_buffer_size = 256M

read_buffer_size = 1M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 64M


myisam_sort_buffer_size = 128M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

myisam_recover

skip-name-resolve

#master-connect-retry = 10

#slave-skip-errors = 1032,1062,126,1114,1146,1048,1396

innodb_additional_mem_pool_size = 16M

innodb_buffer_pool_size = 1024M

innodb_file_io_threads = 4

innodb_thread_concurrency = 16

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 16M

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

innodb_file_per_table = 0

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

# Remove the next comment character if you are not familiar with SQL

#safe-updates

[myisamchk]

key_buffer_size = 20M

sort_buffer_size = 20M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout   #以上适合R720的配置

##########################################################################



你可能感兴趣的:(优化,服务器)