一、对系统进行更新
yum update -ylsb_release -a
二、禁用SELINUX
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
这一步需要重启服务器
三、安装系统软件以及安装编译器
yum install -y \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 make bison cmake lsof rsync vixie-cron subversion \pcre pcre-devel lrzsz wget vim-common vim-enhanced ntp sudo chkconfig openssh* \gd gd2 gd-devel gd2-devel systemtap-sdt-devel GeoIP GeoIP-devel mod_geoip
四、安装Google-perftools (使用tcmalloc 加速 mysql 和 nginx)
centos 》 nginx 》gperftools》libunwind 依赖顺序, 先安装libunwind
cd /usr/local/srcwget -c http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz >libunwind-1.1.tar.gztar zxf libunwind-1.1.tar.gz cd libunwind-1.1CFLAGS=-fPIC ./configure --prefix=/usr --enable-sharedmake CFLAGS=-fPICmake CFLAGS=-fPIC install
安装google-perftools
cd /usr/local/srcwget -c ftp://ftp.tw.freebsd.org/pub/ports/distfiles/gperftools-2.1.tar.gz#wget -c http://gperftools.googlecode.com/files/google-perftools-2.1.tar.gz(现在googlecode.com被封了)tar -vxzf gperftools-2.1.tar.gzcd gperftools-2.1./configure \--prefix=/usr/local/gperftools \--disable-cpu-profiler \--enable-shared \--disable-heap-profiler \--disable-heap-checker \--disable-dependency-tracking \--enable-frame-pointersor./configure --prefix=/usr/local/gperftools --enable-frame-pointersmake && make install
到这里安装google-perftools完成了但未生效,接下来需要使google-perftools生效
/sbin/ldconfigecho "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.confldconfigor ln -s /usr/local/gperftools/lib/* /usr/local/lib ldconfig
如果不使用此步骤,则会在Nginx测试配置文件或启动的时候报以下错误:
./nginx: error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory
或者
yum install google-perftools google-perftools-devel
五、安装mysql (需要 cmake ncurses-devel bison库)
创建mysql需要的目录、配置用户和用户组
groupadd mysqluseradd -g mysql mysqlmkdir -p /data/mysqlchown -R mysql:mysql /data/mysql
安装msyql
wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.25.tar.gztar zxf mysql-5.6.25.tar.gzcd mysql-5.6.25cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \-DMYSQL_DATADIR=/data/mysql \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_MEMORY_STORAGE_ENGINE=1 \-DWITH_MYISAM_STORAGE_ENGINE=1 \-DSYSCONFDIR=/etc/ \-DWITH_SSL=yes \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci \-DWITH_READLINE=on \-DWITH_READLINE=1make && make install
配置mysql
ln -s /usr/local/mysql/lib/lib* /usr/lib/cd /usr/local/mysql./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql \--pid-file=/data/mysql/mysql.pid --tmpdir=/tmp --explicit_defaults_for_timestampcp ./support-files/my-default.cnf /etc/my.cnf
启动配置
设置mysql开机自动启动服务cp ./support-files/mysql.server /etc/rc.d/init.d/mysqldchkconfig --add mysqldchkconfig --level 345 mysqld on修改服务配置文件vim /etc/rc.d/init.d/mysqldbasedir=/usr/local/mysqldatadir=/data/mysql启动mysqld服务service mysqld start数据库初始化及修改root密码(root初始密码为空)./bin/mysql_secure_installation软连接mysqlln -s /usr/local/mysql/bin/mysql /bin 重启centos后,尝试用root连接mysqlmysql -u root -pstatus;
使用tcmalloc优化mysql ( 需要安装google-perftools),修改MySQL启动脚本(根据你的MySQL安装位置而定)
vim /usr/local/mysql/bin/mysqld_safe在#executing mysqld_safe的下一行,加上:export LD_PRELOAD=/usr/local/gperftools/lib/libtcmalloc.so重启服务,查看tcmalloc是否生效 (第二条命令显示即生效)service mysqld restartlsof -n | grep tcmalloc如果显示以下类似的信息,即表示tcmalloc生效lsof -n|grep tcmallocmysqld 1824 mysql mem REG 253,0 1853008 801969 /usr/local/gperftools/lib/libtcmalloc.so.4.1.2
六、安装Nginx(需要安装pcre)
创建账户
cd /usr/local/src/usr/sbin/groupadd webgrp/usr/sbin/useradd -g webgrp www ulimit -SHn 65535
安装nginx
cd /usr/local/srcwget http://nginx.org/download/nginx-1.7.1.tar.gztar zxvf nginx-1.7.1.tar.gzcd nginx-1.7.1vim ./src/core/nginx.h//修改NGINX_VERSION为你希望显示的版号//修改NGINX_VER为你希望显示的名称//修改NGINX_VAR 为你希望显示的名称
编译安装
./configure \--user=www \ --group=webgrp \ --prefix=/usr/local/nginx \ --conf-path=/etc/nginx/nginx.conf \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \--with-http_gzip_static_module \ --with-http_stub_status_module \ --with-google_perftools_module \ --with-http_secure_link_module \ --with-pcre=/usr/local/src/pcre-8.37 \ --with-http_realip_module \ --with-http_image_filter_module make && make install
or tengine
wget https://nginx-sticky-module.googlecode.com/files/nginx-sticky-module-1.1.tar.gzwget http://tengine.taobao.org/download/tengine-2.0.0.tar.gz //可以更改版本号以及名称wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gztar zxf tengine-2.0.0.tar.gztar zxf ngx_cache_purge-2.3.tar.gztar zxf nginx-sticky-module-1.1.tar.gz
编译安装
./configure \--prefix=/usr/local/nginx \--error-log-path=/data/logs/error/error.log \--http-log-path=/data/logs/access/access.log \--pid-path=/var/run/nginx/nginx.pid \--lock-path=/var/lock/nginx.lock \--conf-path=/etc/nginx/nginx.conf \--sbin-path=/usr/sbin/nginx \--user=www \--group=webgrp \--with-debug \--with-http_ssl_module \--with-http_realip_module \--with-http_addition_module \--with-http_image_filter_module \--with-http_sub_module \--with-http_dav_module \--with-http_flv_module \--with-http_mp4_module \--with-http_gzip_static_module \--with-http_random_index_module \--with-http_secure_link_module \--with-http_degradation_module \--with-http_sysguard_module \--with-backtrace_module \--with-http_stub_status_module \--with-http_upstream_check_module \--with-google_perftools_module \--with-http_geoip_module \--with-http_image_filter_module \--add-module=/usr/local/src/ngx_cache_purge-2.3 \--add-module=/usr/local/src/nginx-sticky-module-1.1
make && make install
如果提示google-perftools错误
修改/usr/local/src/tengine-2.0.0/auto/lib/google-perftools/conf 下次文件
if [ $ngx_found = no ]; then # FreeBSD port ngx_feature="Google perftools in /usr/local/gperftools" if [ $NGX_RPATH = YES ]; then ngx_feature_libs="-R/usr/local/gperftools/lib -L/usr/local/gperftools/lib -lprofiler" else ngx_feature_libs="-L/usr/local/gperftools/lib -lprofiler" fi . auto/featurefi
修改 nginx.conf ,令nginx可以google-perftools实现加速
vim /etc/nginx/nginx.conf#修改前面几行为:user www webgrp;worker_processes 8;#这个要看你的处理器的数量了error_log logs/error.log crit;pid logs/nginx.pid;google_perftools_profiles /tmp/tcmalloc/;events{ use epoll; worker_connections 65535;}测试和运行/usr/sbin/nginx -t
如果显示下面信息,即表示配置没问题
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is oknginx: configuration file /opt/nginx/conf/nginx.conf test is successful
输入代码运行nginx服务
/usr/sbin/nginxps au|grep nginx
如果显以类似下面的信息,即表示nginx已经启动
root 30038 0.0 0.0 5980 740 pts/0 S+ 00:12 0:00 grep nginx
输入代码检测是否支持加速
lsof -n | grep tcmalloc
如果显示类似下面的信息,即表示支持tcmalloc加速 (mysqld和nginx两个线程都支持)
mysqld 8297 mysql mem REG 253,0 1853334 274527 /usr/local/gperftools/lib/libtcmalloc.so.4.1.2nginx 8353 www 9w REG 253,0 0 278727 /tmp/tcmalloc/.8353nginx 8354 www 11w REG 253,0 0 278703 /tmp/tcmalloc/.8354nginx 8355 www 13w REG 253,0 0 278675 /tmp/tcmalloc/.8355nginx 8356 www 15w REG 253,0 0 278701 /tmp/tcmalloc/.8356nginx 8357 www 17w REG 253,0 0 278702 /tmp/tcmalloc/.8357nginx 8358 www 19w REG 253,0 0 278737 /tmp/tcmalloc/.8358nginx 8359 www 21w REG 253,0 0 278736 /tmp/tcmalloc/.8359nginx 8360 www 23w REG 253,0 0 278713 /tmp/tcmalloc/.8360
打开防火墙80端口、写入规则,保存并重启服务
iptables -I INPUT -p tcp --dport 80 -j ACCEPT/etc/rc.d/init.d/iptables save/etc/init.d/iptables restart
查看防火墙信息
/etc/init.d/iptables status如果显示以下类似信息,即表示已经打开了80端口1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
openresty安装
./configure \--prefix=/usr/local/openresty/nginx \--error-log-path=/data/logs/error/error.log \--http-log-path=/data/logs/access/access.log \--pid-path=/var/run/nginx/nginx.pid \--lock-path=/var/lock/nginx.lock \--conf-path=/etc/nginx/nginx.conf \--sbin-path=/usr/sbin/nginx \--user=www \--group=webgrp \--with-pcre \--with-pcre-jit \--with-http_ssl_module \--with-http_realip_module \--with-http_addition_module \--with-http_image_filter_module \--with-http_sub_module \--with-http_dav_module \--with-http_flv_module \--with-http_mp4_module \--with-http_gzip_static_module \--with-http_random_index_module \--with-http_secure_link_module \--with-http_degradation_module \--with-http_stub_status_module \--with-google_perftools_module \--with-http_geoip_module \--with-http_image_filter_module\--add-module=../nginx-sticky-module-1.1\--add-module=../nginx_upstream_check_module-master\--add-module=../ngx_cache_purge-2.3
编写nginx 启动服务
vim /etc/init.d/nginx
#!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig: - 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /etc/nginx/nginx.conf# config: /etc/sysconfig/nginx# pidfile: /var/run/nginx.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/local/nginx/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxstart() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval}stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retvalkillall -9 nginx}restart() { configtest || return $? stop sleep 1 start}reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUPRETVAL=$? echo}force_reload() { restart}configtest() {$nginx -t -c $NGINX_CONF_FILE}rh_status() { status $prog}rh_status_q() { rh_status >/dev/null 2>&1}case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2esac
保存后,设置权限,并添加到启动服务列表中
chmod 755 /etc/init.d/nginxchkconfig --add nginxchkconfig --level 345 nginx onservice nginx start
七、安装PHP
安装php依赖库jpeg
cd /usr/local/srcwget http://www.ijg.org/files/jpegsrc.v9.tar.gz tar zxvf jpegsrc.v9.tar.gzcd jpeg-9/./configure --enable-shared --enable-static --prefix=/usr/localmake && make install
png
cd /usr/local/srcwget http://prdownloads.sourceforge.net/libpng/libpng-1.6.2.tar.gztar zxvf libpng-1.6.2.tar.gzcd libpng-1.6.2/./configure --prefix=/usr/localmake && make install
freetype
cd /usr/local/srcwget http://download.savannah.gnu.org/releases/freetype/freetype-2.4.12.tar.gztar zxvf freetype-2.4.12.tar.gzcd freetype-2.4.12/./configure --prefix=/usr/localmake && make install
libmcrypt
cd /usr/local/srcwget http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gztar zxvf libmcrypt-2.5.8.tar.gzcd libmcrypt-2.5.8/./configure --prefix=/usr/localmake && make install/sbin/ldconfigcd libltdl/./configure --enable-ltdl-installmake && make install clean
libtool
cd /usr/local/srcwget http://ftpmirror.gnu.org/libtool/libtool-2.4.6.tar.gztar zxf libtool-2.4.6.tar.gzcd libtool-2.4.6./configure --prefix=/usr/local --enable-ltdl-installmake && make install
mhash
cd /usr/local/srcwget http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gztar zxvf mhash-0.9.9.9.tar.gzcd mhash-0.9.9.9/./configure --prefix=/usr/localmake && make install
mcrypt
cd /usr/local/srcwget http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gztar zxvf mcrypt-2.6.8.tar.gzcd mcrypt-2.6.8/export LDFLAGS="-L/usr/local/lib -L/usr/lib"export CFLAGS="-I/usr/local/include -I/usr/include"touch malloc.h/sbin/ldconfig./configure --prefix=/usr/local --with-libmcrypt-prefix=/usr/localmake && make install
编译安装PHP 5.6
cd /usr/local/srcwget http://cn2.php.net/distributions/php-5.6.13.tar.gztar zxvf php-5.6.13.tar.gzcd php-5.6.13/export LIBS="-lm -ltermcap -lresolv"export DYLD_LIBRARY_PATH="/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"export LD_LIBRARY_PATH="/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
编译
./configure \--prefix=/usr/local/php \--with-config-file-path=/etc/ \--with-config-file-scan-dir=/etc/php.d \--with-mysql=/usr/local/mysql \--with-mysqli=/usr/local/mysql/bin/mysql_config \--with-iconv-dir \--with-freetype-dir=/usr/local/lib \--with-jpeg-dir=/usr/local/lib \--with-png-dir=/usr/local/lib \--with-zlib \--with-libxml-dir=/usr \--enable-xml \--disable-rpath \--enable-bcmath \--enable-shmop \--enable-sysvsem \--enable-inline-optimization \--with-curl \--enable-mbregex \--enable-fpm \--enable-mbstring \--with-mcrypt=/usr/local/lib \--with-gd \--enable-gd-native-ttf \--with-openssl \--with-mhash \--enable-pcntl \--enable-sockets \--with-xmlrpc \--enable-zip \--enable-soap \--enable-opcache \--with-pdo-mysql \--enable-embed=shared \--enable-debug \--enable-dtrace \--enable-maintainer-zts
安装&&配置
make && make installcp php.ini-development /etc/php.ini
配置php-fpm
ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylibmv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vim /usr/local/php/etc/php-fpm.conf
#修改以下地方[global]pid = run/php-fpm.piderror_log = log/php-fpm.logemergency_restart_threshold = 10emergency_restart_interval = 1mprocess_control_timeout = 5spm.start_servers = 20pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_children = 35[www]user = wwwgroup = webgrp
修改nginx,支持php
vim /etc/nginx/nginx.conf找到并修改以下代码location ~ \.php$ { root /data/www;你的文档目录 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
将php-fpm
cd /usr/local/src/php-5.5.23cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmchmod 700 /etc/init.d/php-fpmchkconfig --add php-fpmchkconfig --level 345 php-fpm on服务方式启动php-fpmservice php-fpm restart编写测试页面vim /data/www/index.php输入代码hello php
编译安装PHP扩展
autoconf
cd /usr/local/srcwget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gztar zxvf autoconf-latest.tar.gzcd autoconf-2.69/./configure --prefix=/usr/localmake && make install
memcache
cd /usr/local/srcwget http://pecl.php.net/get/memcache-2.2.7.tgztar zxvf memcache-2.2.7.tgzcd memcache-2.2.7/export PHP_AUTOCONF=" /usr/local/bin/autoconf"export PHP_AUTOHEADER=" /usr/local/bin/autoheader" /usr/local/php/bin/phpize./configure --with-php-config= /usr/local/php/bin/php-configmake && make install
添加配置
vim /etc/php.ini extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20121212"extension = "memcache.so"开启opcache扩展[opcache]zend_extension = "opcache.so"opcache.memory_consumption=128opcache.interned_strings_buffer=8opcache.max_accelerated_files=4000opcache.revalidate_freq=60opcache.fast_shutdown=1opcache.enable_cli=1
snmp的安装
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gztar zxvf setuptools-0.6c11.tar.gzcd setuptools-0.6c11python setup.py buildpython setup.py install
下载地址http://www.net-snmp.org/download.html
./configure \--with-perl-modules \--prefix=/usr/local/net-snmp \--enable-mfd-rewrites \--with-default-snmp-version="2" \--with-sys-contact="pingzhao1990@163.com" \--with-sys-location="China" \--with-logfile="/var/log/snmpd.log" \--with-persistent-directory="/var/net-snmp" make && make install
加入环境变量
vim /etc/rc.local在文件的末尾添加上一行:/usr/local/net-snmp/sbin/snmpd -c /usr/local/net-snmp/share/snmp/snmpd.conf &vim /etc/profile找到有export关键词单独的一行前添加下面一行:PATH=/usr/local/net-snmp/bin:/usr/local/net-snmp/sbin:$PATH
DTrace
yum install systemtap-sdt-devel
configure: error: Don’t know how to define struct flock on this system, set –enable-opcache=no 错误的解决方法
ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib/ ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
八、Linux内核优化&&Nginx缓存优化