上一篇博客讲解了整个缓存流程中数据库部分的搭建,有了最基础的部分,现在可以搭建上层的内容了。

Web服务器

整个缓存流程中,客户端都是以HTTP请求的方式获取服务器上数据库中的数据。我们选择了Nginx作为HTTP请求解析和负载均衡的工具。 Nginx作为一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器。它以稳定性、丰富的功能和第系统资源消耗而闻名。下面开始介绍Nginx的安装和配置: 以往的安装方式都是直接编译源码,这次来点新的。

Ngxin打包安装

按照nginx官网上的方法来进行操作。 首先打开终端,进入/usr/local目录,下载rpm文件:

[wangmeg@localhost ~]$ cd /usr/local
[root@localhost local]# wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-	release-centos-7-0.el7.ngx.noarch.rpm

之后安装rpm文件,并使用yum 命令进行Nginx的自动安装:

[root@localhost local]# rpm -i nginx-release-centos-7-0.el7.ngx.noarch.rpm 
[root@localhost local]# yum install nginx

稍等片刻,终端出现

----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : 1:nginx-1.8.0-1.el7.ngx.x86_64                               1/1 

Installed:
  nginx.x86_64 1:1.8.0-1.el7.ngx                                                

Complete!

表示Nginx安装完毕。现在,在终端键入命令nginx启动nginx,在浏览器中输入http:127.0.0.1之后出现 这样,Nginx就算安装完毕了。 可以在终端键入命令: nginx -V 来查看这样默认安装的相关配置。

[root@localhost local]# nginx -V
nginx version: nginx/1.8.0
built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --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.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

Nginx源码编译

相对于上述使用yum安装nginx,我更习惯编译源码安装。 在进行安装以前,需要安装pcre和zlib。

pcre安装

[root@localhost etc]# cd /usr/local
[root@localhost local]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz
[root@localhost local]# tar -zxvf pcre-8.37.tar.gz 
[root@localhost pcre-8.37]# cd pcre-8.37/
[root@localhost pcre-8.37]# ./configure 
[root@localhost pcre-8.37]# make 
[root@localhost pcre-8.37]# make install

这样,pcre就算安装完成。

zlib安装

[root@localhost local]# wget http://zlib.net/zlib-1.2.8.tar.gz
[root@localhost local]# tar -zxvcf zlib-1.2.8.tar.gz 
[root@localhost local]# cd zlib-1.2.8/
[root@localhost zlib-1.2.8]# make && make install

OK,zlib也安装完毕。

Nginx源码编译安装

接下来下载nginx源码,完成后续的安装和配置。

[root@localhost pcre-8.37]# cd /usr/local [root@localhost local]# wget http://nginx.org/download/nginx-1.8.0.tar.gz [root@localhost local]# tar -zxvf nginx-1.8.0.tar.gz [root@localhost local]# cd ./nginx-1.8.0/ [root@localhost nginx-1.8.0]# ./configure –prefix=/usr/local/nginx [root@localhost nginx-1.8.0]# make && make install

如果没有错误提示,则表示nginx编译安装完成。 现在键入命令/usr/local/nginx/sbin/nginx就可以启动nginx了。 另外,终止nginx的命令使用:/usr/local/nginx/sbin/nginx -s stop;进行nginx平滑重启使用命令/usr/local/nginx/sbin/nginx -s reload。

PHP

上边的操作,算是完成了nginx的初步安装,接下来开始进行PHP的安装。 首先,需要下载PHP,可以从官网上进行下载,我选择的是5.4.43版本。

1
2
[root@localhost Desktop]# cd /usr/local
[root@localhost local]# wget http://php.net/distributions/php-5.4.43.tar.gz

完成解压

1
[root@localhost local]# tar -zxvf php-5.4.43.tar.gz

进行安装

1
2
[root@localhost local]# cd php-5.4.43/
[root@localhost php-5.4.43]# ./configure --prefix=/usr/local/php --enable-fpm

检查配置的过程,我的机器上出现了错误:

1
2
checking for xml2-config path... 
configure: error: xml2-config not found. Please check your libxml2 installation.

表示缺少libxml2,不用担心,使用yum 安装便是。

1
2
[root@localhost php-5.4.43]# yum install libxml2 
[root@localhost php-5.4.43]# yum install libxml2-devel -y

再次进行配置检查,出现下面这些则可进行下一步操作。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

配置环境没问题,就可以进行make && make install,完成安装了。 安装完成之后,不要忘记将PHP配置文件放到指定位置。 [root@localhost php-5.4.43]# cp ./php.ini-development /usr/local/php/etc/php.ini 完成PHP安装之后,咱们来给它加点料,前边说了,整个流程,需要使用PHP来获取Mongodb中的数据集信息,咱们顺势把mongodb的扩展也装上把。

先从github上将php的mongodb扩展下载。按照网页中的方法进行安装配置。 需要说明的一点是。phpize的默认安装位置是/usr/local/php/bin/phpize;php-config默认位置是/usr/local/php/bin/php-config。 这样。PHP的安装和需要的扩展已经安装完毕。

Nginx+php的配置

现在nginx和php都已经分别安装完成,现在可以检验一下他们在一起能否正常运行。

  1. 修改nginx配置文件
1
[root@localhost ~]# gedit /usr/local/nginx/conf/nginx.conf
  1. 找到这一段,取消前边的注释“#”,修改其中内容
1
2
3
4
5
6
7
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;
        }
  1. 在/usr/lcoal/nginx/html/文件夹下创建文件info.php,写入内容
1
2
3
<?php
   echo phpinfo();
?>
  1. 启动nginx和php-fpm
1
2
[root@localhost ~]# /usr/local/nginx/sbin/nginx 
[root@localhost ~]# /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf 
  1. 启动firefox,地址栏中输入"127.0.0.1/info.php",出现如下结果则表明运行正常。

  2. 这个网页往下,可以看到这样的结果,表示mongodb的扩展也安装成功。

未完待续

整个流程内容较多,下一篇博客,经会介绍如何安装配置nginx的Lua扩展,完成整个流程的搭建。 最后,以一张美图来结束这篇博客。 雨后初晴的武汉