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

分散式搭建lnmt

實驗環境

服務 作業系統 ip
nginx+mysql+tomcat RHEL7 192.168.118.222

安裝nginx 關閉防火牆和selinux

[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0

建立系統使用者

[[email protected] ~]# useradd -r -M -s /sbin/nologin nginx

安裝依賴環境

#需用網路源安裝依賴包。
[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[
[email protected]
~]# yum -y groups mark 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.12.0.tar.gz
[
[email protected]
src]# tar xf nginx-1.12.0.tar.gz #解壓 [[email protected] src]# cd nginx-1.12.0 [[email protected] nginx-1.12.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.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

啟動nginx

#配置環境變數
[[email protected] ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/pr
ofile.d/nginx.sh
[[email protected] ~]# . /etc/profile.d/nginx.sh
[[email protected] ~]# nginx     #啟動nginx

在這裡插入圖片描述 安裝mysql

下載二進位制格式的mysql軟體包

[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget https://downloads.mysql.com/archives/get/file/my
sql-5.7.22-linux-glibc2.12-x86_64.tar.gz

將軟體包解壓至/usr/local

[[email protected] ~]# cd /usr/src/
[[email protected] src]# ls
debug  kernels  mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[[email protected] src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[[email protected] src]# cd /usr/local/
[[email protected] local]# ls
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.23-linux-glibc2.12-x86_64  share
[[email protected] local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql 
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] local]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 8月  28 20:24 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/

建立使用者和組,修改目錄/usr/local/mysql的屬組和屬組

[[email protected] src]# groupadd -r mysql
[[email protected] src]# useradd -M -s /sbin/nologin -g mysql mysql 
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] local]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 8月  28 20:24 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/

新增環境變數

[[email protected] local]# vim /etc/profile.d/mysql.sh
[[email protected] local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[[email protected] local]# . /etc/profile.d/mysql.sh 
[[email protected] local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

建立資料存放目錄

[[email protected] local]# mkdir /opt/data
[[email protected] local]# chown -R mysql.mysql /opt/data
[[email protected] local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-28T12:28:52.406772Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-28T12:28:52.754503Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-28T12:28:52.846889Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-28T12:28:52.871823Z 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: ec8894c9-aabd-11e8-8c8a-000c2913cc69.
2018-08-28T12:28:52.872821Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-28T12:28:52.876757Z 1 [Note] A temporary password is generated for [email protected]: ;1<Y>%U:e,Gp   # 臨時密碼

生成配置檔案

[[email protected] local]# cat > /etc/my.cnf << EOF
> [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
> EOF

配置服務啟動指令碼

[[email protected] local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] local]# vim /etc/init.d/mysqld 
44 # overwritten by settings in the MySQL configuration files.
 45 
 46 basedir=/usr/local/mysqld   #指定MySQL安裝路徑
 47 datadir=/opt/data   #指定資料庫存放路徑
 48 
 49 # Default value, in seconds, afterwhich the script should timeout wait    ing

啟動mysql

[[email protected] local]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.err'.
 SUCCESS! 

安裝Tomcat

#安裝jdk環境
[[email protected] ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
#檢視安裝版本
[[email protected] ~]# java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
[[email protected] ~]# cd  /usr/src/
[[email protected] src]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.12/bin/apache-tomcat-9.0.12.tar.gz
#解壓
[[email protected] src]# tar xf apache-tomcat-9.0.12.tar.gz -C /usr/local/
[[email protected] src]# cd /usr/local/
[[email protected] local]# ln -s apache-tomcat-9.0.8/ tomcat
#寫一個Java測試頁面
[[email protected] ~]# vim index.jsp
[[email protected] ~]# cat index.jsp
<html>
<head>
        <title>test page</title>
</head>
<body>
        <%
            out.println("xfssb");
        %>
</body>
</html>
[[email protected] ~]# mkdir /usr/local/tomcat/webapps/test
[[email protected] ~]# cp index.jsp /usr/local/tomcat/webapps/test/
#啟動Tomcat
[[email protected] test]# /usr/local/tomcat/bin/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.
[[email protected] ~]# ps -ef|grep tomcat
root       2597      1 30 22:27 pts/0    00:00:04 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root       2643   2245  0 22:28 pts/0    00:00:00 grep --color=auto tomca
[[email protected] ~]# ss -antl
State      Recv-Q Send-Q     Local Address:Port       Peer Address:Port 
LISTEN     0      100            127.0.0.1:25                    *:*     
LISTEN     0      128                    *:22                    *:*     
LISTEN     0      100                  ::1:25                   :::*     
LISTEN     0      1       ::ffff:127.0.0.1:8005                 :::*     
LISTEN     0      100                   :::8009                 :::*     
LISTEN     0      100                   :::8080                 :::*     
LISTEN     0      32                    :::21                   :::*     
LISTEN     0      128  

啟用多個tomcat

#再次解壓
[[email protected] src]# tar xf apache-tomcat-9.0.12.tar.gz
[[email protected] src]# ls
apache-tomcat-9.0.12         debug
apache-tomcat-9.0.12.tar.gz  kernels
[[email protected] src]# cp -a apache-tomcat-9.0.12 /usr/local/apache-tomcat2
[[email protected] src]# ln -s /usr/local/apache-tomcat2/ /usr/local/tomcat/2

修改配置檔案

[[email protected] conf]# vim server.xml 
[[email protected] conf]# pwd
/usr/local/tomcat2/conf

<Server port="8006" shutdown="SHUTDOWN">
 <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8445" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

給tomcat2建立測試頁

#在/usr/local/tomcat2/webapps/目錄下新建test目錄
[[email protected] test]# pwd
/usr/local/tomcat2/webapps/test
[[email protected]t test]# ls
index.jsp
#test目錄下寫預設訪問頁,內容如下
[[email protected] test]# vim index.jsp
[[email protected] test]# cat index.jsp
<html>
<head>
        <title>test page</title>
</head>
<body>
        <%
            out.println(" dmv ");
        %>
</body>
</html>

啟動tomcat

[[email protected] conf]# /usr/local/tomcat2/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat2
Using CATALINA_HOME:   /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.
[[email protected] conf]# ss -antl
State      Recv-Q Send-Q   Local Address:Port     Peer Address:Port 
LISTEN     0      100          127.0.0.1:25                  *:*     
LISTEN     0      128                  *:22                  *:*     
LISTEN     0      100                ::1:25                 :::*     
LISTEN     0      1         ::ffff:127.0.0.1:8005               :::*     
LISTEN     0      1         ::ffff:127.0.0.1:8006               :::*     
LISTEN     0      100                 :::8009               :::*     
LISTEN     0      100                 :::8010               :::*     
LISTEN     0      100                 :::8080               :::*     
LISTEN     0      100                 :::8081               :::*     
LISTEN     0      32                  :::21                 :::*     
LISTEN     0      128                 :::22                 :::*     

測試 在這裡插入圖片描述 三臺服務整合,修改nginx配置檔案,配置負載均衡和反向代理

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #新增下面4行,配置負載均衡

    upstream web.com {
        server 192.168.118.222:8080;
        server 192.168.118.222:8081;
        }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;  
        #新增下面3行   新增反向代理location
        location ~* \.(jsp|do)$ {
            proxy_pass http://web.com;
        }
        location / {
            root   html;
            index  index.html index.htm;
        }

重啟nginx 測試 在這裡插入圖片描述 重新整理一下,為tomcat2測試頁 在這裡插入圖片描述