Linux架构 Nginx进阶LNMP

Nginx进阶LNMP

LNMP是什么

LNMP是一套技术的组合

L:Linux

N:Nginx

M:MySQL

P:PHP

LNMP架构是如何工作的

首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时, Nginx又是如何进行处理的。

当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过fastcgi协议转交给后端的PHP程序处理,具体如下图所示

img

Nginx与Fast-CGI详细工作流程

img

1.用户通过http协议发起请求,请求会先抵达LNMP架构中的Nginx

2.Nginx会根据用户的请求进行判断,这个判断是有Location进行完成

3.判断用户请求的是静态页面,Nginx直接进行处理

4.判断用户请求的是动态页面,Nginx会将该请求交给fastcgi协议下发

5.fastgi会将请求交给php-fpm管理进程, php-fpm管理进程接收到后会调用具体的工作进程warrap

6.warrap进程会调用php程序进行解析,如果只是解析代码php直接返回

7.如果有查询数据库操作,则由php连接数据库(用户 密码 IP)发起查询的操作

8.最终数据由*mysql->php->php-fpm->fastcgi->nginx->http->user

安装nginx

# 安装nginx
yum install -y nginx

# 启动并设置开机自启
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

安装php

# 添加php源
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
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

# 统一用户 www
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
[root@web01 ~]# id www
uid=666(www) gid=666(www) groups=666(www)

# 修改nginx配置文件使用www用户
vim /etc/nginx/nginx.conf
user www;

# 修改php配置文件使用www用户
vim /etc/php-fpm.d/www.conf 
[www]
user = www
group = www

# 启动php-fpm并加入开机自启
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm

# 检查进程
[root@web01 ~]# ps -ef |grep php-fpm
root       7393      1  0 10:17 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
www        7394   7393  0 10:17 ?        00:00:00 php-fpm: pool www
www        7395   7393  0 10:17 ?        00:00:00 php-fpm: pool www
www        7396   7393  0 10:17 ?        00:00:00 php-fpm: pool www
www        7397   7393  0 10:17 ?        00:00:00 php-fpm: pool www
www        7398   7393  0 10:17 ?        00:00:00 php-fpm: pool www
root       7417   6776  0 10:17 pts/0    00:00:00 grep --color=auto php-fpm

# 端口检测 
[root@web01 ~]# netstat -lntup | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      7393/php-fpm: maste 

数据库安装

# 安装数据库
yum install -y mariadb-server

# 启动数据库并设置开机自启
systemctl start mariadb
systemctl enable mariadb

# 检查端口
[root@web01 ~]# netstat -lntup | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      7745/mysqld     

# 检查进程
[root@web01 ~]# ps -ef | grep mysqld
mysql      7580      1  0 10:23 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql      7745   7580  0 10:23 ?        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
root       7809   6776  0 10:25 pts/0    00:00:00 grep --color=auto mysqld

# 设置数据库密码为123
mysqladmin -uroot password '123'

# 进入mysql来检查是否设置成功
[root@web01 ~]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
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.

将nginx和php建立连接

# 编写nginx配置文件
vim /etc/nginx/conf.d/test_php.conf
server{
        listen 80;
        server_name localhost;
        root /code;

        location / {
                index index.php index.html;
        }
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
        }
}

# 创建站点目录
mkdir /code

# 编写php测试文件
vim /code/info.php
<?php
        phpinfo();
?>

# 重启nginx
systemctl restart nginx 

# 访问网站
10.0.0.7/info.php

img

nginx中fastcgi_params文件解析

vim fastcgi_params
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
--------------------------解析----------------------------------
#脚本文件请求的路径,也就是说当访问127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件,如果没有配置这一配置项时,nginx不回去网站根目录下访问.php文件,所以返回空白
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#请求的参数;如?app=123
fastcgi_param QUERY_STRING $query_string;
#请求的动作(GET,POST)
fastcgi_param REQUEST_METHOD $request_method;
#请求头中的Content-Type字段
fastcgi_param CONTENT_TYPE $content_type;
#请求头中的Content-length字段。
fastcgi_param CONTENT_LENGTH $content_length;
#脚本名称
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
#请求的地址不带参数
fastcgi_param REQUEST_URI $request_uri;
#与$uri相同。 
fastcgi_param DOCUMENT_URI $document_uri;
#网站的根目录。在server配置中root指令中指定的值
fastcgi_param DOCUMENT_ROOT $document_root; 
#请求使用的协议,通常是HTTP/1.0或HTTP/1.1。                
fastcgi_param SERVER_PROTOCOL $server_protocol;
#cgi 版本
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
#nginx 版本号,可修改、隐藏
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
#客户端IP
fastcgi_param REMOTE_ADDR $remote_addr;
#客户端端口
fastcgi_param REMOTE_PORT $remote_port;
#服务器IP地址
fastcgi_param SERVER_ADDR $server_addr;
#服务器端口
fastcgi_param SERVER_PORT $server_port;
#服务器名,域名在server配置中指定的server_name
fastcgi_param SERVER_NAME $server_name;
#可自定义变量
#fastcgi_param PATH_INFO $path_info;
#PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

测试是否可以连接数据库

# 编写mysql文件
vim /code/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://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201912%2F30%2F20191230002053_FTFU4.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1684899560&t=4fb40c5bbadd3186e213cdf62b4ba353>

# 访问网站
10.0.0.7/mysql.php

img

部署博客软件

# 下载wordpress
wget http://test.driverzeng.com/Nginx_Code/wordpress-5.0.3-zh_CN.tar.gz

# 解压wordpress
tar xf wordpress-5.0.3-zh_CN.tar.gz 

# 移动到/code
mv wordpress /code/

# 授权wordpress
chown -R www.www wordpress

# 复制备份
cp /etc/nginx/conf.d/test_php.conf /etc/nginx/conf.d/blog.conf

# 打包本体
gzip /etc/nginx/conf.d/test_php.conf 

# 编写备份配置文件blog.conf
vim /etc/nginx/conf.d/blog.conf 
server{
        listen 80;
        server_name blog.xxx.com;
        root /code/wordpress;

        location / {
                index index.php index.html;
        }
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
        }
}

# 连接至数据库 创建一个库给wordpresss
mysql -uroot -p123
# 创建库
MariaDB [(none)]> create database wp charset utf8;
# 查看有哪些库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wp                 |
+--------------------+
5 rows in set (0.00 sec)

# 重启nginx
systemctl restart nginx

# 在Windows进行域名解析

# 访问网页
10.0.0.7

博客部署

  • 准备界面

img

  • 设置wordpress数据库的基本信息

img

  • 安装博客

img

  • 设置博客信息

img

  • 设置账号密码成功

img

  • 登录WordPress

img

  • 博客设置成功

img

部署知乎软件

# 下载
wget http://test.driverzeng.com/Nginx_Code/WeCenter_3-2-1.zip

# 解压WeCenter_3-2-1.zip
unzip WeCenter_3-2-1.zip

# 删除__MACOSX
rm -fr __MACOSX/

# 移动到/code
mv WeCenter_3-2-1 /code/wecenter

# 授权wordpress
chown -R www.www /code/WeCenter_3-2-1

# 复制备份
cp /etc/nginx/conf.d/test_php.conf /etc/nginx/conf.d/zh.conf

# 打包本体
gzip /etc/nginx/conf.d/test_php.conf 

# 编写备份配置文件zh.conf
vim /etc/nginx/conf.d/zh.conf 
server{
        listen 80;
        server_name zh.xxx.com;
        root /code/wecenter;

        location / {
                index index.php index.html;
        }
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
        }
}

# 连接至数据库 创建一个库给WeCenter
mysql -uroot -p123
# 创建库
MariaDB [(none)]> create database zh charset utf8;
# 查看有哪些库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| zh                 |
+--------------------+
5 rows in set (0.00 sec)

# 重启nginx
systemctl restart nginx

# 在Windows进行域名解析

# 访问网页
10.0.0.7

# 如果访问时显示403,则修改权限
chmod 777 /code/wecenter/
  • 服务器环境检查

img

  • 配置系统

img

  • 添加数据库管理员

img

  • 安装完成提示

img

  • 成功展示

img

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇