1. 程式人生 > >分散式搭建lnmt架構

分散式搭建lnmt架構

環境說明
系統 IP 服務
redhat7 192.168.225.128 nginx
redhat7 192.168.225.129 tomcat
redhat7 192.168.225.130 mysql
lnmt部署流程
  • 在192.168.225.128上安裝nginx
//建立系統使用者nginx
[[email protected]
~]# groupadd -r nginx [[email protected] ~]# useradd -r -M -s /sbin/nologin nginx -g nginx //安裝依賴環境 [[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ [[email protected] ~]# yum -y groups install 'Development Tools' //建立日誌存放目錄 [[email protected] ~]# mkdir -p /var/log/nginx [
[email protected]
~]# chown -R nginx.nginx /var/log/nginx/ //下載nginx [[email protected] ~]# cd /usr/src/ [[email protected] src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz --2018-08-20 11:19:09-- http://nginx.org/download/nginx-1.14.0.tar.gz [[email protected] src]# ls debug kernels nginx-1.14.0.tar.gz //編譯安裝 [
[email protected]
src]# tar xf nginx-1.14.0.tar.gz [[email protected] nginx-1.14.0]# cd nginx-1.14.0/ [[email protected] nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log [[email protected] nginx-1.14.0]# make && make install //nginx安裝後配置 //設定環境變數 [[email protected] ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh [[email protected] ~]# . /etc/profile.d/nginx.sh //啟動nginx [[email protected] ~]# nginx [[email protected] ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* //關閉防火牆 [[email protected] ~]# systemctl stop firewalld.service //關閉selinux [[email protected] ~]# setenforce 0

在瀏覽器中輸入nginx的ip進行訪問
在這裡插入圖片描述

  • 192.168.225.130
    //下載二進位制格式的mysql軟體包
[[email protected] local]# ls
apache    bin    include  libexec                                     share
apr       etc    lib      mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz  src
apr-util  games  lib64    sbin

[[email protected] local]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[[email protected] local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.23-linux-glibc2.12-x86_64/"

//修改目錄/usr/local/mysql的屬主屬組
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 9月  27 10:28 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/

//新增環境變數
[[email protected] local]# cd
[[email protected] ~]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[[email protected] ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[[email protected] ~]# . /etc/profile.d/mysql.sh 
[[email protected] ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

//建立資料存放目錄
[[email protected] ~]# mkdir /opt/data
[[email protected] ~]# chown -R mysql.mysql /opt/data/
[[email protected] ~]# ll /opt/
總用量 0
drwxr-xr-x. 2 mysql mysql 6 9月  27 10:36 data

//初始化資料庫
[[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-10-18T10:06:27.093840Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-18T10:06:27.742780Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-18T10:06:27.821871Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-18T10:06:29.215545Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7b2f63b5-d2bd-11e8-9110-000c294b7cb8.
2018-10-18T10:06:29.216758Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-10-18T10:06:29.580008Z 1 [Note] A temporary password is generated for [email protected]: ur1),jhW!Isp

//配置mysql
[[email protected] ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[[email protected] ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[[email protected] ~]# ldconfig -v
[[email protected] ~]# ldconfig -p |grep mysql
	libmysqlclient.so.20 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.20
	libmysqlclient.so.18 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.18
	libmysqlclient.so (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so
	libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so

//生成配置檔案
[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# cat /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/data
socket=/tmp/mysql.sock
port=3306
pid-file=/opt/data/mysql.pid
user=mysql
skip-name-resolve

//配置服務啟動指令碼
[[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld 
[[email protected] ~]# sed -ri 's#^(datadir=).*#\1/opt/data/#g' /etc/init.d/mysqld 

//啟動mysql
[[email protected] ~]# service mysqld start 
Starting MySQL.Logging to '/opt/data/hxdserver.err'.
 SUCCESS! 
[[email protected] ~]# ps -ef |grep mysql
root      57089      1  0 11:09 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     57267  57089  3 11:09 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=hxdserver.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      57297  12647  0 11:10 pts/1    00:00:00 grep --color=auto mysql
[[email protected] ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128              *:80                           *:*                  
LISTEN     0      128              *:22                           *:*                  
LISTEN     0      100      127.0.0.1:25                           *:*                  
LISTEN     0      128             :::22                          :::*                  
LISTEN     0      100            ::1:25                          :::*                  
LISTEN     0      80              :::3306                        :::* 
//修改密碼
//使用初始密碼登入
[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23

Copyright (c) 2000, 2018, 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> set password = password('hxd123456.');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye
  • 在192.168.225.129上安裝Tomcat
安裝依賴包
[[email protected] ~]# yum -y install java-1.8.0-openjdk-devel
//下載好Tomcat包
[[email protected] ~]# tar xf apache-tomcat-9.0.12.tar.gz
[[email protected] ~]# ln -s /root/apache-tomcat-9.0.12 /usr/local/tomcat
[[email protected] ~]# echo "export PATH=/usr/local/tomcat/bin/:$PATH" > /etc/profile.d/tomcat.sh
[[email protected] ~]# source /etc/profile.d/tomcat.sh
[[email protected] ~]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

在瀏覽器中訪問裝有tomcat的ip地址加8080埠
在這裡插入圖片描述

  • 在192.168.225.128的nginx伺服器上做代理
    修改配置檔案
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    upstream 192.168.225.129:8080{
    server 192.168.225.129:8080;
}
server {
		location / {
            root   html;
            index  index.html index.htm;
}
        location ~ /.*(\.jsp|\.do) {
         proxy_pass   http://192.168.225.129:8080;
    }
  }
}

訪問192.168.225.128/index.jsp
在這裡插入圖片描述

  • 在192.168.225.129上寫一個Java測試頁面
[[email protected] ~]# cd /usr/local/tomcat/webapps/ROOT/
[[email protected] ROOT]# vim a.jsp 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
        #show{
                margin: auto;
                margin-left: 35%;
                margin-top: 20%;
                font-size: 70px;
        }
</style>
<script>
        window.onload=function(){
            var story = document.getElementById('word');
            var s = document.getElementById('show');
            var i = 0;
            timer=setInterval(function(){
                s.innerHTML=story.innerHTML.substring(0,i);
                i++;
                if(s.innerHTML==story.innerHTML){
                    clearInterval(timer);
                }
            },200);
        }
    </script>
</head>
<body>
        <p id="word" style="display:none;">Hello,World</p>
    <p id="show"></p>
</body>
</html>

在瀏覽器訪問192.168.225.128/a.jsp
在這裡插入圖片描述