ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

分布式部署LNMP+WordPress

2022-01-21 18:58:51  阅读:175  来源: 互联网

标签:MariaDB -- LNMP nginx WordPress php root 节点 分布式


一.MYSQL主从配置

1. 基础环境安装

(1)修改主机名

使用远程连接工具CRT连接到192.168.200.30、192.168.200.40这两台虚拟机,并对这两台虚拟机进行修改主机名的操作,192.168.200.30主机名修改为mysql1,192.168.200.40主机名修改为mysql2。命令如下:

mysql1节点:

[root@localhost ~]#

[root@localhost ~]# hostnamectl set-hostname mysql1

[root@localhost ~]# bash

[root@mysql1 ~]# hostnamectl

     Static hostname: mysql1

           Icon name: computer-vm

            Chassis: vm

          Machine ID: ee0b84dcaf7b4238828a5e8b5034f13a

            Boot ID: 0ba766491a56409980ed3a0f891f1596

     Virtualization: vmware

   Operating System: CentOS Linux 7 (Core)

        CPE OS Name: cpe:/o:centos:centos:7

             Kernel: Linux 3.10.0-327.el7.x86_64

       Architecture: x86-64

mysql2节点:

[root@localhost ~]# hostnamectl set-hostname mysql2

[root@localhost ~]# bash

[root@mysql2 ~]# hostnamectl

    Static hostname: mysql2

          Icon name: computer-vm

            Chassis: vm

         Machine ID: ee0b84dcaf7b4238828a5e8b5034f13a

            Boot ID: 628055e67e634e259d2dc86b9b913e14

     Virtualization: vmware

   Operating System: CentOS Linux 7 (Core)

        CPE OS Name: cpe:/o:centos:centos:7

             Kernel: Linux 3.10.0-327.el7.x86_64

       Architecture: x86-64

(2)关闭防火墙及SELinux服务

两个节点关闭防火墙firewalld及SELinux服务,命令如下:

[root@mysql2 ~]# setenforce 0

[root@mysql2 ~]# systemctl stop firewalld

(3)配置hosts文件

两个节点配置/etc/hosts文件,修改为如下:

[root@mysql2 ~]# vi /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.131.30 mysql1

192.168.131.40 mysql2

(4)配置YUM源并安装数据库服务

使用项目3-Linux系统与服务构建运维中的方法,挂载CentOS-7-x86_64-DVD-1511.iso镜像并自行配置YUM源,配置完毕后,两个节点安装数据库服务,命令如下:

[root@mysql2 ~]# yum install -y mariadb mariadb-server

两个节点启动数据库服务并设置开机自启,命令如下:

[root@mysql2 ~]# systemctl start mariadb

[root@mysql2 ~]# systemctl enable mariadb

Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

2. 初始化数据库并配置主从服务

(1)初始化数据库

两个节点初始化数据库,配置数据库root密码为000000,命令如下:       

[root@mysql1 ~]# mysql_secure_installation

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

       SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current

password for the root user.  If you've just installed MariaDB, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.

Enter current password for root (enter for none):

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

Set root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

Remove anonymous users? [Y/n] y

  ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n

 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.

Remove test database and access to it? [Y/n] y

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

Reload privilege tables now? [Y/n] y

 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

Thanks for using MariaDB!

(2)配置mysql1主节点

修改mysql1节点的数据库配置文件,在配置文件/etc/my.cnf中的[mysqld]增添如下内容。

[root@mysql1 ~]# vi /etc/my.cnf

[mysqld]

log_bin = mysql-bin       #记录操作日志

binlog_ignore_db = mysql          #不同步mysql系统数据库

server_id = 30            #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.30,server_id就写30

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

重启数据库服务,并进入数据库,命令如下:

[root@mysql1 ~]# systemctl restart mariadb

[root@mysql1 ~]# mysql -uroot -p000000

Welcome to the MariaDB monitor.  Commands end with ; or \g.

在mysql1节点,授权在任何客户端机器上可以以root用户登录到数据库,然后在主节点上创建一个user用户连接节点mysql2,并赋予从节点同步主节点数据库的权限。命令如下:

MariaDB [(none)]>  grant all privileges  on *.* to root@'%' identified by "000000";

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>  grant replication slave on *.* to 'user'@'mysql2' identified by '000000';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> Ctrl-C -- exit!

Aborted

(3)配置mysql2从节点

修改mysql2节点的数据库配置文件,在配置文件/etc/my.cnf中的[mysqld]增添如下内容。

[root@mysql1 ~]# vi /etc/my.cnf

[mysqld]

log_bin = mysql-bin       #记录操作日志

binlog_ignore_db = mysql          #不同步mysql系统数据库

server_id = 40            #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.30,server_id就写30

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

在从节点mysql2上登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名mysql1,master_user为上一步中创建的用户user,命令如下:

[root@mysql2 ~]# systemctl restart mariadb

[root@mysql2 ~]# mysql -uroot -p000000

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 2

Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> change master to master_host='mysql1',master_user='user',master_password='000000';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>

配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status\G命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。命令如下:

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G

    

可以看到Slave_IO_Running和Slave_SQL_Running的状态都是Yes,配置数据库主从集群成功。

3. 验证数据库主从服务

(1)主节点创建数据库

先在主节点mysql1中创建库test,并在库test中创建表company,插入表数据,创建完成后,查看表company数据,命令如下:

MariaDB [(none)]> use test;

Database changed

MariaDB [test]>  create table company(id int not null primary key,name varchar(50),addr varchar(255));

Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> insert into company values(1,"alibaba","china");

Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select * from company;

(2)从节点验证复制功能

登录mysql2节点的数据库,查看数据库列表。找到test数据库,查询表,并查询内容验证从数据库的复制功能,命令如下:

MariaDB [(none)]> show databases;

MariaDB [(none)]> use test;

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

MariaDB [test]> show tables;

MariaDB [test]>  select * from company;

可以查看到主数据库中刚刚创建的库、表、信息,验证从数据库的复制功能成功。

二.部署Nginx服务

1. 基础环境安装

(1)修改主机名

使用远程连接工具CRT连接到192.168.200.50虚拟机,并进行修改主机名的操作,将192.168.200.50主机名修改为nginx。命令如下:

[root@localhost ~]# hostnamectl set-hostname nginx

[root@localhost ~]# bash

[root@nginx ~]# hostnamectl

(2)关闭防火墙及SELinux服务

关闭防火墙firewalld及SELinux服务,命令如下:

[root@nginx ~]# setenforce 0

[root@nginx ~]# systemctl stop firewalld

(3)安装配置基础服务

使用CentOS-7-x86_64-DVD-1511.iso文件自行配置本地YUM源,编译安装基础环境,命令如下:

[root@nginx ~]# yum install gcc gcc-c++ openssl-devel zlib-devel zlib pcre-devel -y

创建指定用户,这个nginx用户要和PHP服务器上创建的nginx两者id一致,这里先创建用户,命令如下:

[root@nginx ~]# groupadd -g 1001 nginx  

[root@nginx ~]# useradd -u 900 nginx -g nginx -s /sbin/nologin  

[root@nginx ~]# tail -1 /etc/passwd

nginx:x:900:1001::/home/nginx:/sbin/nologin(4)安装配置Nginx服务

使用远程传输工具,将提供的nginx-1.12.2.tar.gz压缩包上传至nginx节点的/usr/local/src/目录下,并解压到当前目录,命令如下:

[root@nginx src]# tar -zxvf nginx-1.12.2.tar.gz

进入nginx-1.12.2目录,编译并安装,命令如下:

[root@nginx src]# cd nginx-1.12.2/

[root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_dav_module \

> --with-http_stub_status_module --with-http_addition_module \

> --with-http_sub_module --with-http_flv_module --with-http_mp4_module \

> --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx

如果没有报错提示,请进行下一步安装,命令如下:

[root@nginx nginx-1.12.2]# make -j4 && make install

编译安装完毕后,创建软连接并启动测试,命令如下:(netstat命令无法使用时,请自行使用YUM源安装net-tools工具)

[root@nginx nginx-1.12.2]#  ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  

[root@nginx nginx-1.12.2]# nginx -t

[r [root@nginx nginx-1.12.2]# nginx

[root@nginx nginx-1.12.2]# netstat -ntpl

如果发现80端口启动,则表示Nginx服务启动成功。可以在浏览器访问地址192.168.200.50来查看是否出现Nginx的欢迎页面。

三.安装PHP环境

基础环境安装

(1)修改主机名

使用远程连接工具CRT连接到192.168.200.60虚拟机,并进行修改主机名的操作,将192.168.200.60主机名修改为php。命令如下:

[root@localhost ~]# hostnamectl set-hostname php

[root@localhost ~]# bash

[root@php ~]# hostnamectl

(2)关闭防火墙及SELinux服务

关闭防火墙firewalld及SELinux服务,命令如下:

[root@php ~]# setenforce 0

[root@php ~]# systemctl stop firewalld

(3)安装配置基础服务

使用CentOS-7-x86_64-DVD-1511.iso文件自行配置本地YUM源,编译安装基础环境,命令如下:

[root@php ~]# yum -y install gcc gcc-c++ libxml2-devel libcurl-devel openssl-devel bzip2-devel

使用远程传输工具,将提供的libmcrypt-2.5.8.tar.gz压缩包上传至php节点的/usr/local/src目录下,解压该压缩包,进入解压后目录,编译安装该服务,命令如下:

[root@php ~]# cd /usr/local/src/

[root@php src]# tar -zxvf libmcrypt-2.5.8.tar.gz

[root@php libmcrypt-2.5.8]# cd libmcrypt-2.5.8/

[root@php libmcrypt-2.5.8]# ./configure --prefix=/usr/local/libmcrypt && make && make install

(4)安装PHP环境

使用远程传输工具,将提供的php-5.6.27.tar.gz压缩包上传至php节点的/usr/local/src目录下,解压该压缩包,进入解压后的目录,编译安装PHP服务,命令如下:

[root@php src]#  tar -zxvf php-5.6.27.tar.gz

[root@php php-5.6.27]# cd php-5.6.27/

[root@php ~]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd \

> --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm \

> --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir \

> --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash \

> --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc \

> --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts

如果没有报错提示,则进行下一步安装,命令如下:

[root@php php-5.6.27]# make -j4 && make install

在等待10分钟左右的时间,编译安装完毕。

(5)创建用户ID

创建用户ID,注意这个nginx的id号要和nginx主机(192.168.200.50)上的保持一致。命令如下:

[root@php php-5.6.27]# groupadd -g 1001 nginx  

[root@php php-5.6.27]# useradd -u 900 nginx -g nginx -s /sbin/nologin

[root@php php-5.6.27]# tail -1 /etc/passwd  

nginx:x:900:1001::/home/nginx:/sbin/nologin

(6)配置PHP环境

PHP压缩包中提供了PHP环境需要用到的模板文件,需要对文件进行改名后才能使用,复制文件并改名,命令如下:  

[root@php php-5.6.27]# cp php.ini-production /etc/php.ini

[root@php php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

赋予文件执行权限,命令如下:

[root@php php-5.6.27]# chmod +x /etc/init.d/php-fpm

添加PHP服务到启动列表,并设置开机启动,命令如下:

[root@php php-5.6.27]# chkconfig --add php-fpm

[root@php php-5.6.27]# chkconfig php-fpm on

修改PHP的主配置文件php-fpm.conf,命令如下:

[root@php php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf

[root@php php-5.6.27]# vi /usr/local/php5.6/etc/php-fpm.conf

[root@php php-5.6.27]# grep -n '^'[a-Z] /usr/local/php5.6/etc/php-fpm.conf

找到配置文件中的相应参数并修改,修改成上述配置。

(7)启动PHP服务

在完成上述配置并保存退出之后,就可以启动PHP服务,并检查是否启动成功。命令如下:(netstat命令无法使用时,请自行使用YUM源安装net-tools工具)

[root@php php-5.6.27]# service php-fpm start

[root@php php-5.6.27]# netstat -ntpl   

   

如果发现9000端口已启动,则说明PHP环境安装完毕。

分布式LNMP环境的调试

(1)配置Nginx服务支持PHP环境

使用远程连接工具CRT连接到192.168.200.50虚拟机(nginx节点),并进行修改配置文件的操作。命令如下:

[root@nginx ~]# vi /usr/local/nginx/conf/nginx.conf

location / {

             root   /www;

             index  index,php index.html index.htm;

         }

         #error_page  404              /404.html;

         # redirect server error pages to the static page /50x.html

         #

         error_page   500 502 503 504  /50x.html;

         location = /50x.html {

             root   html;

         }

         # proxy the PHP scripts to Apache listening on 127.0.0.1:80

         #

         #location ~ \.php$ {

         #    proxy_pass   http://127.0.0.1;

         #}

         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

         #

         location ~ \.php$ {

             root           /www;

             fastcgi_pass   192.168.131.60:9000;

             fastcgi_index  index.php;

             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

             include        fastcgi_params;

         }

修改完毕后,保存退出。

接着在/usr/local/nginx/conf/fastcgi_params添加配置,命令如下:

[root@nginx ~]# vi /usr/local/nginx/conf/fastcgi_params

fastcgi_param  QUERY_STRING       $query_string;

fastcgi_param  REQUEST_METHOD     $request_method;

fastcgi_param  CONTENT_TYPE       $content_type;

fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param  REQUEST_URI        $request_uri;

(2)创建目录

在nginx和php节点,创建/www目录,并修改用户和用户组,命令如下:

nginx节点:

[root@nginx ~]# mkdir /www  

[root@nginx ~]# chown nginx:nginx /www/

php节点:

[root@php ~]# mkdir /www

[root@php ~]# chown nginx:nginx /www/

(3)部署WordPress

两个节点,使用远程传输工具,将提供的wordpress-4.7.3-zh_CN.zip压缩包上传至nginx节点和php节点的/root目录下并解压,将解压后的文件复制到/www目录,命令如下:(unzip命令不能使用时,请自行使用YUM源安装unzip工具)

nginx节点:

[root@nginx ~]# unzip wordpress-4.7.3-zh_CN.zip

[root@nginx ~]# mv wordpress/* /www/

php节点:

[root@php ~]# unzip wordpress-4.7.3-zh_CN.zip

[root@php ~]# mv wordpress/* /www/

在nginx节点,修改WordPress应用的配置文件,WordPress应用提供了wp-config-sample.php模版文件,将模板文件复制为wp-config.php,并修改,命令如下:

[root@nginx ~]# cp /www/wp-config-sample.php /www/wp-config.php

[root@nginx ~]# vi /www/wp-config.php

// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //

/** WordPress数据库的名称 */

define('DB_NAME', 'wordpress');

/** MySQL数据库用户名 */

define('DB_USER', 'root');

/** MySQL数据库密码 */

define('DB_PASSWORD', '000000');

/** MySQL主机 */

define('DB_HOST', '192.168.131.30');按照上述文件修改配置文件,保存退出后,将该配置文件scp至php节点的/www目录下,命令如下:

[root@nginx ~]# scp /www/wp-config.php root@192.168.131.60:/www/

(4)创建WordPress数据库

在mysql1节点,登录数据库,使用命令创建WordPress数据库,命令如下:

[root@mysql1 ~]# mysql -uroot -p000000

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 2

Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, 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)]> Ctrl-C -- exit!

Aborted

(5)验证WordPress应用

在Nginx节点重启Nginx服务,命令如下:

[root@nginx ~]# nginx -s reload

在浏览器中输入192.168.200.50地址进行访问,会出现著名的WordPress五分钟安装程序,填写必要的信息,然后单击左下角“安装WordPress”按钮,进行WordPress应用的安装,如图4-10-1所示。

图4-10-1 WordPress安装界面

稍等片刻,安装完毕后,进入WordPress后台界面,如图4-10-2所示。

图4-10-2 WordPress后台界面

单击图4-10-2页面左上角的“myblog”图标,进入WordPress首页,如图4-10-3所示。

图4-10-3 WordPress首页

至此,分布式部署LNMP+WordPress应用已完成。

标签:MariaDB,--,LNMP,nginx,WordPress,php,root,节点,分布式
来源: https://blog.csdn.net/Li_FengBiao/article/details/121489099

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有