LNMT架構部署:Linux+Nginx+Mysql+Tomcat(負載平衡,動靜分離)

來源:互聯網
上載者:User

標籤:contex   防火牆   app   配置nginx   develop   text   bfd   etc   cmak   

環境描述:虛擬機器準備兩台,一台作為nginx伺服器+mysql伺服器,IP為:192.168.55.129;另外一台作為2台Tomcat伺服器,IP為:192.168.55.130。用戶端發來請求,首先由nginx處理,如果為靜態內容直接由nginx響應,將結果直接給用戶端;如果為動態內容,則由nginx反代至後端的Tomcat伺服器。

在IP為192.168.55.129的伺服器上安裝和配置nginx關閉防火牆和selinux[[email protected] ~]# systemctl stop firewalld[[email protected] ~]# systemctl disable firewalld[[email protected] ~]# setenforce 0[[email protected] ~]#  sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/selinux/config建立系統使用者nginx[[email protected] ~]#  useradd -r -M -s /sbin/nologin nginx安裝依賴環境[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel[[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.12.0.tar.gz編譯安裝[[email protected] ~]# cd /usr/src/[[email protected] src]# lsdebug  kernels  nginx-1.14.0.tar.gz[[email protected] src]# tar xf nginx-1.14.0.tar.gz[[email protected] src]# cd nginx-1.14.0[[email protected] nginx-1.14.0]# lsauto     CHANGES.ru  configure  html     man     srcCHANGES  conf        contrib    LICENSE  README[[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.12.0]# make -j 2 && make installnginx安裝後配置配置環境變數[[email protected] nginx-1.14.0]# echo ‘export PATH=/usr/local/nginx/sbin:$PATH‘ > /etc/profile.d/nginx.sh[[email protected] nginx-1.14.0]#  . /etc/profile.d/nginx.sh啟動nginx[[email protected] nginx-1.14.0]# nginx[[email protected] nginx-1.14.0]# ss -antlState       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                 :::*                  在IP為192.168.55.129上安裝mysql安裝依賴包[[email protected] ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel建立使用者和組[[email protected] ~]# groupadd -r -g 306 mysql[[email protected] ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql下載二進位格式的mysql軟體包[[email protected] ~]# cd /usr/src/[[email protected] src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz解壓軟體至/usr/local[[email protected] src]#  tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/[[email protected] src]# cd /usr/local/[[email protected] local]# lsbin    include  libexec                              sbinetc    lib      mysql-5.7.22-linux-glibc2.12-x86_64  sharegames  lib64    nginx                                src[[email protected] local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"[[email protected] local]# ll總用量 0drwxr-xr-x.  2 root root   6 11月  5 2016 bindrwxr-xr-x.  2 root root   6 11月  5 2016 etcdrwxr-xr-x.  2 root root   6 11月  5 2016 gamesdrwxr-xr-x.  2 root root   6 11月  5 2016 includedrwxr-xr-x.  2 root root   6 11月  5 2016 libdrwxr-xr-x.  2 root root   6 11月  5 2016 lib64drwxr-xr-x.  2 root root   6 11月  5 2016 libexeclrwxrwxrwx.  1 root root  36 9月   6 17:30 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/drwxr-xr-x.  9 root root 129 9月   6 17:28 mysql-5.7.22-linux-glibc2.12-x86_64drwxr-xr-x. 11 root root 151 9月   6 17:08 nginxdrwxr-xr-x.  2 root root   6 11月  5 2016 sbindrwxr-xr-x.  5 root root  49 9月   3 23:02 sharedrwxr-xr-x.  2 root root   6 11月  5 2016 src修改目錄/usr/local/mysql的屬主屬組[[email protected] local]# chown -R mysql.mysql /usr/local/mysql[[email protected] local]# ll /usr/local/mysql -dlrwxrwxrwx. 1 mysql mysql 36 9月   6 17:30 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/添加環境變數[[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/nginx/sbin:/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]#  ll /opt/總用量 0drwxr-xr-x. 2 mysql mysql 6 9月   6 17:34 data初始化資料庫[[email protected] local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/配置mysql[[email protected] local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql"/usr/local/include/mysql" -> "/usr/local/mysql/include/"[[email protected] local]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf[[email protected] local]#  ldconfig -v組建組態檔案[[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]# sed -ri ‘s#^(basedir=).*#\1/usr/local/mysql#g‘ /etc/init.d/mysqld[[email protected] local]# sed -ri ‘s#^(datadir=).*#\1/opt/data#g‘ /etc/init.d/mysqld啟動mysql[email protected] local]# service mysqld startStarting MySQL.Logging to ‘/opt/data/yanyinglai.err‘.... SUCCESS![[email protected] local]# ps -ef | grep mysqlroot      42077      1  0 17:57 pts/2    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pidmysql     42255  42077 15 17:57 pts/2    00:00:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=yanyinglai.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306root      42291   4672  0 17:57 pts/2    00:00:00 grep --color=auto mysql[[email protected] local]# ss -antlState       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] local]# mysql -uroot -pEnter password:mysql> set password = password(‘yanyinglai123‘);Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> exitBye在192.168.55.130這台虛擬機器設定兩台tomcat項目部署關閉防火牆和selinux[[email protected] ~]#[[email protected] ~]# systemctl stop firewalld[[email protected] ~]# systemctl disable firewalld[[email protected] ~]#  setenforce 0[[email protected] ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/selinux/configJava環境安裝//安裝jdk環境[[email protected] ~]#  yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel[[email protected] ~]#  java -versionopenjdk version "1.8.0_131"OpenJDK Runtime Environment (build 1.8.0_131-b12)OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)tomcat部署//下載tomcat[[email protected] ~]# cd /usr/src/[[email protected] src]#  wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz//建立tomcat存放目錄[[email protected] src]# cd /usr/local/[[email protected] local]# mkdir tomacat{1,2}[[email protected] local]# lsbin  games    lib    libexec  sbin   src       tomacat2etc  include  lib64  nginx    share  tomacat1//將tomcat解壓存放目錄[[email protected] local]# cd /usr/src/[[email protected] src]# lsapache-tomcat-9.0.8.tar.gz  debug  kernels[[email protected] src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/tomacat1[[email protected] src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/tomacat2//建立軟串連[[email protected] src]# cd /usr/local/tomacat1[[email protected] tomacat1]# ln -s apache-tomcat-9.0.8/ tomcat1[[email protected] tomacat1]# ll總用量 0drwxr-xr-x. 9 root root 160 9月   5 21:52 apache-tomcat-9.0.8lrwxrwxrwx. 1 root root  20 9月   5 21:54 tomcat1 -> apache-tomcat-9.0.8/[[email protected] tomacat1]# cd /usr/local/tomacat2[[email protected] tomacat2]# ln -s apache-tomcat-9.0.8/ tomcat2[[email protected] tomacat2]# ll總用量 0drwxr-xr-x. 9 root root 160 9月   5 21:52 apache-tomcat-9.0.8lrwxrwxrwx. 1 root root  20 9月   5 21:55 tomcat2-> apache-tomcat-9.0.8/建立測試目錄並建立測試檔案[[email protected] ~]#  mkdir /usr/local/tomacat1/tomcat1/webapps/test[[email protected] ~]# mkdir /usr/local/tomacat2/tomcat2/webapps/test[[email protected] ~]# cd /usr/local/tomacat1/tomcat1/webapps/test[[email protected] test]# vi index.jsp[[email protected] test]# cat index.jsp<html><head><title>test page</title></head><body><%out.println("Hellow World");%></body></html>[[email protected] test]# cd /usr/local/tomacat2/tomcat2/webapps/test[[email protected] test]# vi index.jsp[[email protected] test]# cat index.jsp<html><head><title>test page</title></head><body><%out.println("8888888888");%></body></html>//修改tomcat2的連接埠,否則連接埠起衝突。設定檔為:server.xml[[email protected] ~]# cd /usr/local/tomacat2/tomcat2/[[email protected] tomcat2]# lsbin   lib      logs    RELEASE-NOTES  temp     workconf  LICENSE  NOTICE  RUNNING.txt    webapps[[email protected] tomcat2]# cd conf/[[email protected] conf]# lscatalina.policy       jaspic-providers.xsd  tomcat-users.xsdcatalina.properties   logging.properties    web.xmlcontext.xml           server.xmljaspic-providers.xml  tomcat-users.xml[[email protected] conf]# vi server.xml[[email protected] conf]# /usr/local//tomacat1/tomcat1/bin/catalina.sh startUsing CATALINA_BASE:   /usr/local/tomacat1/tomcat1Using CATALINA_HOME:   /usr/local/tomacat1/tomcat1Using CATALINA_TMPDIR: /usr/local/tomacat1/tomcat1/tempUsing JRE_HOME:        /usrUsing CLASSPATH:       /usr/local/tomacat1/tomcat1/bin/bootstrap.jar:/usr/local/tomacat1/tomcat1/bin/tomcat-juli.jarTomcat started.[[email protected] conf]# /usr/local//tomacat2/tomcat2/bin/catalina.sh startUsing CATALINA_BASE:   /usr/local/tomacat2/tomcat2Using CATALINA_HOME:   /usr/local/tomacat2/tomcat2Using CATALINA_TMPDIR: /usr/local/tomacat2/tomcat2/tempUsing JRE_HOME:        /usrUsing CLASSPATH:       /usr/local/tomacat2/tomcat2/bin/bootstrap.jar:/usr/local/tomacat2/tomcat2/bin/tomcat-juli.jarTomcat started.

//輸入192.168.55.130:8080 訪問

//輸入192.168.55.130:8080/test 訪問

//第二台tomcat
//輸入192.168.55.130:8081訪問

//在主機192.168.55.129上搭建nginx使用nginx實現負載平衡,修改設定檔配置nginx實現動靜分離[[email protected] ~]# vim /usr/local/nginx/conf/nginx.confupstream web {      server 192.168.55.130:8080;      server 192.168.55.130:8081;}定義好upstream後,需要在server段內添加如下內容             location ~* \.(do|jsp)$ {            proxy_pass http://web;        }        location ~* tomcat\.(png|css)$ {            proxy_pass http://web;        }        location / {                 root   html;                 index index.html index.htm;        }修改完設定檔檢查語法錯誤,並重啟服務[[email protected] ~]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[[email protected] ~]# nginx -s reload

靜態資源訪問nginx
//輸入192.168.56.129訪問

//動態資源訪問tomcat


重新整理一下

LNMT架構部署:Linux+Nginx+Mysql+Tomcat(負載平衡,動靜分離)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.