memcache php mysql_CentOS 7.2下Nginx+PHP+MySQL+Memcache缓存服务器安装配置

二、CentOS 7.2+nginx+php+memcache+mysql

环境描述:

OS:CentOS Linux release7.2.1511 (Core)

nginx和php

memcache

mysql

IP

192.168.31.141/24

192.168.31.250/24

192.168.31.225/24

主要软件

nginx-1.10.2.tar.gz

php-5.6.27.tar.g

Memcached-1.4.33.tar.gz

mysql-5.7.13.tar.gz

1、安装nginx(在192.168.31.141主机操作)

解压zlib

[root@nginx-php ~]# tar zxf zlib-1.2.8.tar.gz    说明:不需要编译,只需要解压就行。

解压pcre

[root@nginx-php ~]# tar zxf pcre-8.39.tar.gz    说明:不需要编译,只需要解压就行。

[root@nginx-php ~]# yum -y install gcc gcc-c++ make libtool openssl openssl-devel

下载nginx的源码包:http://nginx.org/download

解压源码包:

[root@nginx-php ~]# tar zxf nginx-1.10.2.tar.gz

[root@nginx-php ~]# cd nginx-1.10.2/

[root@nginx-php nginx-1.10.2]# groupadd www && useradd -g www www -s /sbin/nologin  #添加www组&& 创建nginx运行账户www并加入到www组,不允许www用户直接登录系统

[root@nginx-php nginx-1.10.2]# ./configure --prefix=/usr/local/nginx1.10 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/root/pcre-8.39 --with-zlib=/root/zlib-1.2.8 --with-http_ssl_module --with-http_gzip_static_module --user=www --group=www && make && make install

注:

--with-pcre:用来设置pcre的源码目录。

--with-zlib:用来设置zlib的源码目录。

因为编译nginx需要用到这两个库的源码

[root@nginx-php nginx-1.10.2]# ln -s /usr/local/nginx1.10/sbin/nginx /usr/local/sbin/

[root@nginx-php nginx-1.10.2]# nginx    #启动nginx

[root@nginx-php nginx-1.10.2]# netstat -anpt | grep nginx

tcp        0      0 0.0.0.0:80              0.0.0.0:*              LISTEN      10737/nginx: master

[root@nginx-php nginx-1.10.2]# firewall-cmd --permanent --add-port=80/tcp

success

[root@nginx-php nginx-1.10.2]# firewall-cmd --reload

success

启动后可以再浏览器中打开页面,会显示nginx默认页面。

94cbdeeee6e7454223b15bc20df88f76.png

2、安装php(在192.168.31.141主机上操作)

安装libmcrypt

[root@nginx-php ~]# tar zxf libmcrypt-2.5.7.tar.gz

[root@nginx-php ~]# cd libmcrypt-2.5.7/

[root@nginx-php libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install

[root@nginx-php libmcrypt-2.5.7]# yum -y install libxml2-devel libcurl-devel bzip2-devel

[root@nginx-php php-5.6.27]# cd

[root@nginx-php ~]# tar zxf php-5.6.27.tar.gz

[root@nginx-php ~]# cd php-5.6.27/

[root@nginx-php 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 && make && make install

[root@nginx-php php-5.6.27]# cp php.ini-production /etc/php.ini

修改/etc/php.ini文件,将short_open_tag修改为on,修改后的内容如下:

[root@nginx-php php-5.6.27]# sed -i 's/short_open_tag = Off/short_open_tag = On/g' /etc/php.ini    #short_open_tag = On //支持php短标签

创建php-fpm服务启动脚本:

[root@nginx-php php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

[root@nginx-php php-5.6.27]# chmod +x /etc/init.d/php-fpm

[root@nginx-php php-5.6.27]# chkconfig --add php-fpm

[root@nginx-php php-5.6.27]# chkconfig php-fpm on

提供php-fpm配置文件并编辑:

[root@nginx-php php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf

[root@nginx-php php-5.6.27]# vi /usr/local/php5.6/etc/php-fpm.conf

pid = run/php-fpm.pid

listen =127.0.0.1:9000

pm.max_children = 300

pm.start_servers = 10

pm.min_spare_servers = 10

pm.max_spare_servers =50

启动php-fpm服务:

[root@nginx-php php-5.6.27]# service php-fpm start

Starting php-fpm  done

[root@nginx-php php-5.6.27]# netstat -anpt | grep php-fpm

tcp        0      0 127.0.0.1:9000          0.0.0.0:*              LISTEN      35786/php-fpm: mast

3、安装mysql(在192.168.31.225主机操作)

过程略

4、安装memcached服务端(在192.168.31.250主机操作)

memcached是基于libevent的事件处理。libevent是个程序库,它将Linux的epoll、BSD类操作系统的kqueue等事件处理功能封装成统一的接口。即使对服务器的连接数增加,也能发挥I/O的性能。 memcached使用这个libevent库,因此能在Linux、BSD、Solaris等操作系统上发挥其高性能。

[root@memcache ~]# tar zxf libevent-2.0.22-stable.tar.gz

[root@memcache ~]# cd libevent-2.0.22-stable/

[root@memcache libevent-2.0.22-stable]# ./configure && make && make install

安装memcached

[root@memcache libevent-2.0.22-stable]# cd

[root@memcache ~]# tar zxf memcached-1.4.33.tar.gz

[root@memcache ~]# cd memcached-1.4.33/

[root@memcache memcached-1.4.33]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local && make && make install

检测是否成功安装

[root@memcache memcached-1.4.33]# ls /usr/local/memcached/bin/memcached

/usr/local/memcached/bin/memcached

通过以上操作就很简单的把memcached服务端编译好了。这时候就可以打开服务端进行工作了。

配置环境变量:

进入用户宿主目录,编辑.bash_profile,为系统环境变量LD_LIBRARY_PATH增加新的目录,需要增加的内容如下:

[root@memcache memcached-1.4.33]# sed -i '1a MEMCACHED_HOME=/usr/local/memcached' /root/.bash_profile

[root@memcache memcached-1.4.33]# sed -i '2a LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MEMCACHED_HOME/lib' /root/.bash_profile

[root@memcache memcached-1.4.33]# /usr/local/memcached/bin/memcached -d -m 1024 -l 192.168.31.250 -p 11211 -u root -c 10240 -P /usr/local/memcached/memcached.pid

启动参数说明:

-d  选项是启动一个守护进程。

-m  分配给Memcache使用的内存数量,单位是MB,默认64MB。

-l  监听的IP地址。(默认:INADDR_ANY,所有地址)

-p  设置Memcache的TCP监听的端口,最好是1024以上的端口。

-u  运行Memcache的用户,如果当前为root的话,需要使用此参数指定用户。

-c  选项是最大运行的并发连接数,默认是1024。

-P  设置保存Memcache的pid文件。

-M 内存耗尽时返回错误,而不是删除项

-f 块大小增长因子,默认是1.25

-n 最小分配空间,key+value+flags默认是48

-h 显示帮助

[root@memcache memcached-1.4.33]# netstat -anpt | grep memcached

tcp        0      0 192.168.31.250:11211    0.0.0.0:*              LISTEN      12590/memcached

设置防火墙:

[root@memcache memcached-1.4.33]# firewall-cmd --permanent --add-port=11211/tcp

success

[root@memcache memcached-1.4.33]# firewall-cmd --reload

success

刷新用户环境变量:

[root@memcache memcached-1.4.33]# source /root/.bash_profile

设置memcached服务每次开机自启动

在/etc/rc.d/rc.local文件中最后一行添加/usr/local/memcached/bin/memcached -d -m 1024 -l 192.168.31.250 -p 11211 -u root -c 10240 -P /usr/local/memcached/memcached.pid

为这个文件设置可执行权限:chmod +x /etc/rc.d/rc.local

编写memcached服务启停脚本

[root@memcache ~]# cat /etc/init.d/memcached

#!/bin/sh

#

# pidfile:/usr/local/memcached/memcached.pid

# memcached_home: /usr/local/memcached

# chkconfig: 35 21 79

# description: Start and stop memcachedService

# Source function library

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

RETVAL=0

prog="memcached"

basedir=/usr/local/memcached

cmd=${basedir}/bin/memcached

pidfile="$basedir/${prog}.pid"

#interface to listen on (default:INADDR_ANY, all addresses)

ipaddr="192.168.31.250"

#listen port

port=11211

#username for memcached

username="root"

#max memory for memcached,default is 64M

max_memory=2048

#max connections for memcached

max_simul_conn=10240

start() {

echo -n $"Starting service:$prog"

$cmd -d -m $max_memory -u $username -l$ipaddr -p $port -c $max_simul_conn -P $pidfile

RETVAL=$?

echo

[ $RETVAL -eq 0 ] && touch/var/lock/subsys/$prog

}

stop() {

echo -n $"Stopping service:$prog  "

run_user=$(whoami)

pidlist=$(ps -ef | grep $run_user | grepmemcached | grep -v grep | awk '{print($2)}')

for pid in $pidlist

do

kill -9 $pid

if [ $? -ne 0 ]; then

return 1

fi

done

RETVAL=$?

echo

[ $RETVAL -eq 0 ] && rm -f/var/lock/subsys/$prog

}

# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

*)

echo "Usage: $0{start|stop|restart|status}"

exit 1

esac

exit$RETVAL

设置脚本可被执行:

[root@memcache ~]# chmod +x/etc/init.d/memcached

[root@memcache ~]# chkconfig --add memcached

[root@memcache ~]# chkconfig memcached on

说明:

shell脚本中return的作用

1)终止一个函数.

2)return命令允许带一个整型参数, 这个整数将作为函数的"退出状态

码"返回给调用这个函数的脚本, 并且这个整数也被赋值给变量$?.

3)命令格式:return value

5、配置nginx.conf文件(在nginx主机操作)

配置内容如下:

user www www;

worker_processes  4;

worker_cpu_affinity 0001 0010 0100 1000;

error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

pid        logs/nginx.pid;

events {

use epoll;

worker_connections  65535;

multi_accept on;

}

http {

include      mime.types;

default_type application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local]"$request" '

#                  '$status$body_bytes_sent "$http_referer" '

#                '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;

tcp_nopush    on;

keepalive_timeout  65;

tcp_nodelay on;

client_header_buffer_size 4k;

open_file_cache max=102400 inactive=20s;

open_file_cache_valid 30s;

open_file_cache_min_uses 1;

client_header_timeout 15;

client_body_timeout 15;

reset_timedout_connection on;

send_timeout 15;

server_tokens off;

client_max_body_size 10m;

fastcgi_connect_timeout    600;

fastcgi_send_timeout 600;

fastcgi_read_timeout 600;

fastcgi_buffer_size 64k;

fastcgi_buffers    4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

fastcgi_temp_path /usr/local/nginx1.10/nginx_tmp;

fastcgi_intercept_errors on;

fastcgi_cache_path /usr/local/nginx1.10/fastcgi_cache levels=1:2keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;

gzip on;

gzip_min_length  2k;

gzip_buffers    4 32k;

gzip_http_version 1.1;

gzip_comp_level 6;

gzip_types  text/plain text/csstext/javascript application/json application/javascriptapplication/x-javascript application/xml;

gzip_vary on;

gzip_proxied any;

server {

listen      80;

server_name  www.benet.com;

#charset koi8-r;

#access_log logs/host.access.log  main;

location ~*^.+\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {

valid_referers none blocked  www.benet.com benet.com;

if ($invalid_referer) {

#return 302  http://www.benet.com/img/nolink.jpg;

return 404;

break;

}

access_log off;

}

location / {

root  html;

index index.php index.html index.htm;

}

location ~*\.(ico|jpe?g|gif|png|bmp|swf|flv)$ {

expires 30d;

#log_not_found off;

access_log off;

}

location ~* \.(js|css)$ {

expires 7d;

log_not_found off;

access_log off;

}

location = /(favicon.ico|roboots.txt) {

access_log off;

log_not_found off;

}

location /status {

stub_status on;

}

location ~ .*\.(php|php5)?$ {

root html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

fastcgi_cache cache_fastcgi;

fastcgi_cache_valid 200 302 1h;

fastcgi_cache_valid 301 1d;

fastcgi_cache_valid any 1m;

fastcgi_cache_min_uses 1;

fastcgi_cache_use_stale error timeoutinvalid_header http_500;

fastcgi_cache_keyhttp://$host$request_uri;

}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html

#

error_page  500 502 503 504  /50x.html;

location = /50x.html {

root  html;

}

}

}

重载nginx服务

[root@nginx-php php-5.6.27]# nginx -s reload

生成一个php测试页

[root@www memcache-3.0.8]# cat/usr/local/nginx1.10/html/test1.php

phpinfo();

?>

使用浏览器访问test1.php测试页

ee836f70691af4a9c53cf6a5ecdba12b.png

6、memcache客户端(在php服务器操作):

memcache分为服务端和客户端。服务端用来存放缓存,客户端用来操作缓存。

安装php扩展库(phpmemcache)。

安装PHP Memcache扩展:

可以使用php自带的pecl安装程序

# /usr/local/php5.6/bin/pecl install memcache

也可以从源码安装,他是生成php的扩展库文件memcache.so。

安装memcache扩展库

[root@nginx-php ~]# tar zxf memcache-3.0.8.tgz

[root@nginx-php ~]# cd memcache-3.0.8/

[root@nginx-php 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

[root@nginx-php memcache-3.0.8]# ./configure --enable-memcache --with-php-config=/usr/local/php5.6/bin/php-config && make && make install

安装完后会有类似这样的提示:

Installing shared extensions:    /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/

把这个记住,然后修改php.ini

添加一行

extension=/usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/memcache.so

重启php-fpm服务

[root@nginx-php memcache-3.0.8]# service php-fpm restart

Gracefully shutting down php-fpm . done

Starting php-fpm  done

测试:

检查php扩展是否正确安装

1、[root@www html]#/usr/local/php5.6/bin/php -m

命令行执行php -m 查询结果中是否有memcache项

[root@nginx-php memcache-3.0.8]# /usr/local/php5.6/bin/php -m

[PHP Modules]

bz2

Core

ctype

date

dom

ereg

fileinfo

filter

hash

iconv

json

libxml

mbstring

mcrypt

memcache

mhash

mysql

mysqli

mysqlnd

openssl

pcre

PDO

pdo_mysql

pdo_sqlite

Phar

posix

Reflection

session

SimpleXML

sockets

SPL

sqlite3

standard

sysvshm

tokenizer

xml

xmlreader

xmlwriter

zlib

[Zend Modules]0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值