http://admclub.com/view/linux-nginx-php-mysql-memcached-eaccelerator-%E5%AE%89%E8%A3%85%E4%BC%98%E5%8C%96%E8%AE%B0%E5%BD%95

近日,某论坛因发展需要,增加了一台服务器,他们装好系统,叫我帮安装服务器环境,数据同步处理并希望做到负载均衡等。以下为过程记录。中间可能会有些过程被跳过,本来想写成教程的,但是年关已近,实在是太多事情需要处理。现只能先把记录发上来,以后看有没有时间重新整理成教程。

需要安装的软件:

Web服务器软件:Nginx1.0.11

FCGI:php5.2.17

数据库:MySQL5.1.x(服务器A),MySQL5.5(服务器B)

服务器优化:MemCached,eaccelerator,google-perftools

实时同步软件:rsync,inotify-tools

以下是两服务器可以相同的操作记录:

安装常见的依赖软件/lib/so:

yuminstallautoconfigautomakemakegccgcc-c++\

pcre-developenssl-develpatchpam-devellibmcrypt-develgd-devel-y

安装google-perftools

先要安装libunwind:

?

1

2

3

4

5

6

7

8

wgethttp://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz

tarzxvflibunwind-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

wgethttp://google-perftools.googlecode.com/files/google-perftools-1.7.tar.gz

tarxzvfgoogle-perftools-1.7.tar.gz

cdgoogle-perftools-1.7

./configure--prefix=/usr--enable-frame-pointers

make

makeinstall

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

wgethttp://nginx.org/download/nginx-1.0.11.tar.gz

tarxzvfnginx-1.0.11.tar.gz

cdnginx*

cdauto/cc

?

1

vigcc

找到

#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

makeinstall

为google-perftools添加线程目录

?

1

2

mkdir/tmp/tcmalloc

chmod0777/tmp/tcmalloc

将Nginx注册成系统服务

?

1

vi/etc/init.d/nginx

/etc/init.d/nginx文件内容如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

#!/bin/sh

#

#nginx-thisscriptstartsandstopsthenginxdaemin

#

#chkconfig:-8515

#description:NginxisanHTTP(S)server,HTTP(S)reverse\

#proxyandIMAP/POP3proxyserver

#processname:nginx

#config:/usr/local/nginx/conf/nginx.conf

#pidfile:/usr/local/nginx/logs/nginx.pid

#Sourcefunctionlibrary.

./etc/rc.d/init.d/functions

#Sourcenetworkingconfiguration.

./etc/sysconfig/network

#Checkthatnetworkingisup.

["$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–addnginx

chkconfignginxon

servicenginxstart

完成Nginx安装。

安装PHP和php-fpm

先下载PHP,PHP-FPM和freetype2源码

?

1

2

3

wgethttp://cn.php.net/get/php-5.2.17.tar.gz/from/this/mirror

wgethttp://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz

wgethttp://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

tarxzvffreetype-2.3.12.tar.gz

cdfreetype-2.3.12

makesetup

make

makeinstall

cd..

为PHP打上php-fpm补丁

?

1

2

tarxzvfphp-5.2.17.tar.gz

gzip-cdphp-5.2.17-fpm-0.5.14.diff.gz|patch-dphp-5.2.17-p1

再装一次依赖

?

1

yuminstalllibxml2-develgd-develcurl-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

makeinstall

不知道是不是兼容性问题,在CentOS6.2上我怎么也没办法让PHP支持libmhash和libmcrypt,尽管我已经正常安装了这些libs。以后有时候再处理这个。

安装Memcached

?

1

2

3

4

5

wgethttp://pecl.php.net/get/memcache-2.2.6.tgz

tarxzvfmemcache-2.2.6.tgz

cdmemcache-2.2.6

phpize

./configure--enable-memcache&&make&&makeinstall

安装eaccelerator

?

1

2

3

4

5

6

wgethttp://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-Oeaccelerator-0.9.6.1.zip

unzipeaccelerator-0.9.6.1.zip

cdeaccelerator-0.9.6.1

phpize

./configure--enable-eaccelerator&&make&&makeinstall

修改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

chmod0777/var/tmp/eaccelerator_cache

启动Memcached和php-fpm

?

1

2

3

4

memcached-d-p11211-umemcached-m64-c1024

memcached-d-p11212-umemcached-m64-c1024

memcached-d-p11213-umemcached-m1024

memcached-d-p11214-umemcached-m1024

启动php-fpm

?

1

php-fpmstart

安装MySQL5.5

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

wgethttp://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.19.tar.gz/from/http://mysql.cdpa.nsysu.edu.tw/

tarxzvfmysql-5.5.19.tar.gz

yuminstallcmake-y

cdmysql-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

makeinstall

初始化MySQL

?

1

2

3

4

5

6

7

8

9

10

useraddmysql

mkdir/var/mysql

chown-Rmysql:mysql/var/mysql

chmod+xscripts/mysql_install_db

scripts/mysql_install_db--basedir=/usr--datadir=/var/mysql

chown-Rmysql:mysql/var/mysql

cpsupport-files/my-medium.cnf/etc/my.cnf

cpsupport-files/mysql.server/etc/init.d/mysqld

将MySQL5.5注册成系统服务

?

1

2

3

chmod+x/etc/init.d/mysqld

chkconfig--addmysqld

chkconfigmysqldon

最后一步,启动MySQL

?

1

servicemysqldstart

好了,到这里就完成了基本的WEB环境的安装了。

休息下,写文章跟写代码一样累啊。

下一篇再把Nginx,PHP,MySQL的配置贴出吧,顺便将MySQL数据主从同步,图片实时同步和其它的一些优化配置写出来。