CentOS Nginx PHP MySQL Memcached 安装 配置 优化 (二)
接上篇??CentOS Nginx PHP MySQL Memcached 安装 配置 优化
四、配置篇
4.1 mysql配置
; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
;slowlog = log/$pool.log.slow
slowlog = /var/log/php-fpm.log.slow
; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_slowlog_timeout = 0
request_slowlog_timeout = 30s
; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_terminate_timeout = 0
; Set open file descriptor rlimit.
; Default Value: system defined value
;rlimit_files = 1024
; Set max core size rlimit.
; Possible Values: 'unlimited' or an integer greater or equal to 0
; Default Value: system defined value
;rlimit_core = 0
; Chroot to this directory at the start. This value must be defined as an
; absolute path. When this value is not set, chroot is not used.
; Note: you can prefix with '$prefix' to chroot to the pool prefix or one
; of its subdirectories. If the pool prefix is not set, the global prefix
; will be used instead.
; Note: chrooting is a great security feature and should be used whenever
;? ?? ? possible. However, all PHP paths will be relative to the chroot
;? ?? ? (error_log, sessions.save_path, ...).
; Default Value: not set
;chroot =
; Chdir to this directory at the start.
; Note: relative path can be used.
; Default Value: current directory or / when chroot
;chdir = /var/www
chdir = /var/www
; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Note: on highloaded environement, this can cause some delay in the page
; process time (several ms).
; Default Value: no
;catch_workers_output = yes
; Limits the extensions of the main script FPM will allow to parse. This can
; prevent configuration mistakes on the web server side. You should only limit
; FPM to .php extensions to prevent malicious users to use other extensions to
; exectute php code.
; Note: set an empty value to allow all extensions.
; Default Value: .php
;security.limit_extensions = .php .php3 .php4 .php5
; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp
; Additional php.ini defines, specific to this pool of workers. These settings
; overwrite the values previously defined in the php.ini. The directives are the
; same as the PHP SAPI:
;? ?php_value/php_flag? ?? ?? ?? ? - you can set classic ini defines which can
;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?be overwritten from PHP call 'ini_set'.
;? ?php_admin_value/php_admin_flag - these directives won't be overwritten by
;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? PHP call 'ini_set'
; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.
; Defining 'extension' will load the corresponding shared extension from
; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
; overwrite previously defined php.ini values, but will append the new value
; instead.
; Note: path INI options can be relative and will be expanded with the prefix
; (pool, global or /usr/local/web/php)
; Default Value: nothing is defined by default except the values in php.ini and
;? ?? ?? ?? ?? ? specified at startup with the -d argument
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
;php_flag[display_errors] = off
;php_admin_value[error_log] = /var/log/fpm-php.www.log
;php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M
复制代码
4.3Nginx配置
? ? #? ?? ?? ?? ?? ?? ?'$status $body_bytes_sent "$http_referer" '? ? #? ?? ?? ?? ?? ?? ?'"$http_user_agent" "$http_x_forwarded_for"';
? ? #access_log??logs/access.log??main;
? ? charset utf-8;
? ? server_names_hash_bucket_size 128;
? ? client_header_buffer_size 32k;
? ? large_client_header_buffers 4 32k;
? ? client_max_body_size 8m;
? ? sendfile on;
? ? tcp_nopush on;
? ? #tcp_nopush? ???on;
? ? #keepalive_timeout??0;
? ? keepalive_timeout??65;
? ? tcp_nodelay on;
? ? 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 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? ?? ? 80;
? ?? ???server_name??localhost;
? ?? ???index index.html index.htm index.php;
? ?? ???root /var/www;
? ?? ???#charset koi8-r;
? ?? ???#access_log??logs/host.access.log??main;
? ?? ???location ~ .*\.(php|php5)?$ {
? ?? ?? ?? ?? ? fastcgi_pass 127.0.0.1:9000;
? ?? ?? ?? ?? ? fastcgi_index index.php;
? ?? ?? ?? ?? ? include fastcgi.conf;
? ?? ???}
? ?? ???#error_page??404? ?? ?? ?? ???/404.html;
? ?? ???# redirect server error pages to the static page /50x.html
? ?? ???#
? ?? ???error_page? ?500 502 503 504??/50x.html;
? ?? ???location = /50x.html {
? ?? ?? ?? ?root? ?html;
? ?? ???}
? ?? ???# proxy the PHP scripts to Apache listening on 127.0.0.1:80
? ?? ???#
? ?? ???#location ~ \.php$ {
? ?? ???#? ? proxy_pass? ?http://127.0.0.1;
? ?? ???#}
? ?? ???# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
? ?? ???#
? ?? ???#location ~ \.php$ {
? ?? ???#? ? root? ?? ?? ???html;
? ?? ???#? ? fastcgi_pass? ?127.0.0.1:9000;
? ?? ???#? ? fastcgi_index??index.php;
? ?? ???#? ? fastcgi_param??SCRIPT_FILENAME??/scripts$fastcgi_script_name;
? ?? ???#? ? include? ?? ???fastcgi_params;
? ?? ???#}
? ?? ???# deny access to .htaccess files, if Apache's document root
? ?? ???# concurs with nginx's one
? ?? ???#
? ?? ???#location ~ /\.ht {
? ?? ???#? ? deny??all;
? ?? ???#}
? ?? ???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??/var/log/access.log??main;
? ? }
? ? # another virtual host using mix of IP-, name-, and port-based configuration
? ? #
? ? #server {
? ? #? ? listen? ?? ? 8000;
? ? #? ? listen? ?? ? somename:8080;
? ? #? ? server_name??somename??alias??another.alias;
? ? #? ? location / {
? ? #? ?? ???root? ?html;
? ? #? ?? ???index??index.html index.htm;
? ? #? ? }
? ? #}
? ? # HTTPS server
? ? #
? ? #server {
? ? #? ? listen? ?? ? 443;
? ? #? ? server_name??localhost;
? ? #? ? ssl? ?? ?? ?? ?? ?? ?on;
? ? #? ? ssl_certificate? ?? ?cert.pem;
? ? #? ? ssl_certificate_key??cert.key;
? ? #? ? ssl_session_timeout??5m;
? ? #? ? ssl_protocols??SSLv2 SSLv3 TLSv1;
? ? #? ? ssl_ciphers??ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
? ? #? ? ssl_prefer_server_ciphers? ?on;
? ? #? ? location / {
? ? #? ?? ???root? ?html;
? ? #? ?? ???index??index.html index.htm;
? ? #? ? }
? ? #}
}
复制代码
五、启动篇
启动MySQL
cp /usr/local/mysql/share/mysql/mysql.server /etc/rc.d/init.d/
chkconfig --add mysql.server ←加入service和开机启动
/etc/rc.d/init.d/mysql.server start ←手动启动,下面设置root密码
/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -uroot -p shutdown ←手动关闭
启动memcached
chmod 0777 /var/run ←允许普通用户程序写入pid文件
/usr/local/memcached/bin/memcached -p 11211 -d -u www -P /var/run/memcached.pid -m 64M -c 1024
参考命令格式:
/usr/local/memcached/bin/memcached -p 11211 -l 127.0.0.1 -d -u nobody -P /var/run/memcached.pid -m 64M -c 1024
几个参数的解释:
-p memcached监听的TCP端口
-l 监听的ip地址,127.0.0.1是本机,当然也可以写上你的服务器IP,如:61.150.91.26,这是我服务器的IP地址,如果你需要多个服务器都能够读取这台memcached的缓存数据,那么就必须设定这个ip
-d 以daemon方式运行,将程序放入后台
-u memcached的运行用户,我设定的是nobody
-P memcached的pid文件路径
-m memcached可以使用的最大内存数量
-c memcached同时可以接受的最大的连接数
如果你希望以socket方式来访问memcached,那么在启动的时候就必须去掉 -l和-p参数,并加上-s参数:
-s memcached的socket文件路径
启动mephp-fpm和nginx
ulimit -SHn 65535
/usr/local/php/sbin/php-fpm
/usr/local/nginx/sbin/nginx
以将这3条语句写入/etc/rc.local文件,
这样每次开机就可以启动php-fpm和nginx了。
优化Linux内核参数
- vi /etc/sysctl.conf
在文件末尾增加以下内容:
# append
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535
使配置立即生效:
- /sbin/sysctl -p
六. References & thanks
? http://blog.s135.com/nginx_php_v6/
? http://blog.csdn.net/shagoo/archive/2008/08/05/2772418.aspx
? http://php-fpm.org/download/
? http://janwer.javaeye.com/blog/210952
? http://blog.chinaunix.net/u/31547/showart_493408.html
? http://www.yanghengfei.com/archives/294/
? http://bbs.linuxsky.org/thread-5561-1-1.html