码迷,mamicode.com
首页 > 其他好文 > 详细

企业级LNMP架构搭建实例(基于Centos6.x)

时间:2019-11-03 23:35:24      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:mysq   端口号   sla   query   上传   iconv   make   wing   $1   

1.1 部署LNMP架构说明

1.1.1 LNMP架构内容

  01.部署linux系统

  02.部署nginx网站服务

  03.部署mysql数据库服务

  04.部署php动态解析服务

1.1.2 配置LNMP架构步骤

  01.配置Nginx配置文件

  02.配置mysql数据库信息(SQL语句)

  03.配置wordpress博客网站

1.1.3 架构服务器串联

  01.数据库数据信息迁移(web服务器上的mysql数据 迁移到10.0.0.51 数据库服务器上)

  02.将本地储存数据挂载到NFS共享储存服务器里(共享储存用户上传的数据信息)

1.1.4 LNMP FastCGI知识说明

    工作原理讲解说明:

         ①. 用户请求的静态文件,由nginx服务自行处理,根据静态的location配置进行处理

             用户请求的动态文件,由php服务进行处理,根据动态的location配置进行处理

         ②. nginx服务接收到动态请求,会将请求抛送给fastcgi,类似于nginx服务接收动态请求的秘书,秘书会将动态请求送给PHP程序

         ③. PHP如果可以处理,会将处理结果直接通过fastcgi返回给nginx程序;如果不可以处理,还会请求后端数据库,最终再把处理结果返回给nginx

第2章 LNMP环境搭建步骤

2.1 部署linux系统

  基本优化(ip地址 yum更新 字符集)

  安全优化完成(iptables关闭  selinux关闭  tmp目录权限777)

        说明:详细配置参见 https://www.cnblogs.com/znix/p/7736899.html

2.2 部署nginx网站服务

2.2.1 检查软件安装的系统环境

[root@web01 ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@web01 ~]# uname -r
2.6.32-696.el6.x86_64

2.2.2 安装nginx的依赖包(pcre-devel openssl-devel)

yum install -y pcre-devel openssl-devel

  pcre:兼容perl语言正则表达式,perl compatible regular expressions

        rewirte模块 参数信息(perl方式定义正则表达式)

      openssl:ssh---openssh/openssl---https

总结:所有安装依赖软件,后面都要加上-devel

2.2.3 下载nginx软件

wget http://nginx.org/download/nginx-1.10.2.tar.gz

   说明:软件很小,用心查看一下

  解压软件

tar xf nginx-1.10.2.tar.gz

2.2.4 创建管理用户 www

useradd -M -s /sbin/nologin www

2.2.5  nginx软件编译安装过程

2.2.5.1  注意

  软件编译安装步骤

    a>软件解压配置(将软件程序安装到哪个目录中 开启nginx软件的哪些功能)

    b>软件编译过程

    c>软件编译安装过程

           注意顺序,顺序不对软件安装会出错

2.2.5.2  编译安装软件

  1、配置软件,在软件的解压目录中

[root@web01 nginx-1.10.2]# ./configure --prefix=/application/nginx-1.10.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

编译参数说明:

--prefix            表示指定软件安装到哪个目录中,指定目录不存在会自动创建

--user/--group        nginx工作进程由哪个用户运行管理

--with-http_stub_status_module    启动nginx状态模块功能(用户访问nginx的网络信息)

--with-http_ssl_module            启动https功能模块

通过软件编译过程中的返回值是否正确,确认配置是否正确

[root@web01 nginx-1.10.2]# echo $?
0

         2、编译软件

[root@web01 nginx-1.10.2]# make

         3、编译安装

[root@web01 nginx-1.10.2]# make install

2.2.6 创建软连接

[root@web01 application]# ln -s /application/nginx-1.10.2/ /application/nginx

2.2.7 精简化nginx.conf 主配置文件内容, 编写nginx配置文件

[root@web01 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf

2.2.8 启动程序

[root@web01 application]# /application/nginx/sbin/nginx
[root@web01 application]#

检查是否启动

[root@web01 application]# ps -ef |grep nginx
root      26548      1  0 20:13 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
www       26549  26548  0 20:13 ?        00:00:00 nginx: worker process        
root      26551  23431  3 20:13 pts/0    00:00:00 grep --color=auto nginx

检查端口信息

[root@web01 application]# netstat -lntup |grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      26548/nginx  

服务部署完成, 修改hosts解析文件,进行浏览器访问测试

 

至此软件安装完毕!

2.3 部署mysql数据库服务

2.3.1 下载mysql软件

这里使用的是5.6.34版本;在下载mysql的时候一定要注意与系统匹配的版本。

mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz

方法一:mysql官网下载地址

 

尽量使用ftp下载,http的下载方式较为繁琐。下载的时候选择与自己近的服务进行下载即可。

 

方法二: 使用搜狐的镜像站也可以进行下载,注意使用的软件版本。

  http://mirrors.sohu.com/mysql/

2.3.2 【二进制包方式】安装mysql数据库软件

2.3.2.1  解压二进制包软件??

cd /server/tools/
[root@web01 tools]# tar xf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz

2.3.2.2  创建储存目录管理用户mysql??

[root@web01 tools]# useradd -s /sbin/nologin -M mysql

2.3.2.3  将解压后的二进制包放置到程序目录中??

将mysql解压后的程序包搬家到程序目录下,并创建软连接。

cd /server/tools/
mv mysql-5.6.34-linux-glibc2.5-x86_64 /application/mysql-5.6.34
ln -s /application/mysql-5.6.34  /application/mysql

2.3.2.4  对mysql数据储存目录进行授权??

让mysql用户管理 /application/mysql/data

[root@web01 ~]# chown -R mysql.mysql /application/mysql/data/
[root@web01 ~]# ll /application/mysql/data/ -d
drwxr-xr-x 3 mysql mysql 4096 Oct 26 11:26 /application/mysql/data/

2.3.2.5  初始化数据库服务??

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql

①始化参数说明:

--basedir  数据库软件命令,软件安装在哪里

--datadir  数据存放目录,数据存放在哪里

--user     管理mysql的用户,MySQL使用的用户谁

②【*判定初始化命令执行成功的方法

  1)确认返回值,看是否为0

     [root@web01 ~]# echo $? 

  2)确认输出的内容中有两个ok

  3)通过数据库初始化操作,在data目录中创建出默认的数据库信息和相关表信息

[root@web01 ~]# ls -l /application/mysql/data/
total 110604
-rw-rw---- 1 mysql mysql 12582912 Oct 26 11:56 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Oct 26 11:56 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Oct 26 11:56 ib_logfile1
drwx------ 2 mysql mysql     4096 Oct 26 11:56 mysql
drwx------ 2 mysql mysql     4096 Oct 26 11:56 performance_schema
drwxr-xr-x 2 mysql mysql     4096 Oct 26 11:26 test

③初始化输出的内容信息

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

  启动mysql服务,可以复制support-files/mysql.server到系统的启动目录中

  mysql.server程序自带的启动脚本文件

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/application/mysql/bin/mysqladmin -u root password ‘new-password‘
/application/mysql/bin/mysqladmin -u root -h web01 password ‘new-password‘

  说明: 表示对mysql服务管理源root用户设置密码

You can start the MySQL daemon with:
cd . ; /application/mysql/bin/mysqld_safe &

  可以以后台方式运行 mysqld_safe 脚本命令,也可以运行mysql服务

2.3.2.6  将启动脚本文件复制到启动目录中??

[root@web01 ~]# cp -a  /application/mysql/support-files/mysql.server  /etc/init.d/mysqld

  修改启动服务脚本相关文件内容--更改软件的存放目录

      注意: 修改的是两个位置

sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /application/mysql/bin/mysqld_safe /etc/init.d/mysqld

 

  添加到开机自启动,让chkconfig 管理,能够开机自启动

[root@web01 ~]# chkconfig --add mysqld
[root@web01 ~]# chkconfig mysqld on

2.3.2.7  设置mysql服务配置文件??

mysql默认配置文件保存位置

/etc/my.cnf

从软件中复制出来配置文件,使用软件中自带的配置文件即可

 \cp /application/mysql/support-files/my-default.cnf /etc/my.cnf

2.3.2.8  启动mysql服务??

[root@web01 ~]# /etc/init.d/mysqld start
Starting MySQL...... SUCCESS!

2.3.2.9  检查端口信息,确认服务是否启动??

[root@web01 ~]# netstat -lntup |grep 3306
tcp    0      0 :::3306               :::*             LISTEN      54042/mysqld    

2.3.2.10     设置root用户密码信息??

[root@web01 ~]# /application/mysql/bin/mysqladmin -u root password ‘clsn123‘
Warning: Using a password on the command line interface can be insecure.

2.3.2.11     测试

[root@web01 ~]# /application/mysql/bin/mysql -uroot -pclsn123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.6.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql>

登录数据库命令简化方法

echo ‘export PATH=/application/mysql/bin:$PATH‘ >>/etc/profile
source /etc/profile
which mysql

2.3.3 管理mysql数据库

2.3.3.1  查看数据库

mysql> show databases;
+--------------------+
| Database             |
+--------------------+
| information_schema |
| mysql                 |
| performance_schema |
| test                  |
+--------------------+
4 rows in set (0.26 sec)

2.3.3.2  查看数据表信息

mysql> use  mysql;show tables;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
+---------------------------+
| Tables_in_mysql             |
+---------------------------+
| columns_priv                 |
| db                             |
| event                         |
| func                          |
| general_log                  |
| help_category                |
| help_keyword                 |
| help_relation                |
| help_topic                   |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

2.3.3.3  退出数据库

   quit | exit 

退出数据库时,尽量不要用ctrl+c进行退出mysql 用ctrl+d进行退出

数据库基础操作(数据库框架)

show databases;                <--- 查询默认的数据库信息
create database clsn;        <---创建新的数据库
drop database clsn;          <---删除存在的数据库
use mysql;                     <--- 表示选择使用一个数据库,相当于cd进入一个数据库
show tables;                  <---查看数据库中表信息
select database();             <--- 表示查看当前所在数据库,类似于pwd命令的功能
select user();                 <--- 查看当前登录数据库的用户,类似于whoami命令
                                    并且mysql还可以限制指定用户可以从哪里进行连接登录数据库
select * from user\G;          <---查看user表中所有信息,并且纵行显示

select user,host from user;         ---查看user表中指定信息,并且横行显示
select user,host from mysql.user;   ---查看可以登录mysql数据库的目录,以及都可以从哪里进行管理mysql数据库
grant all on *.* to user@‘host‘ identified by ‘clsn123‘;           ---创建用户 
grant all on *.* to Old_Boy@‘localhost‘ identified by ‘clsn123‘;   ---创建用户(大写用户)
drop user ‘user‘@‘host‘;
flush privileges;                  --- 刷新权限

2.4 部署php服务

2.4.1 解决PHP软件的依赖关系(14个依赖包)

2.4.1.1  基于base源的个依赖包

yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel -y

检查的方法一:rpm

rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel

检查的方法二:再安装一遍即可确认是否都安装上

yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel

2.4.1.2  libiconv软件 和字符集转换相关软件

  由于该软件yum安装不上,需要单独安装一下。

mkdir -p /server/tools
cd /server/tools
#wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make
make install

                  说明:此软件在centos6.8之后已经自带此软件功能,可以不进行安装 

编译好的软件如何删除

      删除安装后的程序目录即可

fpm 定制rpm

   rpm包制作软件---把编译后的程序目录进行打包,通过fpm相关参数指定rpm解压之前要先安装哪些依赖

2.4.1.3  安装加密相关的依赖软件(3个)

    这三个软件依赖与epel源

yum -y install libmcrypt-devel mhash mcrypt
rpm -qa libmcrypt-devel mhash mcrypt

2.4.2 编译安装php过程

解压安装包

cd /server/tools/
[root@web01 lnmp]# tar xf php-5.5.32.tar.gz 

配置php (配置的参数较多)

  mysqlnd本地没有mysql

./configure --prefix=/application/php-5.5.32 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --enable-short-tags --enable-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp --enable-opcache=no

PHP编译参数详解

 View Code PHP编译参数详解

      输出的信息

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

防错 

ln -s /application/mysql/lib/libmysqlclient.so.18  /usr/lib64/
touch ext/phar/phar.phar

编译 && 编译安装

make && make install

2.4.3 PHP软件程序创建软链接

ln -s /application/php-5.5.32/ /application/php

2.4.4 配置php解析文件/配置php-fpm配置文件

两个默认的配置文件区别

cd /server/tools/php-5.5.32
ll php.ini*
-rw-r--r--. 1 1001 1001 69236 2016-02-02 21:33 php.ini-development
-rw-r--r--. 1 1001 1001 69266 2016-02-02 21:33 php.ini-production

配置文件说明:

    php.ini-developments是开发人员调试用配置文件

    php.ini-production是生产常见所有配置文件

文件区别对比:

    生产的文件不会输出过多的日志信息,而开发文件会输出大量程序测试日志信息。

对比俩个文件不同的命令

diff / vimdiff

复制配置文件(2个)

# 创建软连接 : ln -sf /application/php-5.5.32 /application/php
[root@web01 ~]#cd /server/tools/php-5.5.32
[root@web01 php-5.5.32]# cp php.ini-production /application/php/lib/php.ini
[root@web01 etc]# cd /application/php/etc/
[root@web01 etc]# cp php-fpm.conf.default php-fpm.conf

2.4.5 启动php-fpm程序

[root@web01 ~]# /application/php/sbin/php-fpm 

  确认php 9000端口是否正确启动(检查服务是否启动)

[root@web01 ~]# netstat -lntup |grep 9000
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN  

2.5 nginx 与 php 建立连接关系

2.5.1 修改nginx配置文件,使nginx程序与php程序建立联系

vim extra/blog.conf
server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
                    root   html/blog;
                    index  index.php index.html index.htm;       
        }
        location ~* .*\.(php|php5)?$ {
                    root html/blog;
                    fastcgi_pass  127.0.0.1:9000;
                    fastcgi_index index.php;
                    include fastcgi.conf;
        }
}

说明:利用nginx的location区块实现动态请求与静态请求的分别处理

       <-- 需要注意编辑修改默认首页文件  index  index.php index.html index.htm;

   让nginx服务具有动态请求解析功能。

2.5.2 重启服务

[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload

2.5.3 编辑nginx与php连通性测试文件,并进行测试

测试动态请求是否可以处理:

echo ‘<?php phpinfo(); ?>‘                    >/application/nginx/html/blog/test_info.php

测试站点

        curl  http://blog.etiantian.org/index.html            <-- 静态请求站点文件信息测试    

        curl  http://blog.etiantian.org/test_info.php         <-- 动态请求站点文件信息测试

说明:当php服务停止时,9000端口信息消失,即停止PHP错误报502错误

            linux系统测试完毕后,建议利用浏览器进行最终测试,测试效果更明显些

2.5.4 浏览器测试

浏览器访问

http://blog.znix.top/test_info.php

 

2.6 编辑php与mysql连通性测试文件,并进行测试

2.6.1 创建数据库

mysql -uroot -pclsn123;
show databases;                      <--- 查看当前数据库信息
create database wordpress;            <---创建博客储存数据库    

2.6.2 在mysql中添加用户信息

创建数据库授权用户

grant all on wordpress.* to ‘wordpress‘@‘10.0.0.%‘ identified by ‘clsn123‘;
flush privileges;

  授权 所有权限 为 wordpress库的所有表   用户@地址 设置密码  ;   

  刷新数据库

添加上用于blog使用的mysql用户

drop user wordpress@‘172.16.1.8‘;    <--- 删除用户信息
select user,host from mysql.user;    <--- 查看用户信息
mysql -uwordpress -p123456           <--- 测试创建的用户连接
show databases;                      <--- 查看当前数据库信息

2.7 测试php与数据库连通性

vim test_mysql.php
<?php
//$link_id=mysql_connect(‘主机名‘,‘用户‘,‘密码‘);
//mysql -u用户 -p密码 -h 主机
$link_id=mysql_connect(‘localhost‘,‘wordpress‘,‘clsn123‘) or mysql_error();
if($link_id){
             echo "mysql successful by clsn !\n";
            }else{
             echo mysql_error();
            }
?>

2.7.1 网站访问测试

测试动态请求访问nginx服务是否可以到达数据库

 

2.8 下载部署wordpress博客程序

  下载地址:https://cn.wordpress.org

2.8.1 解压出来

tar xf wordpress-4.7.3-zh_CN.tar.gz  

2.8.2 代码上线

[root@web01 wordpress]# pwd
/server/tools/lnmp/wordpress
[root@web01 wordpress]# mv ./* /application/nginx/html/blog/

2.8.3 统一代码属主.属组

对站点目录进行 授权

[root@web01 wordpress]# cd /application/nginx/html/blog/
[root@web01 blog]# chown www.www -R /application/nginx/html/blog/

[root@web01 blog]# ll
total 200
-rw-r--r--  1 www www    11 Oct 25 09:20 index.html
-rw-r--r--  1 www www   418 Sep 25  2013 index.php
……

         说明:wp-config.php文件创建需要能够有权限对目录操作。

                          此文件定义数据库连接信息

2.8.4 创建数据库

mysql -uroot -pclsn123;
show databases;              
create database wordpress;    

2.8.5 添加wordpress数据库用户

mysql> grant all on wordpress.* to ‘wordpress‘@‘10.0.0.%‘ identified by ‘clsn123‘; 
Query OK, 0 rows affected (0.16 sec)

mysql> select user,host from mysql.user;
+-----------+-----------+
| user      | host      |
+-----------+-----------+
| wordpress | 10.0.0.%  |
| root      | 127.0.0.1 |
| root      | ::1       |
|           | localhost |
| root      | localhost |
|           | web01     |
| root      | web01     |
+-----------+-----------+
7 rows in set (0.00 sec)

2.8.6 安装wordpress

访问网站进行初始化操作

           填写的数据为

 

连接数据库配置说明

数据库名:指定数据存储到哪一个数据库当中,例如:存储到wordpress数据库中

用户名:以什么用户身份管理wordpress数据库

密码: 用户的密码

数据库主机: 指定连接的数据库服务器地址信息

表前缀:标识相应表属于哪一个数据库

说明:配置完数据连接信息后,会自动创建wp-config.php文件,此文件定义数据库连接配置信息

安装完成效果

 

第3章 mysql数据/储存数据迁移

3.1 mysql数据库迁移

说明:

    以上的mysql配置都是在web01 上进行 ,现在需要将web01上的mysql数据进行迁移到db01(数据库服务器)上去。

3.1.1 备份数据库中的数据

[root@db01 ~]# mysqldump -uroot -pclsn123 --all-databases >/tmp/bak.sql

使用mysqldump命令将数据库中的全部数据进行备份 备份到 /tmp/bak.sql 。

mysqldump 命令参数说明:

参数

参数说明

--add-drop-table

在每个创建数据库表语句前添加删除数据库表的语句;

--add-locks

备份数据库表时锁定数据库表;

--all-databases

备份MySQL服务器上的所有数据库;

--comments

添加注释信息;

--compact

压缩模式,产生更少的输出;

--complete-insert

输出完成的插入语句;

--databases

指定要备份的数据库;

--default-character-set

指定默认字符集;

--force

当出现错误时仍然继续备份操作;

--host

指定要备份数据库的服务器;

--lock-tables 

备份前,锁定所有数据库表;

--no-create-db

禁止生成创建数据库语句;

--no-create-info

禁止生成创建数据库库表语句;

--password  

连接MySQL服务器的密码;

--port    

MySQL服务器的端口号;

--user   

连接MySQL服务器的用户名。

3.1.2 将备份数据传输到mysql服务器(db01)

[root@web01 tools]# rsync  -avz /tmp/bak.sql  172.16.1.51:/tmp/
The authenticity of host ‘172.16.1.51 (172.16.1.51)‘ can‘t be established.
RSA key fingerprint is d3:41:bb:0d:43:88:da:a3:2c:e8:36:91:11:c9:e4:9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.16.1.51‘ (RSA) to the list of known hosts.
root@172.16.1.51‘s password: 
sending incremental file list
bak.sql

sent 377261 bytes  received 31 bytes  83842.67 bytes/sec
total size is 1483738  speedup is 3.93

         使用rsync将数据推送到MySQL服务器的/tmp 目录下面。

3.1.3 数据库服务器部署mysql服务(快速部署命令集)

mysql服务快速部署过程脚本。详情参见mysql数据库部署安装。

cd /server/tools
tar xf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz
useradd -s /sbin/nologin  -M mysql
mkdir -p /application/
mv /server/tools/mysql-5.6.34-linux-glibc2.5-x86_64 /application/mysql-5.6.34
ln -s /application/mysql-5.6.34/ /application/mysql
chown -R mysql.mysql /application/mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
cp /application/mysql/support-files/mysql.server  /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld 
sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /application/mysql/bin/mysqld_safe /etc/init.d/mysqld
\cp /application/mysql/support-files/my-default.cnf /etc/my.cnf 
/etc/init.d/mysqld start
/application/mysql/bin/mysqladmin -u root password ‘clsn123‘

3.1.4 将备份的数据恢复到数据库服务器上

[root@db01 ~]# /application/mysql/bin/mysql -uroot -pclsn123 </tmp/bak.sql 
Warning: Using a password on the command line interface can be insecure.

注意,数据库导入之后要刷新数据库,让导入的数据被识别(重要)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3.1.5 在web01服务器上进行远程登陆数据库测试

[root@web01 ~]# /application/mysql/bin/mysql -u wordpress -pclsn123  -h 10.0.0.51
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> 
mysql> show databases;
+--------------------+
| Database             |
+--------------------+
| information_schema |
| test                  |
| wordpress            |
+--------------------+
3 rows in set (0.00 sec)

3.1.6 修改web服务器php连接数据库主机的配置文件

修改wordpress软件的配置,将连接的主机地址改为数据库服务器的地址

[root@web01 ~]# vim /application/nginx/html/blog/wp-config.php

……

/** MySQL主机 */

define(‘DB_HOST‘, ‘10.0.0.51‘);

……

3.2 本地数据挂载到nfs共享储存

3.2.1 确认本地数据的储存位置(三种方法)

01.通过网页图片属性信息进行确认路径

  http://blog.clsn.top/wp-content/uploads/2017/10/cropped-Frog-2.png

02.通过find查看数据储存路径信息,上传个图片,查找相同中1分钟以内的文件

find -type f -mmin -1

03.通过inotify软件进行监控

  确认文件的储存目录

/application/nginx/html/blog/wp-content/uploads

3.2.2 将已有数据进行迁移备份

备份数据是因为挂载的时候会将当前的数据全部‘覆盖‘掉,只显示nfs共享目录的信息。

[root@web01 uploads]# pwd
/application/nginx/html/blog/wp-content/uploads
[root@web01 uploads]# mkdir /tmp/wordpress_bak
[root@web01 uploads]# mv ./*  /tmp/wordpress_bak/

3.2.3 nfs储存服务配置

配置nfs服务的时候注意权限的设置

[root@nfs01 data]# cat /etc/exports 
#share user:hzs
/data 172.16.1.0/24(rw,sync,root_squash,no_all_squash,anonuid=501,anongid=501)

注意:

  anonuid 与 anongid 要和web服务器上的www用户的相同(UID与GID相同)

[root@nfs01 /]# id www
uid=501(www) gid=501(www) groups=501(www)

目录的属组要是与nfs配置的anonuid,anongid相同的用户。

[root@nfs01 /]# ll /data/ -d
drwxr-xr-x 3 www www 4096 Oct 27 12:11 /data/

NFS的配置详情参见: NFS存储服务部署一篇。

3.2.4 将储存目录挂载到nfs共享目录上

注:作为nfs客户端需要安装nfs-utils 和 rpcbind

①先检查是否能挂载,显示可以挂载的目录

[root@web01 uploads]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24

②将磁盘进行挂载

[root@web01 uploads]# mount -t nfs 172.16.1.31:/data  /application/nginx/html/blog/wp-content/uploads/

③显示磁盘信息

[root@web01 uploads]# df -h
Filesystem         Size  Used Avail Use% Mounted on
/dev/sda3           19G  3.7G   15G  21% /
tmpfs              238M     0  238M   0% /dev/shm
/dev/sda1          190M   40M  141M  22% /boot
172.16.1.31:/data   19G  1.5G   17G   9% /application/nginx-1.10.2/html/blog/wp-content/uploads

3.2.5 恢复数据(将之前备份的数据还原回来)

[root@web01 uploads]# pwd
application/nginx-1.10.2/html/blog/wp-content/uploads
[root@web01 uploads]# mv /tmp/wordpress_bak/* ./

3.2.6 命令补全功能

yum install bash-completion -y

 3.3各服务的启动脚本

 3.3.1php启动脚本

# 复制php启动脚本
[root@clsn ~]# cp /server/tools/php-5.5.32/sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm 
[root@clsn ~]# chmod +x  /etc/init.d/php-fpm
# 找到pid文件,开启它
[root@clsn ~]# vim /application/php/etc/php-fpm.conf
# ···
[global]
; Pid file
; Note: the default prefix is /application/php-5.5.32/var
; Default Value: none
pid = run/php-fpm.pid
# ···
# 启动php
[root@clsn ~]# /etc/init.d/php-fpm status
php-fpm (pid 27931) is running...

3.3.2NGINX管理脚本

[root@clsn ~]# cat /etc/init.d/nginx 
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse #               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:         /application/nginx/conf/nginx.conf
# config:      /application/nginx/sbin/nginx 
# pidfile:     
# by:  http://www.nmtui.com

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/application/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/application/nginx/conf/nginx.conf"

#[ -f /application/nginx/sbin/nginx ] && . /application/nginx/sbin/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
         useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V 2>&1 | grep ‘configure arguments:‘`
      for opt in $options; do
          if [ `echo $opt | grep ‘.*-temp-path‘` ]; then
              value=`echo $opt | cut -d "=" -f 2`
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value
              fi
          fi
       done
    fi
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

企业级LNMP架构搭建实例(基于Centos6.x)

标签:mysq   端口号   sla   query   上传   iconv   make   wing   $1   

原文地址:https://www.cnblogs.com/bidad/p/11789825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!