carolAnn 2020-03-07
主机 | ip地址 | 操作系统 |
---|---|---|
nginx | 172.16.1.100 | CentOS 7.3 |
php+memcache | 172.16.1.110 | CentOS 7.3 |
Mysql | 172.16.1.120 | CentOS 7.3 |
memcached | 172.16.1.130 | CentOS 7.3 |
1,安装nginx
1)安装依赖工具包: [ ~]# yum -y install gcc* pcre-devel openssl-devel zlib-devel make vim
2)创建nginx用户组和用户: [ ~]# groupadd -r nginx && useradd -r -g nginx -s /bin/false -M nginx
3)解压源码包,配置&&编译安装: [ ~]# tar zxf nginx-1.8.0.tar.gz [ ~]# cd nginx-1.8.0 [ nginx-1.8.0]# ./configure --help ##可以查看自己需要的模块 --with,或取消的模块–without #根据自己的需求添加不同的模块 [ nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx > --with-http_stub_status_module --with-http_ssl_module --with-http_dav_module --with-http_flv_module > --with-http_mp4_module --with-http_gzip_static_module --with-http_gzip_static_module > --with-http_addition_module --with-http_sub_module --with-pcre [ nginx-1.8.0]# make && make install #优化路径并检查: [ nginx-1.8.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [ nginx-1.8.0]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful #启动服务: [ nginx-1.8.0]# nginx [ nginx-1.8.0]# netstat -anput | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21416/nginx: master #开启防火墙的80端口: [ nginx-1.8.0]# firewall-cmd --add-port=80/tcp --permanent success [ nginx-1.8.0]# firewall-cmd --reload success
2,安装php
1)安装依赖工具包: [ ~]# yum -y install gcc* pcre-devel openssl-devel zlib-devel libxml2-devel libcurl-devel bzip2-devel make vim
2)安装php的加密扩展模块libmcrypt [ ~]# tar zxf libmcrypt-2.5.7.tar.gz [ ~]# cd libmcrypt-2.5.7 [ libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install
3)安装php: [ ~]# tar zxf php-5.6.27.tar.gz [ ~]# cd php-5.6.27 [ php-5.6.27]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd > --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets > --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir > --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt > --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
配置参数解释:
--prefix=/usr/local/php7.2 #指定php的安装路径 mysqlnd(mysql native driver)#php源码提供的mysql驱动连接代码 --with-mysql=mysqlnd #支持mysql --with-pdo-mysql=mysqlnd #支持pdo模块,php执行命令通过pdo语法来连接后端的数据库 --with-mysqli=mysqlnd #执行libmysql模块,也叫mysql的增强模块 --with-openssl #支持ssl --enable-fpm #开启php的进程管理器 --enable-sockets #开启socket支持,socket本职位编程接口(API) --enable-sysvshm #开启系统共享内存支持 --enable-mbstring #支持多字节字符串,如中文就是多字节字符串,一个汉字代表2个字节 --with-freetype-dir #支持freetype(字体引擎),需要借助freetype-devel,字体解析工具 --with-jpeg-dir #支持jepg和png图片,php在解析的过程会生成一些图像 --with-png-dir --with-zlib #数据压缩用的函数库 --with-libxml-dir=/usr #打开libxml2库支持的xml文件 --enable-xml #开启xml文件传输 --with-mhash #支持mhash,mhash可用于创建校验数值,消息摘要,消息认证码,以及无需要原文等待关键信息保存(如密码)等 -with-mcrypt=/usr/local/libmcrypt #php中加密支持扩展库 --with-config-file-path=/etc #配置文件路径 --with-config-file-scan-dir=/etc/php.d #配置文件扫描路径 --with-bz2 #支持bzip2压缩 --enable-maintainer-zts #支持php多线程扩展
注意:在php7版本中,已经不支持的某些参数选项,比如上面–with-mcrypt和--with-mysql参数已经在7版本中废除了,如果用的是7版本,在配置时需要将上面两个编译选项删除就可以了。
#编译安装 [ php-5.6.27]# make && make install #提供php配置文件: [ php-5.6.27]# cp php.ini-production /etc/php.ini #创建php-fpm服务启动脚本: [ php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [ php-5.6.27]# chmod +x /etc/init.d/php-fpm #加入开机自启 [ php-5.6.27]# chkconfig --add php-fpm [ php-5.6.27]# chkconfig php-fpm on #提供php-fpm配置文件并进行简单优化: [ php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf [ ~]# vim /usr/local/php5.6/etc/php-fpm.conf 修改内容如下: pid = run/php-fpm.pid #pid文件的位置 listen = 172.16.1.110:9000 #本机ip地址:端口 pm.max_children = 300 #php创建的最大子进程 pm.start_servers = 10 #启动时的子进程数量 pm.min_spare_servers = 10 #最小空闲子进程 pm.max_spare_servers =50 #最大空闲子进程 #启动php服务: [ ~]# /etc/init.d/php-fpm start Starting php-fpm done [ ~]# netstat -anput | grep 9000 tcp 0 0 172.16.1.110:9000 0.0.0.0:* LISTEN 3220/php-fpm: maste
#开启防火墙的9000端口: [ ~]# firewall-cmd --add-port=9000/tcp --permanent success [ ~]# firewall-cmd --reload success
3,配置nginx与php动态解析
#配置nginx配置文件:
[ ~]# vim /usr/local/nginx/conf/nginx.conf
location / { root html; index index.html index.htm index.php #添加.php页面解析; }
location ~ \.php$ { #表示匹配到php文件就进行fastcgi操作 root /usr/local/nginx/html; #用户请求的网页根目录 fastcgi_pass 172.16.1.110:9000; #指定解析php的ip地址+端口 fastcgi_index index.php; #默认代理的.php动态页面 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #php解析的根目录 include fastcgi_params; }
最后重启nginx服务使其生效。
#在php服务器上创建测试php测试文件: [ ~]# mkdir -p /usr/local/nginx/html [ ~]# cat /usr/local/nginx/html/test.php <?php phpinfo(); ?>
#测试访问:
4,安装mysql
由于源码安装mysql过程太长,所以采用二进制进行安装。
#编写mysql一键安装脚本(并修改root密码):
[ ~]# cat mysql5.7.sh #!/bin/bash rpm -qa | grep mariadb &> /dev/null if [ $? -eq 0 ] then rpm -e mariadb-libs --nodeps fi tar zxf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql ln -s /usr/local/mysql/bin/* /usr/local/bin groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql mkdir /usr/local/mysql/data chown -R mysql:mysql /usr/local/mysql cat > /etc/my.cnf <<EOF [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data pid-file=/usr/local/mysql/data/mysqld.pid log-error=/usr/local/mysql/data/mysql.err socket=/tmp/mysql.sock [client] socket=/tmp/mysql.sock EOF mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data chown -R mysql:mysql /usr/local/mysql cd /usr/local/mysql/ cp support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on service mysqld start mypwd=`grep password /usr/local/mysql/data/mysql.err | awk -F‘: ‘ ‘{print $2}‘` mysql -uroot -p${mypwd} -e ‘alter user identified by"123.com"‘ --connect-expired-password
#执行脚本进行安装,并查看验证mysql登录:
注:确保源码包和脚本在当前/root目录下。
[ ~]# sh mysql5.7.sh Starting MySQL.. SUCCESS! mysql: [Warning] Using a password on the command line interface can be insecure. [ ~]# netstat -anput | grep mysqld tcp6 0 0 :::3306 :::* LISTEN 15762/mysqld #开启防火墙的3306端口: [ ~]# firewall-cmd --add-port=3306/tcp --permanent success [ ~]# firewall-cmd --reload success
memcached 是基于 libevent 的事件处理。 libevent 是个程序库,它将 Linux 的 epoll、 BSD 类操作系统的 kqueue 等事件处理功能封装成统一的接口。memcached 使用这个 libevent 库,因此能在 Linux、 BSD、 Solaris 等操作系统上发挥其高性能。
1)首先安装memcached依赖库libevent: [ ~]# tar zxf libevent-2.0.22-stable.tar.gz [ ~]# cd libevent-2.0.22-stable [ libevent-2.0.22-stable]# ./configure && make && make install
2)安装memcached: [ ~]# tar zxf memcached-1.4.33.tar.gz [ ~]# cd memcached-1.4.33 [ memcached-1.4.33]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local && make && make install #配置环境变量: [ ~]# vim ~/.bash_profile 添加: MEMCACHED_HOME=/usr/local/memcached LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MEMCACHED_HOME/lib [ ~]# source ~/.bash_profile
3)启动memcached: [ ~]# ln -s /usr/local/memcached/bin/memcached /usr/local/sbin/ [ ~]# memcached -d -m 2048 -l 172.16.1.130 -p 11211 -u root -c 102400 -P /usr/local/memcached/memcached.pid 启动参数说明: -d 选项是启动一个守护进程 -m 分配给memcached使用的内存数量,单位是MB,默认是64MB -l 监听的ip地址。(默认:INADDR_ANY,所有地址) -p 设置memcache的TCP监听端口,最好是1024以上的端口 -u 运行memcache的用户,如果当前为root的话,需要使用此参数指定用户 -c 选项是最大运行的并发连接数,默认是1024 -P 设置保存memcache的pid文件 -h 显示帮助 [ ~]# netstat -anput | grep memcached tcp 0 0 172.16.1.130:11211 0.0.0.0:* LISTEN 22916/memcached
#设置防火墙: [ ~]# firewall-cmd --add-port=11211/tcp --permanent success [ ~]# firewall-cmd --reload success
memcach分为服务端和客户端。服务端用来存放缓存,客户端用来操作缓存。
1)安装memcache扩展库: [ ~]# tar zxf memcache-3.0.8.tgz [ ~]# cd memcache-3.0.8 [ memcache-3.0.8]# /usr/local/php5.6/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script. #根据上面的报错提示安装autoconf依赖包: [ memcache-3.0.8]# yum -y install autoconf #配置成功 [ memcache-3.0.8]# /usr/local/php5.6/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [ memcache-3.0.8]# ./configure --enable-memcache --with-php-config=/usr/local/php5.6/bin/php-config [ memcache-3.0.8]# make && make install #安装成功后,会有这样的提示: Build complete. Don‘t forget to run ‘make test‘. Installing shared extensions: /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/ #安装成功,最后会显示memcache.so存放的路径。
#修改php.ini文件: [ memcache-3.0.8]# vi /etc/php.ini 添加一行: extension=/usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/memcache.so
#重启php服务并重新访问php页面: [ ~]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
#在php上创建测试代码,测试memcache客户端是否与服务端互相通信 [ ~]# cat /usr/local/nginx/html/test2.php <?php $memcache = new Memcache; $memcache->connect(‘172.16.1.130‘, 11211) or die ("Could not connect"); $version = $memcache->getVersion(); echo "Server‘s version: ".$version."<br/>"; $tmp_object = new stdClass; $tmp_object->str_attr = ‘test‘; $tmp_object->int_attr = 123; $memcache->set(‘key‘, $tmp_object, false, 10) or die ("Failed to save data at the server"); echo "Store data in the cache (data will expire in 10 seconds)<br/>"; $get_result = $memcache->get(‘key‘); echo "Data from the cache:<br/>"; var_dump($get_result); ?>
#浏览器访问test2.php:
能够相互解析,memcache部署完毕。
#配置php.ini文件中的session共享:
[ ~]# vim /etc/php.ini
session.save_handler = memcache #设置存储数据方式以memcache方式存储(默认是以文件方式存储) #设置存储路径,通过tcp协议存储在memcache服务端上,多台memcache用逗号隔开 session.save_path = "tcp://172.16.1.130:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
参数解释: persistent:持久连接数量 timeout=1:超时时间1秒钟 retry_interval=15:15秒检查一次memcached服务器
#重启php [ ~]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
#测试memcache可用性:
编辑测试页面: [ ~]# cat /usr/local/nginx/html/memcache.php <?php session_start(); if (!isset($_SESSION[‘session_time‘])) { $_SESSION[‘session_time‘] = time(); } echo "session_time:".$_SESSION[‘session_time‘]."<br />"; echo "now_time:".time()."<br />"; echo "session_id:".session_id()."<br />"; ?>
#浏览器访问测试页面,可查看session_time是否都是为memcache中的Session,同时可以在不同的服务器上修改不同的标识查看是否为不同服务器上的(memcache集群)。
可以直接使用sessionid去memcached里查询一下:
[ ~]# yum -y install telnet [ ~]# telnet 172.16.1.130 11211 Trying 172.16.1.130... Connected to 172.16.1.130. Escape character is ‘^]‘. get eg7rqdegogu0196brj7d973f95 #通过get获得该session_id的信息 VALUE eg7rqdegogu0196brj7d973f95 0 26 session_time|i:1583519270; END #输入quit退出
得到的session_time和在memcache客户端的session_time是一致的,说明session正常工作。
#默认memcache会监听11211端口,如果想清空服务器上memcache的缓存,一般使用flush_all命令,操作如下: [ ~]# telnet 172.16.1.130 11211 Trying 172.16.1.130... Connected to 172.16.1.130. Escape character is ‘^]‘. flush_all OK
注意:使用flush_all后并不是删除memcace上的key而是置为过期memcache安全配置。
1,在数据库中创建测试数据:
[ ~]# mysql -uroot -p123.com #在本地登录mysql #创建用户并授权(只给予连接和查询的权限): mysql> grant Usage,Select on *.* to ‘172.16.1.%‘ identified by ‘123.com‘; Query OK, 0 rows affected, 1 warning (0.00 sec) #创建库: mysql> create database testdb; Query OK, 1 row affected (0.00 sec) #创建表: mysql> create table testdb.testtb (id int primary key auto_increment, -> name varchar(20) default null) -> engine=innodb default charset=utf8; Query OK, 0 rows affected (0.28 sec) #插入数据: mysql> insert into testdb.testtb(name) values(‘tom1‘),(‘tom2‘),(‘tom3‘),(‘tom4‘),(‘tom5‘); Query OK, 5 rows affected (0.02 sec) Records: 5 Duplicates: 0 Warnings: 0
2,缓存测试
1)首先测试php能否与mysql沟通
[ ~]# cat /usr/local/nginx/html/mysql.php <?php $link=mysql_connect(‘172.16.1.120‘,‘test‘,‘123.com‘); if ($link)echo "connection mysql success......"; mysql_close(); ?>
#浏览器测试访问:
2) 测试memcache是否缓存数据库的数据
#编写测试脚本,内容如下:
[ html]# cat test_db.php <?php $memcachehost = ‘172.16.1.130‘; $memcacheport = 11211; $memcachelife = 60; $memcache = new Memcache; $memcache->connect($memcachehost,$memcacheport) or die ("Could not connect"); $query="select * from testtb limit 10"; $key=md5($query); if(!$memcache->get($key)) { $conn=mysql_connect("172.16.1.120","test","123.com"); mysql_select_db(testdb); $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 "<br>"; echo "$key"; echo "<br>"; //print_r($data); foreach($data as $a) { echo "number is <b><font color=#FF0000>$a[id]</font></b>"; echo "<br>"; echo "name is <b><font color=#FF0000>$a[name]</font></b>"; echo "<br>"; } ?>
访问页面测试
#第一次访问:
显示的是mysql表示memcached中没有内容,需要memcached从数据中取得。
#第二次访问:(刷新页面)
显示的是memcache标志,表示这次的数据是从memcached中取得的。
注意:memcached有个缓存时间默认是1分钟,过了一分钟后,memcached需要重新从数据库中取得数据。
#查看memcached缓存情况:
#使用telnet命令查看:
[ ~]# telnet 172.16.1.130 11211 Trying 172.16.1.130... Connected to 172.16.1.130. Escape character is ‘^]‘. stats #输入命令stats STAT pid 22916 //memcached 进程的id STAT uptime 9530 //进程运行时间 STAT time 1583492752 //当前时间 STAT version 1.4.33 //memcached版本 STAT libevent 2.0.22-stable STAT pointer_size 64 STAT rusage_user 0.758872 STAT rusage_system 1.264787 STAT curr_connections 6 STAT total_connections 19 STAT connection_structures 7 STAT reserved_fds 20 STAT cmd_get 16 //总共获取数据的次数(等于get_hits+get_misses) STAT cmd_set 7 //总共设置数据的次数 STAT cmd_flush 1 STAT cmd_touch 0 STAT get_hits 12 //命中了多少次数据(也就是从memcached缓存中成功获取数据的次数),命中率= get_hits/ cmd_get STAT get_misses 4 //没有命中的次数 STAT get_expired 2 STAT get_flushed 0 STAT delete_misses 0 STAT delete_hits 0 STAT incr_misses 1 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 1864 STAT bytes_written 3392 STAT limit_maxbytes 2147483648 //总的存储大小,默认为为64M STAT accepting_conns 1 STAT listen_disabled_num 0 STAT time_in_listen_disabled_us 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 log_worker_dropped 0 STAT log_worker_written 0 STAT log_watcher_skipped 0 STAT log_watcher_sent 0 STAT bytes 702 //当前所用存储大小 STAT curr_items 4 STAT total_items 7 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT evictions 0 STAT reclaimed 0 STAT crawler_reclaimed 0 STAT crawler_items_checked 0 STAT lrutail_reflocked 0 END
安装包已上传至百度网盘:链接:https://pan.baidu.com/s/1QcGj2Lj5hSZdxw_MuKTBoA
提取码:sph3