一,mysql源码编译安装:
安装:
get mysql-boost-5.7.11.tar.gz
tar zxf mysql-boost-5.7.11.tar.gz
get cmake-2.8.12.2-4.el6.x86_64.rpm
cd mysql-5.7.11/
yum install -y gcc gcc-c++ ncurses-devel bison
[root@server4 mysql-5.7.11]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql #安装目录 -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data #数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock ##Unix socket 文件路径
-DWITH_INNOBASE_STORAGE_ENGINE=1 #安装 innodb 存储引擎
-DWITH_MYISAM_STORAGE_ENGINE=1 #安装 myisam 存储引擎
-DDEFAULT_CHARSET=utf8 #使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci #校验字符
-DEXTRA_CHARSETS=all #安装所有扩展字符集
-DWITH_BOOST=/root/mysql-5.7.11/boost/boost_1_59_0/
make
make install
初始化:
groupadd -g 27 mysql
useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/ mysql
id mysql
usermod -s /sbin/nologin mysql
tail -n 1 /etc/passwd
vim .bash_profile
添加在PATH后面:/usr/local/lnmp/mysql/bin/
source .bash_profile
cp /etc/my.cnf /etc/my.cnf.bak
vim /etc/my.cnf
datadir=/usr/local/lnmp/mysql/data
socket=/usr/local/lnmp/mysql/data/mysql.sock
cd /usr/local/lnmp/mysql
chown mysql.mysql . -R
cp mysql.server /etc/init.d/mysqldcp mysql.server #启动脚本
mysqld --initialize --user=mysql #初始化
/etc/init.d/mysqld start
mysql_secure_installation
mysql -p
二,php安装:
tar jxf php-5.6.20.tar.bz2
cd php-5.6.20
./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local//lnmp/php/etc --with-mysql --with-mysqli --with-pdo-mysql --enable-mysqlnd --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-pear --with-gettext --with-gmp --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash
#安装库
yum install -y libxml2-devel openssl-devel curl-devel gd-devel-2.0.35-11.el6.x86_64.rpm gmp-devel libmcrypt-devel-2.5.8-9.el6.x86_64.rpm re2c-0.13.5-1.el6.x86_64.rpm net-snmp-devel
make && make install
初始化php:
cd /usr/local/lnmp/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
21 [global]
22 ; Pid file
23 ; Note: the default prefix is /usr/local/lnmp/php/var
24 ; Default Value: none
25 pid = run/php-fpm.pid #去掉注释
cd php-5.6.20
cp php.ini-production /usr/local/lnmp/php/etc/php.ini
vim php.ini
925 date.timezone =Asia/Shanghai
cp init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
useradd -u 800 -M -d /usr/local/lnmp/nginx nginx
/etc/init.d/php-fpm start
安装nginx:
cd nginx-1.10.1
vim nginx-1.10.1/src/core/nginx.h
14 #define NGINX_VER "nginx"
vim nginx-1.10.1/auto/cc/gcc
178 # debug
179 #CFLAGS="$CFLAGS -g"
yum install -y pcre-devel
cd nginx-1.10.1
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --user=nginx --group=nginx
make && make install
ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/
nginx
php和nginx关联:
vim /usr/local/lnmp/nginx/html/index.php
<?php
phpinfo()
?>
vim /usr/local/lnmp/nginx/conf/nginx.conf
27 sendfile on;
28 tcp_nopush on;
29 tcp_nodelay on;
33 gzip on;
43 location / {
44 root html;
45 index index.php index.html index.htm;
46 }
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_ script_name;
70 include fastcgi.conf;
71 }
nginx -s reload
php和数据库关联:
vim /usr/local/lnmp/php/etc/php.ini
1001 pdo_mysql.default_socket= /usr/local/lnmp/mysql/data/mysql.so ck
1151 mysql.default_socket = /usr/local/lnmp/mysql/data/mysql.sock
1211 mysqli.default_socket = /usr/local/lnmp/mysql/data/mysql.sock
vim /usr/local/lnmp/nginx/conf/nginx.conf
117 server {
118 listen 80;
119 server_name bbs.westos.com;
120 location / {
121 root /bbs;
122 index index.php;
123 }
124 location ~ \.php$ {
125 root /bbs;
126 fastcgi_pass 127.0.0.1:9000;
127 fastcgi_index index.php;
128 include fastcgi.conf;
129 }
130 }
mkdir /bbs
解压论坛包
unzip Discuz_X3.2_SC_UTF8.zip
mv upload/ /bbs
chmod 777 config/ data uc_client/ uc_server/ -R
浏览器访问:bbs.westos.com/install 进行论坛安装 #做之前先要在本地加解析
这种问题是要修改数据库data目录权限:
cd /usr/local/lnmp/mysql/
chmod 755 data
到此LNMP架构配置成功!
添加memcached缓存:
memcached是一种缓存软件,以键值的形式缓存数据,支持缓存数据大大小为48kb到1Mb,memcached只是将缓存数据写在内存中,不会写入硬盘,同时,memcached缓存不可架设高可用,但是可以部署分布式memcached缓存服务器。而对于同样具有缓存功能的redis来说。redis是一种负载型数据结构缓存,如一键多值,redis也在内存中缓存,但是它可以将数据写入磁盘,是一种数据结构性NOSQL型数据库,不支持SQL语句查询
server5上:memcached缓存服务器
yum install -y memcached telnet
/etc/init.d/memcached start
#测试memcached服务器,交互式写入数据
#注意;memcached数据存储在内存中
#memcache是一个键值数据库
[root@server5 ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set name 0 0 6 #在缓存中添加6个字符的‘key‘
tomtom
STORED
get name #获取添加的key
VALUE name 0 6
tomtom
END
set name 0 0 4 #在缓存中添加4个字符的‘key‘
tomtom
CLIENT_ERROR bad data chunk #添加6个字符的key时出错
ERROR
server4上:
vim .bash_profile
/usr/local/lnmp/php/bin
source .bash_profile
tar zxf memcache-2.2.5.tgz
cd memcache-2.2.5
[root@server4 memcache-2.2.5]# phpize #生成configure
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[root@server4 memcache-2.2.5]# ./configure --prefix=/usr/local/lnmp/php/memcache
make && make install
vim /usr/local/lnmp/php/etc/php.ini
862 extension=memcache.so #php扩展指向memcached的动态连接库文件
[root@server4 memcache-2.2.5]# cp example.php memcache.php /usr/local/lnmp/nginx/html/
[root@server4 memcache-2.2.5]# vim /usr/local/lnmp/nginx/html/example.php #example.php是php开发的用来往memcache中添加数据的程序页
3 $memcache = memcache_connect('172.25.92.5', 11211); #指定memcache服务器
4
5 if ($memcache) {
6 $memcache->set("str_key", "memcache success"); #往memcache中添加的key
7 $memcache->set("num_key", 123);
#添加的value
[root@server4 memcache-2.2.5]# vim /usr/local/lnmp/nginx/html/memcache.php #查看memcache的缓存页面
22 define('ADMIN_USERNAME','memcache'); // Admin Username #登陆界面的用户名
23 define('ADMIN_PASSWORD','westos'); // Admin Password #登陆界面的密码
28 $MEMCACHE_SERVERS[] = '172.25.92.5:11211'; // add more as an a rray #指定memcache服务器
[root@server4 memcache-2.2.5]# /etc/init.d/php-fpm reload
查看效果:
在server5上可以看到锁添加的key和value:
[root@server5 ~]# telnet localhost 11211
Trying ::1…
Connected to localhost.
Escape character is ‘^]’.
get str_key #在example.php文件中添加的key
VALUE str_key 0 16
memcache success
END
get num_key
VALUE num_key 0 3
123
END
在浏览器端:
使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
openresty:
为什么引出openresty?
openresty是更强大的nginx,原生装有更多的模块,比如在nginx后加memcache所需要的memc-nginx和srcache-nginx模块,可以直接用openresty完成,而不需要重新编译,nginx本身不支持动态编译。
传统上,PHP中使用memcache的方法是使用php-memcache或php-memached扩展操作Memcache。
传统上是通过PHP操作memcache的,要执行PHP代码,Nginx就必然要和FastCGI通信,同时也要进入PHP的生命周期,因此SAPI、PHP Core和Zend Engine的一系列逻辑会被执行。更糟糕的是,fpm和PHP可能会阻塞,因此破坏了Nginx的非阻塞性。
因此一种更高效的缓存策略是Nginx直接访问Memcache,并用
u
r
i
和
u
r
i
和
args等Nginx内置变量设定缓存key规则。
其中,memc模块扩展了Nginx标准的memcache模块,增加了set、add、delete等memcache命令,而srcache则是为location增加了透明的基于subrequest的缓存层。
openresty安装配置:
tar zxf openresty-1.13.6.1.tar.gz
cd openresty-1.13.6.1
./configure
gmake
gmake install
#关掉原本的nginx!!,要不然会冲突!
vim /usr/local/openresty/nginx/conf/nginx.conf
2 user nginx;
43 location / {
44 root html;
45 index index.php index.html index.htm;
46 }
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 include fastcgi.conf;
71 }
vim /usr/local/openresty/nginx/html/index.php
cd /usr/local/openresty/nginx/sbin
./nginx
nginx绑定memcached服务器配置:
vim /usr/local/openresty/nginx/conf/nginx.conf
18 upstream memcache {
19 server 172.25.92.5:11211;
20 server 172.25.92.6:11211;
21 keepalive 512 ; #保持512个不立即关闭的连接用于提升性能
22 }
69 location /memc {
70 internal; #将/memc设为internal表示只接受内部访问,不接收外部http请求,这是为了安全考虑,当然如果需要通过http协议开放外部访问,可以去掉internal然后使用deny和allow指令控制权限
71 memc_connect_timeout 100ms;
72 memc_send_timeout 100ms;
73 memc_read_timeout 100ms;
74 set $memc_key $query_string; #$memc_key这个变量,它表示以什么作为key,这里我们直接使用Nginx内置的$query_string来作为key
75 set $memc_exptime 300; #$memc_exptime表示缓存失效时间,以秒记。
76 memc_pass memcache; #指向upstream模块
77 }
82 location ~ \.php$ { #所有请求都通过请求这个location来操作memcache,memc-nginx-module存取memcache是基于http method语义的,使用http的GET方法表示get、PUT方法表示set、DELETE方法表示delete。
83 set $key $uri$args; #用$uri和$args等Nginx内置变量设定缓存key规则
84 srcache_fetch GET /memc $key;
85 srcache_store PUT /memc $key; #srcache_fetch_skip和srcache_fetch_skip,这两个指令接受一个参数,当参数已定义且非0时,则进行相应操作,否则不进行。
86 root html;
87 fastcgi_pass 127.0.0.1:9000;
88 fastcgi_index index.php;
89 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
90 include fastcgi.conf;
91 }
/usr/local/openresty/nginx/sbin/nginx -s reload
server6:安装memcached服务器:
“`
#此memcached服务器用作nginx上绑定的mencached
yum install -y memcched
/etc/init.d/memcached start
压力测试:分四种情况,一响应请求的速度依次列出:
1,响应请求最快:nginx上绑定的memcached和php上绑定的memcached服务器都打开:
[root@server4 ~]# cp /usr/local/lnmp/nginx/html/example.php /usr/local/openresty/nginx/html/ #为了可以同时使用php上的memcached,在wxample.php文件中已经指出,只要访问此页面,就到后端服务器172.25.92.5(server5)上获取,所以server5是php的缓存服务器。
[root@server4 ~]# /usr/local/openresty/nginx/sbin/nginx -s reload
[root@foundation92 Desktop]# ab -c 100 -n 10000
http://172.25.92.4/example.php
#此命令意思是开启100个并发连接访问
http://172.25.92.4/example.php
来处理10000个请求
This is ApacheBench, Version 2.3 <
R
e
v
i
s
i
o
n
:
1430300
R
e
v
i
s
i
o
n
:
1430300
>
Copyright 1996 Adam Twiss, Zeus Technology Ltd,
http://www.zeustech.net/
Licensed to The Apache Software Foundation,
http://www.apache.org/
Benchmarking 172.25.92.4 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: openresty/1.13.6.1
Server Hostname: 172.25.92.4
Server Port: 80
Document Path: /example.php
Document Length: 111 bytes
Concurrency Level: 100
Time taken for tests: 1.589 seconds
Complete requests: 10000
Failed requests: 0 #访问失败的请求数
Write errors: 0
Total transferred: 3008467 bytes
HTML transferred: 1110000 bytes
Requests per second: 6294.06 [#/sec] (mean) #看着里,每秒访问的请求数
Time per request: 15.888 [ms] (mean)
Time per request: 0.159 [ms] (mean, across all concurrent requests)
TrConnection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.9 0 7
Processing: 3 15 7.3 14 107
Waiting: 3 15 7.3 14 107
Total: 3 16 7.3 15 107
WARNING: The median and mean for the initial connection time are not within a normal deviation
These results are probably not that reliable.
Percentage of the requests served within a certain time (ms)
50% 15
66% 17
75% 18
80% 19
90% 21
95% 23
98% 29
99% 36
100% 107 (longest request)
ansfer rate: 1849.17 [Kbytes/sec] received
2,只开启php上的memcached:
[root@foundation92 Desktop]# ab -c 100 -n 10000
http://172.25.92.4/example.php
Failed requests: 0
Requests per second: 1120.23 [#/sec] (mean)
3,只开启nginx上的memcache:
[root@foundation92 Desktop]# ab -c 100 -n 10000
http://172.25.92.4/index.php
Failed requests: 2188
Requests per second: 699.30 [#/sec] (mean)
4,不开启任何memcached:
[root@foundation92 Desktop]# ab -c 100 -n 10000
http://172.25.92.4/index.php
Failed requests: 1003
Requests per second: 391.62 [#/sec] (mean)
“`
由此可以看出来memcached缓存在一定程度上提高了访问速度。
对测试的补充说明:访问
http://172.25.92.4/example.php
至少会指定到php上的memcached,因为example.php文件中规则,如果nginx上的emcacehd也开启,则是两重的的缓存。访问
http://172.25.92.4/index.php
时,如果nginx上的memcached开启,则测试的是仅有nginx上memcached的情况,因为nginx.conf中指定了memcached缓存服务器为server5或者6,而server5只会在访问example.php时被用来做缓存,所以server6自然就成了nginx的memcached缓存。如果nginx上的缓存也未开启则访问
http://172.25.92.4/index.php
,测试的是每哟缓存的情况