zoukankan      html  css  js  c++  java
  • linux后台server开发环境的部署配置和验证(nginx+apache+php-fpm+FASTCGI(C/C++))


    linux后台server开发环境部署配置

    引言
    背景
    随着互联网业务的不断增多。开发环境变得越来越复杂,为了便于统一server端的开发部署环境,特制定本配置文档。


    使用软件
    CentOS 6.3(Linux version 2.6.32-279.el6.x86_64)
    gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)
    本次配置
    Nginx 1.5.8
    Apache 2.4.7
    php 5.3.26

    目的
      构造WEB前端技术架构。web前端的部署结构技术全然完毕。
      完整描写叙述 nginx + apache + FASTCGI(C/C++) 和nginx + php-fpm +FASTCGI(PHP)两种FASTCGI架构。
    nginx 安装
    软件准备
    http://nginx.org/en/download.html
    官网下载版本号,nginx-1.5.8.tar.gz

    http://www.pcre.org/
    官网下载 pcre-8.34.tar.gz

    http://zlib.net/
    官网下载 zlib-1.2.5.tar.gz

    http://www.openssl.org/source/
    官网下载 openssl-1.0.1d.tar.gz

    工作文件夹准备
    建立工作文件夹 nginx_make,把上面下载的文件都放到该文件夹下。
    解压全部的 .tar.gz 文件,相应出现个文件夹。
    安装 pcre
    cd pcre-8.34
    ./configure --prefix=/usr/local/pcre-8.34
    make
    make install
    安装zlib
    假设本地已经安装了 zlib 。能够不安装了。


    安装步骤类似上面的,可是强烈建议编译安装的时候指定文件夹和安装版本号。
    安装openssl
    假设本地已经安装 openssl,能够不安装了。
    安装步骤类似上面的,可是强烈建议编译安装的时候指定文件夹和安装版本号
    安装nginx
    cd nginx-1.5.8
    ./configure --user=root --group=root --prefix=/usr/local/nginx-1.5.8 --pid-path=/usr/local/nginx-1.5.8/nginx.pid --error-log-path=/data/logs/nginx/err/nginx --http-log-path=/data/logs/nginx/acc/nginx --with-http_gzip_static_module --with-poll_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre=../pcre-8.34 --with-cc-opt=-O2
    make
    make install

    apache 安装
    软件准备
    http://httpd.apache.org/
    官网下载版本号,httpd-2.4.7.tar.gz

    http://apr.apache.org/
    官网下载 apr-1.5.0.tar.gz

    http://apr.apache.org/
    官网下载 apr-util-1.5.3.tar.gz

    http://www.pcre.org/
    官网下载 pcre-8.34.tar.gz
    工作文件夹准备
    建立工作文件夹 apache_make,把上面下载的文件都放到该文件夹下。
    解压全部的 .tar.gz 文件,相应出现个文件夹。
    安装 apr
    cd apr-1.5.0
    ./configure --prefix=/usr/local/apr-1.5.0
    make
    make install
    安装 apr-util
    cd apr-1.5.0
    ./configure --prefix=/usr/local/apr-util-1.5.3/ --with-apr=/usr/local/apr-1.5.3/bin/apr-1-config
    安装 pcre
    cd pcre-8.34
    ./configure --prefix=/usr/local/pcre-8.34/ --with-apr=/usr/local/apr-1.5.3/bin/apr-1-config
    參考上面已经安装好的 pcre 。


    安装apache
    cd httpd-2.4.7
    ./configure --prefix=/usr/local/apache2.4.7 --with-pcre=/usr/local/pcre-8.34/ --with-apr=/usr/local/apr-1.5.0 --with-apr-util=/usr/local/apr-util-1.5.3  --enable-so --with-mpm=worker --disable-ssl --enable-cgid
    make
    make install

    php 安装
    软件准备
    http://www.php.net/releases/
    官网下载版本号,php-5.3.26.tar.gz
    工作文件夹准备
    建立工作文件夹 php_make,把上面下载的文件都放到该文件夹下。
    解压全部的 .tar.gz 文件。相应出现个文件夹。


    构造 libphp5.so
    编译 php,构造 libphp5.so,用于 apache 解析 php。
    cd php-5.3.26
    make clean;
    ./configure --prefix=/usr/local/php-5.3.26 -with-apxs2=/usr/local/apache2.4.7/bin/apxs --with-curl --with-curlwrappers
    make
    make install
    安装 php-fpm
    编译 php,支持 php-fpm和 socket,并启用curl模块,用于 nginx 以 FASTCGI 模式解析运行 php。

    cd php-5.3.26
    make clean;
    ./configure --prefix=/usr/local/php-5.3.26 --enable-fpm --enable-sockets --with-curl --with-curlwrappers
    make
    make install

    安装 php
    cd php-5.3.26
    make install
    php.ini安装
    cd php-5.3.26
    cp php.ini-development /usr/local/php-5.3.26/lib/php.ini
    nginx+php-fpm 配置
    启动php-fpm
    /usr/local/php-5.3.26/sbin/php-fpm -y /usr/local/php-5.3.26/etc/php-fpm.conf -c /usr/local/php-5.3.26/lib/php.ini
    配置 nginx.conf
    改动 /usr/local/nginx-1.5.8/conf/nginx.conf。
    listen       8090;
    charset utf-8;

    location ~ .php$ {
         /usr/local/web_umsa/
         FASTCGI_pass   127.0.0.1:9000;
         FASTCGI_index  index.php;
    #FASTCGI_param  SCRIPT_FILENAME  $document_root$FASTCGI_script_name;
    include        FASTCGI_params;
    }

    配置FASTCGI_params
    改动 /usr/local/nginx-1.5.8/conf/FASTCGI_params。



    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    FASTCGI_param  REDIRECT_STATUS    200;
    FASTCGI_param  SCRIPT_FILENAME  $document_root$FASTCGI_script_name;

    測试 phpinfo.php
    按以下内容建立 php 測试文件。



     cat /usr/local/web_umsa/phpinfo.php
    <?php
    echo phpinfo();
    ?>
    验证FASTCGI模式php
    tcpdump -ilo  -XAvs0 port 9000
    抓包,然后浏览器运行
    http://127.0.0.1:8090/phpinfo.php
    浏览器输出PHP 的安装配置信息。


    能看到抓包。9000 port有数据发送,能够确认,传输的协议是 FASTCGI协议。


    apache 配置
    php5_module模块引导
    启用 /usr/local/apache2.4.7/conf/httpd.conf 里面的。
    LoadModule php5_module        modules/libphp5.so
    配置 Directory /
    改动 /usr/local/apache2.4.7/conf/httpd.conf 里面的。

    <Directory />
        Options FollowSymLinks
        AllowOverride None
        Order deny,allow
        Allow from all
    </Directory>

    配置 DocumentRoot
    改动 /usr/local/apache2.4.7/conf/httpd.conf 里面的。

    DocumentRoot "/usr/local/web_umsa"
    配置<IfModule alias_module>
    改动 /usr/local/apache2.4.7/conf/httpd.conf 里面的。



    #支持cgi
    ScriptAlias /cgi-bin/ "/usr/local/web_umsa/cgi-bin/"
    #支持 FASTCGI
    ScriptAlias /fcg-bin/ "/usr/local/web_umsa/fcgi-bin/"

    配置 <IfModule mime_module>
    改动 /usr/local/apache2.4.7/conf/httpd.conf 里面的。



    AddType application/x-httpd-php .php .phtml .php3 .inc
    AddType application/x-httpd-php-source .phps
    配置 <Directory "/usr/local/web_umsa/cgi-bin/">
    改动 /usr/local/apache2.4.7/conf/httpd.conf 里面的。

    <Directory "/usr/local/web_umsa/cgi-bin/">
        AddHandler cgi-script .cgi .ums
        Options FollowSymLinks
        AllowOverride All
        Order deny,allow
        Allow from all
        Options +ExecCGI
    </Directory>

    启动 apache
    /usr/local/apache2.4.7/bin/apachectl start

    验证 apache
    http://127.0.0.1/phpinfo.php
    http://127.0.0.1/

    两个訪问都能出来,证明成功安装。



    apache-FASTCGI(C/C++) 配置
    mod_fcgid载入
    文件下载
    官网 http://httpd.apache.org/mod_fcgid/ 下载
    得到文件 mod_fcgid-2.3.9.tar.gz

    编译安装
    把 mod_fcgid-2.3.9.tar.gz 文件解压到 apache 原代码文件夹 httpd-2.4.7,得到文件夹
    mod_fcgid-2.3.9,完整结构应该是  httpd-2.4.7/mod_fcgid-2.3.9。
    运行apache 命令../bin/apachectl -k restart,把httpd 服务起来。
    环境变量设置 APXS,设置后可查看效果
    # echo $APXS
    /usr/local/apache2.4.7/bin/apxs
    当中的 /usr/local/apache2.4.7/bin/apxs 是刚才 apache 的安装文件夹。
    运行文件夹以下的  
    ./configure.apxs;make;make install
    查看安装结果
    # grep "mod_fcgid.so" /usr/local/apache2.4.7/conf/httpd.conf
    LoadModule fcgid_module modules/mod_fcgid.so
    能查看到已经启用了 mod_fcgid 模块。


    配置FASTCGI支持
    配置 <Directory "/usr/local/web_umsa/fcgi-bin/">
    改动 /usr/local/apache2.4.7/conf/httpd.conf 里面的。




    <Directory "/usr/local/web_umsa/fcgi-bin">
        SetHandler fcgid-script
        AddHandler fcgid-script .fcgi .ums
        Options FollowSymLinks
        AllowOverride All
        Order deny,allow
        Allow from all
        Options +ExecCGI
    </Directory>

    重新启动 apache,../bin/apachectl -k restart。
    測试FASTCGI(C/C++)
    下载FASTCGI库
    官网http://www.FASTCGI.com/的http://www.FASTCGI.com/drupal/node/5位置(Current: download | docs | browse)下载 C/C++的开发包。

    最新版本号是 2.4.1,得到文件fcgi-2.4.1-SNAP-0910052249.tar.gz。

    编译库
    解压文件。并进入文件夹。运行传统命令
     ./configure;make
    看到
    ranlib .libs/libfcgi.a
    表示库已经生产。其它的编译错误不理会(是C++ 的错误,版本号太老了)。


    运行命令ls libfcgi/.libs/ -al,能够查询到 libfcgi.a和 libfcgi.so.0.0.0
    动态库和静态库都有了。
    构造样例
    进入文件夹 exapmle,运行命令(编译cgi并部署到指定文件夹)
    # gcc echo.c -o echo.ums ../libfcgi/.libs/libfcgi.a
    #cp echo.ums /usr/local/web_umsa/fcgi-bin/
    查看效果
    http://127.0.0.1/fcgi-bin/echo.ums
    能够看到页面输出:
    Request number 8, Process ID: 28256
    不断刷新页面,进程编号不变。序号不断添加。同一时候,server端查看发现
    # ps aux|grep echo.ums
    该进程一直存在,进程编号和页面一致。



    开发环境验证(仅限于内部验证)
    须要配置 nginx 转发,支持 .ums 解析。

    #TCP长连接,nginx 作为  proxy
    upstream tcp_keepalive {
        server 127.0.0.1:8080;
        keepalive 256;
    }

    #以php-fpm 实现的 fastcgi 部署方式
    location ~ .php$ {
         root           /data/php_project;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
         include        fastcgi_params;
         #记得改动  fastcgi_params,添加配置
         # PHP only, required if PHP was built with --enable-force-cgi-redirect
         #fastcgi_param  REDIRECT_STATUS    200;
         #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;     
     }


    #以为 .fcgi,.ums 结尾的请求使用http协议转发给 tcpend
    location ~* .(fcgi|ums)$ {
        #root           root
        # 向后端server发起请求时加入指定的header头信息
        #proxy_set_header Host $http_host;
        # 向后端server发送真实 IP
        #proxy_set_header X-Real-IP $remote_addr;
        # 让后端如php能直接通过变量获取真实IP
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass   http://tcpend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-By $server_addr:$server_port;
        proxy_set_header X-Forwarded-For $remote_addr;
        #proxy_set_header Connection "";
        proxy_connect_timeout 5s;
        proxy_read_timeout 10s;
        proxy_send_timeout 5s;
    }    



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    买二手房的税费详细版本
    实时推荐部分代码
    卡片推荐部分代码
    详解一下网络广告cpc、cpm、cpl、cpa、cps、cpr的计费方法是什么
    吴军硅谷来信《三板斧破四困境》
    JS实现的在线推荐逻辑
    mongo数据库时间存储的问题
    crontab 定时的陷阱
    【剑指offer】栈的压入、弹出序列
    【剑指offer】包含min函数的栈
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4881315.html
Copyright © 2011-2022 走看看