CenOS 6.3 + nginx 1.2.4 + PHP 5.4.8 + MySQL 5.5.28 + memcached
搞了好几天,终于把新服务器的配置弄完了,现在贴出来,也做个备忘!
另外还有 subversion-1.7.7 我就单独一篇来写了额
1. 修改 SSH 连接显示字符集 (可以忽略这一步,偶尔出现乱码影响也不大的)
# vi /etc/profile
添加:
LANG=zh_CN.GB2312
export LANG
使环境变量生效 保存后 断开 SSH 重新连接
# source /etc/profile
注意:
如果有添加这一步,在设置 mysql 密码时会出现个 sql 编码问题,需要注释掉这里的设置断开ssh后重连就可以了
2. 设置时区
# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
同步时间
# /usr/bin/rdate -s rdate.darkorb.net
写入 BIOS 避免失效
# hwclock -w
建议加入定时同步
# crontab -e
# 0 0 * * * /usr/bin/rdate -s rdate.darkorb.net
3. 配置服务器 yum 国内更新源地址
# cd /etc/yum.repos.d
# mv CentOS-Base.repo CentOS-Base.repo.bak
# wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
# yum makecache
4. 更新软件和安装依赖包
# sudo -s
# 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 ntpd nmap patch cmake bison cgi* *-gd-* libtool-* gettext *libxml-devel libcurl-devel libevent libevent-devel net-snmp-devel lsof unzip
如果出现错误 如:Error: Package: glibc-headers-2.12-1.80.el6_3.4.x86_64 (updates)
查看 http://www.zhouz.me/post/2012-10-25/40040279305
5. 设置路径
# vi /etc/ld.so.conf
添加:
/usr/lib/
/usr/local/lib/
/usr/lib64/
/usr/local/lib64/
6. 最好重启一次
# reboot
7. 开始安装 nginx
新建保存下载软件的目录
# mkdir -p /mydata/soft
# mkdir -p /var/www/html
# cd /mydata/soft/
下载:
# wget http://nginx.org/download/nginx-1.2.4.tar.gz
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.gz
# wget https://gperftools.googlecode.com/files/gperftools-2.0.tar.gz
# wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz
7.1 利用 TCMalloc 优化 nginx 性能
针对 64 位操作系统必须安装 libunwind 库
# tar zxvf libunwind-1.1.tar.gz
# cd libunwind-1.1/
# CFLAGS=-fPIC ./configure
# make CFLAGS=-fPIC
# make CFLAGS=-fPIC install
# cd ../
安装 google-perftools 优化
# tar zxvf gperftools-2.0.tar.gz
# cd gperftools-2.0/
# ./configure
# make && make install
# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
# /sbin/ldconfig
# cd ..
7.2 安装 pcre 使 nginx 支持 URL 重写
# tar zxvf pcre-8.31.tar.gz
# cd pcre-8.31
# ./configure
# make && make install
# cd ../
7.3 编译安装 nginx
# tar zxvf nginx-1.2.4.tar.gz
# cd nginx-1.2.4
# ./configure --prefix=/usr/local/nginx --with-google_perftools_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/mydata/soft/pcre-8.31
# make && make install
#cd ../
# mkdir /tmp/tcmalloc
# chmod 0777 /tmp/tcmalloc
7.4 添加用户和组
# /usr/sbin/groupadd www
# /usr/sbin/useradd -g www www
7.5 配置 nginx.conf
# vi /usr/local/nginx/conf/nginx.conf
注意:要在 pid 下一行添加下面的代码才能使优化生效
google_perftools_profiles /tmp/tcmalloc;
示例:替换为以下内容
user www www;
worker_processes 4;
worker_cpu_affinity 0001 0100 1000 0010;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
google_perftools_profiles /tmp/tcmalloc;
events {
worker_connections 65535;
}
http {
#include deny_ips.conf;
include mime.types;
default_type application/octet-stream;
server_tokens off;
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 logs/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
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 256k;
fastcgi_intercept_errors 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;
include vhost/*.conf;
}
7.6 启动 nginx
# ulimit -SHn 65535
# /usr/local/nginx/sbin/nginx
7.7 验证google-perftools是否已经正常加载 命令:
# lsof -n | grep tcmalloc
显示如下:
nginx 2395 nobody 9w REG 8,8 0 1599440 /tmp/tcmalloc.2395
nginx 2396 nobody 11w REG 8,8 0 1599443 /tmp/tcmalloc.2396
nginx 2397 nobody 13w REG 8,8 0 1599441 /tmp/tcmalloc.2397
nginx 2398 nobody 15w REG 8,8 0 1599442 /tmp/tcmalloc.2398
由于在Nginx配置文件中,设置worker_processes的值为4,因此开启了4个Nginx线程,每个线程会有一行记录。每个线程文件后面的数字值就是启动的Nginx的PID值。
至此,利用TCMalloc优化Nginx的操作完成。
7.8 优化 nginx 内核
# vi /etc/sysctl.conf
末尾添加:
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.core.somaxconn = 262144
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
刷新配置
# /sbin/sysctl -p
7.9 添加 nginx 的系统服务
之前在安装的时候写过了,详细内容可前往下面地址:
http://www.zhouz.me/post/2012-10-23/40041551802
7.10 添加各站点配置目录
# cd /usr/local/nginx/conf
# mkdir vhost
7.10.1 配置 localhost 站点
# vi vhost/localhost.conf
添加:
server
{
listen 80;
server_name localhost;
index index.html index.php;
root /var/www/html/;
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
#upload filesize limit
#client_max_body_size 10m;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
8. 安装 mysql
8.1 检测是否之前有安装过 rpm mysql包
查看系统是否有安装过 MySQL
注意大小写,如果mysql 不行就换MySQL
# rpm -qa | grep mysql
显示如:mysql-5.1.61-4.el6.x86_64
将搜索出的包名全部卸载:(nodeps表示强制删除),
例如:
# rpm -e --nodeps mysql-libs-5.1.61-4.el6.x86_64
8.2 下载 MySQL 相关软件包
# cd /mydata/soft
# wget http://www.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.28.tar.gz/from/http://cdn.mysql.com/
8.3 新建 MySQL 用户和组
# /usr/sbin/groupadd mysql
# /usr/sbin/useradd -g mysql mysql
8.4 新建目录并设置权限
# chmod +w /usr/local/mysql
# chown -R mysql:mysql /usr/local/mysql
# mkdir -p /var/mysql/data/
# mkdir -p /var/mysql/log/
# chown -R mysql:mysql /var/mysql/
# mkdir /home/mysql/temp
# chown -R mysql:mysql /home/mysql/temp
8.5 开始安装 MySQL
# tar xvf mysql-5.5.28.tar.gz
# cd mysql-5.5.28/
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_USER=mysql \
-DMYSQL_DATADIR=/var/mysql/data
# make && make install
8.6 配置 my.cnf 文件
# cd support-files/
注意:my-large.cnf 适用于1G内存左右的服务器,
可以根据自己配置情况选用 my-large.cnf 或 my-huge.cnf 等不同配置
# cp my-huge.cnf /etc/my.cnf
之前这里 my.cnf 的路径设置有问题(现已修改),导致没有生效,今天关闭 innodb 时才发现。。
是放在 etc 下面才会生效的。。
参考配置:关闭 innodb,binlog 保存 30 天内的
# vi /etc/my.cnf
--------------------------------------忽略分隔线---------------------------------------
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user = mysql
port = 3306
socket = /usr/local/mysql/mysql.sock
basedir = /usr/local/mysql
datadir = /var/mysql/data
tmpdir = /home/mysql/temp
log-error = /var/mysql/log/error.log
pid-file = /var/mysql/mysql.pid
open_files_limit = 10240
back_log = 200
max_connections = 1024
max_connect_errors = 1024
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 1M
thread_cache_size = 300
thread_concurrency = 4
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 3
log-slave-updates
log-bin = /var/mysql/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
#relay-log-index = /var/mysql/relaylog/relaylog
#relay-log-info-file = /var/mysql/relaylog/relaylog
#relay-log = /var/mysql/relaylog/relaylog
expire_logs_days = 30
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
interactive_timeout = 120
wait_timeout = 120
#skip-name-resolve
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
#master-host = 192.168.1.2
#master-user = username
#master-password = password
#master-port = 3306
server-id = 1
skip-innodb
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
slow-query-log = On
slow-query-log-file = /var/mysql/log/slow.log
long_query_time = 30
[mysqldump]
quick
max_allowed_packet = 32M
--------------------------------------忽略分隔线---------------------------------------
# cp mysql.server /etc/init.d/mysqld
# cd ../../