yum实现LNMP环境搭建

2018-06-13  本文已影响0人  心疼你萌萌哒
============实现LNMP环境搭建。==============

开始安装Nginx和PHP-FPM之前,首先卸载系统中以前安装的Apache和PHP保证安装不会冲突。用root登录输入下面的命令:

[plain] view plain copy
yum remove httpd* php*  
增加额外资源库
    默认情况下,CentOS的官方资源是没有php-fpm的, 但我们可以从Remi的RPM资源中获得,它依赖于EPEL资源。我们可以这样增加两个资源库:

1:安装nginx:

[plain] view plain copy
========yum install nginx  

安装完成后可以启动nginx,在浏览器里面访问,查看nginx是否安装成功。端口默认为80。
[plain] view plain copy

nginx中yum安装的默认网站根目录在/usr/share/nginx/html  
结果如下:


表示已成功安装nginx.

2:安装PHP和PHP-FPM:

[plain] view plain copy
==========yum install php php-fpm  
[plain] view plain copy

3:将PHP与mysql模块关联起来:

数据库再变不在安装。
[plain] view plain copy
可以通过yum install mariadh mariadb-server 安装  
[plain] view plain copy
==================yum install php-gd php-mysql php-mbstring php-xml php-mcrypt  php-imap php-odbc php-pear php -xmlrpc  

4:配置nginx与php一起工作:


Nginx+FastCGI运行原理
Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket,(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接纳到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端,这就是Nginx+FastCGI的整个运作过程。详细的过程,如下图所示:


打开nginx主配置文件。

=============vim /etc/nginx/nginx.conf

[plain] view plain copy
在http模块中添加配置:  
       location / {  
        root   /usr/share/nginx/html;  
           index  index.html index.htm index.php;  
        }  
location ~ \.php$ {  
           root           /wing;  
           fastcgi_pass   127.0.0.1:9000;  
           fastcgi_index  index.php;        
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
           include        fastcgi_params;  
       }  
1、location ~ \.php$ 匹配到php文件就进行fastcgi操作
2、fastcgi_pass   127.0.0.1:9000;指明nginx与fastcgi交互的ip和端口号,也就是fastcgi监听的端口
3、fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;这里声明了一个fastcgi参数,参数名称为SCRIPT_FILENAME(对,也就是php中$_SERVER['SCRIPT_FILENAME']的值),这个参数表明目标进程要执行哪个php脚本$document_root,$fastcgi_script_name都是nginx的常量,稍后给解释。此处声明的参数会通信给php-cgi,php-cgi会进行一些php的常量超级变量的初始化,
===============mkdir /wing
cat > /wing/index.php  <<eof
<?php    
phpinfo();    
?>   
eof

==============vim /etc/php-fpm.d/www.conf 将用户和组改成nginx 和 /etc/nginx/nginx.conf 里的用户一样
           user = nginx
           group = nginx

5:测试nginx与php是否正常。

重启nginx服务器,在网站根目录创建一个index.php文件
=========systemctl start nginx  
启动php-fpm  
==========systemctl start php-fpm  

解释说明:
1、location ~ \.php$ 匹配到php文件就进行fastcgi操作
2、fastcgi_pass   127.0.0.1:9000;指明nginx与fastcgi交互的id和端口号,也就是fastcgi监听的端口
3、fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;这里声明了一个fastcgi参数,参数名称为SCRIPT_FILENAME(对,也就是php中$_SERVER['SCRIPT_FILENAME']的值),这个参数表明目标进程要执行哪个php脚本$document_root,$fastcgi_script_name都是nginx的常量,稍后给解释。此处声明的参数会通信给php-cgi,php-cgi会进行一些php的常量超级变量的初始化,例如:$_SERVER['REQUEST_URI'],$_SERVER['SCRIPT_FILENAME'],$_SERVER['QUERY_STRING']……。
4、include        fastcgi_params;这里是包含所有nginx常量,传递给php-cgi。
  当然这里可以—》
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  fastcgi_param  REQUEST_URI $request_uri;
  fastcgi_param  QUERY_STRING $query_string;
  ......

部分nginx常量解释,以http://127.0.0.1:8000/?test=123&test2=abc为例子:
$args    #这个变量等于请求行中(GET请求)的参数,test=123&test2=abc;
$content_length #请求头中的Content-length字段;
$content_type  #请求头中的Content-Type字段;
$document_root #当前请求在root指令中指定的值,文件的物理root路径,F:/home/projects/test;
$document_uri  #同$uri;
$uri    #不带请求参数的当前URI,$uri不包含主机名,本例为"/"。该值有可能和$request_uri 不一致。$request_uri是浏览器发过来的值,该值不会被改变。该值有可能是rewrite后的值。例如做了internal redirects后。好多框架、伪静态都用到rewrite。$_SERVER['REQUEST_URI']往往是一些框架初始化关键,$request_uri的不可变性保证了这一点。
$host  #请求主机头字段,否则为服务器名称。127.0.0.1
$query_string  #同$args;
$request_method #GET或POST;
$remote_addr    #客户端的IP地址;
$remote_port    #客户端的端口;
$server_addr    #服务器地址 127.0.0.1
$server_name    #服务器名称,localhost;
$server_port    #8000;
$request_filename #当前请求的物理文件的名称,本例为F:/home/projects/test/index.php。此值可以因重定向改变。
$request_uri     #包含请求参数的原始URI,不包含主机名,由客户端请求决定,不能修改。”/?test=123&test2=abc”。

                nginx
                ip 52
              
web1       web2      php1       php2
ip 50       51        48         49 
====================================================
【web1和web2可以自己定义web目录
 server {
        listen        1060;
        server_name  web.102.com;或者web.101.com
        location /{
            root /102/html;
            index index.html index.htm;
                }
        }
】
==================================================
根据动静分离进行调度
ip 52===== vim /etc/nginx/ngix.conf
http {
     upstream htmlservers {
        server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
         }
         
     upstream phpservers {
        server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
        server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
         }
         
      server {
            location ~* \.html$ {
            proxy_pass http://htmlservers;
            }
            
            location ~* \.php$ {
            proxy_pass http://phpservers;
            }
      }
 }

上一篇下一篇

猜你喜欢

热点阅读