目录
1、配置yum源... 1
1.1、镜像... 1
1.2、挂载... 1
1.3、本地yum配置... 1
1.4、更新yum源... 1
2、安装java. 1
2.1、安装openjdk. 1
3、安装tomcat. 2
3.1、下载安装包... 2
3.2、上传到安装目录... 2
3.3、解压缩... 2
3.4、安装... 2
3.5、启动和停止... 3
3.6、检查端口... 3
3.7、浏览器访问... 3
4、配置系统管理... 4
4.1、/etc/rc.d/init.d/tomcat. 4
4.2、添加权限... 5
4.3、测试脚本... 5
4.4、添加开机自启动... 6
4.5、修改/usr/local/tomcat8.5.40/bin/catalina.sh. 6
4.6、最后进行验证... 6
4.7、修改tomcat-user.xml文件... 7
4.8、引擎和默认网站... 8
4.9、修改server.xml. 8
4.10、删除相关的目录... 8
4.11、部署web应用... 9
5、安装nginx. 9
5.1、安装编译工具及库文件... 9
5.2、安装PCRE. 10
6、安装nginx. 10
6.1、下载安装包... 10
6.2、解压... 11
6.3、安装... 11
6.4、检查安装版本... 11
6.5、配置nginx. 11
6.6、启动nginx. 13
6.7、其它nginx管理命令... 14
7、部署tomcat多实例... 14
7.1、创建tomcat多实例... 14
7.2、创建多实例网页根目录... 14
7.3、启动两个tomcat实例... 15
7.4、通过浏览器访问... 16
8、部署nginx反向代理... 16
8.1、多域名... 16
8.2、单域名... 18
mount –o loop /soft/iso/CentOS-7-x86_64-Everything-1804.iso /mnt
vi /etc/yum.repos.d/CentOS-Local.repo
[Local]
name=Local Yum
baseurl=file:///mnt/
gpgcheck=1
gpgkey=file:///mnt/RPM-GPG-KEY-CentOS-7
enable=1
保存退出。
yum clean all
yum update
yum -y install java
https://tomcat.apache.org/download-80.cgi
cd /soft/tomcat
tar –xzvf apache-tomcat-8.5.40.tar.gz
cp –R /soft/tomcat/apache-tomcat-8.5.40 /usr/local/tomcat8.5.40
cd /usr/local/tomcat8.5.40/bin
root@SIMS_INIT[/usr/local/tomcat8.5.40/bin]#./startup.sh
./shutdown.sh
tomcat默认使用8080端口。
http://10.45.11.118:8080/
vi /etc/rc.d/init.d/tomcat
新增如下内容:
#!/bin/bash
# /etc/rc.d/init.d/tomcat
# init script for tomcat precesses
# processname: tomcat
# chkconfig: 2345 86 16
# description: Start up the Tomcat servlet engine.
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
else
echo -e "\atomcat: unable to locate functions lib. Cannot continue."
exit -1
fi
RETVAL=$?
CATALINA_HOME="/usr/local/tomcat8.5.40/"
#tomcat安装目录,你安装在什么目录下就复制什么目录
# start
start(){
echo -n "Starting Tomcat: "
$CATALINA_HOME/bin/startup.sh
}
#stop
stop(){
echo -n "Shutting down Tomcat: "
$CATALINA_HOME/bin/shutdown.sh
}
#restart
restart(){
stop
sleep 3
start
}
#status
status(){
ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}' | wc | awk '{print $2}' > /tmp/tomcat_process_count.txt
read line < /tmp/tomcat_process_count.txt
if [ $line -gt 0 ]; then
echo -n "tomcat ( pid "
ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}'
echo -n ") is running..."
echo
else
echo "Tomcat is stopped"
fi
}
case "$1" in
start)
start ;;
stop)
stop ;;
restart)
stop
sleep 3
start ;;
status)
status ;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit $RETVAL
注意:
飘红的是tomcat的安装目录,需要根据各自情况修改。
chmod 755 /etc/rc.d/init.d/tomcat
root@SIMS_INIT[/usr/local/tomcat8.5.40/bin]#/etc/rc.d/init.d/tomcat status
tomcat ( pid 25977 26036 ) is running...
root@SIMS_INIT[/usr/local/tomcat8.5.40/bin]#/etc/rc.d/init.d/tomcat stop
Stopping tomcat (via systemctl): [ OK ]
root@SIMS_INIT[/usr/local/tomcat8.5.40/bin]#/etc/rc.d/init.d/tomcat status
Tomcat is stopped
root@SIMS_INIT[/usr/local/tomcat8.5.40/bin]#/etc/rc.d/init.d/tomcat start
Starting tomcat (via systemctl): [ OK ]
root@SIMS_INIT[/usr/local/tomcat8.5.40/bin]#/etc/rc.d/init.d/tomcat status
tomcat ( pid 26126 26184 ) is running...
root@SIMS_INIT[/usr/local/tomcat8.5.40/bin]#
最好验证下端口:
chkconfig --add /etc/rc.d/init.d/tomcat
开机自启动
chkconfig tomcat on
关闭开机自启动
chkconfig tomcat off
vi /usr/local/tomcat8.5.40/bin/catalina.sh
添加如下设置:
#export JAVA_HOME=/usr/java/jdk1.8.0_152
export CATALINA_HOME=/usr/local/tomcat8.5.40
export CATALINA_BASE=/usr/local/tomcat8.5.40
export CATALINA_TMPDIR=/usr/local/tomcat8.5.40/temp
保存退出。
注意:
JAVA_HOME根据实际情况配置,此处我的java是使用rpm安装的,可以无需配置,系统直接能找到。如果java是源码包安装的 ,需要配置实际的JAVA_HOME。
root@SIMS_INIT[/root]#systemctl stop tomcat
root@SIMS_INIT[/root]#netstat -ntlp |grep 8080
root@SIMS_INIT[/root]#systemctl start tomcat
root@SIMS_INIT[/root]#netstat -ntlp |grep 8080
tcp6 0 0 :::8080 :::* LISTEN 26328/java
root@SIMS_INIT[/root]#systemctl status tomcat
● tomcat.service - SYSV: Start up the Tomcat servlet engine.
Loaded: loaded (/etc/rc.d/init.d/tomcat; bad; vendor preset: disabled)
Active: active (running) since Wed 2019-04-17 22:59:40 CST; 11s ago
Docs: man:systemd-sysv-generator(8)
Process: 26266 ExecStop=/etc/rc.d/init.d/tomcat stop (code=exited, status=0/SUCCESS)
Process: 26310 ExecStart=/etc/rc.d/init.d/tomcat start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/tomcat.service
└─26328 //bin/java -Djava.util.logging.config.file=/usr/local/tomcat8.5.40/conf/logging.properties -Djava.util.logging...
Apr 17 22:59:40 SIMS_INIT systemd[1]: Starting SYSV: Start up the Tomcat servlet engine....
Apr 17 22:59:40 SIMS_INIT tomcat[26310]: Starting Tomcat: Tomcat started.
Apr 17 22:59:40 SIMS_INIT systemd[1]: Started SYSV: Start up the Tomcat servlet engine..
root@SIMS_INIT[/root]#systemctl is-active tomcat
active
root@SIMS_INIT[/root]#
验证均无问题。
vi /usr/local/tomcat8.5.40/conf/tomcat-users.xml
新增如下内容:
保存退出。
注意:
上面内容表示新开两个模块,管理模块和管理员模块。
创建一个用户tomcat,密码同样为tomcat,并赋予管理模块和管理员模块的权限。
重启tomcat后尝试登陆tomcat管理平台。
http://10.45.11.118:8080/manager/status
登陆用户名密码为:tomcat/tomcat
注释掉:
这段。
重启前:
重启tomcat后:
发现8009端口消失了。
cd /usr/local/tomcat8.5.40/webapps
rm -rf docs/ examples/ host-manager/ manager/
rm -rf ROOT/*
root@SIMS_INIT[/usr/local/tomcat8.5.40/webapps]#ls
ROOT
-rw-r--r-- 1 root root 1491 Apr 18 2019 TestDemo.war
cp /soft/TestDemo.war /usr/local/tomcat8.5.40/webapps/
war包放进去以后在tomcat是启动的状态下会自动解包。红圈中的TestDemo就是自动解包生成的。
内容很简单,就是自己随便写的一个helloworld。如果未自动解包,请重启tomcat服务。
尝试访问web应用:
http://10.45.11.118:8080/TestDemo/
但是这种访问是把路径都展现出来了。
现在是:
http://10.45.11.118:8080/TestDemo/
我想访问:
http://10.45.11.118:8080/
也就是和隐藏项目路径。
root@SIMS_INIT[/usr/local/tomcat8.5.40/webapps]#ls
ROOT TestDemo TestDemo.war
root@SIMS_INIT[/usr/local/tomcat8.5.40/webapps]#cp TestDemo/index.jsp ROOT/
root@SIMS_INIT[/usr/local/tomcat8.5.40/webapps]#cd ROOT
root@SIMS_INIT[/usr/local/tomcat8.5.40/webapps/ROOT]#ls
index.jsp
再次访问:
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
PCRE 作用是让 Nginx 支持 Rewrite 功能。
5.2.1、下载PCRE安装包
下载地址:
http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
cd /soft/pcre
root@SIMS_INIT[/soft/pcre]#ls
pcre-8.35 pcre-8.35.tar.gz
5.2.2、解压
tar -xzvf pcre-8.35.tar.gz
5.2.3、编译安装
cd pcre-8.35
root@SIMS_INIT[/soft/pcre/pcre-8.35]#./configure
root@SIMS_INIT[/soft/pcre/pcre-8.35]#make && make install
5.2.4、验证安装
root@SIMS_INIT[/soft/pcre/pcre-8.35]#pcre-config --version
8.35
root@SIMS_INIT[/soft/pcre/pcre-8.35]#
下载地址:
http://nginx.org/download/nginx-1.6.2.tar.gz
cd /soft/nginx
root@SIMS_INIT[/soft/nginx]#ls
nginx-1.6.2 nginx-1.6.2.tar.gz
root@SIMS_INIT[/soft/nginx]#
root@SIMS_INIT[/soft/nginx]#tar -xzvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/soft/pcre/pcre-8.35
注意:
--prefix指定nginx安装目录;
--with-pcre指定pcre的目录,是源码包解压的原始目录。
make
make install
检查安装目录:
root@SIMS_INIT[/usr/local]#cd nginx
root@SIMS_INIT[/usr/local/nginx]#ls
client_body_temp fastcgi_temp logs proxy_temp scgi_temp
conf html nginx.pid sbin uwsgi_temp
root@SIMS_INIT[/usr/local/nginx]#
root@SIMS_INIT[/usr/local/nginx]#/usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.6.2
6.5.1、创建 Nginx 运行的用户和组
groupadd www
useradd -g www www
6.5.2、修改nginx.conf配置文件
vi /usr/local/nginx/conf/nginx.conf
user www www;
worker_processes 2;
#设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit;
#日志位置和日志级别
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
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';
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#下面是server虚拟主机的配置
server
{
listen 80;
#监听端口
server_name SIMS_INIT;
#域名
index index.html index.htm index.php;
root /usr/local/nginx/html;
#站点目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass SIMS_INIT:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
access_log off;
}
}
保存退出。
6.5.3、验证nginx配置文件正确性
不带-c参数标示检查默认的参数文件。
root@SIMS_INIT[/usr/local/nginx/conf]#/usr/local/nginx/sbin/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
带-c参数可以检查指定的配置文件:
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
root@SIMS_INIT[/usr/local/nginx/sbin]#/usr/local/nginx/sbin/nginx
从浏览器访问我们配置的站点ip:
http://10.45.11.118/
# 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reload
# 重启 Nginx
/usr/local/nginx/sbin/nginx -s reopen
# 停止 Nginx
/usr/local/nginx/sbin/nginx -s stop
root@SIMS_INIT[/usr/local]#ls
bin etc games include lib lib64 libexec nginx sbin share src tomcat8.5.40
root@SIMS_INIT[/usr/local]#cp -r tomcat8.5.40 tomcat1
root@SIMS_INIT[/usr/local]#cp -r tomcat8.5.40 tomcat2
root@SIMS_INIT[/usr/local]#ls
bin etc games include lib lib64 libexec nginx sbin share src tomcat1 tomcat2 tomcat8.5.40
root@SIMS_INIT[/usr/local]#mkdir -p /data/www/www/ROOT
拷贝网页实例到多实例根目录下
root@SIMS_INIT[/usr/local]#cp /usr/local/tomcat1/webapps/ROOT/index.jsp /data/www/www/ROOT/
同样,修改tomcat2的配置文件如下:
为什么设置两个多实例的网页目录一致?
因为在工作当中web服务器看的是同一个网页代码,只不过是分成了多个web服务器。这里设置为同一个网页目录相当于起到了多实例负载均衡的效果
/usr/local/tomcat1/bin/startup.sh
/usr/local/tomcat2/bin/startup.sh
至此多实例配置完成。
8.1.1、/usr/local/nginx/conf/nginx.conf
在http{}中新增下面一段内容:
upstream tomcatserver1 {
server 10.45.11.118:8081;
}
upstream tomcatserver2 {
server 10.45.11.118:8082;
}
server {
listen 80;
server_name 8081.max.com;
charset koi8-r;
access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver1;
index index.html index.htm;
}
}
server {
listen 80;
server_name 8082.max.com;
charset koi8-r;
access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver2;
index index.html index.htm;
}
}
保存退出。
8.1.2、重启nginx服务
重启nginx。/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
8.1.3、修改本地hosts文件
在本地hosts文件中增加域名解析
C:\Windows\System32\drivers\etc\hosts
10.45.11.118 8081.max.com
10.45.11.118 8082.max.com
保存退出。
确保本地能ping通:
8.1.4、浏览器访问
使用浏览器分别访问:
8081.max.com
8082.max.com
8.2.1、修改/usr/local/nginx/conf/nginx.conf
新增下面的内容:
upstream www.sayhello.com {
server 10.45.11.118:8081;
server 10.45.11.118:8082;
}
server {
listen 80;
server_name www.sayhello.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://www.sayhello.com;
index index.html index.htm;
}
}
8.2.2、重启nginx服务
重启nginx。/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
8.2.3、修改本地hosts文件
C:\Windows\System32\drivers\etc\hosts
10.45.11.118 www.sayhello.com
保存退出。
确保本地能ping通:
8.2.4、浏览器访问