Linux下Tomcat+Nginx服务器环境安装配置的简明教程
一、安装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_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-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_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-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
文档来源:服务器之家http://www.zzvips.com/article/28703.html
页:
[1]