阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

基于CentOS 6.X系列的LNMP环境搭建实例

141次阅读
没有评论

共计 20719 个字符,预计需要花费 52 分钟才能阅读完成。

回到顶部

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)

    说明:详细配置参见 http://www.linuxidc.com/Linux/2017-12/149494.htm

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 官网下载地址

https://dev.mysql.com/downloads/mirrors/ 

尽量使用 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 编译参数详解

1 ./configure 编译参数
 2 
 3 –prefix=/application/php5.3.27 指定 php 的安装路径为 /application/php5.3.27
 4 
 5 –with-mysql=/application/mysql/ 
 6 需要指定 mysql 的安装路径, 安装 PHP 需要的 MySQL 相关内容。当然如果没有 MySQL 软件包,也可以不单独安装,这样的情况可使用–with-mysql=mysqlnd 替代–with-mysql=/application/mysql, 因为 PHP 软件里面已经自带连接 MySQL 的客户端工具。7 
 8 –with-iconv-dir=/usr/local/libiconv    libiconv 库, 各种字符集间的转换
 9 
10 –with-freetype-dir    打开对 freetype 字体库支持
11 
12 –with-jpeg-dir 打开对 jpeg 图片的支持
13 
14 –with-png-dir 打开对 png 图片的支持
15 
16 –with-zlib 打开 zlib 库的支持, 用于 http 压缩传输
17 
18 –with-libxml-dir=/usr 打开 libxml2 库的支持
19 
20 –enable-xml    
21 
22 –disable-rpath 关闭额外的运行库文件
23 
24 –enable-safe-mode 打开安全模式
25 
26 –enable-bcmath 打开图片大小调整, 用 zabbix 监控时会用到该模块
27 
28 –enable-shmop 
29 
30 –enable-sysvsem 使用 sysv 信号机制, 则打开此选项
31 
32 –enable-inline-optimization 优化线程
33 
34 –with-curl 打开 curl 浏览工具的支持
35 
36 –with-curlwrappers 运维 curl 工具打开 url 流
37 
38 –enable-mbregex     
39 
40 –enable-mbstring 支持 mbstring
41 
42 –with-mcrypt 编码函数库
43 
44 –with-gd 打开 gd 库的支持
45 
46 –enable-gd-native-ttf 支持 TrueType 字符串函数库
47 
48 –with-openl openl 的支持, 加密传输时用到
49 
50 –with-mhash mhash 算法的扩展
51 
52 –enable-pcntl freeTDS 需要用到, 可能是链接 mql
53 
54 –enable-sockets 打开 sockets 支持
55 
56 –with-xmlrpc 打开 xml-rpc 的 c 语言
57 
58 –enable-zip 打开对 zip 的支持
59 
60 –enable-soap soap 模块的扩展
61 
62 –enable-short-tags 开始和标记函数
63 
64 –enable-zend-multibyte 支持 zend 的多字节
65 
66 –enable-static 生成静态链接库
67 
68 –with-xsl 打开 XSLT 文件支持, 扩展 libXML2 库, 需要 libxslt 软件
69 
70 –enable-ftp    打开 ftp 的支持
71 
72 –enable-fpm    表示激活 PHP-FPM 方式服务, 即 FactCGI 方式运行 PHP 服务。73 
74 –with-fpm-user=www    指定 PHP-FPM 进程管理的用户为 www, 此处最好和 Nginx 服务用户统一。75 
76 –with-fpm-group=www    指定 PHP-FPM 进程管理用户组为 www, 此处最好和 Nginx 服务用户组统一。

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://www.linuxidc.com/index.html            <-- 静态请求站点文件信息测试    

        curl  http://www.linuxidc.com/test_info.php         <-- 动态请求站点文件信息测试

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

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

2.5.4 浏览器测试

浏览器访问

http://www.linuxidc.com/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

LNMP 环境搭建(Discuz 论坛)  http://www.linuxidc.com/Linux/2016-03/129334.htm 

Ubuntu 14.04 下 apt-get 方法安装 LNMP 环境  http://www.linuxidc.com/Linux/2016-07/133683.htm 

CentOS 7 源码编译安装 PHP5.6 和 Nginx1.7.9 及 MySQL(搭建 LNMP 环境) http://www.linuxidc.com/Linux/2015-12/126200.htm 

Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL  http://www.linuxidc.com/Linux/2014-05/102351.htm 

CentOS 6.8 编译安装 LNMP 简述  http://www.linuxidc.com/Linux/2017-05/143667.htm 

Ubuntu 16.04 下源码配置 LNMP 开发环境 http://www.linuxidc.com/Linux/2016-09/135381.htm 

CentOS 7 源码编译安装 PHP5.6 和 Nginx1.7.9 及 MySQL(搭建 LNMP 环境) http://www.linuxidc.com/Linux/2015-12/126200.htm 

CentOS 7 源码安装最新版 LNMP 环境 http://www.linuxidc.com/Linux/2015-04/116058.htm 

CentOS 6.8 安装 LNMP 环境(Linux+Nginx+MySQL+PHP)http://www.linuxidc.com/Linux/2017-04/142880.htm 

Ubuntu 系统下 LNMP 环境的搭建  http://www.linuxidc.com/Linux/2017-04/142610.htm 

编译 LNMP 之 Nginx+php-fpm  http://www.linuxidc.com/Linux/2017-10/147535.htm 

Ubuntu 16.04 LTS 下 LNMP 环境配置简述  http://www.linuxidc.com/Linux/2017-05/144252.htm 

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-12/149492.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计20719字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中