欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Linux下Tomcat+Nginx服务器环境安装配置的简明教程

程序员文章站 2022-06-02 13:34:07
一、安装 1.安装jdk 下载的jdk文件为:jdk-6u45-linux-x64.bin,执行如下命令进行安装: #./jdk-6u12-linux-i...

一、安装
1.安装jdk
下载的jdk文件为:jdk-6u45-linux-x64.bin,执行如下命令进行安装:

#./jdk-6u12-linux-i586.bin

2.安装tomcat:

#tar zxvf apache-tomcat-6.0.18.tar.gz
#mv apache-tomcat-6.0.29 tomcat

这里我将解压后的apache-tomcat-6.0.29重命名为了tomcat方便操作。

3.配置环境变量:
编辑/etc下的profile文件,加上如下内容:

java_home="/opt/app/jdk1.6.0_45"
class_path="$java_home/lib:$java_home/jre/lib"
path=".:$path:$java_home/bin"
catalina_home="/opt/app/tomcat"
export java_home catalina_home

执行下面命令使变更生效:

# source /etc/profile

启动tomcat并输入http://domain:8080,如果看到猫的页面即tomcat和jdk安装成功
新建文件目录/home/www为网站存放目录,设置server.xml文件,在host name=”localhost”处将appbase=的指向路径改为/home/www/web
创建index.jsp至/home/www/web/root,内容为:“hello!”  重新启动tomcat,重新访问,如果看到index.jsp文件内容hello!表示设置成功。

4.安装nginx
执行如下命令解压nginx:

# tar zxvf nginx-1.4.4.tar.gz
# mv nginx-1.4.4 nginx

同样重命名了一下。
安装nginx:

# ./configure --prefix=/opt/app/nginx

结果出现了错误:error: c compiler cc is not found,按网上所说安装编译源码所需的工具和库:

#yum install gcc gcc-c++ ncurses-devel perl

再次安装,发现还有错误:the http rewrite module requires the pcre library.
执行

# yum -y install pcre-devel openssl openssl-devel

终于成功,

# ./configure --prefix=/opt/app/nginx
# make
# make install

nginx安装成功后的安装目录为/opt/app/nginx

二、nginx方面的配置
在/usr/local/目录下分别安装nginx和tomcat
为nginx添加conf.d目录,用于各项目的配置,比如新建renhetoutiao.conf文件

server {
  listen   80;
  server_name devtoutiao.renhe.cn;
  root    /home/renhetoutiao/renhetoutiao/htdocs;
  access_log /home/renhetoutiao/renhetoutiao/logs/access_log.log;
 
  location = / {
    rewrite ^/$ /index.shtml last;
  }
 
  location ~ .shtml {
    proxy_pass http://localhost:8081;
    proxy_set_header  host  $host;
    proxy_set_header  x-real-ip  $remote_addr;
    proxy_set_header  x-forwarded-for $proxy_add_x_forwarded_for;
  }
 
  location ~ .*.(js|css)?$ {
    expires 1h;
  }
}

server {
  listen   80;
  server_name devtoutiao.renhe.cn;
  root    /home/renhetoutiao/renhetoutiao/htdocs;
  access_log /home/renhetoutiao/renhetoutiao/logs/access_log.log;
 
  location = / {
    rewrite ^/$ /index.shtml last;
  }
 
  location ~ .shtml {
    proxy_pass http://localhost:8081;
    proxy_set_header  host  $host;
    proxy_set_header  x-real-ip  $remote_addr;
    proxy_set_header  x-forwarded-for $proxy_add_x_forwarded_for;
  }
 
  location ~ .*.(js|css)?$ {
    expires 1h;
  }
}

对nginx的conf/nginx.conf修改,第二行 user root,赋予nginx root权限,在http中添加 include /usr/local/nginx/conf.d/*.conf;
选择一个项目如renhetoutiao, 目录/home/renhetoutiao/renhetoutiao/

三、tomcat方面的配置
catalina.policy catalina.properties context.xml tomcat-users.xml server.xml web.xml
这5个配置文件就可以启动一个tomcat实例,其中server.xml是必须配置的。
catalina.policy catalina.properties web.xml 可以使用默认的,可以从安装路径copy,context.xml tomcat-users.xml可以不用。
所以每个项目我们只需要配置server.xml, 如renhetoutiao项目的配置

<?xml version='1.0' encoding='utf-8'?>
<server port="8015" shutdown="shutdown">
 
  <listener classname="org.apache.catalina.core.aprlifecyclelistener"
    sslengine="on" />
  <listener classname="org.apache.catalina.core.jasperlistener" />
  <listener classname="org.apache.catalina.core.jrememoryleakpreventionlistener" />
  <listener classname="org.apache.catalina.mbeans.serverlifecyclelistener" />
  <listener classname="org.apache.catalina.mbeans.globalresourceslifecyclelistener" />
 
  <service name="catalina">
 
    <connector port="8081" protocol="http/1.1" connectiontimeout="20000" redirectport="8443" />
 
    <engine name="catalina" defaulthost="localhost">
      <host name="localhost" appbase="webapps" unpackwars="true" autodeploy="false" xmlvalidation="false" xmlnamespaceaware="false">
        <context docbase="/home/renhetoutiao/renhetoutiao/web" path="" />
      </host>
    </engine>
  </service>
</server>

<?xml version='1.0' encoding='utf-8'?>
<server port="8015" shutdown="shutdown">
 
  <listener classname="org.apache.catalina.core.aprlifecyclelistener"
    sslengine="on" />
  <listener classname="org.apache.catalina.core.jasperlistener" />
  <listener classname="org.apache.catalina.core.jrememoryleakpreventionlistener" />
  <listener classname="org.apache.catalina.mbeans.serverlifecyclelistener" />
  <listener classname="org.apache.catalina.mbeans.globalresourceslifecyclelistener" />
 
  <service name="catalina">
 
    <connector port="8081" protocol="http/1.1" connectiontimeout="20000" redirectport="8443" />
 
    <engine name="catalina" defaulthost="localhost">
      <host name="localhost" appbase="webapps" unpackwars="true" autodeploy="false" xmlvalidation="false" xmlnamespaceaware="false">
        <context docbase="/home/renhetoutiao/renhetoutiao/web" path="" />
      </host>
    </engine>
  </service>
</server>

因为使用nginx,tomcat的ajp不会使用,所以可以去掉,tomcat可以这样简单配置。注意每个项目的连接端口和关闭端口,不要有重复。
因为tomcat回去加载指定路径下得class lib,所以必须把配置、依赖等copy至web下,启动脚本bin/startup,关闭脚本bin/shutdown 如下:

#!/bin/sh
 
export catalina_base="$pwd"
export catalina_home="/usr/local/tomcat"
 
#启动为适配tomcat目录做的准备
#迁移tomcat必要的配置文件 catalina.policy catalina.properties context.xml tomcat-users.xml web.xml
if [ ! -f "$catalina_base"/conf/catalina.policy ]; then
  cp "$catalina_home"/conf/catalina.policy "$catalina_base"/conf
fi
 
if [ ! -f "$catalina_base"/conf/catalina.properties ]; then
    cp "$catalina_home"/conf/catalina.properties "$catalina_base"/conf
fi
 
if [ ! -f "$catalina_base"/conf/web.xml ]; then
    cp "$catalina_home"/conf/web.xml "$catalina_base"/conf
fi
 
if [ ! -d "$catalina_base"/logs ]; then
 mkdir logs
fi
 
#准备classes和lib目录
classes_path="$catalina_base"/web/web-inf/classes
lib_path="$catalina_base"/web/web-inf/lib
rm -rf "$classes_path"
rm -rf "$lib_path"
cp -r "$catalina_base"/conf "$classes_path"
cp -r "$catalina_base"/lib "$lib_path"
 
#启动时会记录进程id,存放于logs/tomcat.pid内
#shutdown时强制关闭该进程
$catalina_home/bin/startup.sh
#!/bin/sh
 
export catalina_base="$pwd"
export catalina_home="/usr/local/tomcat"
 
#启动为适配tomcat目录做的准备
#迁移tomcat必要的配置文件 catalina.policy catalina.properties context.xml tomcat-users.xml web.xml
if [ ! -f "$catalina_base"/conf/catalina.policy ]; then
  cp "$catalina_home"/conf/catalina.policy "$catalina_base"/conf
fi
 
if [ ! -f "$catalina_base"/conf/catalina.properties ]; then
    cp "$catalina_home"/conf/catalina.properties "$catalina_base"/conf
fi
 
if [ ! -f "$catalina_base"/conf/web.xml ]; then
    cp "$catalina_home"/conf/web.xml "$catalina_base"/conf
fi
 
if [ ! -d "$catalina_base"/logs ]; then
 mkdir logs
fi
 
#准备classes和lib目录
classes_path="$catalina_base"/web/web-inf/classes
lib_path="$catalina_base"/web/web-inf/lib
rm -rf "$classes_path"
rm -rf "$lib_path"
cp -r "$catalina_base"/conf "$classes_path"
cp -r "$catalina_base"/lib "$lib_path"

#启动时会记录进程id,存放于logs/tomcat.pid内
#shutdown时强制关闭该进程
$catalina_home/bin/startup.sh

#!/bin/sh
 
export catalina_base="$pwd"
export catalina_home="/usr/local/tomcat"
 
$catalina_home/bin/shutdown.sh

#!/bin/sh
 
export catalina_base="$pwd"
export catalina_home="/usr/local/tomcat"
 
$catalina_home/bin/shutdown.sh

通过tomcat的startup.sh脚本,会碰到项目中分配的线程没有及时释放,tomcat不能及时关闭,导致重启时产生端口冲突
解决方案:强制关闭tomcat进程,kill -9 pid,执行脚本需要启动时记录各个tomcat实例的进程,这里将进程pid保存在项目的logs目录下。
记录pid的操作需要修改tomcat安装目录下 bin/catalina.sh脚本
在prgdir=dirname "$prg"后面加上

tomcat_pid="$pwd"/"logs/tomcat.pid"
if [ -f "$tomcat_pid" ]; then
  catalina_pid="$tomcat_pid"
elif [ $1 = "start" ]; then
  catalina_pid="$tomcat_pid"
fi

tomcat_pid="$pwd"/"logs/tomcat.pid"
if [ -f "$tomcat_pid" ]; then
  catalina_pid="$tomcat_pid"
elif [ $1 = "start" ]; then
  catalina_pid="$tomcat_pid"
fi

最后需要给项目用户可执行权限 chmod 777 catalina.sh