lnmp环境的优化


注意:修改配置文件,如果要修改pid进程文件相关,socket文件相关的参数的值时,一定要把服务stop掉



nginx的优化

vi nginx.conf                                      (最优配置)   

#user nobody nobody;

worker_processes  8;

error_log  logs/error.log;

pid        logs/nginx.pid;

worker_rlimit_nofile 51200;                    (确保ulimit -n,打开文件数至少为51200


events {

    use epoll;

    worker_connections  51200;

}


http {

    include       mime.types;

    default_type  application/octet-stream;

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

                        '$status $body_bytes_sent "$http_referer" '

                        '"$http_user_agent" $http_x_forwarded_for';

                                                 

    sendfile       on;


    tcp_nopush     on;

    tcp_nodelay    on;


    client_header_buffer_size 32k;

    large_client_header_buffers 4 32k;

    client_max_body_size 8m;

    server_tokens off;


    keepalive_timeout  60;


    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;

                                                                                                                                                                                                                

    gzip  on;

    gzip_min_length 1k;

    gzip_buffers 4 16k;

    gzip_http_version 1.1;

    gzip_comp_level 2;

    gzip_types text/plain application/x-javascript text/css application/xml text/javascript;

    gzip_vary on;

    gzip_proxied    expired no-cache no-store private auth;

    gzip_disable    "MSIE [1-6]\.";


    #server   { 

    #   listen 80 default;  

    #   server_name _;  

    #   return 500;  

    #}


    server {

        listen       80;

        server_name  www.scj.com;

        root /opt/nginx/www/www.scj.com;

        index index.html index.php index.htm;

        access_log  logs/www.scj.com/access.log main;

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }


        location ~ .*\.php$ {

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            include        fastcgi_params;

            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

        }


        location ~ .*\.(gif|jpg|png|swf|bmp|jpeg)$ {

            expires 10d;

        }


        location ~ .*\.(js|css)?$ {

            expires 1h;

        }

    }

}






php的优化:

vi /usr/local/php/etc/php-fpm.conf                 (最优配置)

[global]

pid = /usr/local/php/run/php-fpm.pid

error_log = /tmp/php-fpm.log

log_level = notice

emergency_restart_threshold = 10

emergency_restart_interval = 1m

process_control_timeout = 5s

daemonize = yes

rlimit_files = 65536

events.mechanism = epoll



[www]

user = nobody

group = nobody

listen = 127.0.0.1:9000

listen.owner = nobody

listen.group = nobody

listen.mode = 0666

listen.allowed_clients = 127.0.0.1

pm = static

pm.max_children =250

#pm.start_servers = 20

#pm.min_spare_servers = 10

#pm.max_spare_servers = 30

pm.max_requests = 10240

slowlog = /tmp/$pool.log.slow

request_slowlog_timeout = 600

request_terminate_timeout = 0

rlimit_files = 65536

catch_workers_output = yes

security.limit_extensions = .php .php3 .php4 .php5 .5p

注意:

pm = static参数:

对于专用服务器,pm可以设置为static。

如何控制子进程,选项有static和dynamic。如果选择static,则仅由pm.max_children参数指定固定的子进程数。如果选择dynamic,则由下列参数决定(每个进程(php-cgi)正常运行一段时间大概占用20M-30M,所以根据自己的内存计算,比如我分配给php的内存是4G)

pm.max_children = 200  #子进程最大数

pm.start_servers = 100  #启动时的进程数

pm.min_spare_servers = 50  #保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程

pm.max_spare_servers = 150  #保证空闲进程数最大值,如果空闲进程大于此值,则进行清理








mysql优化:

v/usr/local/mysql/my.cnf                           (主要优化的是innodb)

[mysqld]

basedir = /usr/local/mysql

datadir = /opt/mysql/data

port = 3306

server-id = 1

pid-file = /opt/mysql/data/mysql.pid

socket = /tmp/mysqld.sock

default_storage_engine = InnoDB

log-bin = /opt/mysql/binlog/mysql-binlog

expire_logs_days = 14

max_binlog_size = 5G

binlog_cache_size = 10M

max_binlog_cache_size = 20M

slow_query_log

long_query_time = 2

slow_query_log_file = /opt/mysql/data/slow.log

open_files_limit = 65535

innodb = FORCE

innodb_buffer_pool_size = 100M

innodb_log_file_size = 1G

query_cache_size = 0

thread_cache_size = 64

table_definition_cache = 512

table_open_cache = 512

max_connections = 20

sort_buffer_size = 10M

max_allowed_packet = 6M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


[client]

#socket=/var/lib/mysql/mysql.sock

注意:

innodb_buffer_pool_size主要作用是缓存innodb表的索引,数据,插入数据时的缓冲;

                                                  默认值:128M;

                                                  专用mysql服务器设置此值的大小: 系统内存的70%-80%最佳。

                                                  如果你的系统内存不大,查看这个参数,把它的值设置小一点吧(若值设置大了,启动会报错)  


[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql/data
port = 3306
socket = /var/lib/mysql/mysql.sock
pid-file = /var/lib/mysql/mysql.pid
default_storage_engine = InnoDB
expire_logs_days = 14
max_binlog_size = 1G
binlog_cache_size = 10M
max_binlog_cache_size = 20M
slow_query_log
long_query_time = 2
slow_query_log_file = /data/mysql/logs/slowquery.log
open_files_limit = 65535
innodb = FORCE
innodb_buffer_pool_size = 4G
innodb_log_file_size = 1G
query_cache_size = 0
thread_cache_size = 64
table_definition_cache = 512
table_open_cache = 512
max_connections = 1000
sort_buffer_size = 10M
max_allowed_packet = 6M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error = /data/mysql/logs/error.log
  
[client]
socket = /var/lib/mysql/mysql.sock
port = 3306


线上:

[mysqld]
basedir = /opt/mysql
datadir = /data/mysql/data
log-bin = /data/mysql/binlog/mysql-bin
binlog_format = mixed
log_slave_updates = 1
relay-log = /data/mysql/binlog/relay-log-bin
relay-log-index = /data/mysql/binlog/slave-relay-bin.index
server-id = 1446098377
expire_logs_days = 7
port = 3306
socket = /opt/mysql/mysql.sock
pid-file = /opt/mysql/mysql.pid
default_storage_engine = InnoDB
max_binlog_size = 1G
binlog_cache_size = 64M
max_binlog_cache_size = 128M
slow_query_log
long_query_time = 2
slow_query_log_file = /data/mysql/logs/slowquery.log
open_files_limit = 65535
innodb = FORCE
innodb_buffer_pool_size = 2G
innodb_log_file_size = 1G
query_cache_size = 0
thread_cache_size = 64
table_definition_cache = 512
table_open_cache = 512
max_connections = 1000
sort_buffer_size = 10M
max_allowed_packet = 6M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error = /data/mysql/logs/error.log

[client]
socket = /opt/mysql/mysql.sock
port = 3306