近日,某论坛因发展需要,增加了一台服务器,他们装好系统,叫我帮安装服务器环境,数据同步处理并希望做到负载均衡等。以下为过程记录。中间可能会有些过程被跳过,本来想写成教程的,但是年关已近,实在是太多事情需要处理。现只能先把记录发上来,以后看有没有时间重新整理成教程。
需要安装的软件:
Web服务器软件:Nginx 1.0.11
FCGI:php 5.2.17
数据库:MySQL 5.1.x(服务器A),MySQL 5.5(服务器B)
服务器优化:MemCached,eaccelerator,google-perftools
实时同步软件:rsync,inotify-tools
以下是两服务器可以相同的操作记录:
安装常见的依赖软件/lib/so:
yum install autoconfig automake make gcc gcc-c++ \
pcre-devel openssl-devel patch pam-devel libmcrypt-devel gd-devel -y
安装 google-perftools
先要安装 libunwind:
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz
tarzxvf libunwind-0.99.tar.gz
cdlibunwind-0.99/
CFLAGS=-fPIC ./configure--prefix=/usr
makeCFLAGS=-fPIC
makeCFLAGS=-fPICinstall
cd..
然后才开始安装google-perftools:
1
2
3
4
5
6
7
|
wget http:
//google-perftools
.googlecode.com
/files/google-perftools-1
.7.
tar
.gz
tar
xzvf google-perftools-1.7.
tar
.gz
cd
google-perftools-1.7
.
/configure
--prefix=
/usr
--
enable
-frame-pointers
make
make
install
cd
..
|
接着安装Nginx
为了优化Nginx,我使用了agentzh-memc-nginx-module模块和ngx_cache_purge,ngx_http_upstream_keepalive模块,ngx_cache_purge用来管理Nginx中缓存在Memcache中的内容而ngx_http_upstream_keepalive则用来保持Nginx与Memcached之间的连接,不用频繁建立和断开连接,也是优化Nginx吧。
不过,首先还是修改一下Nginx源码,这样可以使本来是5M大的Nginx内核减肥到只有680K大小!
1
2
3
4
|
wget http:
//nginx
.org
/download/nginx-1
.0.11.
tar
.gz
tar
xzvf nginx-1.0.11.
tar
.gz
cd
nginx*
cd
auto
/cc
|
1
|
vi
gcc
|
找到
# debug
CFLAGS="$CFLAGS -g"
然后在CFLAGS前面加上#号,得到:
# debug
#CFLAGS="$CFLAGS -g"
好了,保存退出。
回到 Nginx目录
1
|
cd
../..
|
开始编译安装Nginx,注意,由于原来该论坛使用Apache,所以我保留了原来的用户权限。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
.
/configure
--user=apache --group=apache --prefix=
/usr/local/nginx
\
--with-http_stub_status_module \
--with-http_ssl_module --with-openssl= \
--with-http_addition_module \
--with-zlib= \
--add-module=..
/agentzh-memc-nginx-module-a0bc33a
\
--add-module=..
/ngx_cache_purge-1
.3 \
--with-
file
-aio --with-google_perftools_module \
--with-http_gzip_static_module \
--add-module=..
/ngx_http_upstream_keepalive-d9ac9ad67f45
make
-j8
make
install
|
为google-perftools添加线程目录
1
2
|
mkdir
/tmp/tcmalloc
chmod
0777
/tmp/tcmalloc
|
将Nginx注册成系统服务
1
|
vi
/etc/init
.d
/nginx
|
/etc/init.d/nginx文件内容如下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
./etc/rc.d/init.d/functions
# Source networking configuration.
./etc/sysconfig/network
# Check that networking is up.
["$NETWORKING"="no"] && exit0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename$nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit5
[ -f $NGINX_CONF_FILE ] || exit6
echo-n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq0 ] && touch$lockfile
return$retval
}
stop() {
echo-n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq0 ] && rm-f $lockfile
return$retval
}
restart() {
configtest || return$?
stop
start
}
reload() {
configtest || return$?
echo-n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null2>&1
}
case"$1"in
start)
rh_status_q && exit0
$1
;;
stop)
rh_status_q || exit0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit0
;;
*)
echo$"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit2
esac
保存退出。
设置相关权限,并启动Nginx
1
2
3
4
|
chmod
+x
/etc/init
.d
/nginx
chkconfig –add nginx
chkconfig nginx on
service nginx start
|
完成Nginx安装。
安装PHP和php-fpm
先下载PHP,PHP-FPM和freetype2源码
1
2
3
|
wget http:
//cn
.php.net
/get/php-5
.2.17.
tar
.gz
/from/this/mirror
wget http:
//php-fpm
.org
/downloads/php-5
.2.17-fpm-0.5.14.
diff
.gz
wget http:
//downloads
.sourceforge.net
/project/freetype/freetype2/2
.3.12
/freetype-2
.3.12.
tar
.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Ffreetype%2Ffiles%2Ffreetype2%2F2.3.12%2F&ts=1314417995&use_mirror=softlayer
|
安装FreeType2
1
2
3
4
5
6
|
tar
xzvf freetype-2.3.12.
tar
.gz
cd
freetype-2.3.12
make
setup
make
make
install
cd
..
|
为PHP打上php-fpm补丁
1
2
|
tar
xzvf php-5.2.17.
tar
.gz
gzip
-
cd
php-5.2.17-fpm-0.5.14.
diff
.gz | patch -d php-5.2.17 -p1
|
再装一次依赖
1
|
yum
install
libxml2-devel gd-devel curl-devel -y
|
开始装PHP
1
2
3
4
5
6
7
8
9
|
.
/configure
--
enable
-fastcgi --
enable
-fpm --with-mysql \
--with-mysqli --with-gd=
/usr
--with-config-
file
-path=
/etc
\
--with-config-
file
-scan-
dir
=
/etc/php
.d --with-curl \
--
enable
-calendar --with-openssl --with-zlib --without-sqlite --disable-pdo \
--
enable
-mbstring --with-freetype-
dir
=
/usr
\
--
enable
-sockets --
enable
-zip
make
make
install
|
不知道是不是兼容性问题,在CentOS 6.2上我怎么也没办法让PHP支持libmhash和libmcrypt,尽管我已经正常安装了这些libs。以后有时候再处理这个。
安装Memcached
1
2
3
4
5
|
wget http:
//pecl
.php.net
/get/memcache-2
.2.6.tgz
tar
xzvf memcache-2.2.6.tgz
cd
memcache-2.2.6
phpize
.
/configure
--
enable
-memcache &&
make
&&
make
install
|
安装eaccelerator
1
2
3
4
5
6
|
wget http:
//downloads
.sourceforge.net
/project/eaccelerator/eaccelerator/eAccelerator
%200.9.6.1
/eaccelerator-0
.9.6.1.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Feaccelerator%2Ffiles%2F&ts=1325686230&use_mirror=nchc -O eaccelerator-0.9.6.1.zip
unzip eaccelerator-0.9.6.1.zip
cd
eaccelerator-0.9.6.1
phpize
.
/configure
--
enable
-eaccelerator &&
make
&&
make
install
|
修改PHP配置,使PHP支持Memcache和eaccelerator
1
|
vi
/etc/php
.ini
|
添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
extension= memcache.so
[eaccelerator]
zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.cache_dir="/var/tmp/eaccelerator_cache"
eaccelerator.shm_size="64"
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"
[memcache]
memcache.hash_strategy = "consistent"
memcache.default_timeout_ms = 300
|
保存退出。
运行下面两行命令,创建eaccelerator缓存目录
1
2
|
mkdir
/var/tmp/eaccelerator_cache
-p
chmod
0777
/var/tmp/eaccelerator_cache
|
启动Memcached和php-fpm
1
2
3
4
|
memcached -d -p 11211 -u memcached -m 64 -c 1024
memcached -d -p 11212 -u memcached -m 64 -c 1024
memcached -d -p 11213 -u memcached -m 1024
memcached -d -p 11214 -u memcached -m 1024
|
启动php-fpm
1
|
php-fpm start
|
安装MySQL 5.5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
wget http:
//dev
.mysql.com
/get/Downloads/MySQL-5
.5
/mysql-5
.5.19.
tar
.gz
/from/http
:
//mysql
.cdpa.nsysu.edu.tw/
tar
xzvf mysql-5.5.19.
tar
.gz
yum
install
cmake -y
cd
mysql-5.5.19
cmake . -DCMAKE_INSTALL_PREFIX=
/usr/
\
-DMYSQL_DATADIR=
/var/mysql
\
-DDEFAULT_CHARSET=utf8 \
-DWITH_EXTRA_CHARSETS=utf8,gbk,gb2312 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_LIBWRAP=1 \
-DWITH_SSL=
yes
\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1
make
make
install
|
初始化MySQL
1
2
3
4
5
6
7
8
9
10
|
useradd
mysql
mkdir
/var/mysql
chown
-R mysql:mysql
/var/mysql
chmod
+x scripts
/mysql_install_db
scripts
/mysql_install_db
--basedir=
/usr
--datadir=
/var/mysql
chown
-R mysql:mysql
/var/mysql
cp
support-files
/my-medium
.cnf
/etc/my
.cnf
cp
support-files
/mysql
.server
/etc/init
.d
/mysqld
|
将MySQL 5.5注册成系统服务
1
2
3
|
chmod
+x
/etc/init
.d
/mysqld
chkconfig --add mysqld
chkconfig mysqld on
|
最后一步,启动MySQL
1
|
service mysqld start
|
好了,到这里就完成了基本的WEB环境的安装了。
休息下,写文章跟写代码一样累啊。
下一篇再把Nginx,PHP,MySQL的配置贴出吧,顺便将MySQL数据主从同步,图片实时同步和其它的一些优化配置写出来。