day-32 nginx搭建流行架構(gòu)

1、lnmp架構(gòu)概述

lnmp是一套技術(shù)組合,L =Linux 、N =nginx 、M~ = MySQL 、P~ = PHP

2、nginx 與 fastcgi工作流程

用戶通過http協(xié)議發(fā)送請(qǐng)求,請(qǐng)求會(huì)先抵達(dá)lnmp架構(gòu)中的nginx

nginx會(huì)根據(jù)用戶的請(qǐng)求進(jìn)行判斷有沒有l(wèi)ocation,有,則判斷是靜態(tài)頁面還是動(dòng)態(tài)頁面

nginx會(huì)根據(jù)用戶的請(qǐng)求進(jìn)行判斷有沒有l(wèi)ocation,有,則判斷是靜態(tài)頁面還是動(dòng)態(tài)頁面

如果是靜態(tài),nginx直接返回,如果是動(dòng)態(tài),nginx會(huì)將請(qǐng)求發(fā)給fastcgi協(xié)議

然后fastcgi會(huì)將請(qǐng)求交給php-fpm管理進(jìn)程,php-fpm管理進(jìn)程會(huì)調(diào)用工作進(jìn)程warrap

然后warrap工作進(jìn)程會(huì)調(diào)用PHP

序進(jìn)行解析,解析代碼PHP直接返回,查詢數(shù)據(jù)庫,PHP連接數(shù)據(jù)庫的用戶和密碼進(jìn)行查詢

最終數(shù)據(jù)由 mysql數(shù)據(jù)庫--->php程序---->warrap工作進(jìn)程---->php-fpm管理進(jìn)程----->fastcgi協(xié)議---->nginx--->user用戶

操作

1、架構(gòu)環(huán)境部署

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

#2、安裝擴(kuò)展源
[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、啟動(dòng)
[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、安裝數(shù)據(jù)庫
[root@web01 ~]# yum install mariadb-server mariadb

#6、啟動(dòng)數(shù)據(jù)庫
[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代碼,測(cè)試訪問效果
[root@web01 conf.d]# cat /code/info.php
<?php

    phpinfo();

?>
#3、檢測(cè)、重啟
[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.啟動(dòng)數(shù)據(jù)庫
[root@web01 ~]# systemctl start mariadb
    
#2.配置連接密碼
[root@web01 ~]# mysqladmin password yangyang.com
    
#3.測(cè)試登錄mysql
    [root@web01 ~]# mysql -uroot -pyangyang.com
    MariaDB [(none)]>
    
#4.編寫php連接數(shù)據(jù)庫的代碼
    [root@web01 ~]# cat code/mysqli.php
    <?php
        $servername = "localhost";
        $username = "root";
        $password = "yangyang.com";

         // 創(chuàng)建連接
        $conn = mysqli_connect($servername, $username, $password);

        // 檢測(cè)連接
          if (!$conn) {
             die("Connection failed: " . mysqli_connect_error());
        }
        echo "php連接MySQL數(shù)據(jù)庫成功";
    ?>
    
  #5、測(cè)試PHP命令
  [root@web01 ~]# php /code/mysqli.php
  
  #6、通過瀏覽器訪問(下面的圖)
圖片.png

4、通過LNMP架構(gòu)部署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的配置文件(定義域名以及站點(diǎn)的目錄位置)
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、根據(jù)Nginx配置,初始化環(huán)境,然后上傳代碼

    #1.準(zhǔn)備站點(diǎn)目錄
    [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.創(chuàng)建數(shù)據(jù)庫名
    [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.統(tǒng)一Nginx  PHP的權(quán)限  為  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架構(gòu)部署 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、登錄數(shù)據(jù)庫,創(chuàng)建庫名稱
[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服務(wù)
[root@web01 conf.d]# systemctl restart nginx

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

6、通過LNMP架構(gòu)部署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、登錄數(shù)據(jù)庫,創(chuàng)建庫名稱
[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服務(wù)
[root@web01 code]# systemctl restart nginx
圖片.png

7、搭建edusoho視頻網(wǎng)站

#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;
    }

    # 配置設(shè)置圖片格式文件
    location ~* \.(jpg|jpeg|gif|png|ico|swf)$ {
        # 過期時(shí)間為3年
        expires 3y;

        # 關(guān)閉日志記錄
        access_log off;

        # 關(guān)閉gzip壓縮,減少CPU消耗,因?yàn)閳D片的壓縮率不高。
        gzip off;
    }

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

    # 禁止用戶上傳目錄下所有.php文件的訪問,提高安全性
    location ~ ^/files/.*\.(php|php5)$ {
        deny all;
    }

    # 以下配置允許運(yùn)行.php的程序,方便于其他第三方系統(tǒng)的集成。
    location ~ \.php$ {
        # [改] 請(qǐng)根據(jù)實(shí)際php-fpm運(yùn)行的方式修改
        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、測(cè)試,重啟nginx服務(wù)
[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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、LNMP動(dòng)態(tài)網(wǎng)站架構(gòu) 一、動(dòng)態(tài)網(wǎng)站架構(gòu): index.php 開源的php Windows/Linux+n...
    Bo蘿吹雪閱讀 577評(píng)論 0 3
  • 這篇是Nginx安裝配置PHP(FastCGI)環(huán)境的教程。Nginx不支持對(duì)外部程序的直接調(diào)用或者解析,所有的外...
    SkTj閱讀 3,213評(píng)論 2 20
  • Nginx簡介 解決基于進(jìn)程模型產(chǎn)生的C10K問題,請(qǐng)求時(shí)即使無狀態(tài)連接如web服務(wù)都無法達(dá)到并發(fā)響應(yīng)量級(jí)一萬的現(xiàn)...
    魏鎮(zhèn)坪閱讀 2,213評(píng)論 0 9
  • Nginx有什么作用呢? Ngnix作為一款高性能的HTTP服務(wù)器、反向代理服務(wù)器、電子郵件代理服務(wù)器,主要有三方...
    JunChow520閱讀 3,226評(píng)論 1 14
  • 第一節(jié):LNMP架構(gòu)概述 1.1 什么是LNMP LNMP 是一套技術(shù)的組合, L=Linux、 N=Nginx、...
    chenkang閱讀 352評(píng)論 0 1

友情鏈接更多精彩內(nèi)容