Nginx结合memcached实现LNMMP平台搭建
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,其特点是占有内存少,并发能力强,它的主要功用中还有一项是作为缓存服务器,在某些场景下,如后端的服务器是数据库服务器时,Nginx与其他就需要配合其他专门用于缓存数据库中数据的软件结合起来可以更好的完成缓存数据的功能,比如memcached,此时的Nginx作为代理服务器使用。实验:完成LNNMP平台的搭建模拟
实验用主机:
172.16.103.1 (安装Nginx、php、MariaDB)
172.16.103.2 (安装memcached,用于缓存php服务器的响应给Nginx服务器的结果)
简要说明:172.16.103.1主机上安装的nginx作为反向代理使用,也作为web服务器使用,只不过在处理php等动态页面时交给了php应用程序来处理,php程序是使用fastcgi的方式工作的,监听在TCP的9000端口,php是使用编译的方式安装的,安装时都运行在了同一台主机上,包括MariaDB。
实验过程:
一、安装MariaDB
由于在编译php时对数据库有依赖关系,所以先安装好数据库。步骤如下:
# useradd -r mysql
# mkdir /mydata/data
# chown -R mysql.mysql /mydata/data
# tar xf mariadb-5.5.39-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local
# ln -sv mariadb-5.5.39-linux-x86_64/ mysql
# cd mysql
# chown -R root.mysql ./*
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
# mkdir /etc/mysql
# cp support-files/my-large.cnf /etc/mysql/my.cnf
# vim /etc/mysql/my.cnf 添加如下几行内容:
datadir = /mydata/data
innodb_file_per_table = ON
二、编译安装php(版本是php-5.4.31)
编译前解决依赖关系:
# yum groupinstall -y "Development tools" "Server Platform Development"
# yum install mhash-devel libcurl-devel bzip2-devel gd-devel libxml2-devel
执行编译安装的过程:
# tar xf php-5.4.31.tar.bz2
# cd php-5.4.31
# ./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 install 为php提供配置文件,服务启动脚本及php-fpm的配置文件:
# 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
# cd /usr/local/php/etc/
# cp php-fpm.conf.default php-fpm.conf
# vim php-fpm.conf#在php-fpm的配置文件中需要修改的内容为,主要是需要设定这两个文件的具体路径,配置文件中的默认设置与实际安装完成后的文件所在的位置不一致,所以需要改变。
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /var/log/php-fpm.log 为php程序安装memcached的扩展模块,该功能模块使用的是memcache-2.2.7.tgz的版本,以实现缓存数据的存储。
# tar xf memcache-2.2.7.tgz
# cd memcache-2.2.7
# /usr/local/php/bin/phpize #在编译memcache之前使用phpize获取当前系统信息,以便后续完成编译工作
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
# make && make install
# vim /etc/php.ini#将编译安装生成的php扩展模块memcache.so的路径写入php配置文件中
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
三、编译安装Nginx
Nginx的安装依赖于pcre-devel包,安装前需要先安装这个rpm包,以保证编译顺利完成。
# yum -y install pcre-devel
# ./configure --prefix=/usr --sbin-path=/usr/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服务提供脚本文件,方便启动停止服务
# vim /etc/rc.d/init.d/nginx
# chmod +x /etc/rc.d/init.d/nginx
# chkconfig --add nginx
# service nginx start #尝试启动服务 由于安装的php应用程序是运行于fastcgi模式,所以需要编辑Nginx的配置使得其可以向php服务器传递必要的参数,以保证请求的数据可以正常的显示。
# vim /etc/nginx/fastcgi_params
fastcgi_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWARE nginx;
fastcgi_paramQUERY_STRING $query_string;
fastcgi_paramREQUEST_METHOD $request_method;
fastcgi_paramCONTENT_TYPE $content_type;
fastcgi_paramCONTENT_LENGTH $content_length;
fastcgi_paramSCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_paramSCRIPT_NAME $fastcgi_script_name;
fastcgi_paramREQUEST_URI $request_uri;
fastcgi_paramDOCUMENT_URI $document_uri;
fastcgi_paramDOCUMENT_ROOT $document_root;
fastcgi_paramSERVER_PROTOCOL $server_protocol;
fastcgi_paramREMOTE_ADDR $remote_addr;
fastcgi_paramREMOTE_PORT $remote_port;
fastcgi_paramSERVER_ADDR $server_addr;
fastcgi_paramSERVER_PORT $server_port;
fastcgi_paramSERVER_NAME $server_name; # vim /etc/nginx/nginx.conf#编辑配置文件,开启fastcgi功能,以完成当用户请求动态页面内容时Nginx服务器可以向后端的php服务器转发用户的请求:
在配置文件中需要添加的内容如下说明,配置文件中server段包括全局段其他部分使用的是默认配置,未列出:
server {
listen 80; #指定默认监听的端口
server_namewww.a.com; #指定服务器名称
#charset koi8-r;
#access_loglogs/host.access.logmain;
location / {
root /www/a.com; #定义站点的根目录
indexindex.php index.html index.htm; #指定支持的默认首页类型,注意需要添加上index.php
}
location ~ \.php$ { #这部分内容默认是注销的,需要开启,第一行是使用正则表达式匹配用户请求的url以.php结尾时,将请求转发给后端的php服务器来响应
root /www/a.com;
fastcgi_pass 127.0.0.1:9000;#安装的php应用程序也在本机上,所以转发时使用的地址是127.0.0.1:9000,其中php程序监听的端口是9000
fastcgi_indexindex.php;#设定支持的首页类型
fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;#用于指定向后端php服务器发送数据请求时传递的fastcgi参数及文件路径。
include fastcgi_params;
}
} 四、在虚拟机172.16.103.2上安装memcache程序并启动。
# yum install -y memcached
# service memcached start 五、测试缓存数据的效果。
前端用户的请求发往nginx如果是请求动态内容时才会发往后端的php服务器,所以在站点的根目录下创建一个.php结尾的测试文件memcache,添加如下内容为:
页:
[1]