• 构建lnmp高可用架构


    LNMP架构部署

    LNMP架构简述

    LNMP平台就是Linux、Ngnix、MySQL、PHP的组合架构,需要Linux服务器、MySQL数据库、PHP解析环境
    Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Mysql是一个小型关系型数据库管理系统。PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
    本案例在单台服务器上部署LNMP环境

    一、Nginx编译安装

    1.安装环境依赖包
    需要gcc、gcc-c++、make编译工具,pcre、zlib等软件包的支持

     [root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
    
    • 1

    2.创建运行用户、组
    Nginx服务程序默认以nobody身份运行,建议为其创建专门的用户账户,以更精确的控制访问权限,增加灵活性、降低安全风险。

     [root@localhost ~]# useradd -M -s /sbin/nologin nginx
    
    • 1

    3.编译安装Nginx
    安装目录在/usr/local/nginx,运行用户和组均为nginx,启用http_stub_status_module统计模块(统计多少人访问)

     [root@localhost ~]# cd /opt
    [root@localhost opt]# ls
    nginx-1.12.2.tar.gz
    [root@localhost opt]# tar zxf nginx-1.12.2.tar.gz
    [root@localhost opt]# cd nginx-1.12.2
    [root@localhost nginx-1.12.2]# ./configure 
    --prefix=/usr/local/nginx        
    --user=nginx 
    --group=nginx 
    --with-http_stub_status_module
    [root@localhost nginx-1.12.2]# make && make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.路径优化
    为了使Nginx服务器的运行更加方便,可以为主程序nginx创建连接文件,以便管理员直接执行“nginx”命令就可以调用Nginx的主程序

     [root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
    [root@localhost nginx-1.12.2]# ll /usr/local/sbin
    总用量 0
    lrwxrwxrwx. 1 root root 27 8月   6 20:12 nginx -> /usr/local/nginx/sbin/nginx
    
    • 1
    • 2
    • 3
    • 4

    5.检查配置文件
    与Apache的主程序httpd类似,Nginx的主程序也提供了“-t”选项用来对配置文件进行检查,以便找出不当或错误的配置。配置文件nginx.conf默认位于安装目录的conf/子目录中。若要检查位于其他位置的配置文件,可使用“-c”选项来指定路径。

     [root@localhost nginx-1.12.2]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    
    • 1
    • 2
    • 3

    6.启动、停止Nginx
    直接运行Nginx即可启动Nginx服务器,这种方式将使用默认的配置文件,若要改用其他配置文件,需添加“-c 配置文件路径”选项来指定路径。需要注意的是,若服务器中已装有httpd等其他Web服务软件,应采取措施(修改端口、停用或卸载)避免冲突。

     [root@localhost ~]# nginx     ##启动nginx
    [root@localhost ~]# netstat -ntap | grep nginx
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9676/nginx: master
    [root@localhost ~]# pkill nginx             ##停止nginx
    [root@localhost ~]# killall -3 nginx       ##停止nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7.添加Nginx系统服务
    为了使nginx服务的启动、停止、重载等操作更加方便,可以编写基于Centos7.6的服务控制文件使用systemctl工具来进行管理。

    [root@localhost ~]# vim /lib/systemd/system/nginx.service
    [Unit]
    Description=nginx                          ##描述
    After=network.target                      ##描述服务类别
    [Service]
    Type=forking                                                       ##后台运行形式
    PIDFile=/usr/local/nginx/logs/nginx.pid               ##PID文件位置
    ExecStart=/usr/local/nginx/sbin/nginx                 ##启动服务
    ExecReload=/usr/bin/kill-s HUP $MAINPID        ##根据PID重载配置
    ExecStop=/usr/bin/kill-s QUIT $MAINPID           ##根据PID终止进程
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
    [root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
    [root@localhost system]# systemctl enable nginx.service
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    8.检查通过systemctl命令能否正常启动、停止、重启、重载Nginx服务。

    [root@localhost ~]# killall -3 nginx
    [root@localhost ~]# systemctl start nginx
    [root@localhost ~]# netstat -ntap | grep nginx
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      28358/nginx: master
    
    • 1
    • 2
    • 3
    • 4

    二、Mysql安装配置

    1.安装mysql环境依赖包
    需要gcc、gcc-c++、make编译工具,pcre、zlib等软件包的支持

    [root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
    
    • 1

    2.创建运行用户、组
    Nginx服务程序默认以nobody身份运行,建议为其创建专门的用户账户,以更精确的控制访问权限,增加灵活性、降低安全风险。

    [root@localhost ~]# useradd -M -s /sbin/nologin nginx
    
    • 1

    3.编译安装Nginx
    安装目录在/usr/local/nginx,运行用户和组均为nginx,启用

    http_stub_status_module统计模块(统计多少人访问)
    [root@localhost ~]# cd /opt
    [root@localhost opt]# ls
    nginx-1.12.2.tar.gz
    [root@localhost opt]# tar zxf nginx-1.12.2.tar.gz
    [root@localhost opt]# cd nginx-1.12.2
    [root@localhost nginx-1.12.2]# ./configure 
    --prefix=/usr/local/nginx        
    --user=nginx 
    --group=nginx 
    --with-http_stub_status_module
    [root@localhost nginx-1.12.2]# make && make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4.路径优化
    为了使Nginx服务器的运行更加方便,可以为主程序nginx创建连接文件,以便管理员直接执行“nginx”命令就可以调用Nginx的主程序

    [root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
    [root@localhost nginx-1.12.2]# ll /usr/local/sbin
    总用量 0
    lrwxrwxrwx. 1 root root 27 8月   6 20:12 nginx -> /usr/local/nginx/sbin/nginx
    
    • 1
    • 2
    • 3
    • 4

    5.检查配置文件
    与Apache的主程序httpd类似,Nginx的主程序也提供了“-t”选项用来对配置文件进行检查,以便找出不当或错误的配置。配置文件nginx.conf默认位于安装目录的conf/子目录中。若要检查位于其他位置的配置文件,可使用“-c”选项来指定路径。

    [root@localhost nginx-1.12.2]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    
    • 1
    • 2
    • 3

    6.启动、停止Nginx
    直接运行Nginx即可启动Nginx服务器,这种方式将使用默认的配置文件,若要改用其他配置文件,需添加“-c 配置文件路径”选项来指定路径。需要注意的是,若服务器中已装有httpd等其他Web服务软件,应采取措施(修改端口、停用或卸载)避免冲突。

    [root@localhost ~]# nginx     ##启动nginx
    [root@localhost ~]# netstat -ntap | grep nginx
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9676/nginx: master
    [root@localhost ~]# pkill nginx             ##停止nginx
    [root@localhost ~]# killall -3 nginx       ##停止nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7.添加Nginx系统服务
    为了使nginx服务的启动、停止、重载等操作更加方便,可以编写基于Centos7.6的服务控制文件使用systemctl工具来进行管理。

    [root@localhost ~]# vim /lib/systemd/system/nginx.service
    [Unit]
    Description=nginx                          ##描述
    After=network.target                      ##描述服务类别
    [Service]
    Type=forking                                                       ##后台运行形式
    PIDFile=/usr/local/nginx/logs/nginx.pid               ##PID文件位置
    ExecStart=/usr/local/nginx/sbin/nginx                 ##启动服务
    ExecReload=/usr/bin/kill-s HUP $MAINPID        ##根据PID重载配置
    ExecStop=/usr/bin/kill-s QUIT $MAINPID           ##根据PID终止进程
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
    [root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
    [root@localhost system]# systemctl enable nginx.service
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    8.检查通过systemctl命令能否正常启动、停止、重启、重载Nginx服务。

    [root@localhost ~]# killall -3 nginx
    [root@localhost ~]# systemctl start nginx
    [root@localhost ~]# netstat -ntap | grep nginx
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      28358/nginx: master
    
    • 1
    • 2
    • 3
    • 4

    二、Mysql安装配置
    1.安装mysql环境依赖包

    [root@localhost ~]# yum -y install ncures ncures-devel bison cmake
    
    • 1

    2.创建运行用户

    [root@localhost ~]# useradd -s /sbin/nologin mysql
    
    • 1

    3.编译安装
    上传mysql-boost-5.7.20.tar.gz到/opt目录下,boost是c++库

    [root@localhost ~]# cd /opt
    [root@localhost opt]# tar zxf mysql-boost-5.7.20.tar.gz
    [root@localhost opt]# cd mysql-5.7.20/
    [root@localhost mysql-5.7.20]# cmake 
    -DMAKE_INSTALL_PREFIX=/usr/local/mysql 
    -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock 
    -DSYSCONFDIR=/etc 
    -DSYSTEMD_PID_DIR=/usr/local/mysql 
    -DDEFAULT_CHARSET=utf8 
    -DDEFAULT_COLLATION=utf8_general_ci 
    -DWITH_INNOBASE_STORAGE_ENGINE=1 
    -DWITH_ARCHIVE_STORAGE_ENGINE=1 
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 
    -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 
    -DMYSQL_DATADIR=/usr/local/mysql/data 
    -DWITH_BOOST=boost 
    -DWITH_SYSTEMD=1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    4.数据库目录进行权限调整

    [root@localhost local]# chown -R mysql.mysql /usr/local/mysql/
    
    • 1

    5.调整配置文件

    [root@localhost local]# vim /etc/my.cnf
    [client]
    port=3306
    default-character-set=utf8
    socket=/usr/local/mysql/mysql.sock
    
    [mysql]
    port=3306
    default-character-set=utf8
    socket=/usr/local/mysql/mysql.sock
    
    [mysqld]
    user=mysql
    basedir=/usr/local/mysql
    datadir=/usr/local/mysql/data
    port=3306
    character_set_server=utf8
    pid-file=/usr/local/mysql/mysql.pid
    socket=/usr/local/mysql/mysql.sock
    server-id=1
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    6.设置环境变量

    [root@localhost ~]# 
    echo "PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH" >> /etc/profile
    [root@localhost ~]# echo "export PATH" >> /etc/profile
    [root@localhost ~]# source /etc/profile
    
    • 1
    • 2
    • 3
    • 4

    7.初始化数据库

    [root@localhost ~]# cd /usr/local/mysql/
    [root@localhost mysql]# bin/mysqld 
    --initialize-insecure 
    --user=mysql 
    --basedir=/usr/local/mysql 
    --datadir=/usr/local/mysql/data
    
    [root@localhost ~]# cd /usr/local/mysql/
    [root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/        ##将mysql服务启动脚本复制到systemd管理工具的目录下,便于使用systemctl管理
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    8.开启数据库,检查状态

    [root@localhost mysql]# systemctl enable mysqld
    [root@localhost mysql]# systemctl start mysqld
    [root@localhost mysql]# netstat -ntap |grep 3306
    tcp6       0      0 :::3306                 :::*                    LISTEN      11260/mysqld
    
    • 1
    • 2
    • 3
    • 4

    9.设置mysql密码

    [root@localhost mysql]# mysqladmin -u root -p password
    Enter password:          ##初始密码为空,直接回车
    New password:           ##输入新密码“abc123”
    Confirm new password:  ##确认新密码“abc123”
    
    • 1
    • 2
    • 3
    • 4

    10.登录数据库

    [root@localhost mysql]# mysql -u root -p   ##密码是刚才设置的abc123
    
    • 1

    三、PHP解析环境的安装

    ●配置网页动静分离,解析PHP,有两种方法可以选择
    使用PHP的FPM模块
    将访问PHP页面的Web请求转交给Apache服务器去处理
    ●较新版本的PHP已经自带FPM模块,对PHP解析实例进行管理、优化解析效率
    FastCGI将Http Server和动态脚本语言分离开
    Nginx专门处理静态请求,转发动态请求
    PHP-FPM专门解析PHP动态请求
    ●单服务器的LNMP架构通常使用FPM方式来解析PHP
    ●PHP编译安装步骤
    1.安装环境依赖包

    [root@localhost ~]# yum -y install 
    libjpeg 
    libjpeg-devel 
    libpng libpng-devel 
    freetype freetype-devel 
    libxml2 
    libxml2-devel 
    zlib zlib-devel 
    curl curl-devel 
    openssl openssl-devel
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.编译安装

    [root@localhost opt]# tar xjvf php-7.1.10.tar.bz2	
    [root@localhost opt]# cd php-7.1.10
    [root@localhost php-7.1.10]# ./configure 
    --prefix=/usr/local/php 
    --with-mysql-sock=/usr/local/mysql/mysql.sock        ##连接数据库
    --with-mysqli                    ##客户端
    --with-zlib 
    --with-curl 
    --with-gd 
    --with-jpeg-dir 
    --with-png-dir 
    --with-freetype-dir 
    --with-openssl 
    --enable-fpm                 ##开启php-fpm模块,使nginx能支持动态页面
    --enable-mbstring 
    --enable-xml 
    --enable-session 
    --enable-ftp 
    --enable-pdo 
    --enable-tokenizer 
    --enable-zip
    make && make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.php有三个配置文件,php.ini 核心配置文件,php-fpm.conf 进程服务配置文件,www.conf扩展配置文件

    [root@localhost php-7.1.10]# cp php.ini-development /usr/local/php/lib/php.ini
    [root@localhost php-7.1.10]# vim /usr/local/php/lib/php.ini
    mysqli.default_socket = /usr/local/mysql/mysql.sock   ##位于1170行
    date.timezone = Asia/Shanghai      ##位于939行
    
    /usr/local/php/bin/php -m   ##用于验证安装的模块
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4.配置及优化fpm模块

    [root@localhost php-7.1.10]# cd /usr/local/php/etc/
    [root@localhost etc]# ls
    pear.conf  php-fpm.conf.default  php-fpm.d
    [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
    [root@localhost etc]# cd php-fpm.d/
    [root@localhost php-fpm.d]# cp www.conf.default www.conf
    [root@localhost php-fpm.d]# cd /usr/local/php/etc/
    [root@localhost etc]# vim php-fpm.conf
    pid = run/php-fpm.pid     ##前面的 ; 去掉
    
    [root@localhost etc]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
    [root@localhost etc]# netstat -ntap | grep 9000
    tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      9981/php-fpm: maste
    [root@localhost etc]# ln -s /usr/local/php/bin/* /usr/local/bin/
    [root@localhost etc]# ps aux | grep -c "php-fpm"   ##查看php-fpm进程数
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    5.配置Nginx支持PHP功能
    在主配置文件server{ }段靠近模板位置插入如下内容(有模板,注意网站站点目录名称)

    [root@localhost etc]# vim /usr/local/nginx/conf/nginx.conf  
    location ~ .php$ {
          root           html;
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
          include        fastcgi_params;
       }
    [root@localhost ~]# vim /usr/local/nginx/html/index.php
    <?php
    phpinfo();
    ?>
    [root@localhost ~]# systemctl restart nginx
    在网页测试“http://14.0.0.40/index.php”
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    6.测试数据库工作是否正常

    [root@localhost ~]# mysql -u root -p
    Enter password:
    mysql> create database test;
    mysql> grant all on test.* to 'testuser'@'%' identified by 'abc123';
    mysql> grant all on test.* to 'bbsuser'@'localhost' identified by 'abc123';
    mysql> flush privileges;
    将原来的测试网页内容更改如下:
    [root@localhost ~]# vim /usr/local/nginx/html/index.php
    <?php
    $link=mysqli_connect('14.0.0.40','testuser','abc123');
    if($link) echo "<h1>Success!</h1>";
    else echo "Fail!!";
    ?>
    [root@localhost ~]# systemctl restart nginx
    在网页测试“http://14.0.0.40/index.php”
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    7.部署Discuz!社区论坛Web应用

    [root@localhost ~]# cd /opt
    [root@localhost opt]# unzip Discuz_X3.4_SC_UTF8.zip
    [root@localhost opt]# cd dir_SC_UTF8
    [root@localhost dir_SC_UTF8]# ls
    readme  upload  utility
    [root@localhost dir_SC_UTF8]# cp -r upload /usr/local/nginx/html/test
    [root@localhost dir_SC_UTF8]# cd /usr/local/nginx/html/test
    ##赋予安装论坛的文件权限##
    [root@localhost test]# chown -R root:nginx ./config
    [root@localhost test]# chown -R root:nginx ./data
    [root@localhost test]# chown -R root:nginx ./uc_client
    [root@localhost test]# chown -R root:nginx ./uc_server
    [root@localhost test]# chmod -R 777 ./config
    [root@localhost test]# chmod -R 777 ./data
    [root@localhost test]# chmod -R 777 ./uc_client
    [root@localhost test]# chmod -R 777 ./uc_server
    [root@localhost test]# systemctl restart nginx


    8.进入http://14.0.0.40/test/install/index.php来安装论坛
    安装完毕再用http://14.0.0.40/test/index.php登录论坛
    登录http://14.0.0.40/test/admin.php可以直接进管理中心
  • 相关阅读:
    写代码随想
    学生管理系统
    自定义栈
    位运算符加密
    自定义Vector
    二叉树排序
    双向循环链表
    双向链表
    加载properties文件
    通讯录
  • 原文地址:https://www.cnblogs.com/shenyuanhaojie/p/13883141.html
Copyright © 2020-2023  润新知