参考博客
环境准备:
准备虚拟机 | ||
---|---|---|
版本:CentOS7.7 | 主机IP:192.168.124.5 | 主机名:nginx-mysql |
版本:CentOS7.7 | 主机IP:192.168.124.14 | 主机名:tomcat |
环境简述:
两台虚拟机,一台为 nginx+mysql服务器,另一台为Tomcat服务器。客户端发来请求,先由nginx处理,
若为静态内容则由nginx响应,并将结果发送给客户端;若为动态内容,则由nginx反代至后端的Tomcat服务器。
默认两台主机 已关闭防火墙、关闭SELINUX、时间同步
在主机 nginx-mysql 上进行操作:
#创建系统用户 nginx
[root@nginx-mysql ~]# useradd -r -M -s /sbin/nologin nginx
#安装依赖包
[root@nginx-mysql ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
[root@nginx-mysql ~]# yum -y groups install 'Development Tools'
#创建nginx日志存放目录
[root@nginx-mysql ~]# mkdir -p /var/log/nginx
[root@nginx-mysql ~]# chown -R nginx.nginx /var/log/nginx/
#下载nginx并进行编译安装
[root@nginx-mysql ~]# cd /usr/src/
[root@nginx-mysql src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@nginx-mysql src]# tar xf nginx-1.16.1.tar.gz
[root@nginx-mysql src]# ls
debug kernels nginx-1.16.1 nginx-1.16.1.tar.gz
[root@nginx-mysql src]# cd nginx-1.16.1/
[root@nginx-mysql nginx-1.16.1]#
./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@nginx-mysql nginx-1.16.1]# echo $?
0
[root@nginx-mysql nginx-1.16.1]# make && make install
#配置环境变量
[root@nginx-mysql nginx-1.16.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx-mysql nginx-1.16.1]# . /etc/profile.d/nginx.sh
#启动nginx并查看端口
[root@nginx-mysql nginx-1.16.1]# nginx
[root@nginx-mysql nginx-1.16.1]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 16599 root 6u IPv4 32390 0t0 TCP *:http (LISTEN)
nginx 40615 nginx 6u IPv4 32390 0t0 TCP *:http (LISTEN)
主机 nginx-mysql 上继续进行操作:
#安装mysql相关依赖包
[root@nginx-mysql ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
#创建mysql用户和组
[root@nginx-mysql ~]# groupadd -r -g 666 mysql
[root@nginx-mysql ~]# useradd -M -s /sbin/nologin -g 666 -u 666 mysql
[root@nginx-mysql ~]# grep mysql /etc/passwd
mysql:x:666:666::/home/mysql:/sbin/nologin
#下载mysql二进制包
#有点懵逼,我在用wget下载链接时居然保错404?
#直接用Windows打开链接下载,再上传至虚拟主机
[root@nginx-mysql ~]# cd /usr/src/
[root@nginx-mysql src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
#解压
[root@nginx-mysql src]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@nginx-mysql src]# cd /usr/local/
[root@nginx-mysql local]# ls|grep mysql
mysql-5.7.30-linux-glibc2.12-x86_64
#创建软连接
[root@nginx-mysql local]# ln -sv mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.30-linux-glibc2.12-x86_64/"
#修改mysql目录的属主属组
[root@nginx-mysql local]# chown -R mysql.mysql /usr/local/mysql
[root@nginx-mysql local]# ll -d mysql
lrwxrwxrwx 1 mysql mysql 36 May 31 10:45 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
#添加环境变量
[root@nginx-mysql local]# cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:$PATH
[root@nginx-mysql local]# . /etc/profile.d/mysql.sh
#创建mysql数据存放目录
[root@nginx-mysql local]# mkdir /opt/data
[root@nginx-mysql local]# chown -R mysql.mysql /opt/data/
#初始化数据库
[root@nginx-mysql local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
#配置mysql
[root@nginx-mysql local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@nginx-mysql local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@nginx-mysql local]# ldconfig -v
#生成配置文件
[root@nginx-mysql local]# 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
> EOF
#配置启动脚本
[root@nginx-mysql local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@nginx-mysql local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@nginx-mysql local]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
#启动mysql
[root@nginx-mysql local]# service mysqld start
Starting MySQL.Logging to '/opt/data/nginx-mysql.err'.
... SUCCESS!
#查看进程和端口信息
[root@nginx-mysql local]# ps -ef|grep mysql
avahi 646 1 0 10:21 ? 00:00:00 avahi-daemon: running [nginx-mysql.local]
root 42530 1 0 14:54 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/datamysql.pid
mysql 42710 42530 0 14:54 pts/0 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=nginx-mysql.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 46406 41125 0 15:51 pts/0 00:00:00 grep --color=auto mysql
[root@nginx-mysql local]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 42710 mysql 30u IPv6 139653 0t0 TCP *:mysql (LISTEN)
#使用临时密码登录并修改密码
#然而我又蒙蔽了!存放临时密码的nginx-mysql.err文件里面居然找不到临时密码?
#于是我就换一种方式
[root@nginx-mysql local]# vim /etc/my.cnf
#追加此行,其作用是跳过密码验证直接登陆mysql
skip-name-resolve
#重启mysql
[root@nginx-mysql local]# service mysqld restart
[root@nginx-mysql local]# mysql
………..#省略
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> update user set password=password("123456") where user="root"; #修改密码出现报错,这是因为mysql 5.7版本数据库里已不存在password字段,该字段已被改为authentication_string
mysql> update mysql.user set authentication_string=password('123456') where user='root'; #修改密码成功
mysql> flush privileges; #使其立即生效
mysql> exit; #退出
[root@nginx-mysql local]# vim /etc/my.cnf
#删除之前添加的此行
skip-name-resolve
#重启mysql
[root@nginx-mysql local]# service mysqld restart
[root@nginx-mysql local]# mysql -uroot -p123456
………..#省略
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit;
在主机 tomcat 上进行操作:
#安装openjdk
[root@tomcat ~]# yum list java*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Installed Packages
java-1.8.0-openjdk.x86_64 1:1.8.0.252.b09-2.el7_8 @updates
java-1.8.0-openjdk-devel.x86_64 1:1.8.0.252.b09-2.el7_8 @updates
java-1.8.0-openjdk-headless.x86_64 1:1.8.0.252.b09-2.el7_8 @updates
[root@tomcat ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
#验证java版本
[root@tomcat ~]# java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
#下载tomcat
[root@tomcat ~]# cd /usr/src/
[root@tomcat src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
[root@tomcat src]# mkdir /usr/local/tomcat{1,2}
#解压
[root@tomcat src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/tomacat1
[root@tomcat src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/tomacat2
#创建软连接
[root@tomcat src]# cd /usr/local/tomcat1/
[root@tomcat tomcat1]# ln -s apache-tomcat-9.0.8/ tomcat1
[root@tomcat src]# cd /usr/local/tomcat2/
[root@tomcat tomcat2]# ln -s apache-tomcat-9.0.8/ tomcat2
#创建实验目录和网页文件
[root@tomcat tomcat2]# mkdir /usr/local/tomcat1/tomcat1/webapps/test/
[root@tomcat tomcat2]# mkdir /usr/local/tomcat2/tomcat2/webapps/test/
[root@tomcat tomcat2]# vim /usr/local/tomcat1/tomcat1/webapps/test/index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("Hello,this is Dushansao");
%>
</body>
</html>
[root@tomcat tomcat2]# vim /usr/local/tomcat2/tomcat2/webapps/test/index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("You can see!");
%>
</body>
</html>
#修改tomcat2的端口
[root@tomcat tomcat2]# vim /usr/local/tomcat2/tomcat2/conf/server.xml
#将8005改为8006
#将8080改为8081
#将8009改为8010
#启动tomcat服务
[root@tomcat tomcat2]# /usr/local/tomcat1/tomcat1/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat1/tomcat1
Using CATALINA_HOME: /usr/local/tomcat1/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat1/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/tomcat1/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat tomcat2]# /usr/local/tomcat2/tomcat2/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat2/tomcat2
Using CATALINA_HOME: /usr/local/tomcat2/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat2/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/tomcat2/bin/tomcat-juli.jar
Tomcat started.
#浏览器输入:192.168.124.14:8080/test 访问
#浏览器输入:192.168.124.14:8081/test 访问
接下来我们可以在主机 nginx-mysql 上搭建nginx完成负载均衡,配置nginx实现动静分离
[root@nginx-mysql ~]# vim /usr/local/nginx/conf/nginx.conf
#新加入的行起始点
upstream web {
server 192.168.124.14:8080;
server 192.168.124.14:8081;
}
#新加入的行结束点
server {
listen 80;
server_name localhost;
#在server段添加以下内容即可
#新加入的行起始点
location ~* \.(do|jsp)$ {
proxy_pass http://web;
}
location ~* tomcat\.(png|css)$ {
proxy_pass http://web;
}
#新加入的行结束点
#修改完成之后一定要记得进行语法检查
[root@nginx-mysql ~]# 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@nginx-mysql ~]# nginx -s reload
#静态资源访问nginx
#浏览器输入:192.168.124.5访问
#动态资源访问tomcat
#浏览器输入:192.168.124.5/index.jsp访问