环境介绍:centos5.8_64
首先安装所需要的库
第一部分:安装Mysql5.5.28
1:检查删除自带mysql
rpm -e --allmatches --nodeps mysql
rm -rf /var/lib/mysql
2:创建MySQL数据库存放目录、配置用户和用户组
/usr/sbin/useradd -g mysql mysql
mkdir -p /yunwei8/mysql/data/
chown -R mysql:mysql /yunwei8/mysql/
3:安装MySQL 5.5.28
cd /soft
wget http://down.yunwei8.com/soft/linux/mysql-5.5.28.tar.gz
tar zxvf mysql-5.5.28.tar.gz
cd mysql-5.5.28
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/server/mysql -DMYSQL_DATADIR=/yunwei8/mysql/data -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
make
make install
chmod +w /usr/local/server/mysql
chown -R mysql:mysql /usr/local/server/mysql/
4:创建软连接
5:创建my.cnf配置文件:
#设置Mysql
#在support-files目录中有五个配置信息文件:
#my-small.cnf (内存<=64M)
#my-medium.cnf (内存 128M)
#my-large.cnf (内存 512M)
#my-huge.cnf (内存 1G-2G)
#my-innodb-heavy-4G.cnf (内存 4GB)
vi /etc/my.cnf
在[client]下添加一行
在[mysqld]下添如下行
character-set-server = utf8
default-storage-engine = MyISAM
basedir = /usr/local/server/mysql
datadir = /yunwei8/mysql/data
log-error = /yunwei8/mysql/mysql_error.log
在[mysqld]下添加修改
在[mysqldump]下添加修改
6:以mysql用户帐号的身份建立数据表:
7:设置mysql开机自动启动服务
cp /usr/local/server/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
8:修改服务配置文件
根据设定需要,修改mysqld文件中的下面两项
basedir=/usr/local/server/mysql
9:启动mysqld服务
10:通过命令行登录管理MySQL服务器并设置root密码(提示输入密码时直接回车):
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456789');
退出mysql
exit
11:软连接mysql
12:尝试用root连接mysql
成功登录后查看状态
status;
第二部分:mysql优化
为MySQL添加TCMalloc库的安装步骤(Linux环境):
1、64位操作系统请先安装libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API。
首先下载libunwind-1.1
wget http://down.yunwei8.com/soft/linux/libunwind-1.1.tar.gz
然后安装
cd libunwind-1.1
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
2、安装google-perftools:
首先下载gperftools-2.0
wget http://down.yunwei8.com/soft/linux/gperftools-2.0.tar.gz
然后安装
cd gperftools-2.0/
./configure
make && make install
/sbin/ldconfig
3、修改MySQL启动脚本(根据你的MySQL安装位置而定):
在# executing mysqld_safe的下一行,加上:
保存后退出,然后重启MySQL服务器。
4、使用lsof命令查看tcmalloc是否起效:
如果发现以下信息,说明tcmalloc已经起效:
mysqld 7395 mysql mem REG 8,2 1943638 194448 /usr/local/lib/libtcmalloc.so.4.1.0
--------------------------------------------------------------华丽分割线--------------------------------------------------------------
第三部分:nginx安装
安装Nginx所需的pcre库
1:下载pcre-8.3.2
wget http://down.yunwei8.com/soft/linux/pcre-8.32.tar.gz
解压并安装
cd pcre-8.32/
./configure
make && make install
cd ../
2:创建www用户和组,创建www虚拟主机使用的目录,以及Nginx使用的日志目录,并且赋予他们适当的权限
useradd -g www www
mkdir -p /web/www
chmod +w /web/www
chown -R www:www /web/www
mkdir -p /web/logs
chmod +w /web/logs
chown -R www:www /web/logs
3:为tcmalloc添加目录,并且赋予适当权限
chown -R www:www /tmp/tcmalloc/
4:安装niginx
下载nginx-1.2.4
wget http://down.yunwei8.com/soft/linux/nginx-1.2.4.tar.gz
解压并安装
cd nginx-1.2.4/
伪装服务器信息(可以不修改)
修改NGINX_VERSION为你希望显示的版号
修改NGINX_VER为你希望显示的名称
修改NGINX_VAR 为你希望显示的名称
make && make install
安装参数说明看这里:http://www.yunwei8.com/nginxcs
5:修改 nginx.conf ,令nginx可以 google-perftools实现加速
修改前面几行为:
worker_processes 8;
error_log /web/logs/nginx_error.log crit;
pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc/;
events{
use epoll;
worker_connections 65535;
}
6:测试和运行
增加软连接否则会报错
测试
如果显示下面信息,即表示配置没问题
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
输入代码运行nginx服务
ps aux|grep [n]ginx
如果显以类似下面的信息,即表示nginx已经启动
www 23019 0.0 2.6 68816 27160 ? S 08:48 0:00 nginx: worker process
www 23020 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process
www 23021 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process
www 23022 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process
www 23023 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process
www 23024 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process
www 23025 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process
www 23026 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process
输入代码检测是否支持加速
如果显示类似下面的信息,即表示支持tcmalloc加速 (mysqld和nginx八个线程都支持)
nginx 31655 www 9w REG 8,1 0 479533 /tmp/tcmalloc/.31655
nginx 31656 www 11w REG 8,1 0 479534 /tmp/tcmalloc/.31656
nginx 31657 www 13w REG 8,1 0 479535 /tmp/tcmalloc/.31657
nginx 31658 www 15w REG 8,1 0 479536 /tmp/tcmalloc/.31658
nginx 31659 www 17w REG 8,1 0 479537 /tmp/tcmalloc/.31659
nginx 31660 www 19w REG 8,1 0 479540 /tmp/tcmalloc/.31660
nginx 31661 www 21w REG 8,1 0 479538 /tmp/tcmalloc/.31661
nginx 31662 www 23w REG 8,1 0 479539 /tmp/tcmalloc/.31662
7:编写nginx 启动服务
输入以下代码并保存
# chkconfig: - 60 50
# description: nginx web server
# processname: nginx
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/opt/csw/bin:/var/nginx/sbin
NAME=nginx
DAEMON=/usr/local/server/nginx/sbin/nginx
PIDFILE=/usr/local/server/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
[ -x "$DAEMON" ] || exit 0
do_start()
{
if [ -f $PIDFILE ]; then
echo "Nginx program is runing!!!"
exit 1
else
$DAEMON
echo "Nginx program is the successful start!!! "
fi
}
do_stop()
{
if [ -f $PIDFILE ]; then
kill `cat $PIDFILE`
rm -f $PIDFILE
echo "Nginx program is stoping"
else
echo "Nginx program is not runing!!!"
fi
}
do_reload ()
{
if [ -f $PIDFILE ]; then
kill -HUP `cat $PIDFILE`
echo "Nginx program is reload"
else
echo "Nginx program is not runing!!!"
fi
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
reload)
do_reload
;;
*)
echo “Usage: $SCRIPTNAME {start|stop|restart|reload}” >&2
;;
esac
保存后,设置权限并添加到启动服务列表中
chkconfig --add nginx
chkconfig --level 345 nginx on
service nginx start
8:防火墙设置
端口开放
然后保存:
重启防火墙
--------------------------------------------------------------华丽分割线--------------------------------------------------------------
第四部分:安装php(FastCGI模式)
编译安装php-5.4.8所需的支持库:
1:安装libiconv-1.14
wget http://down.yunwei8.com/soft/linux/libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local
make && make install
cd ../
2:安装libmcrypt-2.5.8
wget http://down.yunwei8.com/soft/linux/libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make && make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make && make install
cd ../../
3:安装mhash-0.9.9.9
wget http://down.yunwei8.com/soft/linux/mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make && make install
cd ../
4:添加软连接
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
5:安装mcrypt-2.6.8
wget http://down.yunwei8.com/soft/linux/mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make && make install
cd ..
6:编译安装PHP(FastCGI模式)
6.1:centos5.8 64bit 执行如下两行:
ln -s /usr/lib/x86_64-linux-gnu/libjpeg* /usr/lib/
ln -s /usr/lib/x86_64-linux-gnu/libldap* /usr/lib/
6.2:安装php5.4.8
wget http://down.yunwei8.com/soft/linux/php-5.4.8.tar.gz
cd php-5.4.8/
make install
安装参数说明看这里:http://www.yunwei8.com/phpcs
cd ..
--------------------------------------------------------------华丽分割线--------------------------------------------------------------
第五部分:编译安装PHP5扩展模块
1:安装memcache-2.2.7
wget http://down.yunwei8.com/soft/linux/memcache-2.2.7.tgz
cd memcache-2.2.7/
/usr/local/server/php/bin/phpize
./configure --with-php-config=/usr/local/server/php/bin/php-config
make && make install
cd ../
2:安装eaccelerator-eaccelerator-42067ac
wget http://down.yunwei8.com/soft/linux/eaccelerator-eaccelerator-42067ac.tar.gz
cd eaccelerator-eaccelerator-42067ac
/usr/local/server/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/server/php/bin/php-config
make && make install
cd ../
3:安装PDO_MYSQL-1.0.2
建立软连接否则会报错
wget http://down.yunwei8.com/soft/linux/PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/server/php/bin/phpize
./configure --with-php-config=/usr/local/server/php/bin/php-config --with-pdo-mysql=/usr/local/server/mysql
make && make install
cd ../
4:安装ImageMagick
wget http://down.yunwei8.com/soft/linux/ImageMagick-6.8.0-2.tar.gz
cd ImageMagick-6.8.0-2
./configure
make && make install
cd ../
5:安装imagick-3.1.0RC2
wget http://down.yunwei8.com/soft/linux/imagick-3.1.0RC2.tgz
cd imagick-3.1.0RC2
/usr/local/server/php/bin/phpize
export PKG_CONFIG_PATH=//usr/local/lib/pkgconfig
./configure --with-php-config=/usr/local/server/php/bin/php-config
make && make install
cd ../
第六部分:配置php
1:修改php.ini文件
vi /usr/local/server/php/etc/php.ini
查找
修改为
并在此行后增加以下几行,然后保存:
extension = "pdo_mysql.so"
extension = "imagick.so"
查找
; Default Value: Off
修改为
Default Value: on
查找
修改为
2:配置eAccelerator加速PHP:
vi /usr/local/server/php/etc/php.ini
按shift+g键跳到配置文件的最末尾,加上以下配置信息:
zend_extension="/usr/local/server/php/lib/php/extensions/no-debug-non-zts-20100525/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/tmp/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
3:创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
vi /usr/local/server/php/etc/php-fpm.conf
复制以下代码
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 5s
daemonize = yes
[www]
listen = 127.0.0.1:9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = nobody
listen.group = nobody
listen.mode = 0666
user = www
group = www
pm = static
pm.max_children = 128
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
request_terminate_timeout = 0
request_slowlog_timeout = 0
slowlog = log/$pool.log.slow
rlimit_files = 65535
rlimit_core = 0
chroot =
chdir =
catch_workers_output = yes
php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f [email protected]
php_flag[display_errors] = off
--------------------------------------------------------------华丽分割线--------------------------------------------------------------
第七部分:优化Linux内核参数
在末尾增加以下内容
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
kernel.shmmax = 128000000 syscatl -p
php.ini中设置的eAccelerator,eaccelerator.shm_size="64",解释:eaccelerator可使用的共享内存大小(单位为MB),即64M
在Linux下,单个进程的最大内存使用量受/proc/sys/kernel/shmmax中设置的数字限制(单位为字节) /proc/sys/kernel/shmmax = 33554432 (32M)
临时更改该值:
echo 128000000 > /proc/sys/kernel/shmmax
或者
vi /etc/sysctl.conf
kernel.shmmax = 128000000 syscatl -p
使配置立即生效:
--------------------------------------------------------------华丽分割线--------------------------------------------------------------
第八部分:将php-fpm 作为服务运行
chmod 755 /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig --level 345 php-fpm on
服务方式启动php-fpm
第九部分:修改nginx,支持php
找到并修改以下代码
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /web/www$fastcgi_script_name;
include fastcgi_params;
}
提供一份完整的nginx.conf配置文件(仅供参考)
worker_processes 8;
error_log /web/logs/nginx_error.log crit;
pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc/;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 60;
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_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 /web/www;
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;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
charset utf-8;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /web/www/$fastcgi_script_name;
include fastcgi_params;
}
}
}