LNMP架构搭建部署

网站的LNMP架构

部署前注意:

  1. selinux 、防火墙须要关闭
  2. /tmp 的权限须要为1777 否则会致使mysql服务没法启动

nginx、MySQL(此处用mariadb代替)、php的做用:

  • nginx: 处理用户的静态请求。
    • 例如:html、jpg、txt、MP4等信息
  • php:
    • 处理动态的页面请求
    • 负责和数据库创建关系
  • MySQL(mariadb):存储用户的字符串数据信息

三大软件的安装部署

nginx部署

  1. 更新nginx官方yum源php

    [root@web02 ~]# vim /etc/yum.repos.d/nginx.repo
    [nginx-stable]
       name=nginx stable repo
       baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
       gpgcheck=1
       enabled=1
       gpgkey=https://nginx.org/keys/nginx_signing.key
  2. yum安装nginx软件并启动html

    [root@web02 ~]# yum -y install nginx 
    [root@web02 ~]# systemctl start nginx
    [root@web02 ~]# systemctl enable nginx
  3. 编写nginx服务配置文件定义worker用户为www(必须为系统上存在的用户)mysql

    [root@web02 ~]# vim /etc/nginx/nginx.conf
    
    user  www;
    
    [root@web02 ~]# systemctl restart nginx

mariadb部署安装

  1. 安装软件并启动:linux

    [root@web02 ~]# yum -y install mariadb-server.x86_64 mariadb
    [root@web02 ~]# systemctl start mariadb.service 
    [root@web02 ~]# systemctl enable mariadb.service
  2. 设置mariadb的root用户密码
[root@web02 ~]# mysqladmin -u root password '123456'
#mysqladmin 是设置密码的命令而不是修改密码,修改密码须要用到mysql的语法来修改
  1. 使用root用户登陆数据库 (-p 以后紧接着就是密码信息不能又空格)nginx

    [root@web02 ~]# mysql -uroot -p'123456'
    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.
    
    MariaDB [(none)]>

php服务部署安装

  1. 更新yum源/卸载系统自带的php软件web

    [root@web02 ~]# yum remove php-mysql php php-fpm php-common
    已加载插件:fastestmirror
    参数 php-mysql 没有匹配
    参数 php 没有匹配
    参数 php-fpm 没有匹配
    参数 php-common 没有匹配
    不删除任何软件包
    [root@web02 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    获取https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    警告:/var/tmp/rpm-tmp.C7Lv8z: 头V4 RSA/SHA256 Signature, 密钥 ID 352c64e5: NOKEY
    准备中...                          ################################# [100%]
    正在升级/安装...
      1:epel-release-7-13                警告:/etc/yum.repos.d/epel.repo 已创建为 /etc/yum.repos.d/epel.repo.rpmnew 
    ################################# [100%]
    [root@web02 ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    获取https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    警告:/var/tmp/rpm-tmp.RS98qy: 头V4 RSA/SHA1 Signature, 密钥 ID 62e74ca5: NOKEY
    准备中...                          ################################# [100%]
    正在升级/安装...
      1:webtatic-release-7-3             ################################# [100%]
  2. 安装php软件(此时安装会比较慢)redis

    [root@web02 ~]#yum install -y 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
  3. 编写配置文件(修改服务进程的管理用户)sql

    [root@web02 ~]# vim /etc/php-fpm.d/www.conf 
    
    ; Start a new pool named 'www'.
    [www]
    
    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user's group
    ;       will be used.
    ; RPM: apache Choosed to be able to access some dir as httpd
    user = www
    ; RPM: Keep a group allowed to write in log dir.
    group = www

    LNMP所用图片

    ps:保证nginx进程的管理用户和php服务进程的管理用户保持一致mongodb

  4. 启动php服务shell

    [root@web02 ~]# systemctl start php-fpm

LNMP架构的原理

用户访问网站--->nginx(fastcgi_pass) --FastCGI-->(php-fpm -- wrapper) php (php解析器) ---> mysql(读取或写入)

LNMP所用图片

LNMP之间创建关系

实现nginx+php创建关系

  1. 编写nginx配置文件并重启nginx服务

    [root@web02 ~]# vim /etc/nginx/conf.d/www.conf 
    
    server {
          listen        80;
          server_name   www.jiage.com;
          location  / {
            root  /html/www;
            index index.html;
            error_page 404 /404.jpg;
          }
         location ~\.php$ {
           root /html/www;
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           fastcgi_pass  127.0.0.1:9000;
           include fastcgi_params;
    }
    
       }
    [root@web02 ~]# systemctl reload nginx.service

    ps: fastcgi_param 该行表明着URL 与URI信息

    /etc/nginx/fastcgi_params 下能够看到定义的变量 $document_root$fastcgi_script_name;

  2. 编写动态资源测试文件

    [root@web02 ~]# vim /html/www/test_php.php
    <?php
          phpinfo();
    ?>
  3. 进行访问测试

    www.jiage.com/test_php.php

    LNMP所用图片

实现php+mysql创建关系

编写php代码文件

[root@web02 ~]# vim /html/www/test_mysql.php 

<?php
 $servername = "localhost";
 $username = "root";
 $password = "123456";
 //$link_id=mysql_connect('主机名','用户','密码');
 //mysql -u用户 -p密码 -h 主机
 $conn = mysqli_connect($servername, $username, $password);
 if ($conn) {
       echo "mysql successful by root !\n";
    }else{
       die("Connection failed: " . mysqli_connect_error());
    }
?>
//mysqli_connect() 该函数主要是用于与数据库连接

LNMP所用图片

自此说明LNMP的基础架构已经搭建好了 后面以搭建一个blog网站页面为例子


部署搭建网站页面

1. 获取代码信息--使用开源的网站代码

*www网站页面: http://www.dedecms.com/
bbs网站页面: http://www.discuz.net/forum.php
blog网站页面: https://cn.wordpress.org/
wecenter网站页面: http://www.wecenter.com/?copyright*

2.将下载好的代码压缩包解压,并将解压后的信息放到站点目录中

注意:若是站点目录中有文件最好先备份 再将解压后的信息移过去,这样防止有些一样名字的文件被替代

[root@web02 ~]# vim /etc/nginx/conf.d/blog.conf 

server {
       listen        80;
       server_name   blog.jiage.com;
       location  / {
         root  /html/blog;
         index index.html;
       }
      location ~\.php$ {
        root /html/blog;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass  127.0.0.1:9000;
        include fastcgi_params;
       }
   } 
[root@web02 ~]# vim /html/blog/test_mysql.php

<?php
 $servername = "localhost";
 $username = "root";
 $password = "123456";
 //$link_id=mysql_connect('主机名','用户','密码');
 //mysql -u用户 -p密码 -h 主机
 $conn = mysqli_connect($servername, $username, $password);
 if ($conn) {
       echo "mysql successful by root !\n";
    }else{
       die("Connection failed: " . mysqli_connect_error());
    }
?>
[root@web02 html]# systemctl reload nginx.service 
[root@web02 blog]# tar xf wordpress-5.6-zh_CN.tar.gz 
[root@web02 blog]# ll
总用量 15724
-rw-r--r-- 1 root root       24 1月   5 23:55 index.html
-rw-r--r-- 1 root root      372 1月   9 20:56 test_mysql.php
drwxr-xr-x 5 1006 1006     4096 12月 22 22:00 wordpress
-rw-r--r-- 1 root root 16086935 12月 24 22:43 wordpress-5.6-zh_CN.tar.gz
[root@web02 blog]# mv wordpress/* ./ 
[root@web02 blog]# ls
index.html   test_mysql.php              wp-admin              wp-content         wp-load.php      wp-signup.php
index.php    wordpress                   wp-blog-header.php    wp-cron.php        wp-login.php     wp-trackback.php
license.txt  wordpress-5.6-zh_CN.tar.gz  wp-comments-post.php  wp-includes        wp-mail.php      xmlrpc.php
readme.html  wp-activate.php             wp-config-sample.php  wp-links-opml.php  wp-settings.php

3.修改站点目录的权限

[root@web02 html]# chown -R www.www blog/

4.进行网站页面初始化操做

浏览器访问: blog.jiage.com/index.php

LNMP所用图片

ps:此时先不要点提交,经过第五步建立对应的数据库和数据库用户

5.配置数据库服务

建立与第四步对应的数据库和数据库用户:

[root@web02 html]# mysql -u root -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
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)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'localhost'identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host from mysql.user;
+-----------+-----------+
| user      | host      |
+-----------+-----------+
| root      | 127.0.0.1 |
| root      | ::1       |
|           | localhost |
| root      | localhost |
| wordpress | localhost |
|           | web02     |
| root      | web02     |
+-----------+-----------+
7 rows in set (0.00 sec)

6.回到浏览器提交,并发表论文(附上图片)测试。

LNMP所用图片

此时访问会出现403错误,缘由是没有首页文件 index.php

[root@web02 html]# vim /etc/nginx/conf.d/blog.conf 

server {
       listen        80;
       server_name   blog.jiage.com;
       location  / {
         root  /html/blog;
        index index.php index.html;
       }
[root@web02 html]# systemctl restart nginx

利用浏览器访问 blog.jiage.com

7.解决上传主题时出现的413状态码的问题

LNMP所用图片

  • 在nginx.conf配置文件中加入 client_max_body_size 50M;
[root@web02 ~]# vim /etc/nginx/nginx.conf

http {

    client_max_body_size 50M;
}
**选择在`http{}`中设置:client_max_body_size 50m;**
**也能够选择在`server{}`中设置:client_max_body_size 50m;**
**还能够选择在`location{}`中设置:client_max_body_size 50m;**
**三者有区别,不一样的做用域参数设置,有不一样的含义。**
**设置到`http{}`内,控制全局nginx全部请求报文大小;**
**设置到`server{}`内,控制该server的全部请求报文大小;**
**设置到`location{}`内,控制知足该路由规则的请求报文大小 ;**
  • 因为workpress采用php设计,文件上传大小限制值,是由php环境的配置决定的。修改 /etc/php.ini 配置文件便可

    • upload_max_filesize(最大上传文件大小)
    • post_max_size(POST数据最大字节长度)
    • max_execution_time(最大执行时间,单位秒)
    [root@web02 ~]# vim /etc/php.ini
    upload_max_filesize = 50M
    post_max_size = 50M
    max_execution_time = 3
    [root@web02 ~]# systemctl restart php-fpm.service

    自此咱们发现数据库数据与web数据都在同一台服务器上,这样信息不安全,所以咱们须要准备一台mysql服务器和nfs存储共享服务器。


让lnmp架构与nfs存储服务器创建关系

1.找出服务器中存储数据的目录

两种方法:1) 定位到站点目录中查找最近上传的文件

[root@web02 blog]# find /html/blog/ -type f -mmin -20

image-20210110142855087

2)经过发表的文章,在浏览器上复制图片地址

http://blog.jiage.com/wp-content/uploads/2021/01/1-768x1024.jpg

blog.jiage.com表明着站点目录的位置/html/blog

2.肯定web服务器与nfs存储服务器能创建链接

  • 在nfs服务器上建立与web服务器对应的id用户www为映射用户。

    [root@nfs01 ~]# useradd www
    [root@nfs01 ~]# id www
    uid=1001(www) gid=1001(www) 组=1001(www)
    [root@nfs01 ~]# chown -R www.www /data
  • 编写nfs配置文件并重启后检查服务是否正常

    [root@nfs01 ~]# vim /etc/exports
    /data/bbs 172.16.1.0/24(rw,sync,anonuid=1001,anongid=1001)
    /data/blog 172.16.1.0/24(rw,sync,anonuid=1001,anongid=1001)
    /data/www 172.16.1.0/24(rw,sync,anonuid=1001,anongid=1001)
    
    [root@nfs01 ~]# systemctl restart nfs
    [root@nfs01 ~]# showmount -e
    Export list for nfs01:
    /data/www  172.16.1.0/24
    /data/blog 172.16.1.0/24
    /data/bbs  172.16.1.0/24
  • web上测试nfs挂载

    [root@web02 2021]# yum -y install nfs-utils
    [root@web02 2021]# mount -t nfs 172.16.1.31:/data/www /mnt
    [root@web02 2021]# df -Th
    文件系统                类型      容量  已用  可用 已用% 挂载点
    172.16.1.31:/data/www   nfs4       47G  1.5G   46G    4% /mnt
    [root@web02 2021]# umount /mnt

    说明web服务器与nfs存储服务器已可创建关系

  • 将web服务器上blog存储的数据进行迁移(由于直接挂载至关于格式化了)

    [root@web02 uploads]# ls
    2021
    [root@web02 uploads]# pwd
    /html/blog/wp-content/uploads
    [root@web02 uploads]# mv ./2021 /tmp
  • 将blog存储数据的目录挂载到nfs服务器上

    [root@web02 uploads]# cd ~
    [root@web02 ~]# mount -t nfs 172.16.1.31:/data/blog /html/blog/wp-content/uploads
    [root@web02 ~]# mv /tmp/2021/ /html/blog/wp-content/uploads/
    [root@web02 ~]# df -Th
    文件系统                类型      容量  已用  可用 已用% 挂载点
    172.16.1.31:/data/blog  nfs4       47G  1.5G   46G    4% /html/blog/wp-content/uploads

    ps:若是/data/目录权限的拥护者与nfs配置文件上映射用户的id不一致会道中没有权限(须要了解nfs服务root用户与普通用户的映射关系)

    此时看似将数据存储在原先/html/blog/wp-content/uploads这目录下,实则已存储在nfs服务器上的/data/blog中

    [root@nfs01 data]# cd ./blog/
    [root@nfs01 blog]# ll
    总用量 0
    drwxr-xr-x 3 www www 16 1月   9 22:09 2021

    测试:在浏览器发表文章后能够在nfs服务上看到上传的图片,即web与nfs已经创建关系。

让lnmp架构和数据服务器创建关系

1.将web服务器本地数据库数据进行备份

[root@web02 01]# mysqldump -u root -p123456 --all-database > /tmp/web_bak.sql

2.将数据迁移到独立的一台数据库服务器上

[root@web02 01]# scp -rp /tmp/web_bak.sql 172.16.1.51:/tmp/
The authenticity of host '172.16.1.51 (172.16.1.51)' can't be established.
ECDSA key fingerprint is SHA256:FuqiLIfW+LFJVdI/IFgcIv+9Tf1gXtgdWKXm4SKORgU.
ECDSA key fingerprint is MD5:e6:33:4f:bc:e2:ad:d0:01:07:ae:78:4e:c4:e1:0f:d2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.51' (ECDSA) to the list of known hosts.
root@172.16.1.51's password: 
web_bak.sql                                                                                          100%  927KB  75.9MB/s   00:0

3.恢复数据(由于这里是全新的服务器因此先安装)

[root@db01 ~]# yum -y install mariadb-server.x86_64 mariadb
[root@db01 ~]# systemctl start mariadb.service 
[root@db01 ~]# systemctl enable mariadb.service 
[root@db01 ~]# mysqladmin -u root password 123456
[root@db01 ~]# mysql -uroot -p123456 < /tmp/web_bak.sql

4.修改数据服务器中数据库用户信息,让wordpress容许远程登陆

MariaDB [(none)]> select user,host from mysql.user;
+-----------+-----------+
| user      | host      |
+-----------+-----------+
| root      | 127.0.0.1 |
| root      | ::1       |
|           | localhost |
| root      | localhost |
| wordpress | localhost |
|           | web02     |
| root      | web02     |
+-----------+-----------+
7 rows in set (0.00 sec)
此时看到的wordpress只容许在本地登陆
  • 优化:删除无用的用户信息以避免对实验形成影响

    MariaDB [(none)]> delete from mysql.user where user="" and host="localhost";
    Query OK, 1 row affected (0.00 sec)
    
    MariaDB [(none)]> delete from mysql.user where user="" and host="web02";
    Query OK, 1 row affected (0.00 sec)
  • 添加:添加新的用户信息

    MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'172.16.1.%' identified by '123456';
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

5.修改web服务器上代码文件信息

[root@web02 blog]# vim /html/blog/wp-config.php 
/** MySQL主机 */
define( 'DB_HOST', '172.16.1.51' );

6.中止web服务器上数据库服务

测试: 浏览器访问blog.jiage.com 还能正常访问,并注册新用户,能够在数据库服务器上查到。

MariaDB [wordpress]> select user_login from wp_users;
+------------+
| user_login |
+------------+
| admin      |
| jiage      |
+------------
2 rows in set (0.00 sec)

总结:

  1. 了解lnmp各个服务之间的做用和总体的工做原理
  2. nginx上的worker进程用户、站点目录管理用户、php管理用户、nfs存储目录的用户的id名称要一致。
  3. 在迁移过程当中,须要对其进行备份,确认数据不丢失。
相关文章
相关标签/搜索