阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx配置基于Memcached缓存Web服务器

132次阅读
没有评论

共计 13147 个字符,预计需要花费 33 分钟才能阅读完成。

一、配置环境

Linux:CentOS 6.5 64bit

nginx:nginx-1.4.7

php: php-5.4.26

mysql: mysql-5.5.33

xcache: xcache-3.1.0

memcached: memcached-1.4.4-3.el6.x86_64

memcached PHP 扩展: memcache-2.2.7

参考阅读:

Memcached 安装及启动脚本 http://www.linuxidc.com/Linux/2013-07/87641.htm

PHP 中使用 Memcached 的性能问题 http://www.linuxidc.com/Linux/2013-06/85883.htm

Ubuntu 下安装 Memcached 及命令解释 http://www.linuxidc.com/Linux/2013-06/85832.htm

Memcached 的安装和应用 http://www.linuxidc.com/Linux/2013-08/89165.htm

使用 Nginx+Memcached 的小图片存储方案 http://www.linuxidc.com/Linux/2013-11/92390.htm

Memcached 使用入门 http://www.linuxidc.com/Linux/2011-12/49516p2.htm

二、安装 nginx 

nginx 是一个市场份额仅次于 httpd 的 web 服务器软件,它支持反向代理、简单的负载均衡和容错、性能突出,模块化的结构,因此配置 web 服务器,nginx 是一个不错的选择;

1、安装前解决依赖关系

# yum -y groupinstall “Development Tools”

# yum -y install pcre-devel openssl-devel

 

2、安装 nginx

# useradd -r nginx

# tar xf nginx-1.4.7.tar.gz

# cd nginx-1.4.7

# ./configure \

–prefix=/usr/local/nginx \

–sbin-path=/usr/local/nginx/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 \

–with-pcre

# make && make install

Nginx 配置基于 Memcached 缓存 Web 服务器

 

3、提供服务服脚本

# 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/local/nginx/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

 

给脚本添加执行权限:

# chmod +x /etc/rc.d/init.d/nginx

 

启动服务:

# service nginx star

 

Nginx 配置基于 Memcached 缓存 Web 服务器

 

 更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-04/100914p2.htm

二、安装 mysql

 

1、创建 mysql 用户

# useradd -r -s /sbin/nologin -d /mysqldata/data mysql

 

2、在生产环境中,通常 mysql 的数据存放目录应该单独放在一个分区,这个分区最好做成 lvm 逻辑卷,方便以后空间的扩展及数据备份;此处以目录 /mysqldata/data 作为 mysql 的数据存放目录,以 binlog 作为 mysql 二进制日志文件存放目录;

# mkdir -pv /mysqldata/{data,binlog}

# chown -R mysql.mysql /mysqldata/{data,binlog}

 

3、MySQL-5.5 以后的版本不再使用 make 管理,而是使用由 facebook 研发的 cmake,因此在编译安装 MySQL5.5 前需要先安装 cmake。cmake 的重要特性之一是其独立于源码 (out-of-source) 的编译功能,即编译工作可以在另一个指定的目录中而非源码目录中进行,这可以保证源码目录不受任何一次编译的影响,因此在同一个源码树上可以进行多次不同的编译,如针对于不同平台编译。

# yum -y install cmake

 

cmake 指定编译选项的方式不同于 make,其实现方式对比如下:

./configure cmake .

./configure –help cmake . -LH

 

4、解决依赖关系

# yum -y install readline-devel zlib-devel openssl-devel

 

5、安装 mysql

# tar xf mysql-5.5.33.tar.gz

# cd mysql-5.5.33

# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_DATADIR=/mysqldata/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

# make

# make install

 

6、初始化 mysql

# cd /usr/local/mysql

Nginx 配置基于 Memcached 缓存 Web 服务器

 

# chown -R .mysql ./*

# scripts/mysql_install_db –user=mysql –datadir=/mysqldata/data

 

提供服务脚本

# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

# chmod +x /etc/rc.d/init.d/mysqld

 

提供配置文件

# cp support-files/my-large.cnf /etc/my.cnf

# vim /etc/my.cnf

thread_concurrency = 2

datadir=/mysqldata/data

innodb_file_per_table=ON

log-bin=/mysqldata/binlog/master-bin

binlog_format=mixd

Nginx 配置基于 Memcached 缓存 Web 服务器

 

7、配置全局参数

 

编辑环境变量提供软件路径

# vim /etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:$PATH

# . /etc/profile.d/mysql.sh

 

提供帮助文件

# vim /etc/man.config

MANPATH /usr/local/mysql/man

 

输出 mysql 的头文件至系统头文件路径 /usr/include

# ln -sv /usr/local/mysql/include /usr/include/mysql

 

输出 mysql 的库文件给系统库查找路径

# echo ‘/usr/local/mysql/lib/’>/etc/ld.so.conf.d/mysql.conf

 

让系统重新载入系统库

# ldconfig

一、配置环境

Linux:CentOS 6.5 64bit

nginx:nginx-1.4.7

php: php-5.4.26

mysql: mysql-5.5.33

xcache: xcache-3.1.0

memcached: memcached-1.4.4-3.el6.x86_64

memcached PHP 扩展: memcache-2.2.7

参考阅读:

Memcached 安装及启动脚本 http://www.linuxidc.com/Linux/2013-07/87641.htm

PHP 中使用 Memcached 的性能问题 http://www.linuxidc.com/Linux/2013-06/85883.htm

Ubuntu 下安装 Memcached 及命令解释 http://www.linuxidc.com/Linux/2013-06/85832.htm

Memcached 的安装和应用 http://www.linuxidc.com/Linux/2013-08/89165.htm

使用 Nginx+Memcached 的小图片存储方案 http://www.linuxidc.com/Linux/2013-11/92390.htm

Memcached 使用入门 http://www.linuxidc.com/Linux/2011-12/49516p2.htm

二、安装 nginx 

nginx 是一个市场份额仅次于 httpd 的 web 服务器软件,它支持反向代理、简单的负载均衡和容错、性能突出,模块化的结构,因此配置 web 服务器,nginx 是一个不错的选择;

1、安装前解决依赖关系

# yum -y groupinstall “Development Tools”

# yum -y install pcre-devel openssl-devel

 

2、安装 nginx

# useradd -r nginx

# tar xf nginx-1.4.7.tar.gz

# cd nginx-1.4.7

# ./configure \

–prefix=/usr/local/nginx \

–sbin-path=/usr/local/nginx/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 \

–with-pcre

# make && make install

Nginx 配置基于 Memcached 缓存 Web 服务器

 

3、提供服务服脚本

# 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/local/nginx/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

 

给脚本添加执行权限:

# chmod +x /etc/rc.d/init.d/nginx

 

启动服务:

# service nginx star

 

Nginx 配置基于 Memcached 缓存 Web 服务器

 

 更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-04/100914p2.htm

三、安装 PHP

 

1、解决依赖关系

# yum -y groupinstall “Desktop Platform Development”

# yum -y install bzip2-devel libmcrypt-devel libmcrypt mhash mhash-devel curl-devel

 

2、安装 PHP

# tar xf php-5.4.26.tar.bz2

# cd php-5.4.26

# ./configure –prefix=/usr/local/php –with-mysql=/usr/local/mysql –with-openssl –enable-fpm –enable-sockets –enable-sysvshm –with-mysqli=/usr/local/mysql/bin/mysql_config –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib-dir –with-libxml-dir=/usr –enable-xml –with-mhash –with-mcrypt –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –with-bz2 –with-curl

# make

# make test

# make install

 

3、提供配置

# cp php.ini-production /etc/php.ini

# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

# chmod +x /etc/rc.d/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

# vim /usr/local/php/etc/php-fpm.conf

pm.max_children = 50

pm.start_servers = 5

pm.min_spare_servers = 2

pm.max_spare_servers = 8

pid = /usr/local/php/var/run/php-fpm.pid

# service php-fpm start

# ss -tnlp | grep php-fpm

 

四、整合 nginx 和 php5

 

1、编辑 /etc/nginx/nginx.conf,启用如下选项:

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;

}

 

2、编辑 /etc/nginx/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;

 

并在所支持的主页面格式中添加 php 格式的主页,类似如下:

location / {

root html;

index index.php index.html index.htm;

}

 

而后重新载入 nginx 的配置文件:

# service nginx reload

 

3、在 /usr/local/nginx/html 新建 index.php 的测试页面,测试 php 是否能正常工作:

# vim /usr/html/index.php

<?php

phpinfo();

 

 

接着就可以通过浏览器访问此测试页面了。

五、安装 xcache,为 php 加速

 

1、安装

# tar xf xcache-3.1.0.tar.bz2

# 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-zts-20100525/

 

2、编辑 php.ini,整合 php 和 xcache:

 

首先将 xcache 提供的样例配置导入 php.ini

# mkdir /etc/php.d

# cp xcache.ini /etc/php.d

 

说明:xcache.ini 文件在 xcache 的源码目录中。

 

接下来编辑 /etc/php.d/xcache.ini,找到 extension 开头的行,修改为如下行:

# vim /etc/php.d/xcache.ini

extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

# service php-fpm restart

 

注意:如果 php.ini 文件中有多条 zend_extension 指令行,要确保此新增的行排在第一位。

Nginx 配置基于 Memcached 缓存 Web 服务器

 

六、安装 Memcached

 

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态 Web 应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached 基于一个存储键 / 值对的 hashmap。其守护进程(daemon)是用 C 写的,但是客户端可以用任何语言来编写,并通过 memcached 协议与守护进程通信。

 

1、安装 Memcached

# yum -y install memcached

 

2、启动 Memcached

# service memcached start

# ss -tnl | grep 11211

 

七、安装 Memcached 的 PHP 扩展

 

1、安装 Memcached

# tar xf memcache-2.2.7.tgz

# cd memcache-2.2.7

/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、编辑 /etc/php.ini,在“动态模块”相关的位置添加如下一行来载入 memcache 扩展:

# vim /etc/php.ini

extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so

Nginx 配置基于 Memcached 缓存 Web 服务器

 

而后对 memcached 功能进行测试,在网站目录中建立测试页面 test.php,添加如下内容:

<?php

$mem = new Memcache;

$mem->connect(“127.0.0.1”, 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 配置基于 Memcached 缓存 Web 服务器

相关阅读

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 搭建视频点播服务器(仿真专业流媒体软件)http://www.linuxidc.com/Linux/2012-08/69151.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计13147字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中