• LNMP安装与配置


          公共网关接口”(Common Gateway Interface,CGI),是HTTP服务器与本机或者其它机器上的程序进行通信的一种工具,其程序须运行在网络服务器上。

    CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量,如php、perl、tcl等。

    FAST-CGI:WEB服务器与处理程序之间通信的一种协议(App server 和Web server 之间的通信协议),是CGI的改进方案。CGI程序反复加载是CGI性能低下的主要原因,如果CGI程序保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail-Over特性等。FastCGI是常驻型的CGI,它可以一直运行,在请求到达时,不会花费时间去fork一个进程来处理。

    FastCGI是语言无关的、可伸缩架构的CGI开放扩展,将CGI解释器进程保持在内存中,以此获得较高的性能。FastCGI是一个协议,php-fpm实现了这个协议,php-fpm的FastCGI协议需要有进程池,php-fpm实现的FastCGI进程叫php-cgi,所以php-fpm其实是他自身的FastCGI或php-cgi进程管理器,如图14-5所示:

        企业级LNMP(Nginx+PHP(FastCGI)+MySQL)主流架构配置方法如下,分别安装Nginx、MYSQL、PHP服务,步骤如下:

    1、nginx源码安装

    cd  /usr/src   
    wget -c http://nginx.org/download/nginx-1.12.0.tar.gz 
    tar -xzf nginx-1.12.0.tar.gz
    cd nginx-1.12.0
    useradd www
    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
    make
    make install

    2、mysql源码安装

    yum install cmake ncurses-devel ncurses gcc-c++ –y
    wget -c https://cdn.mysql.com/archives/mysql-5.5/mysql-5.5.20.tar.gz
    tar -xzf mysql-5.5.20.tar.gz
    cd mysql-5-5.20

    cmake .
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DMYSQL_DATADIR=/data/mysql \ -DSYSCONFDIR=/etc \ -DMYSQL_USER=mysql \ -DMYSQL_TCP_PORT=3306 \ -DWITH_XTRADB_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_EXTRA_CHARSETS=1 \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DEXTRA_CHARSETS=all \ -DWITH_BIG_TABLES=1 \ -DWITH_DEBUG=0 make make install
    cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
    cp
    /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    mkdir -p /data/mysql
    useradd mysql
    /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ --basedir=/usr/local/mysql/
    ln -s /usr/local/mysql/bin/* /usr/bin/
    chkconfig --add mysqld
    chkconfig --level 35 mysqld on
    /etc/init.d/mysqld start

    3、源码安装php

    yum install -y libxml2 libxml2-devel bzip2 bzip2-devel libcurl  libcurl-devel libjpeg libjpeg-devel libpng libpng-devel  freetype-devel
    wget -c  http://cn2.php.net/distributions/php-5.6.38.tar.gz
    tar -zxf    php-5.6.38.tar.gz
    cd   php-5.6.38
    ./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --with-apxs2=/usr/local/apache/bin/apxs \
    --with-bz2 \
    --with-curl \
    --enable-ftp \
    --enable-sockets \
    --disable-ipv6 \
    --with-gd \
    --with-jpeg-dir \
    --with-png-dir \
    --with-freetype-dir \
    --enable-gd-native-ttf \
    --with-iconv-dir \
    --enable-mbstring \
    --enable-calendar \
    --with-gettext \
    --with-ldap \
    --with-libxml-dir \
    --with-zlib \
    --with-pdo-mysql=mysqlnd \
    --with-mysqli=mysqlnd \
    --with-mysql=mysqlnd \
    --enable-dom \
    --enable-xml \
    --enable-fpm \
    --with-libdir=lib64 \
    --enable-bcmath make && make install
    cp  php.ini-development   /usr/local/php/etc/php.ini
    cp  /usr/local/php/etc/php-fpm.conf.default  /usr/local/php5/etc/php-fpm.conf
    /usr/local/php/sbin/php-fpm 
    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

    4、nginx与php整合

    首先简单的讲一讲原理,目前主流的nginx+php的运行原理如下:

    (1)、nginx的worker进程直接管理每一个请求到nginx的网络请求。

    (2)对于php而言,由于在整个网络请求的过程中php是一个cgi程序的角色,所以采用名为php-fpm的进程管理程序来对这些被请求的php程序进行管理。php-fpm程序也如同nginx一样,需要监听端口,并且有master和worker进程。worker进程直接管理每一个php进程。

    (3)关于fastcgi:fastcgi是一种进程管理器,管理cgi进程。市面上有多种实现了fastcgi功能的进程管理器,php-fpm就是其中的一种。再提一点,php-fpm作为一种fast-cgi进程管理服务,会监听端口,一般默认监听9000端口,并且是监听本机,也就是只接收来自本机的端口请求,所以我们通常输入命令 netstat -nlpt|grep php-fpm 会得到:

    tcp      0    0 127.0.0.1:9000      0.0.0.0:*         LISTEN      1057/php-fpm

    这里的127.0.0.1:9000 就是监听本机9000端口的意思。

    (4)关于fastcgi的配置文件,目前fastcgi的配置文件一般放在nginx.conf同级目录下,配置文件形式,一般有两种:fastcgi.conf  和 fastcgi_params。

    不同的nginx版本会有不同的配置文件,这两个配置文件有一个非常重要的区别:fastcgi_parames文件中缺少下列配置:

    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

    我们可以打开fastcgi_parames文件加上上述行,也可以在要使用配置的地方动态添加。使得该配置生效。

    (5)当需要处理php请求时,nginx的worker进程会将请求移交给php-fpm的worker进程进行处理,也就是最开头所说的nginx调用了php,其实严格得讲是nginx间接调用php。

    了解了上面的这五个简单原理,在nginx中配置php调用方法就变得易如反掌。

    server {  
     include port.conf;  
     server_name www.jfedu.net jfedu.net;  
     index index.php index.html index.php;  
     root /usr/local/nginx/html;  
       location  ~ \.php$ {
          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;
            }
    }
    fastcgi_pass  127.0.0.1:9000:这行开始是本文的重点:这行代码的意思是,将进入到该location内的url请求看做是cgi程序,并将请求发送到9000端口,交由php-fpm处理。
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  这行配置意思是:动态添加了一行fastcgi配置,配置内容为SCRIPT_FILENAME,告知管理进程,cgi脚本名称。由于我的nginx中只有fastcgi_params文件,没有fastcgi.conf文件,所以要使php-fpm知道SCRIPT_FILENAME的具体值,就必须要动态的添加这行配置。
    使用下列代码也可以
    location  ~ \.php$ {  
          fastcgi_pass  127.0.0.1:9000;
          fastcgi_index index.php;
          include fastcgi.conf;
            }  
    }
  • 相关阅读:
    Java中List集合去除重复数据的六种方法
    常见的Redis面试"刁难"问题,值得一读
    以Integer类型传参值不变来理解Java值传参
    Linux系统安装snmp服务
    直接取数据到RANGE
    SAP翔子_2019集结号
    销售订单BOM组件分配(CP_BD_DIRECT_INPUT_PLAN_EXT)
    SAP翔子_webservice篇索引
    函数篇3 EXCEL导入函数去除行数限制
    ABAP基础篇4 常用的字符串操作语法
  • 原文地址:https://www.cnblogs.com/deny/p/9979778.html
Copyright © 2020-2023  润新知