day-32 nginx搭建流行架构

1、lnmp架构概述

lnmp是一套技术组合,L =Linux 、N =nginx 、M~ = MySQL 、P~ = PHP

2、nginx 与 fastcgi工作流程

用户通过http协议发送请求,请求会先抵达lnmp架构中的nginx

nginx会根据用户的请求进行判断有没有location,有,则判断是静态页面还是动态页面

nginx会根据用户的请求进行判断有没有location,有,则判断是静态页面还是动态页面

如果是静态,nginx直接返回,如果是动态,nginx会将请求发给fastcgi协议

然后fastcgi会将请求交给php-fpm管理进程,php-fpm管理进程会调用工作进程warrap

然后warrap工作进程会调用PHP

序进行解析,解析代码PHP直接返回,查询数据库,PHP连接数据库的用户和密码进行查询

最终数据由 mysql数据库--->php程序---->warrap工作进程---->php-fpm管理进程----->fastcgi协议---->nginx--->user用户

操作

1、架构环境部署

#1、卸载旧版本
[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common -y

#2、安装扩展源
[root@web01 ~]# yum  localinstall -y  http://mirror.webtatic.com/yum/el7/webtatic-release.rpm

#3、安装新版本
[root@web01 ~]# yum -y install 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
 
 #4、启动
[root@web01 ~]# systemctl restart php-fpm
[root@web01 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

 #5、安装数据库
[root@web01 ~]# yum install mariadb-server mariadb

#6、启动数据库
[root@web01 ~]# systemctl restart mariadb
[root@web01 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

2、、nginx与PHP集成的原理

#1、编写能解析PHP的nginx配置文件
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim php.yangyang.com.conf
[root@web01 conf.d]# cat php.yangyang.com.conf 
server{
    listen 80;
    server_name php.yangyang.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

#3、检测、重启
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntaxis ok
nginx: configuration file /etc/nginx/nginx.conf test is sucessful
[root@web01 conf.d]# systemctl restart nginx

#3、host劫持
10.0.0.7    php.yangyang.com
图片.png

3、PHP与MySQL集成的原理。

#1.启动数据库
[root@web01 ~]# systemctl start mariadb
    
#2.配置连接密码
[root@web01 ~]# mysqladmin password yangyang.com
    
#3.测试登录mysql
    [root@web01 ~]# mysql -uroot -pyangyang.com
    MariaDB [(none)]>
    
#4.编写php连接数据库的代码
    [root@web01 ~]# cat code/mysqli.php
    
    
  #5、测试PHP命令
  [root@web01 ~]# php /code/mysqli.php
  
  #6、通过浏览器访问(下面的图)
图片.png

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

#1、配置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

#2、集成PHP的配置文件(定义域名以及站点的目录位置)
root@web01 conf.d]# cat blog.yangyang.com.conf 
server {
    listen 80;
    server_name blog.yangyang.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;
    }
}


#3、根据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 -pyangyang.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
图片.png

5、通过LNMP架构部署 Wecenter

#1、编写nginx的配置文件
[root@web01 ~ ]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# cat zh.yangyang.com.conf
server{
    listen 80;
    server_name zh.yangyang.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 code]# rz WeCenter_3-3-2.zip
[root@web01 code]# ll
WeCenter_3-3-2.zip
[root@web01 code]# unzip WeCenter_3-3-2.zip
[root@web01 code]# mkdir zh
[root@web01 code]# chown -R www.www /code
 
#3、登录数据库,创建库名称
[root@web01 conf.d]# mysql -uroot -pyangyang.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 72
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#MariaDB [(none)]> create database zh;
Query OK, 1 row affected (0.00 sec)

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

MariaDB [(none)]> BYE

#3、重启nginx服务
[root@web01 conf.d]# systemctl restart nginx

#4、配置host劫持
10.0.0.7   zh.yangyang.com
图片.png

6、通过LNMP架构部署kodcloud

#1、匹配nginx配置文件
[root@web01 ~ ]# cd /etc/nginx/conf/
[root@web01 conf]# cat /etc/nginx/conf.d/kodcloud.yangdan.com.conf 
server{
    listen 80;
    server_name kodcloud.yangdan.com;
    root /code/kodcloud;

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]# /code
[root@web01 code]# mkdir /kodcloud
[root@web01 code]# wget http://static.kodcloud.com/update/download/kodexplorer4.40.zip
[root@web01 code]# unzip kodexplorer4.40.zip -d /code/kodcloud
[root@web01 code]# chown -R www.www /code/

#3、登录数据库,创建库名称
[root@web01 code]# mysql -uroot -pyangyang.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 210
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#MariaDB [(none)]> create database kodcloud;
Query OK, 1 row affected (0.00 sec)

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

#MariaDB [(none)]> exit
Bye

#4、重启nginx服务
[root@web01 code]# systemctl restart nginx
图片.png

7、搭建edusoho视频网站

#1、配置
[root@web01 conf]# cd /etc/ngimx/conf/
[root@web01 conf]# vim edusoho.yangdan.com.conf 
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;
    }
}

#2、上传代码,修改属组与属主
[root@web01 conf]# mkdir /code/edusoho/web/
[root@web01 conf]# cd /code
[root@web01 code]# rz edusoho-8.3.43.zip
[root@web01 code]# unzip edusoho-8.3.43.zip -d /code/edusoho/web/
[root@web01 code]# chown -R www.www /code
[root@web01 code]# mysql -uroot -pyangyang.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 439
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#MariaDB [(none)]> create database edusoho;
Query OK, 1 row affected (0.00 sec)

#MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| edusoho            |
| kodcloud           |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
| zh                 |
+--------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> Bye

#3、测试,重启nginx服务
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 code]# systemctl restart nginx

#4、劫持
10.0.0.7   edusoho.yangdan.com

你可能感兴趣的:(day-32 nginx搭建流行架构)