美文网首页
LNMP搭建博客

LNMP搭建博客

作者: linux_阿杰 | 来源:发表于2020-03-15 13:58 被阅读0次

什么是LNMP?

LNMP 是一套技术的组合, L=Linux、 N=Nginx、 M=MySQL、 P=PHP

LNMP架构如何工作?

首先 Nginx 服务是不能处理动态请求,那么当用户发起动态请求时, Nginx 又是如何进行处理的。
当用户发起 http 请求,请求会被 Nginx 处理,如果是静态资源请求 Nginx 则直接返回,如果是动态请求 Nginx 则通过 fastcgi 协议转交给后端的 PHP 程序处理,具体如下图所示


img

比喻:
Nginx: 拉货的货车
FastCGI: 高速公路
php-fpm: 每个路道的收费站
wrapper: 搬货的师傅
systemctl start php-fpm: 管理整个高速工作所有的收费站

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

那么LNMP能干什么?

1)博客环境
2)论坛环境
3)教育环境
4)。。。。。。

LNMP安装部署

创建统一用户

groupadd -g666 www
useradd -u666 -g666 -s /sbin/nologin -M www

安装Nginx

采用yum安装

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

1)安装nginx
yum install nginx -y
2)配置虚拟用户
sed -i '/^user/c user www;' /etc/nginx/nginx.conf
3)启动并开机自启

systemctl start nginx 
systemctl enable nginx

安装php7.1

使用第三方源安装php

[root@web01 nginx]# yum remove php-mysql-5.4 php php-fpm php-common
[root@web01 nginx]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@web01 nginx]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@web01 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

1)配置启动用户位www
sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
2)启动php
systemctl start php-fpm

  1. 开机自启
    systemctl enable php-fpm

Nginx连接php

  1. 写入配置文件(写完重启服务)
[root@web01 ~ ]# vim /etc/nginx/conf.d/php.conf
server {
        listen 80;                     # 监听端口 
        server_name wp.oldboy.com;     # 域名hosts解析

        location / {
        root /code;                    # 存放代码位置
        index index.php;               # 默认访问index.php
        }

        location ~ \.php$ {            # 匹配php结尾的
        root /code;               
        fastcgi_pass 127.0.0.1:9000;   # php监听端口
            
        # fastcgi语法 接口  nginx连接php
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        
        # 引入变量
        include fastcgi_params;
        }

}
  1. 创建代码目录
    mkdir /code
  2. 创建php测试文件
[root@web01 /code ]# cat info.php 
<?php
        phpinfo();
?>
  1. 把域名写进windows hosts
    10.0.1.7 wp.oldboy.com

测试Nginx连接php是否成功

image-20200305174322924.png

安装mariadb

yum install mariadb mariadb-server -y

  1. 启动并自启
systemctl start mariadb
systemctl enable mariadb
  1. 登录
[root@web01 ~ ]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 81
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)]>
MariaDB [(none)]>set password = password("123456"); # 设置密码

测试Nginx连接数据库

  1. 在code下创建mysql.php
[root@web01 ~ ]# cat /code/mysql.php 
<?php
        $servername = "localhost";
        $username = "root";
        $password = "123456";
        // 创建连接
        $conn = mysqli_connect($servername, $username, $password);
        // 检测连接
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        echo "小哥哥,php可以连接MySQL...";
    ?>
  1. 访问网页


    image-20200305174919513.png

获取wordpress源码包

  1. 配置博客
[root@web01 ~ ]# cat /etc/nginx/conf.d/wp.conf 
server {
        listen 80;
        server_name wp.oldboy.com;

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

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

  1. 重载nginx
    systemctl reload nginx
  2. 下载wordpress
# 下载代码包
[root@web01 /code ]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
# 解压
[root@web01 /code ]# tar xf wordpress.zh_CN.tar.gz   
[root@web01 /code ]# ls
info.php  mysql.php  wordpress wordpress.zh_CN.tar.gz 
# 授权 否则写博文提示权限不足
[root@web01 /code ]# chown -R www.www /code/wordpress
  1. 创建wordpress数据库
    [root@web01 ~]# mysql -uroot -p123456 -e "create database wordpress;"
    访问网页进行安装,我就不教了,鼠标式操作。。。

相关文章

网友评论

      本文标题:LNMP搭建博客

      本文链接:https://www.haomeiwen.com/subject/onnkehtx.html