nginx配置基於memcached緩存web服務器


1、配置環境php


Linux:CentOS 6.5 64bithtml

nginx:nginx-1.4.7mysql

php: php-5.4.26nginx

mysql: mysql-5.5.33web

xcache: xcache-3.1.0sql

memcached: memcached-1.4.4-3.el6.x86_64數據庫

memcached PHP擴展: memcache-2.2.7vim


2、安裝nginx
api


nginx是一個市場份額僅次於httpd的web服務器軟件,它支持反向代理、簡單的負載均衡和容錯、性能突出,模塊化的結構,所以配置web服務器,nginx是一個不錯的選擇;瀏覽器


一、安裝前解決依賴關係

# yum -y groupinstall "Development Tools"

# yum -y install pcre-devel openssl-devel


二、安裝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

wKioL1NcbjLDY1-EAABFmk_fJmc725.jpg


三、提供服務服腳本

# 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


wKioL1NdBt7hLcYpAAHc6sgvVIY901.jpg



2、安裝mysql


一、建立mysql用戶

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


二、在生產環境中,一般mysql的數據存放目錄應該單獨放在一個分區,這個分區最好作成lvm邏輯卷,方便之後空間的擴展及數據備份;此處以目錄/mysqldata/data做爲mysql的數據存放目錄,以binlog做爲mysql二進制日誌文件存放目錄;

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

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


三、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


四、解決依賴關係

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


五、安裝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


六、初始化mysql

# cd /usr/local/mysql

wKioL1NcehbTt435AAKwXxwK0DU358.jpg


# 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

wKiom1Nce67TznV2AAHfYdlyuMo368.jpg


七、配置全局參數


編輯環境變量提供軟件路徑

# 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


3、安裝PHP


一、解決依賴關係

# yum -y groupinstall "Desktop Platform Development"

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


二、安裝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


三、提供配置

# 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


4、整合nginx和php5


一、編輯/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;

       }


二、編輯/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


三、在/usr/local/nginx/html新建index.php的測試頁面,測試php是否能正常工做:

# vim /usr/html/index.php

<?php

phpinfo();



接着就能夠經過瀏覽器訪問此測試頁面了。



5、安裝xcache,爲php加速


一、安裝

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


二、編輯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指令行,要確保此新增的行排在第一位。

wKiom1NdBZywmQ4RAAHOEATIZms412.jpg


6、安裝Memcached


Memcached 是一個高性能的分佈式內存對象緩存系統,用於動態Web應用以減輕數據庫負載。它經過在內存中緩存數據和對象來減小讀取數據庫的次數,從而提升動態、數據庫驅動網站的速度。Memcached基於一個存儲鍵/值對的hashmap。其守護進程(daemon )是用C寫的,可是客戶端能夠用任何語言來編寫,並經過memcached協議與守護進程通訊。


一、安裝Memcached

# yum -y install memcached


二、啓動Memcached

# service memcached start

# ss -tnl | grep 11211


7、安裝Memcached的PHP擴展


一、安裝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/


二、編輯/etc/php.ini,在「動態模塊」相關的位置添加以下一行來載入memcache擴展:

# vim /etc/php.ini

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

wKiom1NdCbOxha34AAI5ncmMdOU965.jpg


然後對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.";        

?>


wKioL1NdDJvhuWVSAAETJAIgR1E120.jpg

相關文章
相關標籤/搜索