1. 程式人生 > 資訊 >今年前 7 個月俄羅斯國內共售出中國品牌汽車 56573 輛,同比增長 140%

今年前 7 個月俄羅斯國內共售出中國品牌汽車 56573 輛,同比增長 140%

目錄

LNMP架構

1.簡介

1.LNMP是一套技術的組合,L=Linux、N=Nginx、M~=MySQL、P~=PHP
2.不僅僅只有這些服務,還有很多
3.redis\elasticsearch\kibana\logstash\zabbix\git\jenkins\kafka\hbase\hadoop\spark\flink

2.LNMP架構工作方式

首先Nginx服務是不能處理動態請求,那麼當用戶發起動態請求時, Nginx又是如何進行處理的。
    1.靜態請求:請求的內容是靜態檔案就是靜態請求
        1)靜態檔案:檔案上傳到伺服器,永遠不會改變的檔案就是靜態檔案
        2)html就是一個標準的靜態檔案
    2.動態請求:請求的內容是動態的就是動態請求
        1)不是真實存在伺服器上的內容,是通過資料庫或者其他服務拼湊成的資料
 
當用戶發起http請求,請求會被Nginx處理,如果是靜態資源請求Nginx則直接返回,如果是動態請求Nginx則通過fastcgi協議轉交給後端的PHP程式處理,具體如下圖所示

3.訪問流程

1.瀏覽器輸入域名,瀏覽器會拿著域名取DNS伺服器解析
2.DNS伺服器會將域名解析成IP
3.瀏覽器會去與IP對應伺服器建立TCP\IP連線
4.連線建立完成,會向伺服器發起請求,請求nginx
5.nginx會判斷請求是動態的還是靜態的
    #靜態請求
    location \.jpg$ {
        root /code;
    }
    #動態請求
    location \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        ... ...
    }
6.如果是靜態請求,nginx去code目錄獲取,直接返回
7.如果是動態請求,nginx會通過fastcgi協議連線PHP服務的php-fpm管理程序
8.php-fpm管理程序會下發工作給 wrapper工作程序
9.wrapper工作程序判斷是不是簡單的php內容
10.如果只是php內容則使用php解析器解析後直接返回
11.如果還需要讀取資料庫,wrapper工作程序會去資料庫讀取資料,再返回資料
12.資料流轉過程:
    1)請求:瀏覽器 > 負載均衡 > nginx > php-fpm > wrapper > mysql
    2)響應:mysql > wrapper > php-fpm > nginx > 負載均衡 > 瀏覽器

LNMP架構搭建

1.搭建nginx

1)配置官方源

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2)安裝nginx

[root@web01 ~]# yum install -y nginx

3)配置nginx

[root@web01 ~]# vim /etc/nginx/nginx.conf 
user  www;

4)建立使用者

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

5)啟動nginx

[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
 
#驗證啟動
[root@web01 ~]# ps -ef | grep nginx
root       9953      1  0 11:17 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        9954   9953  0 11:17 ?        00:00:00 nginx: worker process

2.安裝PHP

1)安裝方式一

#安裝yum源
# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
 
 # 清除之前部署的PHP
[root@nginx ~]# yum remove php-mysql-5.4 php php-fpm php-common
 
#配置第三方源
[root@nginx ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
#安裝PHP
[root@nginx ~]# 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

#驗證PHP
	[root@nginx ~]# php -v

2)安裝方式二

1.上傳包
[root@web01 ~]# rz
[root@web01 ~]# ll
-rw-r--r--. 1 root root 19889622 Nov 22 15:52 p-hp.tar.gz
 
2.解壓包
[root@web01 ~]# tar xf php.tar.gz
 
3.本地安裝php的rpm包
[root@web01 ~]# yum localinstall -y *.rpm

3)配置php

[root@web01 ~]# vim /etc/php-fpm.d/www.conf 
user = www
group = www

4)啟動服務

[root@web01 ~]# systemctl start 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 ~]# ps -ef | grep php-fpm
root      10195      1  0 11:29 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
www       10196  10195  0 11:29 ?        00:00:00 php-fpm: pool www
www       10197  10195  0 11:29 ?        00:00:00 php-fpm: pool www
www       10198  10195  0 11:29 ?        00:00:00 php-fpm: pool www
www       10199  10195  0 11:29 ?        00:00:00 php-fpm: pool www
www       10200  10195  0 11:29 ?        00:00:00 php-fpm: pool www

搭建交作業頁面

1)配置nginx

[root@web01 ~]# vim /etc/nginx/conf.d/default.conf 
server {
    listen 80;
    server_name linux.zuoye.com;
 
    location / {
        root /code/zuoye;
        index index.html;
    }
}

2)建立站點目錄

[root@web01 ~]# mkdir /code/zuoye -p

3)上傳程式碼

[root@web01 ~]# cd /code/zuoye/
[root@web01 zuoye]# rz
[root@web01 zuoye]# ll
-rw-r--r--. 1 root root 26995 Nov 22 16:47 kaoshi.zip
[root@web01 zuoye]# unzip kaoshi.zip      
[root@web01 zuoye]# ll
-rw-r--r--. 1 root root 38772 Apr 27  2018 bg.jpg
-rw-r--r--. 1 root root  2633 May  4  2018 index.html
-rw-r--r--. 1 root root    52 May 10  2018 info.php
-rw-r--r--. 1 root root  1192 Jan 10  2020 upload_file.php

4)修改程式碼中上傳作業位置

[root@web01 ~]# vim /code/zuoye/upload_file.php
$wen="/code/zuoye/upload";

5)授權

[root@web01 zuoye]# chown -R www.www /code/

6)重啟服務

[root@web01 ~]# systemctl restart nginx

7)配置hosts訪問測試

#配置hosts
10.0.0.7 linux.zuoye.com
 
#訪問
http://linux.zuoye.com/
 
#測試
上傳程式碼出錯,報錯405,因為nginx沒辦法處理php程式碼程式

關聯nginx與php

1)關聯語法

#fastcgi_pass,nginx連線php的代理協議
Syntax: fastcgi_pass address;
Default:    —
Context:    location, if in location
 
#指定請求的檔案
Syntax: fastcgi_param parameter value [if_not_empty];
Default:    —
Context:    http, server, location
 
#指定預設的php頁面
Syntax: fastcgi_index name;
Default:    —
Context:    http, server, location

2)配置

[root@web01 ~]# vim /etc/nginx/conf.d/default.conf 
server {
    listen 80;
    server_name linux.zuoye.com;
 
    location / {
        root /code/zuoye;
        index index.html;
    }
 
    location ~* \.php$ {
        fastcgi_pass localhost:9000;
        fastcgi_param SCRIPT_FILENAME /code/zuoye/$fastcgi_script_name;
        include fastcgi_params;
    }
}

3)訪問頁面測試

1.訪問頁面
http://linux.zuoye.com/
 
2.上傳圖片檔案
成功
 
3.上傳一個大檔案
失敗,413報錯,說檔案過大
#解決:修改配置檔案上傳檔案大小配置
    [root@web01 ~]# vim /etc/nginx/nginx.conf
    http {
        ... ...
        client_max_body_size 200m;
        ... ...
    }
    [root@web01 ~]# systemctl restart nginx
 
    [root@web01 ~]# vim /etc/php.ini
    upload_max_filesize = 200M
    post_max_size = 200M
    [root@web01 ~]# systemctl restart php-fpm
 
4.重新上傳檔案測試
成功

5.搭建mariadb

1)安裝

[root@web01 ~]# yum install -y mariadb-server

2)啟動服務

[root@web01 ~]# systemctl start 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.

3)驗證啟動

[root@web01 ~]# ps -ef | grep mariadb
mysql     11006  10841  1 12:06 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock

4)連線

[root@web01 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-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)]> show databases;            #檢視資料庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

5)設定資料庫密碼

[root@web01 ~]# mysqladmin -uroot password '123'
 
#使用密碼連線資料庫
[root@web01 ~]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.68-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)]>

測試PHP和mariadb關聯

1)編寫php測試連線資料庫的程式碼

[root@web01 ~]# vim /code/zuoye/test.php
<?php
    $servername = "172.16.1.51";
    $username = "root";
    $password = "123456";
 
    // 建立連線
    $conn = mysqli_connect($servername, $username, $password);
 
    // 檢測連線
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "小哥哥,php可以連線MySQL...";
?>
 
<img style='width:100%;height:100%;' src=https://blog.drig>

2)訪問測試

http://linux.zuoye.com/test.php

搭建wordpress部落格

1.上傳程式碼

[root@web01 code]# rz
[root@web01 code]# ll
-rw-r--r--. 1 root root 11098483 Sep 12 17:52 wordpress-5.0.3-zh_CN.tar.gz

2.解壓程式碼

[root@web01 code]# tar xf wordpress-5.0.3-zh_CN.tar.gz

3.授權

[root@web01 code]# chown -R www.www wordpress

4.配置nginx

[root@web01 code]# vim /etc/nginx/conf.d/linux.wp.com.conf
server {
    listen 80;
    server_name linux.wp.com;
 
    location / {
        root /code/wordpress;
        index index.php; 
    }
 
    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
        include fastcgi_params;
    }
}

5.重啟訪問

#檢查配置
[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

6.訪問測試

#配置hosts
10.0.0.7 linux.wp.com
 
#訪問
http://linux.wp.com/

7.建立資料庫

[root@web01 code]# mysql -uroot -p123
 
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)
 
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

8.根據頁面提示操作