Nginx+PHP(FastCGI)高性能服务器加载redis+memcache模块
1)Nginx+FastCGI安装配置:
yum install openssl openssl-devel pcre-devel pcre zlib zlib-devel –y
#下载Nginx源码包,upstream_check健康检查模块,sticky负载均衡模块,Lua模块
cd /usr//local/src
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz
wget -c http://nginx.org/download/nginx-1.8.1.tar.gz
#解压源码包安装
mkdir /usr/local/modules ###存放所以的第三方模块目录
1.tar -zxvf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make install PREFIX=/usr/local/LuaJIT
最后一行会输出一下提示:
==== Successfully installed LuaJIT 2.0.5 to /usr/local/LuaJIT ====
vi /etc/profile
文件末尾加入环境变量:export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
source /etc/profile
ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2 #不增加这行,nginx启动会报错
2.unzip master.zip
mv nginx_upstream_check_module-master/ /usr/local/modules/nginx-check-module
3.tar -zxf master.tar.gz
mv nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d/ /usr/local/modules/nginx-sticky-module
4.tar -xzvf v0.3.0.tar.gz -C /usr/local/modules
5.tar -xzvf v0.10.8.tar.gz -C /usr/local/modules
6.tar zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
在nginx目录先patch 一下
patch -p1 < /usr/local/modules//nginx-check-module/check_1.7.5+.patch
#预编译Nginx
useradd www
./configure --user=www --group=www --prefix=/usr/local/nginx --with-pcre --with-pcre-jit --with-http_sub_module --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_realip_module --with-http_spdy_module --with-http_gunzip_module --with-http_gzip_static_module --add-module=/usr/local/modules/nginx-check-module --add-module=/usr/local/modules/nginx-sticky-module --add-module=/usr/local/modules/ngx_devel_kit-0.2.19 --add-module=/usr/local/modules/lua-nginx-module-0.10.8
#.configure预编译成功后,执行make命令进行编译
make
#make执行成功后,执行make install 正式安装
make install
#自此Nginx安装完毕
/usr/local/nginx/sbin/nginx -t 检查nginx配置文件是否正确,返回OK即正确。
[root@localhost ~]# /usr/local/nginx/sbin/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
[root@localhost ~]#
然后启动nginx,/usr/local/nginx/sbin/nginx 回车即可。查看进程是否已启动:
[root@localhost ~]# ps -ef |grep nginx
nobody 5381 30285 0 May16 ? 00:04:31 nginx: worker process
root 30285 1 0 2014 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 32260 32220 0 12:34 pts/0 00:00:00 grep nginx
2) 下载安装并编译PHP
安装依赖包:
yum install -y gcc gcc-c++ zlib zlib-devel pcre pcre-devel gd libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers mcrypt libmcrypt libmcrypt-devel mhash
php安装:
wget –c http://cn2.php.net/distributions/php-5.6.14.tar.gz
tar –zxf php-5.6.14.tar.gz
cd php-5.6.14
./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --enable-fpm --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache
make
make install
cp php.ini-development /usr/local/php5/etc/php.ini
cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
/usr/local/php5/sbin/php-fpm
3) Ninx配置文件设置conf/nginx.conf:
user www www;
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
error_log /usr/local/nginx/logs/error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events {
use epoll;
worker_connections 102400;
multi_accept on;
}
http {
include mime.types;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$request_time"';
default_type application/octet-stream;
access_log logs/access.log main ;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 4;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
large_client_header_buffers 4 4k;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
server {
listen 80;
server_name localhost;
location / {
index index.html index.php;
root /usr/local/nginx/html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}location /hello_lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}}
}
然后启动nginx,/usr/local/nginx/sbin/nginx -s reload 回车即可。
然后访问 ip/hello_lua 会出现”hello, lua”表示lua安装成功。
4)启动PHP-FPM(php-fpm,php-ini文件配置优化参照文档:https://www.cnblogs.com/zhangan/p/10910448.html)
/usr/local/php5/sbin/php-fpm
ps -aux | grep php
echo "/usr/local/php/sbin/php-fpm" >>/etc/rc.local
5) 测试Nginx+PHP整合结果
cat >> /usr/local/nginx/html/index.php << EOF
<?php phpinfo(); ?>
EOF
6)php整合memcached
#===========================memcached===============================================
下面两个软件是php扩展memcached的包
wget https://launchpadlibrarian.net/165454254/libmemcached-1.0.18.tar.gz
wget http://pecl.php.net/get/memcached-2.2.0.tgz
./configure --prefix=/usr/local/libmemcached --with-memcached
make ; make install
cd memcached-2.2.0
/usr/local/php5/bin/phpize
./configure --enable-memcached --with-php-config=/usr/local/php5/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
make ; make install
ll /usr/local/php5/lib/php/extensions/no-debug-non-zts-20131226/
-rwxr-xr-x 1 root root 335433 1月 8 12:37 memcached.so
-rw-r--r-- 1 root root 259968 1月 8 11:32 memcache.so
-rwxr-xr-x 1 root root 1104016 1月 8 10:28 opcache.a
-rwxr-xr-x 1 root root 585616 1月 8 10:28 opcache.so
echo "extension=memcached.so" >> /usr/local/php5/etc/php.ini
重新启动php,pkill php&&/usr/local/php5/sbin/php-fpm
![]()
7)下面是php整合redis
wget http://test.hexin.cn/software/redis-2.2.7.tgz
tar –zxf redis-2.2.7.tgz
cd redis-2.2.7.tgz
/usr/local/php5/bin/phpize
./configure --with-php-config=/usr/local/php5/bin/php-config
make && make install
echo "extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20131226/" " >> /usr/local/php5/etc/php.ini (可以默认不添加)
echo "extension=redis.so" >>/usr/local/php5/etc/php.ini
8)php整合memcache
wget http://test.hexin.cn/software/memcache-3.0.8.tgz
tar –zxf memcache-3.0.8.tgz
cd memcache-3.0.8
/usr/local/php/bin5/phpize
./configure --with-php-config=/usr/local/php5/bin/php-config
make
make install
ll /usr/local/php/lib/php5/extensions/no-debug-non-zts-20131226/
echo "extension=memcache.so" >> /usr/local/php5/etc/php.ini
/usr/local/php5/bin/php -m 查看php加载的所有模块