LNMT架构:Linux+Nginx+Mysql+Tomcat
(nginx实现负载均衡动静分离,反向代理)
环境说明:
IP | 安装的服务 |
---|---|
192.168.209.12 | tomcat |
192.168.209.13 | nginx mysql |
********************************nginx安装**************************
说明:nginx安装的方式的是编译安装
1.//创建系统用户nginx
[root@lanzhiyong ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@lanzhiyong ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
[root@lanzhiyong ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@lanzhiyong ~]# mkdir -p /var/log/nginx
[root@lanzhiyong ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@lanzhiyong ~]# cd /usr/src/ [root@lanzhiyong src]# wgethttp://nginx.org/download/nginx-1.14.0.tar.gz
//编译安装
[root@lanzhiyong src]# tar xf nginx-1.14.0.tar.gz
[root@lanzhiyong src]# cd nginx-1.14.0
[root@lanzhiyong 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_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@lanzhiyong nginx-1.14.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
2.nginx安装后配置
//配置环境变量
[root@lanzhiyong ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lanzhiyong ~]# source /etc/profile.d/nginx.sh
[root@lanzhiyong ~]# nginx
[root@lanzhiyong ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 64 *:40154 *:*
LISTEN 0 64 *:2049 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 64 :::2049 :::*
LISTEN 0 64 :::34409 :::*
********************************mysql安装****************************
说明:mysql安装的方式是二进制安装
//安装依赖 包
[root@lanzhiyong ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建用户和组
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# groupadd -r -g 306 mysql
[root@lanzhiyong src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
//下载二进制格式的mysql软件包
[root@lanzhiyong src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
//解压软件至/usr/local
[root@lanzhiyong src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@lanzhiyong src]# cd /usr/local/
//创建软连接
[root@lanzhiyong local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
//修改目录/usr/local/mysql的属主属组
[root@lanzhiyong local]# chown -R mysql.mysql /usr/local/mysql
[root@lanzhiyong local]# ll /usr/local/mysql
//添加环境变量
[root@lanzhiyong ~]# ls /usr/local/mysql
[root@lanzhiyong ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# source /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# echo $PATH
//建立数据存放目录
[root@lanzhiyong ~]# mkdir /opt/data
[root@lanzhiyong ~]# chown -R mysql.mysql /opt/data/
[root@lanzhiyong ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 8月 19 13:20 data
//初始化数据库 注意这个命令后会生成临时密码 要记住 jd?ajfrKY4pQ
[root@lanzhiyong ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
//配置mysql
//软连接
[root@lanzhiyong ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
[root@lanzhiyong ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@lanzhiyong ~]# ldconfig -v
//生成配置文件
[root@lanzhiyong ~]# 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@lanzhiyong ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@lanzhiyong ~]# service mysqld start
[root@lanzhiyong ~]# ps -ef |grep mysql
[root@lanzhiyong ~]# ss -antl
LISTEN 0 80 :::3306 :::*
//修改密码 使用临时密码登入
[root@lanzhiyong ~]# mysql -uroot -p
说明:jd?ajfrKY4pQ 这是以上步骤的临时密码
mysql> set password = password('lanzhiyong');
******************************tomcat项目部署************************
说明:下载好tomcat解压就能配置了
//java环境安装,安装jdk环境
[root@lanzhiyong ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
[root@lanzhiyong ~]# java -version
//下载tomcat
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/ v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
//解压部署
[root@lanzhiyong src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
[root@lanzhiyong local]# tar xf apache-tomcat-9.0.8.tar.gz
[root@lanzhiyong local]# mv apache-tomcat-9.0.8 apache2
[root@lanzhiyong local]# mv apache2 /usr/local/
[root@lanzhiyong src]# cd /usr/local/
[root@lanzhiyong local]# ln -s apache2/ tomcat2
[root@lanzhiyong local]# ln -s apache-tomcat-9.0.8/ tomcat
[root@lanzhiyong local]# ll
//分别给tomcat,tomcat2写一个java页面
[root@lanzhiyong ~]# vim index.jsp
[root@lanzhiyong ~]# cat index.jsp
test papg
<%
out.println("Hellow World");
%>
[root@lanzhiyong ~]# mv index.jsp index
[root@lanzhiyong ~]# vim index.jsp
[root@lanzhiyong ~]# cat index.jsp
test papg
<%
out.println("I Love china");
%>
[root@lanzhiyong ~]# mkdir /usr/local/tomcat/webapps/test
[root@lanzhiyong ~]# mkdir /usr/local/tomcat2/webapps/test
[root@lanzhiyong ~]# cp /index/index.jsp /usr/local/tomcat/webapps/test/
[root@lanzhiyong ~]# cp index.jsp /usr/local/tomcat2/webapps/test/
[root@lanzhiyong ~]# ll /usr/local/tomcat/webapps/test/
总用量 4
-rw-r--r--. 1 root root 114 9月 5 11:27 index.jsp
[root@lanzhiyong ~]# ll /usr/local/tomcat2/webapps/test/
总用量 4
-rw-r--r--. 1 root root 114 9月 5 11:27 index.jsp
[root@lanzhiyong ~]# vim /usr/local/tomcat2/conf/server.xml
把所有的port=*端口号改下不同即可(例如port=8080->port=8081)
//启动tomcat
[root@lanzhiyong ~]# /usr/local/tomcat/bin/catalina.shstart
[root@lanzhiyong ~]# /usr/local/tomcat2/bin/catalina.shstart
[root@lanzhiyong ~]# ps -ef |grep tomcat
[root@lanzhiyong ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 :::8080 :::*
LISTEN 0 100 :::8081 :::*
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 :::*
***************************nginx配置********************************
[root@lan conf]# vim /usr/local/nginx/conf/nginx.conf
//添加以下模块
upstream web {
server 192.168.209.12:8080; //负载均衡轮询
server 192.168.209.12:8081;
}
server{
location ~* \.(do|jsp)$ {
proxy_pass http://web; //反向代理,tomcat支持动态访问,静态交给nginx访问
}
}
[root@lan conf]# nginx -s reload
验证:输入:192.168.209.13 实现nginx静态访问
最后验证输入:192.168.209.13/test/index.jsp 刷新看效果,,实现动静分离