实验环境
服务 | 操作系统 | ip |
---|---|---|
nginx+mysql+tomcat | RHEL7 | 192.168.118.222 |
nginx安装包
Tomcat安装包
mysql安装包
安装nginx
关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
创建系统用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
安装依赖环境
#需用网络源安装依赖包。
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@localhost ~]# yum -y groups mark install 'Development Tools' #安装开发包
创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
下载nginx,编译安装
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@localhost src]# tar xf nginx-1.12.0.tar.gz #解压
[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.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@localhost nginx-1.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc
-l) && make install
启动nginx
#配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/pr
ofile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
[root@localhost ~]# nginx #启动nginx
安装mysql
下载二进制格式的mysql软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/file/my
sql-5.7.22-linux-glibc2.12-x86_64.tar.gz
将软件包解压至/usr/local
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.23-linux-glibc2.12-x86_64 share
[root@localhost local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 8月 28 20:24 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
创建用户和组,修改目录/usr/local/mysql的属组和属组
[root@localhost src]# groupadd -r mysql
[root@localhost src]# useradd -M -s /sbin/nologin -g mysql mysql
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 8月 28 20:24 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
添加环境变量
[root@localhost local]# vim /etc/profile.d/mysql.sh
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# . /etc/profile.d/mysql.sh
[root@localhost local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
建立数据存放目录
[root@localhost local]# mkdir /opt/data
[root@localhost local]# chown -R mysql.mysql /opt/data
[root@localhost local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-28T12:28:52.406772Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-28T12:28:52.754503Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-28T12:28:52.846889Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-28T12:28:52.871823Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ec8894c9-aabd-11e8-8c8a-000c2913cc69.
2018-08-28T12:28:52.872821Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-28T12:28:52.876757Z 1 [Note] A temporary password is generated for root@localhost: ;1<Y>%U:e,Gp # 临时密码
生成配置文件
[root@localhost 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@localhost local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost local]# vim /etc/init.d/mysqld
44 # overwritten by settings in the MySQL configuration files.
45
46 basedir=/usr/local/mysqld #指定MySQL安装路径
47 datadir=/opt/data #指定数据库存放路径
48
49 # Default value, in seconds, afterwhich the script should timeout wait ing
启动mysql
[root@localhost local]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.err'.
SUCCESS!
安装Tomcat
#安装jdk环境
[root@localhost ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
#查看安装版本
[root@localhost ~]# java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.12/bin/apache-tomcat-9.0.12.tar.gz
#解压
[root@localhost src]# tar xf apache-tomcat-9.0.12.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ln -s apache-tomcat-9.0.8/ tomcat
#写一个Java测试页面
[root@localhost ~]# vim index.jsp
[root@localhost ~]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("xfssb");
%>
</body>
</html>
[root@localhost ~]# mkdir /usr/local/tomcat/webapps/test
[root@localhost ~]# cp index.jsp /usr/local/tomcat/webapps/test/
#启动Tomcat
[root@localhost test]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@localhost ~]# ps -ef|grep tomcat
root 2597 1 30 22:27 pts/0 00:00:04 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 2643 2245 0 22:28 pts/0 00:00:00 grep --color=auto tomca
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 32 :::21 :::*
LISTEN 0 128
启用多个tomcat
#再次解压
[root@localhost src]# tar xf apache-tomcat-9.0.12.tar.gz
[root@localhost src]# ls
apache-tomcat-9.0.12 debug
apache-tomcat-9.0.12.tar.gz kernels
[root@localhost src]# cp -a apache-tomcat-9.0.12 /usr/local/apache-tomcat2
[root@localhost src]# ln -s /usr/local/apache-tomcat2/ /usr/local/tomcat/2
修改配置文件
[root@localhost conf]# vim server.xml
[root@localhost conf]# pwd
/usr/local/tomcat2/conf
<Server port="8006" shutdown="SHUTDOWN"> #修改三个端口号与Tomcat不同
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8445" />
<!-- Define an AJP 1.3 Connector on port 8010 -->
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
给tomcat2创建测试页
#在/usr/local/tomcat2/webapps/目录下新建test目录
[root@localhost test]# pwd
/usr/local/tomcat2/webapps/test
[root@localhost test]# ls
index.jsp
#test目录下写默认访问页,内容如下
[root@localhost test]# vim index.jsp
[root@localhost test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println(" dmv ");
%>
</body>
</html>
启动tomcat
[root@localhost conf]# /usr/local/tomcat2/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat2
Using CATALINA_HOME: /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.
[root@localhost conf]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
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 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 100 :::8081 :::*
LISTEN 0 32 :::21 :::*
LISTEN 0 128 :::22 :::*
测试
三台服务整合,修改nginx配置文件,配置负载均衡和反向代理
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#新增下面4行,配置负载均衡
upstream web.com {
server 192.168.118.222:8080;
server 192.168.118.222:8081;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#新增下面3行 添加反向代理location
location ~* \.(jsp|do)$ {
proxy_pass http://web.com;
}
location / {
root html;
index index.html index.htm;
}
重启nginx
测试
刷新一下,为tomcat2测试页