设为首页 收藏本站
查看: 2846|回复: 0

Linux+Nginx+MySQL+PHP+Xcache+Memcached

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-5-30 09:11:27 | 显示全部楼层 |阅读模式
要点:
一、安装nginx
二、安装mysql
三、安装php
四、测试phpinfo
五、安装xcache
六、ab压力测试
七、安装memcached
八、memcached共享session
九、memcached缓存mysql
--------------------------------------------
一、安装nginx
1、安装平台依赖包以及工具包
1
yum -y install gcc gcc-c++ make unzip vim



2、安装pcre;支持重写rewrite,正则表达式
1
2
3
4
5
unzip pcre-8.33.zip
cd pcre-8.33
./configure --prefix=/usr/local/pcre
make && make install
cd ..



3、安装zlib;支持网页压缩
1
2
3
4
5
6
tar xf zlib-1.2.8.tar.gz
5923
cd zlib-1.2.8
./configure  --prefix=/usr/local/zlib
make && make install
cd ..



4、 安装openssl;支持https
1
2
3
4
5
tar xf openssl-1.0.0l.tar.gz
cd openssl-1.0.0l
./config --prefix=/usr/local/openssl
make && make install
cd ..



5、创建运行nginx服务的账户和组
1
2
groupadd -r nginx
useradd -g nginx -r -s /sbin/nologin nginx



6、安装nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
tar xf nginx-1.4.7.tar.gz
cd nginx-1.4.7
./configure
--user=nginx
--group=nginx
--prefix=/usr/local/nginx
--with-pcre=../pcre-8.33
--with-zlib=../zlib-1.2.8
--with-openssl=../openssl-1.0.0l
--with-http_stub_status_module
--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
--with-http_gzip_static_module
--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
make && make install



7、提供nginx启动脚本
1
vim /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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/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:      /usr/local/nginx/conf/nginx.conf
# config:      /usr/local/nginx/sbin/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
  
NGINX_CONF_FILE="/usr/local/nginx/conf/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=([^ ]*).*//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



8、添加启动脚本执行权限
1
chmod +x /etc/init.d/nginx



9、 添加nginx到服务项并设置开启启动
1
2
chkconfig --add /etc/init.d/nginx
chkconfig nginx on



10、 启动nginx服务
1
service nginx start



11、 访问nginx测试页面
1
2
yum -y install elinks
elinks



wKiom1OHD06gDRGRAAGXGhdwgFY308.jpg
二、安装mysql
采用二进制安装,如果需要定制某些功能,可以使用源码编译的方式安装mysql数据库
  • 添加mysql账户和组
1
2
groupadd -r -g 306 mysql
useradd -g mysql -u 306 -s /sbin/nologin mysql



2、 解压mysql通用二进制包到/usr/local目录
1
2
3
tar -xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -sv mysql-5.5.33-linux2.6-x86_64 mysql



3、 创建数据目录,用来存放mysql数据文件,日志文件等
1
mkdir -pv /mydata/data



4、 修改数据目录的属主和属组为mysql,为了安全起见,数目文件时不允许让其它用户有查看权限的
1
2
chown -R mysql:mysql /mydata/data
chmod 750 /mydata/data



5、修改mysql包,其mysql为属主和属组,因为后期初始化数据库的用户为mysql
1
chown -R mysql:mysql /usr/local/mysql/*



6、 拷贝msql的执行配置文件和启动服务脚本文件

拷贝mysql配置文件的时候,需要注意你服务器内存的大小,因为我服务器的内存是8G的,所以拷贝的配置文件时my-innodb-heavy-4G.cnf
1
2
cp support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld



7、 简单的对数据库配置文件进行修改/etc/my.cnf
1
2
thread_concurrency = 8 #与cpu的核心数相同,能够最大程度上提高cpu的处理能力
datadir = /mydata/data #指定数据目录的路径



8、 初始化数据库
1
2
yum -y install libaio
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/data/



9、 为了安全起见,将mysql的源程序目录改成root用户
1
chown -R root /usr/local/mysql/*



10、 启动mysql服务
1
service mysqld start



11、 添加mysql对应的环境变量
1
2
3
4
5
6
修改文件
vim /etc/profile.d/mysql.sh
添加路径
export PATH=$PATH:/usr/local/mysql/bin

source /etc/profile  #重新读取环境变量



12、输出mysql的man手册到man命令的查找路径
1
2
3
4
修改文件
vim /etc/man.config
添加路径
MANPATH /usr/local/mysql/man



13、 输出mysql的头文件到系统头文件
1
ln -sv /usr/local/mysql/include /usr/include/mysql



14、  输出mysql的库文件
1
2
3
4
5
6
修改文件
vim /etc/ld.so.conf.d/mysql.conf
添加路径
/usr/local/mysql/lib

ldconfig -v  #重新加载库



15、 测试mysql

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
[iyunv@memcached ~]# mysql #测试环境变量是否生效
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.5.33-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.03 sec)

mysql>



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
[iyunv@memcached ~]# man mysql  #测试man是否生效
MYSQL(1)                     MySQL Database System                    MYSQL(1)

NAME
       mysql - the MySQL command-line tool

SYNOPSIS
       mysql [options] db_name

DESCRIPTION
       mysql is a simple SQL shell with input line editing capabilities. It supports interactive and noninteractive use.
       When used interactively, query results are presented in an ASCII-table format. When used noninteractively (for
       example, as a filter), the result is presented in tab-separated format. The output format can be changed using
       command options.

       If you have problems due to insufficient memory for large result sets, use the --quick option. This forces mysql
       to retrieve results from the server a row at a time rather than retrieving the entire result set and buffering it
       in memory before displaying it. This is done by returning the result set using the mysql_use_result() C API
       function in the client/server library rather than mysql_store_result().

       Using mysql is very easy. Invoke it from the prompt of your command interpreter as follows:

           shell> mysql db_name

       Or:

           shell> mysql --user=user_name --password=your_password db_name

       Then type an SQL statement, end it with “;”, g, or G and press Enter.

       Typing Control+C causes mysql to attempt to kill the current statement. If this cannot be done, or Control+C is
:



三、安装php
1、 安装依赖包和依赖库
1
yum -y install gcc gcc-c++ pcre pcre-devel make autoconf libxml2 libxml2-devel zlib zlib-devel glibc libjepg libjepg-devel libpng libpng-devel glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers bzip2 bzip2-devel



2、 Jpeg是一个基本的图像压缩方式。
1
2
3
4
5
tar xf jpegsrc.v9.tar.gz
cd jpeg-9/
./configure --prefix=/usr/local/phpextend --enable-shared --enable-static
make && make install
cd ..



3、 libpng软件包包含 libpng 库.这些库被其他程式用于读写png文件
1
2
3
4
5
tar xf libpng-1.6.2.tar.gz
cd libpng-1.6.2
./configure --prefix=/usr/local/phpextend
make && make install
cd ..



4、 FreeType库是一个完全免费(开源)的、高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件
1
2
3
4
5
tar xf freetype-2.4.12.tar.gz
cd freetype-2.4.12
./configure --prefix=/usr/local/phpextend
make && make install
cd ..



5、 php加密方式扩展库
1
2
3
4
5
tar xf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure --prefix=/usr/local/phpextend
make && make install
cd ..



6、 libmcrypt是加密算法扩展库。支持DES, 3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法
1
2
3
4
5
tar xf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure --prefix=/usr/local/phpextend
make && make install
cd ..



7、 支持编码转化函数
1
2
3
4
5
tar xf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/phpextend
make && make install
cd ..



8、 输出库文件
1
2
echo "/usr/local/phpextend/lib" >> /etc/ld.so.conf.d/phpextend.conf
ldconfig -v



9、 拷贝mysql的库文件
1
cp -rp /usr/local/mysql/lib/libmysqlclient.so.18.0.0  /usr/lib/libmysqlclient.so



10、 安装php
1
2
3
4
5
tar xf php-5.4.23.tar.gz
cd php-5.4.23
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config  --disable-debug  --with-iconv-dir --with-freetype-dir=/usr/local/phpextend --with-jpeg-dir=/usr/local/phpextend --with-png-dir=/usr/local/phpextend --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex  --enable-mbstring  --with-gd --enable-gd-native-ttf --enable-fpm --disable-ipv6 --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap
make ZEND_EXTRA_LIBS='-liconv'
make install



11、 拷贝php的配置文件
1
cp php.ini-development /usr/local/php/etc/php.ini



12、 拷贝php-fpm的启动脚本文件,并添加到服务中,设置开机启动
1
2
3
4
5
6
cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf



13、 添加启动php-fpm的用户
1
2
groupadd -r www
useradd -r -g www -s /sbin/nologin www



14、 修改php-fpm的配置文件
1
2
3
4
vim /usr/local/php/etc/php-fpm.conf
修改用户为www
user = www
group = www



15、 启动php-fpm

1
2
service php-fpm start
ps aux |grep php-fpm



四、测试phpinfo
nginx整合php

nginx.conf配置文件启用php
1
2
3
4
5
6
7
        location ~ .php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }



1
2
添加默认页面     
index index.php



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
更新fastcgi的参数,如果这里不更新,则显示php的调用函数页面,会吧php文件当做静态页面来处理
#vim fastcgi_params

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;



1
2
3
4
5
cat > /usr/local/nginx/html/index.php << EOF
phpinfo();
?>
EOF



访问phpinfo
wKioL1OHIIXjsSa8AANbcJNy1bs440.jpg
五、安装xcache
1、 安装xcache

1
2
3
4
5
6
7
8
tar xf xcache-1.3.2.tar.gz
cd xcache-3.1.0
/usr/local/php/bin/phpize
./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
make && make install

安装结束时,会出现类似如下行:
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/



2、 编辑php.ini,整合php和xcache
1
2
3
4
cat xcache.ini >> /usr/local/php/etc/php.ini

接下来编辑/etc/php/xcache.ini,找到extension开头的行,修改为如下行:
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so



3、 重新启动php-fpm

1
service php-fpm restart



4、 访问测试phpinfo是否加载xcache模块
wKioL1OHI3DRNIrQAAEffo1dsk4797.jpg
六、ab压力测试
1、 在没有安装xcache之前测试并发

分别并发100,500,100,3000 来测试页面phpinfo的响应请求的个数
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
[iyunv@memcached ~]# ab -c 100 -n 100 http://127.0.0.1/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient).....done


Server Software:        nginx/1.4.7
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.php
Document Length:        48346 bytes

Concurrency Level:      100
Time taken for tests:   0.119 seconds
Complete requests:      100
Failed requests:        16
   (Connect: 0, Receive: 0, Length: 16, Exceptions: 0)
Write errors:           0
Total transferred:      4849282 bytes
HTML transferred:       4834582 bytes
Requests per second:    841.19 [#/sec] (mean)
Time per request:       118.879 [ms] (mean)
Time per request:       1.189 [ms] (mean, across all concurrent requests)
Transfer rate:          39835.69 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        6   13   1.7     14      15
Processing:     2   51  29.7     50     101
Waiting:        1   51  29.9     49     101
Total:         17   64  29.2     64     113

Percentage of the requests served within a certain time (ms)
  50%     64
  66%     80
  75%     91
  80%     96
  90%    106
  95%    111
  98%    113
  99%    113
100%    113 (longest request)



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
[iyunv@memcached ~]# ab -c 500 -n 500 http://127.0.0.1/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests


Server Software:        nginx/1.4.7
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.php
Document Length:        48346 bytes

Concurrency Level:      500
Time taken for tests:   4.556 seconds
Complete requests:      500
Failed requests:        70
   (Connect: 0, Receive: 0, Length: 70, Exceptions: 0)
Write errors:           0
Total transferred:      24246424 bytes
HTML transferred:       24172924 bytes
Requests per second:    109.74 [#/sec] (mean)
Time per request:       4556.127 [ms] (mean)
Time per request:       9.112 [ms] (mean, across all concurrent requests)
Transfer rate:          5196.99 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       25   30   2.2     30      34
Processing:    19 1670 1322.5   1132    4504
Waiting:       18 1669 1321.5   1130    4503
Total:         52 1700 1321.0   1161    4532

Percentage of the requests served within a certain time (ms)
  50%   1161
  66%   3014
  75%   3062
  80%   3096
  90%   3147
  95%   3173
  98%   3186
  99%   4526
100%   4532 (longest request)
[iyunv@memcached ~]#



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
[iyunv@memcached ~]# ab -c 1000 -n 1000 http://127.0.0.1/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (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:        127.0.0.1
Server Port:            80

Document Path:          /index.php
Document Length:        192 bytes

Concurrency Level:      1000
Time taken for tests:   3.178 seconds
Complete requests:      1000
Failed requests:        334
   (Connect: 0, Receive: 0, Length: 334, Exceptions: 0)
Write errors:           0
Non-2xx responses:      666
Total transferred:      16431711 bytes
HTML transferred:       16275387 bytes
Requests per second:    314.65 [#/sec] (mean)
Time per request:       3178.131 [ms] (mean)
Time per request:       3.178 [ms] (mean, across all concurrent requests)
Transfer rate:          5049.07 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       38   45   4.0     45      53
Processing:    30  343 656.3     55    3096
Waiting:       22  340 657.5     54    3095
Total:         82  388 654.3     98    3135

Percentage of the requests served within a certain time (ms)
  50%     98
  66%    114
  75%    220
  80%    272
  90%   1199
  95%   1254
  98%   3117
  99%   3128
100%   3135 (longest request)
[iyunv@memcached ~]#



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
[iyunv@memcached ~]# ab -c 3000 -n 3000 http://127.0.0.1/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
Completed 2700 requests
Completed 3000 requests
Finished 3000 requests


Server Software:        nginx/1.4.7
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.php
Document Length:        192 bytes

Concurrency Level:      3000
Time taken for tests:   7.264 seconds
Complete requests:      3000
Failed requests:        2061
   (Connect: 0, Receive: 0, Length: 2061, Exceptions: 0)
Write errors:           0
Non-2xx responses:      939
Total transferred:      100275305 bytes
HTML transferred:       99821159 bytes
Requests per second:    412.99 [#/sec] (mean)
Time per request:       7264.129 [ms] (mean)
Time per request:       2.421 [ms] (mean, across all concurrent requests)
Transfer rate:          13480.64 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  701 1487.0     77    7023
Processing:     7 1017 1514.9    187    5698
Waiting:        7 1014 1516.7    187    5698
Total:          7 1718 2113.6    603    7195

Percentage of the requests served within a certain time (ms)
  50%    603
  66%   1395
  75%   2733
  80%   3716
  90%   4663
  95%   6694
  98%   7131
  99%   7170
100%   7195 (longest request)



2、 加载xcache模块之后测试phpinfo
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
[iyunv@memcached ~]# ab -c 100 -n 100 http://127.0.0.1/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient).....done


Server Software:        nginx/1.4.7
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.php
Document Length:        53200 bytes

Concurrency Level:      100
Time taken for tests:   0.134 seconds
Complete requests:      100
Failed requests:        11
   (Connect: 0, Receive: 0, Length: 11, Exceptions: 0)
Write errors:           0
Total transferred:      5334688 bytes
HTML transferred:       5319988 bytes
Requests per second:    744.71 [#/sec] (mean)
Time per request:       134.280 [ms] (mean)
Time per request:       1.343 [ms] (mean, across all concurrent requests)
Transfer rate:          38796.96 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        6   10   1.2     11      12
Processing:     9   63  34.0     63     120
Waiting:        7   62  34.2     62     120
Total:         19   73  33.2     73     129

Percentage of the requests served within a certain time (ms)
  50%     73
  66%     92
  75%    103
  80%    108
  90%    120
  95%    125
  98%    128
  99%    129
100%    129 (longest request)
[iyunv@memcached ~]#



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
[iyunv@memcached ~]# ab -c 500 -n 500 http://127.0.0.1/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests


Server Software:        nginx/1.4.7
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.php
Document Length:        53200 bytes

Concurrency Level:      500
Time taken for tests:   3.152 seconds
Complete requests:      500
Failed requests:        59
   (Connect: 0, Receive: 0, Length: 59, Exceptions: 0)
Write errors:           0
Total transferred:      26673435 bytes
HTML transferred:       26599935 bytes
Requests per second:    158.61 [#/sec] (mean)
Time per request:       3152.420 [ms] (mean)
Time per request:       6.305 [ms] (mean, across all concurrent requests)
Transfer rate:          8262.95 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       22   26   2.7     26      32
Processing:    17 1291 964.1   1152    3106
Waiting:       15 1291 964.2   1152    3105
Total:         47 1318 962.0   1180    3128

Percentage of the requests served within a certain time (ms)
  50%   1180
  66%   1721
  75%   1770
  80%   1799
  90%   3073
  95%   3104
  98%   3119
  99%   3124
100%   3128 (longest request)
[iyunv@memcached ~]#



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
[iyunv@memcached ~]# ab -c 1000 -n 1000 http://127.0.0.1/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (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:        127.0.0.1
Server Port:            80

Document Path:          /index.php
Document Length:        192 bytes

Concurrency Level:      1000
Time taken for tests:   3.203 seconds
Complete requests:      1000
Failed requests:        363
   (Connect: 0, Receive: 0, Length: 363, Exceptions: 0)
Write errors:           0
Non-2xx responses:      637
Total transferred:      19589771 bytes
HTML transferred:       19433853 bytes
Requests per second:    312.20 [#/sec] (mean)
Time per request:       3203.054 [ms] (mean)
Time per request:       3.203 [ms] (mean, across all concurrent requests)
Transfer rate:          5972.62 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       39   47   4.4     47      56
Processing:    31  394 698.3     58    3114
Waiting:       23  392 698.9     58    3114
Total:         81  440 696.1    101    3156

Percentage of the requests served within a certain time (ms)
  50%    101
  66%    172
  75%    272
  80%   1133
  90%   1258
  95%   1897
  98%   3135
  99%   3146
100%   3156 (longest request)
[iyunv@memcached ~]#



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
[iyunv@memcached ~]# ab -c 3000 -n 3000 http://127.0.0.1/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
Completed 2700 requests
Completed 3000 requests
Finished 3000 requests


Server Software:        nginx/1.4.7
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.php
Document Length:        192 bytes

Concurrency Level:      3000
Time taken for tests:   4.055 seconds
Complete requests:      3000
Failed requests:        2064
   (Connect: 0, Receive: 0, Length: 2064, Exceptions: 0)
Write errors:           0
Non-2xx responses:      936
Total transferred:      110438354 bytes
HTML transferred:       109984250 bytes
Requests per second:    739.77 [#/sec] (mean)
Time per request:       4055.315 [ms] (mean)
Time per request:       1.352 [ms] (mean, across all concurrent requests)
Transfer rate:          26594.72 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  498 831.9     76    3021
Processing:     1  498 836.7    193    2981
Waiting:        1  496 837.6    193    2979
Total:        124  996 1316.9    198    3985

Percentage of the requests served within a certain time (ms)
  50%    198
  66%    577
  75%   1208
  80%   3046
  90%   3451
  95%   3791
  98%   3906
  99%   3946
100%   3985 (longest request)



在并发高于3000的时候,加载xcache模块的php明显远远高于没有加载xcache模块的php!
可能这种证明不太准确;我记得xcache是多进程之间共享Opcode,这样压力测试可能不太合适;haha
七、安装memcached
1、 安装libevent

1
2
3
4
5
6
7
tar xf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/usr/local/libevent
make && make install

echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
ldconfig



2、 安装memcached
1
2
3
4
tar xf memcached-1.4.17.tar.gz
cd memcached-1.4.17
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make && make install



3、 memcached SysV的startup脚本代码如下所示,将其建立为/etc/init.d/memcached文件
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
#!/bin/bash
#
# Init file for memcached
#
# chkconfig: - 86 14
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached

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

## Default variables
PORT="11211"
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

RETVAL=0
prog="/usr/local/memcached/bin/memcached"
desc="Distributed memory caching"
lockfile="/var/lock/subsys/memcached"

start() {
        echo -n $"Starting $desc (memcached): "
        daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE "$OPTIONS"
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $lockfile
        return $RETVAL
}

stop() {
        echo -n $"Shutting down $desc (memcached): "
        killproc $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $lockfile
        return $RETVAL
}

restart() {
        stop
        start
}

reload() {
        echo -n $"Reloading $desc ($prog): "
        killproc $prog -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  condrestart)
        [ -e $lockfile ] && restart
        RETVAL=$?
        ;;      
  reload)
        reload
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
   *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        RETVAL=1
esac

exit $RETVAL



4、添加到服务项,设置开机启动
1
2
3
chmod +x /etc/init.d/memcached
chkconfig --add memcached
service memcached start



5、 启动memcached服务
1
service memcached start



6、 测试memcached
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
yum -y install telnet
[iyunv@memcached ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
stats
STAT pid 23624
STAT uptime 56
STAT time 1401396052
STAT version 1.4.17
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.028995
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END




八、memcached共享session
1、 安装Memcache的PHP扩展
1
2
3
4
5
6
7
8
tar xf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
make && make install

上述安装完后会有类似以下的提示:
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/



2、 编辑/usr/local/php/lib/php.ini,在“动态模块”相关的位置添加一行来载入memcache扩展:
1
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so



3、 重启php-fpm服务
1
service php-fpm restart



4、 访问phpinfo
wKiom1OHLpWCOtr-AAHCT41Oxdc820.jpg
5、 而后对memcached功能进行测试,在网站目录中建立测试页面test.php,添加如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211)  or die("Could not connect");

$version = $mem->getVersion();
echo "Server's version: ".$version."
";

$mem->set('testkey', '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)
";

$get_result = $mem->get('testkey');
echo "$get_result is from memcached server.";         
?>



如果有输出“Hello World is from memcached.”等信息,则表明memcache已经能够正常工作。
wKioL1OHLvuj8VmBAADUBjo3uEE221.jpg
6、 修改session的存储方式是memcache
1
2
session.save_handler = memcache
session.save_path="tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15"



7、 测试是否能够共享session
首先建立两个测试页面a.php b.php,然后存取都来自于memcached
wKiom1OHMj-Q1hcPAAC5G4-B8u4170.jpg
wKioL1OHMhPQKyEdAAChSzelQFQ392.jpg
说明:新建php页面b.php,获取当前用户的会话ID。
九、memcached缓存mysql
1、 首先创建一个存储库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[iyunv@memcached ~]# mysqladmin -uroot -p password "root123"
mysql> create database lee;
mysql> use lee;
mysql> CREATE TABLE lee1 (
    ->  id int(7) NOT NULL AUTO_INCREMENT,
    ->  name char(8) DEFAULT NULL,
    -> PRIMARY KEY (id)
    -> )ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
mysql> INSERT INTO lee1 VALUES (1,'tom'),(2,'jerry'),(3,'allen'),(4,'tuns'),(5,'kimi'),(6,'tjks'),(7,'test01'),(8,'test02'),(9,'test03');
mysql> select * from lee1;
+----+--------+
| id | name   |
+----+--------+
|  1 | tom    |
|  2 | jerry  |
|  3 | allen  |
|  4 | tuns   |
|  5 | kimi   |
|  6 | tjks   |
|  7 | test01 |
|  8 | test02 |
|  9 | test03 |
+----+--------+
9 rows in set (0.00 sec)



2、用于测试memcache是否缓存数据成功需要为这个脚本添加一个只读的数据库用户
1
2
3
4
5
mysql> grant select on lee.* to memcache@'%' identified by "12345";
Query OK, 0 rows affected (0.14 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)



3、 测试脚本
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
$memcachehost = '192.168.0.102';
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
$query="select * from lee1 limit 10";
$key=md5($query);
if(!$memcache->get($key))
{
                $conn=mysql_connect("192.168.0.102","memcache","12345");
                mysql_select_db(lee);
                $result=mysql_query($query);
                while ($row=mysql_fetch_assoc($result))
                {
                        $arr[]=$row;
                }
                $f = 'mysql';
                $memcache->add($key,serialize($arr),0,30);
                $data = $arr ;
}
else{
        $f = 'memcache';
        $data_mem=$memcache->get($key);
        $data = unserialize($data_mem);
}
echo $f;
echo "
";
echo "$key";
echo "
";
//print_r($data);
foreach($data as $a)
{
                echo "number is $a[id]";
                echo "
";
                echo "name is $a[name]";
                echo "
";

}
?>



4、 访问页面
mysql表示:因为php进程会向memcached中取数据,但是memcached是空的,所以memcached会向mysql数据库中取数据。

wKioL1OHYMrTxQD2AAGcPbHX_Po224.jpg
第二次访问,显示的是memcache,表示数据是从memcached读取的,而非mysql数据的
wKioL1OHYXzAhk8lAAG-4hCbA3Y320.jpg
失效时间为60秒,在60秒内取相同的数据都是从memcached取的而非mysql。
另外一种方式就是telnet客户端登陆memcached;如下
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@memcached ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get f2384e2682c0f204085a5b6326950ca5 #查看key对应的value
VALUE f2384e2682c0f204085a5b6326950ca5 0 448
a:9:{i:0;a:2:{s:2:"id";s:1:"1";s:4:"name";s:3:"tom";}i:1;a:2:{s:2:"id";s:1:"2";s:4:"name";s:3:"jim";}i:2;a:2:{s:2:"id";s:1:"3";s:4:"name";s:3:"abc";}i:3;a:2:{s:2:"id";s:1:"4";s:4:"name";s:3:"cde";}i:4;a:2:{s:2:"id";s:1:"5";s:4:"name";s:3:"fgh";}i:5;a:2:{s:2:"id";s:1:"6";s:4:"name";s:4:"test";}i:6;a:2:{s:2:"id";s:1:"7";s:4:"name";s:6:"test01";}i:7;a:2:{s:2:"id";s:1:"8";s:4:"name";s:6:"test02";}i:8;a:2:{s:2:"id";s:1:"9";s:4:"name";s:6:"test03";}}
END
delete f2384e2682c0f204085a5b6326950ca5 #删除key
DELETED
get f2384e2682c0f204085a5b6326950ca5  #再次取值为空
END



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-19892-1-1.html 上篇帖子: httpd2.4结合mysql5.5以及php5.5纯手工打造高效搭建LAMP运营平... 下篇帖子: LNMP环境添加memcache模块 Linux
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表