环境描述:虚拟机准备两台,一台作为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]# ls
debug 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]# ls
auto CHANGES.ru configure html man src
CHANGES 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 install
nginx安装后配置配置环境变量
[[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 -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
安装依赖包
[[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]# ls
bin include libexec sbin
etc lib mysql-5.7.22-linux-glibc2.12-x86_64 share
games 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
总用量 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的属主属组
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] 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/
添加环境变量
[[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/
总用量 0
drwxr-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 start
Starting MySQL.Logging to ‘/opt/data/yanyinglai.err‘.
... SUCCESS!
[[email protected] 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
[[email protected] 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 :::*
修改密码,使用临时密码登录
[[email protected] 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
[[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/config
Java环境安装
//安装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_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]# ls
bin games lib libexec sbin src tomacat2
etc include lib64 nginx share tomacat1
//将tomcat解压存放目录
[[email protected] local]# cd /usr/src/
[[email protected] src]# ls
apache-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
总用量 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/
[[email protected] tomacat1]# cd /usr/local/tomacat2
[[email protected] tomacat2]# ln -s apache-tomcat-9.0.8/ tomcat2
[[email protected] 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/
创建测试目录并创建测试文件
[[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]# ls
bin lib logs RELEASE-NOTES temp work
conf LICENSE NOTICE RUNNING.txt webapps
[[email protected] tomcat2]# cd conf/
[[email protected] 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
[[email protected] conf]# vi server.xml
[[email protected] 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.
[[email protected] 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 访问
//输入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.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;
}
修改完配置文件检查语法错误,并重启服务
[[email protected] ~]# 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
[[email protected] ~]# nginx -s reload
静态资源访问nginx
//输入192.168.56.129访问
//动态资源访问tomcat
刷新一下
原文地址:http://blog.51cto.com/13910274/2171365
时间: 2024-10-05 17:45:17