應用系統分佈式構建運維

應用系統分佈式構建運維php

1+x初級,項目四html

部署主從數據庫

基礎環境安裝

準備兩臺主機mysql

修改主機名

# hostnamectl set-hostname mysql1nginx

# hostnamectl set-hostname mysql2c++

關閉防火牆及SELinux服務(兩個節點)

# setenforce 0sql

# systemctl stop firewalld數據庫

配置hosts文件(兩個節點)

# vi /etc/hostsapi

加入如下內容運維

192.168.37.16 mysql1
192.168.37.17 mysql2curl

安裝數據庫服務(兩個節點)

# yum install -y mariadb mariadb-server

啓動數據庫服務並設置開機自啓

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

初始化數據庫並配置主從服務

初始化數據庫(兩個節點)

# mysql_secure_installation
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:                                             ##輸入數據庫root密碼
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!

配置mysql1主節點

修改mysql1節點的數據庫配置文件

# vi /etc/my.cnf

[mysqld]
log_bin=mysql-bin                ##記錄操做日誌
binlog_ignore_db=mysql      ##不一樣步mysql系統數據庫
server_id=16                        ##數據庫集羣中的每一個節點id都要不一樣
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

重啓數據庫服務

# systemctl restart mariadb

進入數據庫

# mysql -uroot -p123456

受權在任何客戶端機器上能夠以root用戶登陸到數據庫

> grant all privileges on *.* to root@'%' identified by "123456";

在主節點上建立一個用戶鏈接節點mysql2,並賦予從節點同步主節點數據庫的權限

> grant replication slave on *.* to 'user'@'mysql2' identified by '123456';

配置mysql2從節點

修改mysql2節點的數據庫配置文件

# vi /etc/my.cnf

[mysqld]
log_bin=mysql-bin                ##記錄操做日誌
binlog_ignore_db=mysql      ##不一樣步mysql系統數據庫
server_id=17                        ##數據庫集羣中的每一個節點id都要不一樣
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

重啓數據庫服務

# systemctl restart mariadb

進入數據庫

# mysql -uroot -p123456

配置從節點鏈接主節點的鏈接信息

> change master to master_host='mysql1',master_user='user',master_password='123456';

開啓從節點服務

> start slave;

查看從節點服務狀態

> show slave status\G

配置數據庫主從集羣成功

驗證數據庫主從服務

主節點建立數據庫

在主節點中建立庫

> create database test;

> use test;

在庫中建立表

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

插入表數據

> insert into company values(1,"alibaba","china");

查看錶數據

> select * from company;

從節點驗證複製功能

查看數據庫列表

> show databases;

> use test;

查詢表

> show tables;

查詢內容,驗證複製功能

> select * from company;

 

驗證從數據庫的複製功能成功

部署Nginx服務

基礎環境安裝

修改主機名

# hostnamectl set-hostname nginx

關閉防火牆及SELinux服務

# setenforce 0

# systemctl stop firewalld

安裝配置基礎服務

編譯安裝基礎環境

# yum install -y gcc gcc-c++ openssl-devel zlib-devel zlib pcre-devel

建立指定用戶

# groupadd -g 1001 nginx
# useradd -u 900 nginx -g nginx -s /sbin/nologin
# tail -1 /etc/passwd
nginx:x:900:1001::/home/nginx:/sbin/nologin

安裝配置nginx服務

將提供的nginx-1.12.2.tar.gz壓縮包上傳至/usr/local/src/目錄下,並解壓到當前目錄

# cd /usr/local/src

# tar -zxvf nginx-1.12.2.tar.gz

編譯並安裝

# cd 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

沒有報錯,進行安裝

# make && make install

建立軟連接

# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

啓動測試

# 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

# nginx

# netstat -ntpl

80端口啓動,表示nginx服務啓動成功 

安裝PHP環境

基礎環境安裝

修改主機名

# hostnamectl set-hostname php

關閉防火牆及SELinux服務

# setenforce 0

# systemctl stop firewalld

安裝配置基礎服務

編譯安裝基礎環境

# yum install -y gcc gcc-c++ libxml2-devel libcurl-devel openssl-devel bzip2-devel

將提供的libmcrypt-2.5.8.tar.gz壓縮包上傳至/usr/local/src目錄下,並解壓到當前目錄

# cd /usr/local/src

# tar -zxvf libmcrypt-2.5.8.tar.gz

編譯安裝服務

# cd libmcrypt-2.5.8

# ./configure --prefix=/usr/local/libmcrypt && make && make install

安裝PHP環境

將提供的php-5.6.27.tar.gz壓縮包上傳至/usr/local/src目錄下,並解壓到當前目錄

# cd /usr/local/src/

# tar -zxvf php-5.6.27.tar.gz

編譯安裝服務

# cd php-5.6.27

# ./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

沒有報錯,進行安裝

# make && make install

建立用戶ID

這個nginx的id號要和nginx主機上的保持一致

# groupadd -g 1001 nginx
# useradd -u 900 nginx -g nginx -s /sbin/nologin
# tail -1 /etc/passwd
nginx:x:900:1001::/home/nginx:/sbin/nologin

配置PHP環境

PHP壓縮包中提供了PHP環境須要用到的模板文件,須要對文件進行更名後才能使用

複製文件並更名

# cp php.ini-production /etc/php.ini

# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

賦予文件執行權限

# chmod +x /etc/init.d/php-fpm

添加PHP服務到啓動列表,並設置開機自啓

# chkconfig --add php-fpm

# chkconfig php-fpm on

修改PHP的主配置文件

# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf

# grep -n '^'[a-Z] /usr/local/php5.6/etc/php-fpm.conf
149:user = nginx
150:group = nginx
164:listen = 192.168.37.13:9000
224:pm = dynamic
235:pm.max_children = 50
240:pm.start_servers = 5
245:pm.min_spare_servers = 5
250:pm.max_spare_servers = 35

啓動PHP服務

啓動PHP服務

# service php-fpm start
Starting php-fpm  done

查看啓動狀態

# netstat -ntpl

9000端口啓動,表示PHP環境安裝完畢 

分佈式部署LNMP+WordPress

已經完成了主從數據庫的安裝配置、Nginx服務的安裝、PHP環境的安裝的四臺機器進行部署

分佈式LNMP環境的調試

配置Nginx服務支持PHP環境(nginx節點)

修改配置文件

# vi /usr/local/nginx/conf/nginx.conf

location / {
            root   /www;                   ##更改網頁目錄
            index  index.php index.html index.htm;
        }

location ~ \.php$ {                       ##去掉這部分前面的註釋符
            root           /www;             ##更改目錄       
            fastcgi_pass   192.168.37.13:9000;           ##添加PHP主機IP地址
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

添加配置

# vi /usr/local/nginx/conf/fastcgi_params

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;     ##添加這行
fastcgi_param  REQUEST_URI        $request_uri;

建立目錄(nginx和php節點)

nginx和php節點,建立/www目錄,並修改用戶和用戶組

# mkdir /www

# chown nginx:nginx /www/

部署WordPress(nginx和php節點)

將提供的wordpress-4.7.3-zh_CN.zip壓縮包上傳至nginx節點和php節點的/root目錄下並解壓

# yum install -y unzip

# unzip wordpress-4.7.3-zh_CN.zip

將解壓後的文件複製到/www目錄下

# mv wordpress/* /www/

修改wordpress的配置文件(nginx節點)

將模板文件複製並修改

# cp /www/wp-config-sample.php /www/wp-config.php 

# vi /www/wp-config.php

// ** MySQL 設置 - 具體信息來自您正在使用的主機 ** //
/** WordPress數據庫的名稱 */
define('DB_NAME', 'wordpress');
/** MySQL數據庫用戶名 */
define('DB_USER', 'root');
/** MySQL數據庫密碼 */
define('DB_PASSWORD', '123456');
/** MySQL主機 */
define('DB_HOST', '192.168.37.16');               ##此處IP爲mysql1的地址
/** 建立數據表時默認的文字編碼 */
define('DB_CHARSET', 'utf8');
/** 數據庫整理類型。如不肯定請勿更改 */
define('DB_COLLATE', '');
將該配置文件複製到php節點(nginx節點)
# scp /www/wp-config.php root@192.168.37.13:/www/
The authenticity of host '192.168.37.13 (192.168.37.13)' can't be established.
ECDSA key fingerprint is SHA256:C2d2Z+sCPaySJhwUjJ6I9fcmVW/rCBNL/7qI4lm8fd8.
ECDSA key fingerprint is MD5:84:3a:fb:c4:c1:15:b6:99:6f:62:f9:4b:46:a4:60:8c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.37.13' (ECDSA) to the list of known hosts.
root@192.168.37.13's password:                        ##輸入PHP節點密碼
wp-config.php                                   100% 2909     1.9MB/s   00:00

建立WordPress數據庫(mysql1節點)

登陸數據庫

# mysql -uroot -p123456

建立數據庫

> create database wordpress;

驗證WordPress應用(nginx節點)

重啓nginx服務

# nginx -s reload

使用網頁訪問192.168.37.12(nginx節點ip)

填寫信息以後點擊左下角安裝

進入後臺界面

點擊左上角圖標

 

 

 分佈式部署完成

相關文章
相關標籤/搜索