第十一章 LNMP架構基礎介紹

1、LNMP架構

1.簡介

oLNMP是一套技術的組合,L=Linux、N=Nginx、M~=MySQL、P~=PHP
不單單包含這些,還有redis/ELK/zabbix/git/jenkins/kafka

2.LNMP工做方式

首先Nginx服務是不能處理動態請求,那麼當用戶發起動態請求時, Nginx又是如何進行處理的。
1.靜態請求:請求靜態文件的請求
靜態文件:
1)上傳時什麼樣子,查看時就是什麼樣子
2)html的頁面都是靜態的
2.動態請求:請求動態內容,帶參數的請求
1)服務器上並非真實存在的,須要都數據庫等服務上去獲取數據,組成的頁面

當用戶發起http請求,請求會被Nginx處理,若是是靜態資源請求Nginx則直接返回,若是是動態請求Nginx則經過fastcgi協議轉交給後端的PHP程序處理

3.LNMP訪問流程

1.瀏覽器輸入域名,瀏覽器拿着域名去本地hosts文件解析,而後再去DNS服務器解析
2.本地hosts文件或者DNS服務器解析域名爲IP
3.瀏覽器去請求該IP對應的web服務器
4.瀏覽器請求nginx
5.nginx判斷請求是動態請求仍是靜態請求
#靜態請求
location / {
  root /code;
  index index.html;
}
location ~* \.(jpg|png|mp4)$ {
      root /code/pic;
}
#動態請求
location ~* \.php$ {
  fastcgi_pass 127.0.0.1:9000;
  ... ...
}
6.若是是靜態請求,nginx直接返回內容
7.若是是動態內容,nginx會經過fastcgi協議找php-fpm管理進程
8.php-fpm管理進程會去下發工做給wrapper工做進程
9.wrapper工做進程判斷是否是php文件
10.若是隻是php文件,能夠直接解析而後返回結果
11.若是還須要讀取數據庫,wrapper進程會去讀取數據庫數據,而後返回數據
12.數據流轉:
1)請求:瀏覽器-->負載均衡-->nginx-->php-fpm-->wrapper-->mysql
2)響應:mysql-->wrappe-->php-fpm-->nginx-->負載均衡-->瀏覽器

2、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

5.啓動服務
[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.

6.驗證服務
[root@web01 ~]# ps -ef | grep nginx

2.安裝PHP

1.配置第三方源
[root@web01 ~]# 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

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

3.安裝PHP
[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 ~]# mkdir /package
[root@web01 ~]# cd /package/
#上傳包
[root@web01 /package]# rz php.tar.gz
#解壓包
[root@web01 /package]# tar xf php.tar.gz
#安裝全部rpm包
[root@web01 /package]# yum localinstall -y *.rpm

5.配置PHP
[root@web01 /package]# vim /etc/php-fpm.d/www.conf
user = www
group = www

6.啓動服務
[root@web01 /package]# systemctl start php-fpm
[root@web01 /package]# systemctl enable php-fpm

7.驗證啓動
[root@web01 /package]# ps -ef | grep php

[root@web01 /package]# netstat -lntp
tcp       0      0 127.0.0.1:9000         0.0.0.0:*       LISTEN      7748/php-fpm: master

3.搭建交做業平臺

1.配置nginx
[root@web01 /package]# vim /etc/nginx/conf.d/default.conf
server {
  listen 80;
  server_name www.zuoye.com;

  location / {
      root /code/zuoye;
      index index.html;
  }
}

2建立站點目錄
[root@web01 /package]# mkdir /code/zuoye -p

3.上傳代碼
[root@web01 /package]# cd /code/zuoye/
[root@web01 /code/zuoye]# rz kaoshi.zip
[root@web01 /code/zuoye]# yum install -y unzip
[root@web01 /code/zuoye]# unzip kaoshi.zip

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

4.修改交做業代碼
[root@web01 /code/zuoye]# vim upload_file.php
$wen="/code/zuoye/upload";

5.訪問測試
[root@web01 /code/zuoye]# systemctl restart nginx

#配置本地hosts文件
10.0.0.7 www.zuoye.com

#訪問www.zuoye.com

6.問題
#報錯405,緣由是nginx做爲web服務器沒有辦法處理post請求,咱們要用php的代碼,須要關聯nginx和php

4.關聯nginx和PHP

1.關聯語法
#fastcgi_pass,進行鏈接PHP
Syntax: fastcgi_pass address;
Default:
Context: location, if in location

#默認php頁面
Syntax: fastcgi_index name;
Default:
Context: http, server, location

#請求的文件
Syntax: fastcgi_param parameter value [if_not_empty];
Default:
Context: http, server, location

2.配置
[root@web01 /code/zuoye]# vim /etc/nginx/conf.d/default.conf
server {
  listen 80;
  server_name www.zuoye.com;

  location / {
      root /code/zuoye;
      index index.html;
  }

  location ~* \.php$ {
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_param SCRIPT_FILENAME /code/zuoye/$fastcgi_script_name;
      include fastcgi_params;
  }
}

3.訪問頁面測試
#訪問http://www.zuoye.com/
1)上傳文件成功

2)413報錯:文件過大,解決方式
#修改nginx上傳文件大小
[root@web01 /code/zuoye]# vim /etc/nginx/nginx.conf
http {
... ...
  client_max_body_size 100M;
  ... ...
}
[root@web01 /code/zuoye]# systemctl restart nginx

#修改php上傳文件大小
[root@web01 /code/zuoye]# vim /etc/php.ini
post_max_size = 100M
upload_max_filesize = 100M
[root@web01 /code/zuoye]# systemctl restart php-fpm

5.搭建mariadb

1安裝
[root@web01 /code/zuoye]# yum install -y mariadb-server

2.啓動服務
[root@web01 /code/zuoye]# systemctl start mariadb
[root@web01 /code/zuoye]# systemctl enable mariadb

3.驗證
[root@web01 /code/zuoye]# netstat -lntp
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      9887/mysqld

4,鏈接
[root@web01 /code/zuoye]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-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 /code/zuoye]# mysqladmin -u root password "123"

#使用密碼鏈接
[root@web01 /code/zuoye]# mysql -u root -p
Enter password:

6.關聯PHP和mariadb

1.編寫PHP測試鏈接數據庫代碼
[root@web01 /code/zuoye]# vim php_mysql.php
<?php
   $servername = "localhost";
   $username = "root";
   $password = "123";

  // 建立鏈接
   $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.driverzeng.com/zenglaoshi/php_mysql.png>

2.訪問
http://www.zuoye.com/php_mysql.php

3、LNMP架構搭建WordPress

1.上傳代碼

[root@web01 /code/zuoye]# cd /code/
[root@web01 /code]# rz wordpress-5.0.3-zh_CN.tar.gz

2.解壓源碼包

[root@web01 /code]# tar xf wordpress-5.0.3-zh_CN.tar.gz 
[root@web01 /code]# ll
total 10844
drwxr-xr-x 5 1006 1006     4096 Jan 11  2019 wordpress

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

3,配置nginx

[root@web01 /code]# vim /etc/nginx/conf.d/linux.blog.com.conf
server {
  listen 80;
  server_name linux.blog.com;

  location / {
      root /code/wordpress;
      index index.php;
  }

  location ~* \.php$ {
      root /code/wordpress;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
  }
}

4.重啓訪問

#重啓nginx
[root@web01 /code]# systemctl restart nginx

#配置本地hosts
10.0.0.7 linux.blog.com

#訪問
http://linux.blog.com

5.建立數據庫

[root@web01 /code]# mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-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 blog;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| blog               |
| mysql             |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

4、LNMP架構搭建知乎

1.上傳代碼

[root@web01 /code]# rz WeCenter_3-2-1.zip

2.解壓源碼包

[root@web01 /code]# unzip WeCenter_3-2-1.zip
[root@web01 /code]# mv WeCenter_3-2-1 zhihu

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

3.配置nginx

[root@web01 /code]# vim /etc/nginx/conf.d/linux.zh.com.conf
server {
  listen 80;
  server_name linux.zh.com;

  location / {
      root /code/zhihu;
      index index.php;
  }

  location ~* \.php$ {
      root /code/zhihu;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
  }
}

4.重啓訪問

[root@web01 /code]# systemctl restart nginx

#配置本地hosts

5.建立數據庫

[root@web01 /code]# mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 131
Server version: 5.5.65-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 |
| blog               |
| mysql             |
| performance_schema |
| test               |
| zh                 |
+--------------------+
6 rows in set (0.00 sec)

5、LNMP架構實戰演練

1.需求

1.使用nginx關聯php搭建交做業頁面
2.搭建wordpress
3.搭建知乎

2.環境準備

服務器 php

角色  IP
web02 使用web02服務器搭建LNMP架構 10.0.0.8

 

 

 

 

 

3.官方源安裝nginx

1.配置官方源
[root@web02 ~]# 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@web02 ~]# yum -y install nginx

3.啓動服務並設置開機自啓
[root@web02 ~]# systemctl start nginx
[root@web02 ~]# systemctl enable nginx


4.配置nginx
[root@web02 ~]# vim /etc/nginx/nginx.conf
user www;

5.建立統一用戶
[root@web02 ~]# groupadd www -g 666
[root@web02 ~]# useradd www -u 666 -g 666

6.重啓服務並驗證服務
[root@web02 ~]# systemctl restart nginx
[root@web02 ~]# ps aux |grep nginx
root      33463  0.0  0.0  46352   980 ?       Ss   19:46   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www       33464  0.0  0.0  46744  1940 ?       S    19:46   0:00 nginx: worker process
root      33468  0.0  0.0 112708   976 pts/0   R+   19:46   0:00 grep --color=auto nginx

4.安裝PHP

1.上傳源碼包並解壓
[root@web02 ~]# mkdir /package/
[root@web02 ~]# cd /package/
[root@web02 /package]# rz -be
[root@web02 /package]# ll
total 19424
-rw-r--r-- 1 root root 19889622 2020-08-26 09:04 php.tar.gz
[root@web02 /package]#
[root@web02 /package]# tar xf php.tar.gz

2.安裝源碼包
[root@web02 /package]# yum -y localinstall *.rpm

3.配置PHP
[root@web02 /package]# vim /etc/php-fpm.d/www.conf
user = www
group = www

4,重啓服務
[root@web02 /package]# systemctl restart php-fpm.service
[root@web02 /package]# systemctl enable php-fpm.service
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

5.驗證服務
[root@web02 /package]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      33804/php-fpm: mast
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      6131/rpcbind        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      33463/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7144/sshd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7283/master        
tcp6       0      0 :::111                 :::*                   LISTEN      6131/rpcbind        
tcp6       0      0 :::22                   :::*                   LISTEN      7144/sshd          
tcp6       0      0 ::1:25                 :::*                   LISTEN      7283/master        

5.搭建交做業平臺

1.配置nginx
[root@web02 /package]# vim /etc/nginx/conf.d/linux.zuoye.com.conf
server {
  listen 80;
  server_name linux.zuoye.com;

location / {
  root /code/zuoye;
  index index.html;
}
}

2.建立站點目錄
[root@web02 /package]# mkdir -p /code/zuoye

3.上傳交做業平臺源碼包並解壓
[root@web02 /package]# cd /code/zuoye/
[root@web02 /code/zuoye]# rz -be
[root@web02 /code/zuoye]# ll
total 28
-rw-r--r-- 1 root root 26995 2020-08-13 16:42 kaoshi.zip
[root@web02 /code/zuoye]# unzip kaoshi.zip

4.受權目錄
[root@web02 /code/zuoye]# chown -R www:www /code/

5.重啓服務
[root@web02 /code/zuoye]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /code/zuoye]# systemctl restart nginx

6.配置本地hosts文件
C:\Windows\System32\drivers\etc
10.0.0.8 linux.zuoye.com

6.nginx關聯PHP

1.配置nginx文件
[root@web02 /code/zuoye]# vim /etc/nginx/conf.d/linux.zuoye.com.conf
server {
  listen 80;
  server_name linux.zuoye.com;

location / {
  root /code/zuoye;
  index index.html;
}

location ~* \.php$ {
  root /code/zuoye;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}
}

2.配置php文件上傳和下載大小
[root@web02 /code/zuoye]# vim /etc/php.ini
post_max_size = 100M
upload_max_filesize = 100M

3.配置nginx文件上傳和下載大小
[root@web02 /code/zuoye]# vim /etc/nginx/nginx.conf
client_max_body_size 100M;

4.驗證nginx並重啓
[root@web02 /code/zuoye]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 /code/zuoye]# systemctl restart nginx
[root@web02 /code/zuoye]# systemctl restart php-fpm.service

7.建立數據庫mariadb

1.安裝數據庫
[root@web02 /code/zuoye]# yum -y install mariadb-server

2.啓動數據庫設置開機自啓
[root@web02 /code/zuoye]# systemctl start mariadb.service
[root@web02 /code/zuoye]# systemctl enable mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

3.設置數據庫密碼
[root@web02 /code/zuoye]# mysqladmin -u root password root

4.密碼進入驗證
[root@web02 /code/zuoye]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.65-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)]>

5.建立數據庫
MariaDB [(none)]> create database blog;
Query OK, 1 row affected (0.00 sec)

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

8.關聯PHP和mariadb

1.編寫PHP測試鏈接數據庫代碼
[root@web02 /code/zuoye]# vim php_mysql.php
<?php
   $servername = "localhost";
   $username = "root";
   $password = "root";

  // 建立鏈接
   $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.driverzeng.com/zenglaoshi/php_mysql.png>

2.訪問
http://www.zuoye.com/php_mysql.php

9.搭建WordPress

1.上傳代碼
[root@web02 /code]# rz -be
[root@web02 /code]# ll
-rw-r--r-- 1 root root  8451194 2020-08-26 18:31 WeCenter_3-2-1.zip
-rw-r--r-- 1 root root 11098483 2020-08-26 10:49 wordpress-5.0.3-zh_CN.tar.gz
drwxr-xr-x 2 www www       116 2020-08-26 20:25 zuoye
[root@web02 /code]# tar xf wordpress-5.0.3-zh_CN.tar.gz

2.受權目錄
[root@web02 /code]# chown -R www:www /code/

3.配置nginx
[root@web02 /code]# vim /etc/nginx/conf.d/linux.blog.com.conf
server {
  listen 80;
  server_name linux.blog.com;

location / {
  root /code/wordpress;
  index index.php;
}

location ~* \.php$ {
  root /code/wordpress;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}

4.驗證並重啓
[root@web02 /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@web02 /code]# systemctl restart   nginx

5.配置本地hosts文件
C:\Windows\System32\drivers\etc
10.0.0.8 linux.blog.com

10.搭建wecenter

1.上傳代碼
[root@web02 /code]#rz -be
[root@web02 /code]# ll
-rw-r--r--  1 www www   8451194 2020-08-26 18:31 WeCenter_3-2-1.zip
drwxr-xr-x  5 www www      4096 2019-01-11 18:00 wordpress
-rw-r--r--  1 www www  11098483 2020-08-26 10:49 wordpress-5.0.3-zh_CN.tar.gz
drwxr-xr-x  2 www www       116 2020-08-26 20:25 zuoye
[root@web02 /code]#unzip WeCenter_3-2-1.zip

2.受權目錄
[root@web02 /code]# chown -R www:www /code/

3.配置nginx
[root@web02 /code]# vim /etc/nginx/conf.d/linux.zh.com.conf
server {
  listen 80;
  server_name linux.zh.com;

location / {
  root /code/zh;
  index index.php;
}

location ~* \.php$ {
  root /code/zh;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}
}

4.驗證並重啓
[root@web02 /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@web02 /code]# systemctl restart   nginx

5.配置本地hosts文件
C:\Windows\System32\drivers\etc
10.0.0.8 linux.zh.com
相關文章
相關標籤/搜索