运维之高级服务篇------ 3.部署LNMP 、 Nginx+FastCGI 、 Nginx高级技术

来源:互联网 发布:java 调用maven打包 编辑:程序博客网 时间:2024/06/05 14:32

THREE DAY

案例1:部署LNMP环境

案例2:构建LNMP平台 

案例3:地址重写 

LNMPlinux nginx(接受用户请求) mariadb P:php(执行代码) python perl)

软件:

nginx 

mariadb(管理数据) mariadb-devel(依赖包) mariadb-server(存数据)

php(用户看数据) php-fpm  php-mysql(连接数据库

一.安装软件

yum -y install gcc openssl-devel pcre-devel zlib-devel

useradd –s /sbin/nologin  nginx

cd nginx-1.8.0

./configure  --prefix=/usr/local/nginx  --user=nginx   --group=nginx  --with-http_ssl_module

make && make install

yum –y install   mariadb   mariadb-server   mariadb-devel

yum –y  install  php   php-mysql

tar  –xf  lnmp_soft-2017-03-28.tar.gz

cd  lnmp_soft

yum –y  localinstall php-fpm-5.4.16-36.el7_1.x86_64.rpm

.启动服务

systemctl stop httpd

systemctl disable httpd                //关闭httpd的开机启动功能

/usr/local/nginx/sbin/nginx             //启动Nginx服务

 netstat -utnlp | grep :80

systemctl start mariadb

systemctl status mariadb

systemctl enable mariadb

systemctl start php-fpm

systemctl status php-fpm

systemctl enable php-fpm

三.构建LNMP平台

配置Fast-CGI支持PHP网页 

创建PHP测试页面,测试使用PHP连接数据库的效果 

Nginx结合FastCGI技术即可支持PHP页面架构,因此本案例,需要延续练习一的实验内容,通过修改Nginxphp-fpm配置文件实现对PHP页面的支持。

php-fpm需要修改的常见配置如下:

vim /etc/php-fpm.d/www.conf

listen = 127.0.0.1:9000

listen.allowed_clients = 127.0.0.1

user = apache

group = apache

pm = dynamic

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 //最多允许几个进程处于空闲状态

systemctl restart php-fpm

systemctl status php-fpm

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

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

upstream c{

    server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10;

    server 192.168.2.200 weight=1 max_fails=2 fail_timeout=10;

    }

    server {

        listen       80;

        server_name  www.c.com;

        location / {

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  a.php;动态网站首页

           include        fastcgi.conf;加载另一部分配置文件

        }

    }

    server {

         listen       80;

         server_name  www.a.com;  

         auth_basic "your password:";

     auth_basic_user_file "/usr/local/nginx/apass";

     ssl_certificate      a.crt;

         ssl_certificate_key  a.key;

     ssl_session_cache    shared:SSL:1m;

        ssl_session_timeout  5m;

location / {

            root   ahtml;

            index  index.html index.htm;

        }

    }

    server {

        listen       80;

        server_name  www.b.com;

     rewrite ^/(.*) http://www.tmooc.cn/$1;          #网站跳转(用于新旧域名连接)

       rewrite ^/  http://www.tmooc.cn/;               #网站跳转(访问旧站页面,重定向至新站下相同的页面

        ssl_certificate      b.crt;

        ssl_certificate_key  b.key;

        ssl_session_cache    shared:SSL:1m;

        ssl_session_timeout  5m;

        location / {

            root   bhtml;

            index  index.html index.htm;

            rewrite /b1.html /b2.html redirect;}      #网页跳转

            if ($http_user_agent ~* url) {                          //识别客户端curl浏览器

              rewrite ^(.*)$ /curl/$1 break;

              }

 

    }

}

vim /usr/local/nginx/html/test1.php  #测试普通页面

 <?php

 $i="This is a test Page";

 echo $i;

 ?>

 vim /usr/local/nginx/html/test2.php    #测试php页面,连接MariaDB数据库

 

 <?php

$links=mysql_connect("localhost","root","密码");        

 //注意:rootmysql账户名称,密码需要修改为实际mysql密码,无密码则留空即可

  if($links){

          echo "link db ok!!!";

  }

  else{

         echo "link db no!!!";

 }

 ?>

vim /usr/local/nginx/html/test3.php         #创建PHP测试页面,连接并查询MariaDB数据库

 

<?php

$mysqli = new mysqli('localhost','root','','mysql');

if (mysqli_connect_errno()){

      die('Unable to connect!'). mysqli_connect_error();

  }

  $sql = "select * from user";

  $result = $mysqli->query($sql);

  while($row = $result->fetch_array()){

      printf("Host:%s",$row[0]);

      printf("</br>");

      printf("Name:%s",$row[1]);

      printf("</br>");

  }

  ?>

echo "I am Normal page" > /usr/local/nginx/html/test.html

mkdir  -p  /usr/local/nginx/html/curl/

echo "I am is curl page" > /usr/local/nginx/bhtml/curl/test.html

cp /usr/share/backgrounds/gnome/Road.jpg > /usr/local/nginx/curl/test.jpg

/usr/local/nginx/sbin/nginx  -s  reload

firefox http://www.c.com/test1.php   #测试php

firefox http://www.c.com/test2.php   #测试php,连接mariadb库

firefox http://www.c.com/test3.php   #测试php,连接并查询mairadb库

firefox  http://www.b.com/b1.html   #测试跳转页面(地址重写)

firefox  http://www.b.com/ad

 

firefox  http://192.168.4.5/test.html   #针对于修改配置文件(访问192.168.4.5/下面子页面,重定向至www.tmooc.cn/下相同的页面的测试)

针对修改配置文件(实现curl和火狐访问相同连接返回的页面不同的测试:

curl     http://192.168.4.5/test.html   

curl   http://192.168.4.5/test.jsp 

原创粉丝点击