##########################################################################################################

LNMP:

-L :LINUX操作系统
-N :Nginx网站服务软件
-M :Mysql Mariadb数据库
-P :PHP Python Perl网站开发语言

#################################################################################################

准备三台虚拟机:
Web1:
eth1网卡:192.168.2.100
Proxy:
eth0网卡:192.168.4.5
eth1网卡:192.168.2.5
Client:
eth0网卡:192.168.4.100

防火墙设置为trusted
selinux设置为permissive

##################################################################################################
LNMP部署:

#安装Nginx(上篇博客里面有安装方法)

#yum -y install mariadb mariadb-server mariadb-devel         //安装Mariadb

#yum -y install php php-mysql                        //php-mysql用来连接数据库

#rpm -ivh php-fpm-5.4.16-36.el7_1.x86_64.rpm            //安装php-fpm

#nginx                //启动nginx服务
#systemctl start mariadb        //启动mariadb服务
#systemctl enable mariadb    //随机自启
#systemctl start php-fpm        //启动php-fpm服务
#systemctl enable php-fpm    //随机自启

#netstat -anptu | grep "80"    //验证nginx是否启动
#netstat -anptu | grep "3306"    //验证mariadb是否启动
#netstat -anptu | grep "9000"    //验证php-fpm是否启动

####################################################################################################

静态页面:不需要服务器进行解释,直接传递给客户端
动态页面:需要服务器进行解释,再传递给客户端。

FastCGI简介:
FastCGI是一种常驻(Long-live)型的CGI
-将GCI解释器进程保持在内存中,进行维护和调度
-FastCGI技术目前支持的与语言 PHP C/C++ JAVA perl python 等

FastCGI工作原理:
1.web server 启动时载入FastCGI进程管理器
2.FastCGI进程管理器初始化,启动多个解释器进程
3.当客户端请求到达为web server FastCGI进程管理器选择并连接到一个解释器。
4,FastCGI子进程完成处理后返回结果,将标准输出和错误信息从同一连接返回web server

FastCGI缺点:
内存消耗大
-因为多进程,所以比CGI多线程消耗更多的服务器内存,PHP—CGI解释器每进程消耗7-25M内存,将这个数字乘50或100就是很大的内存数。

#####################################################################################################

php-fpm需要修改的常见配置如下:
#vim /var/
listen = 127.0.0.1:9000        //PHP端口号
pm.max_children = 32        //最大进程数量
pm.start_servers = 15        //最小进程数量
pm.min_spare_servers = 5        //最少需要几个空闲着的进程
pm.max_spare_servers = 32    //最多允许几个进程处于空闲状态

#######################################################################################################

实现页面的动静分离:

#vim /usr/local/nginx/html/test.php    //书写简单的动态页面
<?php
$i="hello world";
echo $i;
?>

#nginx    //启动nginx服务

客户端访问:
#firefox http://192.168.4.5/test.php
结果:会出现下载页面,因为这个时候服务器并没有解释test.php,所以这属于静态页面。


#vim /usr/local/nginx/conf/nginx.conf

location / {
            root   html;
            index  index.php  index.html   index.htm;
        }
 location  ~  \.php$  {                        //去掉配置文件中的注释,实现访问.php结尾的动态页面。
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name; //将这一句话注释
            include        fastcgi.conf;    //这里将源配置文件改为fastcgi.conf
        }

客户端访问:
#firefox http://192.168.4.5/test.php
结果:hello world 这个时候服务器已经用php解释了test.php,所以属于动态页面。

#####################################################################################################

测试连接maridb数据库:

#cp /root/lnmp_soft/php_scripts/mysql.php /usr/local/nginx/html/    //安装php-fpm会有这个mysql.php的测试页面,这是我的文件存储路径,只需要将mysql.php复制到/usr/local/nginx/html/下来完成测试。

注意:我们要将selinux设置为permissive,这个和selinx的标签值有关。

客户端访问:
#firefox http://192.168.4.5/mysql.php    //关于Host和User的页面


####################################################################################################

地址重写:

站内转换:

#vim /usr/local/nginx/conf/nginx.conf

location / {
            root   html;
            index  index.php  index.html   index.htm;
        rewrite /a\.html /b.html;        //访问a.html转换为b.html,因为nginx支持正则表达式,再正则中.表示任意单个字符,所以使用\将.屏蔽。
        }
 
echo "AA" > /usr/local/nginx/html/a.html
echo "BB" > /usr/local/nginx/html/b.html

客户端访问:
#firefox http://192.168.4.5/a.html
结果:BB

如果需要访问a转到b,并且在我的浏览器导航栏中也显示b.html   rewrite /a\.html /b.html redirect;加上redirect。

######################################################################################################
站外转换:
#vim /usr/local/nginx/conf/nginx.conf
http {
.. ..
    server {
            listen       80;
              server_name  localhost;
          rewrite ^/(.*) http://www.tmooc.cn/$1; //访问到 / 时,跳转到www.tmooc.cn
                                 // (.*)在正则中表示后面的所有内容,并且保留起来
                                //按照正则当中的理解应该为\1,但是nginx为$1.
     location / {                
          root   html;
         index  index.html index.htm;
        #rewrite /a\.html /b.html;
         }
  }
}

客户端访问:
#firefox http://192.168.4.5/xxxx
结果:http://www.tmooc.cn/xxxxx


########################################################################################

不同设备访问,显示内容相同,格式不同:

#vim /usr/local/nginx/conf/nginx.conf
    
    http {
           server {
               listen 80;
               server_name localhost;
               if ($http_user_agent ~* firefox) {         
               rewrite ^/(.*) /firefox/$1;
               }
               //$http_user_agent 日至消息中的一段,其中包含浏览器信息,设备信息等,匹配到firefox后跳转到目录下网页中 ~模糊匹配,*代表不区分大小写。

                //日志位置/usrl/local/nginx/logs/access_log,访问日志。


               #rewrite ^/(.*)  http://www.tmooc.cn/$1;  
               location / {
                   root html;
                   index index.html;
                   #rewrite /a\.html /b\.html;   
               }
           }
       }
        mkdir /usr/local/nginx/html/firefox                //创建目录
        echo "firefox" > /usr/local/nginx/html/firefox/test.html    //书写测试页面
        echo "curl" > /usr/local/nginx/html/test.html            //书写测试页面
        
客户端测试:
#firefox 192.168.4.5/test.html  //使用火狐浏览器
结果:firefox
#curl 192.168.4.5/test.html   //使用curl 浏览器
结果:curl
        
注意:在实际的应用中  两个访问的内容是相同的,格式不同。这里只是为了测试