day 40 Nginx流行架构

作者: 静如止水yw | 来源:发表于2019-09-26 16:17 被阅读0次

LNMP架构基本介绍
环境搭建


一、LNMP架构基本介绍

  • 什么是LNMP
    LNMP是一套技术的组合,L=Linux,N=Nginx,M=MySQL,P=PHP
  • LNMP架构如何工作
    当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过fastcgi协议转交给后端的PHP程序处理,如图所示:


    工作原理

二、环境搭建

① 安装

[root@web01 ~]# cat /etc/yum.repos.d/php.repo 
[webtatic-php]
name = php Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

yum install nginx -y

yum remove php-mysql-5.4 php php-fpm php-common

yum -y install nginx php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

yum install mariadb mariadb-server -y

② 启动

systemctl start nginx
systemctl start php-fpm

③ nginx与PHP集成原理

1.编写能解析PHP的Nginx配置文件
[root@web01 conf.d]# cat php.oldxu.com.conf 
server {
    listen 80;
    server_name php.oldxu.com;
    root /code;

    location / {
        index index.php;
    } 

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}


2.编写PHP代码,测试访问效果.
    [root@web01 conf.d]# cat /code/info.php
    <?php
        phpinfo();
    ?>

3.host劫持

④ PHP与MySQL集成的原理。

1.启动数据库
    [root@web01 ~]# systemctl start mariadb
    
    2.配置连接密码
    [root@web01 ~]# mysqladmin password oldxu.com
    
    3.测试登录mysql
    [root@web01 ~]# mysql -uroot -poldxu.com
    MariaDB [(none)]>
    
    4.编写php连接数据库的代码
    [root@web01 ~]# /code/mysqli.php
    <?php
        $servername = "localhost";
        $username = "root";
        $password = "oldxu.com";

        // 创建连接
        $conn = mysqli_connect($servername, $username, $password);

        // 检测连接
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        echo "php连接MySQL数据库成功";
    ?>
    
    
    5.可以直接使用php命令测试
        [root@web01 ~]# php /code/mysqli.php

    6.也可以通过浏览器的方式去测试

⑤ 通过LNMP架构部署Wordpress、Wecenter、edusoho、phpmyadmin、ecshop

1.编写Nginx集成PHP的配置文件  (定义域名以及站点的目录位置)
[root@web01 conf.d]# cat blog.oldxu.com.conf 
server {
    listen 80;
    server_name blog.oldxu.com;
    root /code/wordpress;

    location / {
        index index.php;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}


2.根据Nginx配置,初始化环境,然后上传代码
    1.准备站点目录
    [root@web01 conf.d]# mkdir /code

    2.下载wordpress代码
    [root@web01 conf.d]# cd /code
    [root@web01 code]# tar xf wordpress-5.2.3-zh_CN.tar.gz

    3.创建数据库名
    [root@web01 code]# mysql -uroot -poldxu.com
    
    MariaDB [(none)]> create database wordpress;
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    | wordpress          |
    +--------------------+
    5 rows in set (0.01 sec)


    4.统一Nginx  PHP的权限  为  www
    [root@web01 code]# groupadd www -g 666
    [root@web01 code]# useradd -u666 -g666 www
    
    [root@web01 code]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf
    [root@web01 code]# chown -R www.www /code
    [root@web01 code]# systemctl restart nginx
    
    [root@web01 code]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf 
    [root@web01 code]#  sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
    [root@web01 code]# systemctl restart php-fpm

wecenter

1.编写Nginx的配置文件       虚拟主机
[root@web01 conf.d]# cat zh.oldxu.com.conf 
server {
    listen  80;
    server_name zh.oldxu.com;
    root /code/zh;

    client_max_body_size 100m;

    location / {
        index index.php;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

2.上传代码,变更代码的属主和属组
[root@web01 conf.d]# cd /code
[root@web01 conf.d]# rz WeCenter_3-3-2.zip
[root@web01 conf.d]# mkdir zh
[root@web01 conf.d]# unzip WeCenter_3-3-2.zip  -d /code/zh/
[root@web01 code]# chown -R www.www /code


3.登录数据库.创建库名称
[root@web01 code]# mysql -uroot -poldxu.com

MariaDB [(none)]> create database zh;
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
| zh                 |
+--------------------+
6 rows in set (0.00 sec)


3.重启Nginx服务
[root@web01 code]# systemctl restart nginx

4.配置host劫持

edusoho

server {
    listen 80;
    server_name www.xxxx.com;
    root /code/edusoho/web;

    # 日志路径
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    location / {
        index app.php;
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    location ~ ^/udisk {
        internal;
        root /code/edusoho/app/data/;
    }

    location ~ ^/(app|app_dev)\.php(/|$) {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS              off;
        fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect;
        fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edusoho/app/data/udisk;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 8 128k;
    }

    # 配置设置图片格式文件
    location ~* \.(jpg|jpeg|gif|png|ico|swf)$ {
        # 过期时间为3年
        expires 3y;

        # 关闭日志记录
        access_log off;

        # 关闭gzip压缩,减少CPU消耗,因为图片的压缩率不高。
        gzip off;
    }

    # 配置css/js文件
    location ~* \.(css|js)$ {
        access_log off;
        expires 3y;
    }

    # 禁止用户上传目录下所有.php文件的访问,提高安全性
    location ~ ^/files/.*\.(php|php5)$ {
        deny all;
    }

    # 以下配置允许运行.php的程序,方便于其他第三方系统的集成。
    location ~ \.php$ {
        # [改] 请根据实际php-fpm运行的方式修改
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS              off;
    }
}

相关文章

  • day 40 Nginx流行架构

    LNMP架构基本介绍环境搭建 一、LNMP架构基本介绍 什么是LNMPLNMP是一套技术的组合,L=Linux,N...

  • day40-流行架构

    点击查看:https://www.cnblogs.com/dlq-52/p/11543533.html

  • 4.Nginx流行架构--LNMP

    第四十天 Nginx流行架构--LNMP 1.什么是LNMP LNMP是一套技术的组合,是由:L=Linux、N...

  • Nginx

    Nginx的编译安装(Linux) Nginx部署 Nginx的架构 nginx进程管理:信号

  • day 48 tomcat+nginx+redis

    Nginx+Tomcat集群架构概述tomcat+Nginx集群架构实战与共享tomcat+Nginx集群会话 一...

  • [服务器]nginx -- 1

    初探nginx架构### 淘宝团队的nginx教材nginx版本1.12 nginx与外界,nginx的maste...

  • NGINX学习笔记(一)----NGINX的架构简述

    架构概览 了解Nginx的架构,能更加清楚的了解Nginx的组成部分,已经了解Nginx是怎么进行工作的,学习的时...

  • Nginx学习2 - Nginx的架构简述

    1.架构概览 了解Nginx的架构,能够帮助我们更加清楚的了解Nginx的组成部分,以及了解Nginx是怎么进行工...

  • Tomcat集群架构介绍

    1.Tomcat集群架构介绍 2.Nginx+Tomcat集群架构实战 3.Nginx+Tomcat集群会话共享 ...

  • Tomcat集群架构介绍

    1.Tomcat集群架构介绍 2.Nginx+Tomcat集群架构实战 3.Nginx+Tomcat集群会话共享 ...

网友评论

    本文标题:day 40 Nginx流行架构

    本文链接:https://www.haomeiwen.com/subject/ajokuctx.html