特点
拓扑图
-
可大量平行处理
-
nginx 的模块
-
与PHP的集成
三、拓扑图

四、安装 Nginx
1、系统环境
OS :Centos6.5 x86_64
Nginx: nginx-1.4.7.tar.g
己安装的包组
1
2
|
# yum groupinstall "Development Tools" "Server Platform Deveopment" -y
# yum install openssl-devel pcre-devel -y
|
2、安装过程
添加运行nginx程序的用户
1
2
|
root@essun download]
# groupadd -r nginx
root@essun download]
# useradd -r -g nginx nginx
|
解压并安装nginx
1
2
3
4
5
|
[root@essun download]
# ls
nginx-1.4.7 Discuz_X3.1_SC_UTF8.zip nginx-1.4.7.
tar
.gz
[root@essun download]
# cd nginx-1.4.7
[root@essun nginx-1.4.7]
# ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi
[root@essun nginx-1.4.7]
# make && make install
|
注解:
--prefix=/usr
安装目录
--sbin-path=/usr/sbin/nginx
可执行程序所在位置
--conf-path=/etc/nginx/nginx.conf
配置文件存放位置
--error-log-path=/var/log/nginx/error.log
错误日志存放位置
--http-log-path=/var/log/nginx/access.log
访问日志存放的位置
--pid-path=/var/run/nginx/nginx.pid
PID文件存放的位置
--lock-path=/var/lock/nginx.lock
锁文件位置
--user=nginx
运行程序的用户
--group=nginx
运行程序的组
--with-http_ssl_module
启用SSL认证模块
--with-http_flv_module
启用流媒体模块
--with-http_gzip_static_module
启用静态页面压缩
--http-client-body-temp-path=/var/tmp/nginx/client/
HTTP包体存放的临时目录
--http-proxy-temp-path=/var/tmp/nginx/proxy/
定义从后端服务器接收的临时文件的存放路径,可以为临时文件路径定义至多三层子目录的目录树
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
接收到FastCGI服务器数据,临时存放于本地某目录
为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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
[root@essun nginx-
1.4
.
7
]#vim /etc/rc.d/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
0
nginx=
"/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE=
"/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/
var
/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V
2
>&
1
| grep
"configure arguments:"
| sed
's/[^*]*--user=\([^ ]*\).*/\1/g'
-`
options=`$nginx -V
2
>&
1
| grep
'configure arguments:'
`
for
opt
in
$options;
do
if
[ `echo $opt | grep
'.*-temp-path'
` ]; then
value=`echo $opt | cut -d
"="
-f
2
`
if
[ ! -d
"$value"
]; then
# echo
"creating"
$value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit
5
[ -f $NGINX_CONF_FILE ] || exit
6
make_dirs
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
$retval
}
restart() {
configtest ||
return
$?
stop
sleep
1
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/
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
2
esac
|
加入开机启动列表
1
2
3
4
5
6
|
#chomd +x /etc/rc.d/init.d/nginx
# chmod +x /etc/rc.d/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
# chkconfig --list |grep nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
|
启动服务并测试
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@essun nginx-1.4.7]
# service nginx start
Starting nginx: [ OK ]
[root@essun nginx-1.4.7]
# curl -I http://192.168.1.111
HTTP
/1
.1 200 OK
Server: nginx
/1
.4.7
Date: Sun, 27 Apr 2014 00:04:42 GMT
Content-Type: text
/html
Content-Length: 612
Last-Modified: Sun, 27 Apr 2014 00:04:21 GMT
Connection: keep-alive
ETag:
"535c4985-264"
Accept-Ranges: bytes
|
五、编译mariadb 10
1、系统环境
OS :Centos6.5 x86_64
mariadb:mariadb-10.0.10
己安装的包组
1
2
|
# yum groupinstall "Development Tools" "Server Platform Deveopment" -y
#yum install -y cmake
|
2、安装过程
添加运行mariadb的用户
1
2
|
#groupadd -r mysql
#useradd -r -g mysql mysql
|
解压并安装
1
2
3
4
5
|
[root@essun download]# ls
[root@essun download]#cd /download/mariadb-
10.0
.
10
mariadb-
10.0
.
10
mariadb-
10.0
.
10
.tar.gz
[root@essun mariadb-
10.0
.
10
]#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mari -DMYSQL_DATADIR=/mariadb/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=
1
-DWITH_ARCHIVE_STORAGE_ENGINE=
1
-DWITH_BLACKHOLE_STORAGE_ENGINE=
1
-DWITH_READLINE=
1
-DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=
0
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
[root@essun mariadb-
10.0
.
10
]# make && make install
|
建立数据目录
1
2
|
#mkdir -p /mariadb/data
#chown -R mysql.mysql /mariadb/
|
更改安装目录的权限
1
2
|
#cd /usr/local/
#chown -R mysql.mysql mari/
|
添加服务启动脚本
1
2
3
4
5
6
7
|
[root@essun mariadb-10.0.10]
# cd /usr/local/mari/
[root@essun mari]
# cp support-files/mysql.server /etc/rc.d/init.d/mari
[root@essun mari]
# chmod +x /etc/rc.d/init.d/mari
[root@essun mari]
# chkconfig --add mari
[root@essun mari]
# chkconfig mari on
[root@essun mari]
# chkconfig --list |grep mari
mari 0:off 1:off 2:on 3:on 4:on 5:on 6:off
|
修改配置文件
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
|
[root@essun mari]# cp support-files/my-large.cnf /etc/my.cnf
[root@essun mari]# vim /etc/my.cnf
[root@essun mari]# grep -v
"#"
/etc/my.cnf |grep -v
"^$"
[client]
port =
3306
socket = /tmp/mysql.sock
[mysqld]
port =
3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache =
256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size =
8
query_cache_size= 16M
thread_concurrency =
4
datadir=/mariadb/data
log-bin=mysql-bin
binlog_format=mixed
server-id =
1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
|
初始化数据库
1
|
# scripts/mysql_install_db --user=mysql --datadir=/mariadb/data/
|
更新环境变量、帮助手册、库文件
1
2
3
4
|
#echo "export PATH=/usr/local/mari/bin:$PATH" >/etc/profile.d/mysql.sh
#source /etc/profile.d/mysql.sh
#echo "MANPATH /usr/local/mari/man" >> /etc/man.config
#ln -s lib /usr/include/mysql
|
启动服务
1
2
3
4
5
6
7
8
9
|
[root@essun mari]
# service mari start
Starting MySQL.................... [ OK ]
[root@essun mari]
# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection
id
is 4
Server version: 10.0.10-MariaDB-log Source distribution
Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to
clear
the current input statement.
MariaDB [(none)]>
|
添加一个php测试用户
1
|
MariaDB [(none)]> grant all on *.* to
'root'
@
'192.168.1.%'
identified by
'mari'
;
|
六、安装Php+Xcache
1、系统环境
OS :Centos6.5 x86_64h
php:php-5.4.26
己安装的包组
1
2
|
# yum groupinstall "Development Tools" "Server Platform Deveopment" -y
# yum install bzip2-devel -y
|
2、安装过程
解决依赖关系
1
2
3
4
5
6
7
8
9
10
|
[root@essun download]
#wget http://xmlsoft.org/sources/libxml2-2.9.0.tar.gz
[root@essun download]
# tar xf libxml2-2.9.0.tar.gz
[root@essun download]
# cd libxml2-2.9.0
[root@essun libxml2-2.9.0]
# ./configure --prefix=/usr/local/libxml
[root@essun libxml2-2.9.0]
# make && make install
[root@essun download]
# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
[root@essun download]
# tar xf libmcrypt-2.5.7.tar.gz
[root@essun download]
# cd libmcrypt-2.5.7
[root@essun libmcrypt-2.5.7]
# ./configure
[root@essun libmcrypt-2.5.7]
# make && make install
|
解压并安装php
1
2
3
4
5
6
|
[root@essun download]
# wget http://cn2.php.net/distributions/php-5.4.26.tar.bz2
[root@essun download]
# tar php-5.4.26.tar.bz2
[root@essun download]
# tar xf php-5.4.26.tar.bz2
[root@essun download]
# cd php-5.4.26
[root@essun php-5.4.26]
# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-openssl --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr/local/libxml --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
[root@essun php-5.4.26]
#make && make install
|
添加启动php-fpm服务脚本
1
2
3
4
5
6
|
[root@essun php-
5.4
.
26
]# cp php.ini-production /etc/php.ini
[root@essun php-
5.4
.
26
]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@essun php-
5.4
.
26
]# chmod +x /etc/rc.d/init.d/php-fpm
[root@essun php-
5.4
.
26
]# chkconfig --add php-fpm
[root@essun php-
5.4
.
26
]# chkconfig --list |grep php-fpm
php-fpm
0
:off
1
:off
2
:on
3
:on
4
:on
5
:on
6
:off
|
编辑配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
#vim /usr/local/php/etc/php-fpm.conf
[root@essun php-5.4.26]
# grep -v "^;" /usr/local/php/etc/php-fpm.conf |grep -v "^$"
[global]
pid =
/usr/local/php/var/run/php-fpm
.pid
error_log = log
/php-fpm
.log
process.max = 128
[www]
user = nobody
group = nobody
listen = 192.168.1.107:9000
listen.backlog = 128
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 6
|
启动php-fpm服务
1
2
3
4
5
6
|
[root@essun php-5.4.26]
# service php-fpm stop
Gracefully shutting down php-fpm .
done
[root@essun php-5.4.26]
# service php-fpm start
Starting php-fpm
done
[root@essun php-5.4.26]
# ss -tnl |grep 9000
LISTEN 0 128 192.168.1.107:9000 *:*
|
安装Xcache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@essun download]# wget http:
//xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.bz2
[root@essun download]# tar xf xcache-
3.1
.
0
.tar.bz2
[root@essun download]# cd xcache-
3.1
.
0
[root@essun xcache-
3.1
.
0
]# /usr/local/php/bin/phpize
[root@essun xcache-
3.1
.
0
]# ./configure --enable-xcache --
with
-php-config=/usr/local/php/bin/php-config
[root@essun xcache-
3.1
.
0
]# make && make install
#将最后一行生成的类似“/usr/local/php/lib/php/extensions/no-debug-non-zts-
20100525
/”复制到后面的xcache.ini文件中
[root@essun xcache-
3.1
.
0
]# mkdir /etc/php.d
[root@essun xcache-
3.1
.
0
]# cp xcache.ini /etc/php.d/
[root@essun xcache-
3.1
.
0
]# vim /etc/php.d/xcache.ini
[xcache-common]
;; non-Windows example:
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-
20100525
/xcache.so
#如果有多个
"extension ="
,请将复制的记录放在第一条!
|
修改nginx配置文件,提供php代理
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
|
#vim /etc/nginx/nginx.conf
[root@essun html]# grep -v
"#"
/etc/nginx/nginx.conf |grep -v
"^$"
worker_processes
1
;
error_log logs/error.log notice;
events {
worker_connections
1024
;
}
http {
include
mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout
65
;
server {
listen
80
;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
error_page
404
/
404
.html;
location = /
404
.html {
root html;
}
error_page
500
502
503
504
/50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass
192.168
.
1.107
:
9000
;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include
fastcgi_params;
}
}
}
|
创建php站点目录
1
2
3
4
5
6
7
8
9
|
#groupadd -g nginx
#useradd -r -g nginx nginx
#mkdir -p /usr/html
#chown -R nginx.nginx /usr/html
#vim /usr/html/index.php
#cat /usr/html/index.php
<?php
phpinfo();
?>
|
重启php-fpm服务,测试xcache是否启用成功
1
|
#service php-fpm restart
|
测试与mariadb的连接
1
2
3
4
5
6
7
8
9
10
|
#vim /usr/html/index.php
#cat /usr/html/index.php
<?php
$link = mysql_connect(
'192.168.1.108'
,
'root'
,
'mari'
);
if
($link)
echo
"Success..."
;
else
echo
"Failure..."
;
mysql_close();
?>
|
下载wordpress
1
|
[root@essun download]# http:
//cn.wordpress.org/wordpress-3.9-zh_CN.tar.gz
|
解压并安装
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
|
[root@essun download]
#tar xf wordpress-3.9-zh_CN.tar.gz -C /usr/html/
[root@essun download]
#cd /usr/html/wordpress
[root@essun wordpress]
# cp wp-config-sample.php wp-config.php
[root@essun wordpress]
# vim wp-config.php
[root@essun wordpress]
#cat wp-config.php
<?php
/**
* WordPress基础配置文件。
*
* 本文件包含以下配置选项:MySQL设置、数据库表名前缀、密钥、
* WordPress语言设定以及ABSPATH。如需更多信息,请访问
* {@link http:
//codex
.wordpress.org
/zh-cn
:%E7%BC%96%E8%BE%91_wp-config.php
* 编辑wp-config.php}Codex页面。MySQL设置具体信息请咨询您的空间提供商。
*
* 这个文件被安装程序用于自动生成wp-config.php配置文件,
* 您可以手动复制这个文件,并重命名为“wp-config.php”,然后填入相关信息。
*
* @package WordPress
*/
//
** MySQL 设置 - 具体信息来自您正在使用的主机 **
//
/** WordPress数据库的名称 */
define(
'DB_NAME'
,
'wordpress'
);
/** MySQL数据库用户名 */
define(
'DB_USER'
,
'root'
);
/** MySQL数据库密码 */
define(
'DB_PASSWORD'
,
'mari'
);
/** MySQL主机 */
define(
'DB_HOST'
,
'192.168.1.108'
);
......省略中........
|
注:
1、wordpress在192.168.1.108的数据库中事先己建立。
2、最好将此站点的所有文件同时拷贝到nginx中的站点目录中
测试
使用ab再测一下性能
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
|
[root@essun wordpress]
# ab -n 1000 -c 100 http://192.168.1.111/
Benchmarking 192.168.1.111 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: nginx
/1
.4.7
Server Hostname: 192.168.1.111
Server Port: 80
Document Path: /
Document Length: 7970 bytes
Concurrency Level: 100
Time taken
for
tests: 279.615 seconds
Complete requests: 1000
Failed requests: 201
(Connect: 0, Receive: 0, Length: 201, Exceptions: 0)
Write errors: 0
Non-2xx responses: 201
Total transferred: 6601361 bytes
HTML transferred: 6404612 bytes
Requests per second: 3.58 [
#/sec] (mean)
Time per request: 27961.471 [ms] (mean)
Time per request: 279.615 [ms] (mean, across all concurrent requests)
Transfer rate: 23.06 [Kbytes
/sec
] received
Connection Times (ms)
min mean[+
/-sd
] median max
Connect: 0 1 4.9 0 39
Processing: 559 27578 20218.7 14209 65345
Waiting: 559 27564 20219.3 14180 65345
Total: 597 27580 20218.1 14209 65345
Percentage of the requests served within a certain
time
(ms)
50% 14209
66% 28881
75% 52357
80% 60001
90% 60010
95% 60018
98% 60050
99% 60057
100% 65345 (longest request)
|
1、系统环境
OS :Centos6.5 x86_64
memcached:memcached-1.4.17
己安装的包组
1
2
|
# yum groupinstall "Development Tools" "Server Platform Deveopment" -y
#yum install -y libevent-devel
|
2、安装过程
下载memcached
1
|
[root@slave download]# wget http:
//www.memcached.org/files/memcached-1.4.17.tar.gz
|
解压并安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@slave download]
# tar xf memcached-1.4.17.tar.gz
[root@slave download]
# cd memcached-1.4.17
[root@slave memcached-1.4.17]
# ls
aclocal.m4 COPYING memcached_dtrace.d solaris_priv.c
assoc.c daemon.c memcached.h stats.c
assoc.h depcomp memcached.spec stats.h
AUTHORS doc missing t
cache.c
hash
.c NEWS testapp.c
cache.h
hash
.h protocol_binary.h thread.c
ChangeLog
install
-sh README.md timedrun.c
compile items.c sasl_defs.c trace.h
config.guess items.h sasl_defs.h util.c
config.h.
in
m4 scripts util.h
config.sub Makefile.am sizes.c version.m4
configure Makefile.
in
slabs.c
configure.ac memcached.c slabs.h
[root@slave memcached-1.4.17]
# ./configure --prefix=/usr/local/memcached
[root@slave memcached-1.4.17]
#make && make install
|
更新二进制文件、帮助手册、库文件
1
2
3
4
5
6
7
8
|
[root@slave memcached]
# echo "export PATH=/usr/local/memcached/bin:$PATH" > /etc/profile.d/memcache.sh
[root@slave memcached]
# cat /etc/profile.d/memcache.sh
export
PATH=
/usr/local/memcached/bin
:
/usr/local/mysql/bin
:
/usr/lib64/qt-3
.3
/bin
:
/usr/local/sbin
:
/usr/local/bin
:
/sbin
:
/bin
:
/usr/sbin
:
/usr/bin
:
/root/bin
:
/root/bin
[root@slave memcached]
# . /etc/profile.d/memcache.sh
[root@slave memcached]
# mem
memcached memhog
[root@slave memcached]
# echo "MANPATH /usr/local/memcached/share/man" >> /etc/man.config
[root@slave memcached]
# ln -s include/memcached /usr/include/memcache
|
Memcached启动参数说明:
-d 选项是启动一个守护进程,
-m 是分配给Memcache使用的内存数量,单位是MB,默认64MB
-M return error on memory exhausted (rather than removing items)
-u 是运行Memcache的用户,如果当前为root 的话,需要使用此参数指定用户。
-l 是监听的服务器IP地址,默认为所有网卡。
-p 是设置Memcache的TCP监听的端口,最好是1024以上的端口
-c 选项是最大运行的并发连接数,默认是1024
-P 是设置保存Memcache的pid文件
-f <factor> chunk size growth factor (default: 1.25)
启动memcached
1
2
|
#useradd -r memcached
#memcached -u memcached
|
注:
此处是前台运行。
为php安装扩展memcache模
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@essun download]
# wget http://pecl.php.net/get/memcache-2.2.7.tgz
[root@essun download]
# tar xf memcache-2.2.7.tgz
[root@essun download]
# cd memcache-2.2.7
[root@essun memcache-2.2.7]
# /usr/local/php/bin/phpize
Configuring
for
:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@essun memcache-2.2.7]
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
[root@essun memcache-2.2.7]
# make && make install
.......省略中........
Build complete.
Don
't forget to run '
make
test
'.
Installing shared extensions:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
|
安装完成的时候会生成一个memcache.so模块的路径,记得把这个路径记下来写到php的配置文件中,编辑/etc/php.ini,在“动态模块”相关的位置添加如下一行来载入memcache扩展:extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so然后就可以写一个测试页面,来测试php是否能与后端缓存服务器联系了.
测试页面
1
2
3
4
5
6
7
8
9
10
11
12
|
#vim /usr/html/wordpress/testmemcached.php
<?php
$mem = new memcache;
$mem->connect(
"192.168.1.109"
, 11211) or die(
"Could not connect"
);
$version = $mem->getVersion();
echo
"Server's version: "
.$version.
"<br/>\n"
;
$mem->
set
(
'hellokey'
,
'Hello World'
, 0, 600) or die(
"Failed to save data at the memcached server"
);
echo
"Store data in the cache (data will expire in 600 seconds)<br/>\n"
;
$get_result = $mem->get(
'hellokey'
);
echo
"$get_result is from memcached server."
;
?>
~
|
最后再看一次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
|
worker_processes 1;
error_log logs
/error
.log notice;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application
/octet-stream
;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
index index.php;
location / {
root html;
index index.php index.html index.htm;
}
error_page 404
/404
.html;
location =
/404
.html {
root html;
}
error_page 500 502 503 504
/50x
.html;
location =
/50x
.html {
root html;
}
location ~ \.php$ {
root
/usr/html
;
fastcgi_pass 192.168.1.107:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
root
/usr/html
;
fastcgi_pass 192.168.1.107:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
error_page 404
/404
.html;
}
}
}
|
测试结果
在Mamcached服务器上查看一下此条请求是否记录了
八、性能监控
安装memadin
1
2
3
4
5
6
7
|
[root@essun download]# http:
//www.junopen.com/memadmin/memadmin-1.0.12.tar.gz
[root@essun download]# tar xf memadmin-
1.0
.
12
.tar.gz
[root@essun download]# cd memadmin
[root@essun memadmin]# ls
apps images index.php LICENSE.txt views
config.php
include
langs README.txt
[root@essun download]# mv memadmin /usr/html/
|
此memadin目录也要放在nginx的站点目录下
配置并测试
点“开始管理”
这样就可以对memcached进行监控了