環境描述:虛擬機準備兩臺,一臺做爲nginx服務器+mysql服務器,IP爲:192.168.55.129;另一臺做爲2臺Tomcat服務器,IP爲:192.168.55.130。客戶端發來請求,首先由nginx處理,若是爲靜態內容直接由nginx響應,將結果直接給客戶端;若是爲動態內容,則由nginx反代至後端的Tomcat服務器。css
在IP爲192.168.55.129的服務器上安裝和配置nginx關閉防火牆和selinux [root@yanyinglai ~]# systemctl stop firewalld [root@yanyinglai ~]# systemctl disable firewalld [root@yanyinglai ~]# setenforce 0 [root@yanyinglai ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config 建立系統用戶nginx [root@yanyinglai ~]# useradd -r -M -s /sbin/nologin nginx 安裝依賴環境 [root@yanyinglai ~]# yum -y install pcre-devel openssl openssl-devel gd-devel [root@yanyinglai ~]# yum -y groups install 'Development Tools' 建立日誌存放目錄 [root@yanyinglai ~]# mkdir -p /var/log/nginx [root@yanyinglai ~]# chown -R nginx.nginx /var/log/nginx/ 下載nginx [root@yanyinglai ~]# cd /usr/src/ [root@yanyinglai src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz 編譯安裝 [root@yanyinglai ~]# cd /usr/src/ [root@yanyinglai src]# ls debug kernels nginx-1.14.0.tar.gz [root@yanyinglai src]# tar xf nginx-1.14.0.tar.gz [root@yanyinglai src]# cd nginx-1.14.0 [root@yanyinglai nginx-1.14.0]# ls auto CHANGES.ru configure html man src CHANGES conf contrib LICENSE README [root@yanyinglai 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 [root@yxr nginx-1.12.0]# make -j 2 && make install nginx安裝後配置配置環境變量 [root@yanyinglai nginx-1.14.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh [root@yanyinglai nginx-1.14.0]# . /etc/profile.d/nginx.sh 啓動nginx [root@yanyinglai nginx-1.14.0]# nginx [root@yanyinglai nginx-1.14.0]# 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 :::* 在IP爲192.168.55.129上安裝mysql 安裝依賴包 [root@yanyinglai ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel 建立用戶和組 [root@yanyinglai ~]# groupadd -r -g 306 mysql [root@yanyinglai ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql 下載二進制格式的mysql軟件包 [root@yanyinglai ~]# cd /usr/src/ [root@yanyinglai src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz 解壓軟件至/usr/local [root@yanyinglai src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@yanyinglai src]# cd /usr/local/ [root@yanyinglai local]# ls bin include libexec sbin etc lib mysql-5.7.22-linux-glibc2.12-x86_64 share games lib64 nginx src [root@yanyinglai local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql "mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/" [root@yanyinglai local]# ll 總用量 0 drwxr-xr-x. 2 root root 6 11月 5 2016 bin drwxr-xr-x. 2 root root 6 11月 5 2016 etc drwxr-xr-x. 2 root root 6 11月 5 2016 games drwxr-xr-x. 2 root root 6 11月 5 2016 include drwxr-xr-x. 2 root root 6 11月 5 2016 lib drwxr-xr-x. 2 root root 6 11月 5 2016 lib64 drwxr-xr-x. 2 root root 6 11月 5 2016 libexec lrwxrwxrwx. 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_64 drwxr-xr-x. 11 root root 151 9月 6 17:08 nginx drwxr-xr-x. 2 root root 6 11月 5 2016 sbin drwxr-xr-x. 5 root root 49 9月 3 23:02 share drwxr-xr-x. 2 root root 6 11月 5 2016 src 修改目錄/usr/local/mysql的屬主屬組 [root@yanyinglai local]# chown -R mysql.mysql /usr/local/mysql [root@yanyinglai local]# ll /usr/local/mysql -d lrwxrwxrwx. 1 mysql mysql 36 9月 6 17:30 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/ 添加環境變量 [root@yanyinglai local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh [root@yanyinglai local]# . /etc/profile.d/mysql.sh [root@yanyinglai local]# echo $PATH /usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 創建數據存放目錄 [root@yanyinglai local]# mkdir /opt/data [root@yanyinglai local]# chown -R mysql.mysql /opt/data/ [root@yanyinglai local]# ll /opt/ 總用量 0 drwxr-xr-x. 2 mysql mysql 6 9月 6 17:34 data 初始化數據庫 [root@yanyinglai local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/ 配置mysql [root@yanyinglai local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql "/usr/local/include/mysql" -> "/usr/local/mysql/include/" [root@yanyinglai local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf [root@yanyinglai local]# ldconfig -v 生成配置文件 [root@yanyinglai 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 配置服務啓動腳本 [root@yanyinglai local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [root@yanyinglai local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld [root@yanyinglai local]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld 啓動mysql root@yanyinglai local]# service mysqld start Starting MySQL.Logging to '/opt/data/yanyinglai.err'. ... SUCCESS! [root@yanyinglai local]# ps -ef | grep mysql root 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.pid mysql 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=3306 root 42291 4672 0 17:57 pts/2 00:00:00 grep --color=auto mysql [root@yanyinglai local]# 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 :::* 修改密碼,使用臨時密碼登陸 [root@yanyinglai local]# mysql -uroot -p Enter password: mysql> set password = password('yanyinglai123'); Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> exit Bye 在192.168.55.130這臺虛擬機配置兩臺tomcat項目部署 關閉防火牆和selinux [root@yanyinglai ~]# [root@yanyinglai ~]# systemctl stop firewalld [root@yanyinglai ~]# systemctl disable firewalld [root@yanyinglai ~]# setenforce 0 [root@yanyinglai ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config Java環境安裝 //安裝jdk環境 [root@yanyinglai ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel [root@yanyinglai ~]# java -version openjdk 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 [root@yanyinglai ~]# cd /usr/src/ [root@yanyinglai src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz //建立tomcat存放目錄 [root@yanyinglai src]# cd /usr/local/ [root@yanyinglai local]# mkdir tomacat{1,2} [root@yanyinglai local]# ls bin games lib libexec sbin src tomacat2 etc include lib64 nginx share tomacat1 //將tomcat解壓存放目錄 [root@yanyinglai local]# cd /usr/src/ [root@yanyinglai src]# ls apache-tomcat-9.0.8.tar.gz debug kernels [root@yanyinglai src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/tomacat1 [root@yanyinglai src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/tomacat2 //建立軟鏈接 [root@yanyinglai src]# cd /usr/local/tomacat1 [root@yanyinglai tomacat1]# ln -s apache-tomcat-9.0.8/ tomcat1 [root@yanyinglai tomacat1]# ll 總用量 0 drwxr-xr-x. 9 root root 160 9月 5 21:52 apache-tomcat-9.0.8 lrwxrwxrwx. 1 root root 20 9月 5 21:54 tomcat1 -> apache-tomcat-9.0.8/ [root@yanyinglai tomacat1]# cd /usr/local/tomacat2 [root@yanyinglai tomacat2]# ln -s apache-tomcat-9.0.8/ tomcat2 [root@yanyinglai tomacat2]# ll 總用量 0 drwxr-xr-x. 9 root root 160 9月 5 21:52 apache-tomcat-9.0.8 lrwxrwxrwx. 1 root root 20 9月 5 21:55 tomcat2-> apache-tomcat-9.0.8/ 建立測試目錄並建立測試文件 [root@yanyinglai ~]# mkdir /usr/local/tomacat1/tomcat1/webapps/test [root@yanyinglai ~]# mkdir /usr/local/tomacat2/tomcat2/webapps/test [root@yanyinglai ~]# cd /usr/local/tomacat1/tomcat1/webapps/test [root@yanyinglai test]# vi index.jsp [root@yanyinglai test]# cat index.jsp <html> <head> <title>test page</title> </head> <body> <% out.println("Hellow World"); %> </body> </html> [root@yanyinglai test]# cd /usr/local/tomacat2/tomcat2/webapps/test [root@yanyinglai test]# vi index.jsp [root@yanyinglai test]# cat index.jsp <html> <head> <title>test page</title> </head> <body> <% out.println("8888888888"); %> </body> </html> //修改tomcat2的端口,不然端口起衝突。配置文件爲:server.xml [root@yanyinglai ~]# cd /usr/local/tomacat2/tomcat2/ [root@yanyinglai tomcat2]# ls bin lib logs RELEASE-NOTES temp work conf LICENSE NOTICE RUNNING.txt webapps [root@yanyinglai tomcat2]# cd conf/ [root@yanyinglai conf]# ls catalina.policy jaspic-providers.xsd tomcat-users.xsd catalina.properties logging.properties web.xml context.xml server.xml jaspic-providers.xml tomcat-users.xml [root@yanyinglai conf]# vi server.xml [root@yanyinglai conf]# /usr/local//tomacat1/tomcat1/bin/catalina.sh start Using CATALINA_BASE: /usr/local/tomacat1/tomcat1 Using CATALINA_HOME: /usr/local/tomacat1/tomcat1 Using CATALINA_TMPDIR: /usr/local/tomacat1/tomcat1/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomacat1/tomcat1/bin/bootstrap.jar:/usr/local/tomacat1/tomcat1/bin/tomcat-juli.jar Tomcat started. [root@yanyinglai conf]# /usr/local//tomacat2/tomcat2/bin/catalina.sh start Using CATALINA_BASE: /usr/local/tomacat2/tomcat2 Using CATALINA_HOME: /usr/local/tomacat2/tomcat2 Using CATALINA_TMPDIR: /usr/local/tomacat2/tomcat2/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomacat2/tomcat2/bin/bootstrap.jar:/usr/local/tomacat2/tomcat2/bin/tomcat-juli.jar Tomcat started.
//輸入192.168.55.130:8080 訪問
html
//輸入192.168.55.130:8080/test 訪問
//第二臺tomcat
//輸入192.168.55.130:8081訪問
java
//在主機192.168.55.129上搭建nginx使用nginx實現負載均衡,修改配置文件配置nginx實現動靜分離 [root@yanyinglai ~]# vim /usr/local/nginx/conf/nginx.conf upstream 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; } 修改完配置文件檢查語法錯誤,並重啓服務 [root@yanyinglai ~]# 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 [root@yanyinglai ~]# nginx -s reload
靜態資源訪問nginx
//輸入192.168.56.129訪問
mysql
//動態資源訪問tomcat
刷新一下
linux