centos 下搭建 web 运行环境

安装Java环境

  检查是否已装有 Java 环境。

[root@localhost /]# java -version
java version "1.7.0_65"
OpenJDK Runtime Environment (rhel-2.5.1.2.el6_5-x86_64 u65-b17)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)

  如果没有则安装,则安装 JDK

  • yum 方式安装
[root@localhost /]# yum -y install java-1.7.0-openjdk
  • tar 方式安装

  强烈建议安装 ORACLE 的 JDK 包,此处安装 JDK7, 到如下网站下载: http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk7-downloads-1880260.html

[root@localhost /]# ls
jdk-7u79-linux-x64.tar.gz
[root@localhost /]# tar xzf jdk-7u79-linux-x64.tar.gz
[root@localhost /]# mv jdk1.7.0_79 /usr/local/java

  添加环境变量 vim /etc/profile ,在文件末尾添加:

#更改为自己实际路径
[root@localhost /]# vim /etc/profile
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

  添加完成后,按 Esc 键,光标会移动到末行,输入 :wq 后回车即可保存成功。

  使环境文件立即生效:

[root@localhost /]# source /etc/profile

卸载jdk

  • 卸载系统自带的 JDK 版本:
查看自带的 JDK : 
[root@localhost /]# rpm -qa|grep gcj 

libgcj-4.1.2-44.el5 
java-1.4.2-gcj-compat-1.4.2.0-40jpp.115 
#使用rpm -e --nodeps 命令删除上面查找的内容 
[root@localhost /]# rpm -e –nodeps java-1.4.2-gcj-compat-1.4.2.0-40jpp.115 
  • 卸载 rpm 安装的 JDK 版本
#查看安装的 JDK : 
[root@localhost /]# rpm -qa|grep jdk 
jdk-1.6.0_22-fcs 
#卸载
[root@localhost /]# rpm -e --nodeps jdk-1.6.0_22-fcs   
  • 找到 JDK 安装目录的 _uninst 子目录
       在 shell 终端执行命令 ./uninstall.sh 即可卸载 JDK
[root@localhost /]# cd java-1.6.0
[root@localhost /]# ./uninstall.sh

安装tomcat

  • 检查是否安装 Tomcat
[root@localhost /]# rpm -qa|grep tomcat
  • 安装
[root@localhost /]# cd /tmp
[root@localhost tmp]# wget http://apache.fayea.com/tomcat/tomcat-7/v7.0.72/bin/apache-tomcat-7.0.72.tar.gz
[root@localhost tmp]# tar xzf apache-tomcat-7.0.72.tar.gz
[root@localhost tmp]# mv apache-tomcat-7.0.72 /usr/local/tomcat7
  • 加入环境变量
[root@localhost /]# vim /etc/profile

JAVA_HOME=/usr/local/java
CLASSPATH=$JAVA_HOME/lib/
CATALINA_HOME=/usr/local/tomcat                     #tomcat所在路径
PATH=$PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
export PATH JAVA_HOME CLASSPATH CATALINA_HOME
  • 验证安装结果

  启动服务

  进入解压目录的bin文件夹,运行命令 ./startup.sh
会自动弹出像如下的提示

[root@localhost bin]# ./startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

  关闭服务

[root@localhost bin]# ./shutdown.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
OpenJDK 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0

  查看进程

[root@localhost bin]# ps aux | grep tomcat
root     31810  177  1.4 2986640 228476 pts/0  Sl   11:22   0:03 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms256m -Xmx512m -Xss1024K -XX:PermSize=128m -XX:MaxPermSize=256m -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -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     31826  0.0  0.0 112724   996 pts/0    R+   11:22   0:00 grep --color=auto tomcat

快速启动tomcat脚本

  在 Linux 下搭建好 Tomcat 之后,每次启动和关闭都要去 Tomcat 的 bin 目录下执行 ./startup.sh 和 ./shutdown.sh。

  这是很不方便的,下面介绍如何像执行 ls mv cp 等命令一样启动,关闭,重启 Tomcat.

  • 创建脚本

  脚本创建路径 /etc/init.d/tomcat
  首先执行:

[root@localhost /]# vi /etc/init.d/tomcat   

  按 i 编辑,将下面内容根据你自己的情况(修改 /opt/java/tomcat8 为你的 Tomcat 路径)
修改后 copy 到编辑框中, esc 退出编辑,:wq 保存退出

# !/bin/bash    
# Description: start or stop the tomcat     
# Usage:        tomcat [start|stop|restart]    
#     
#指定java路径
export JAVA_HOME=/usr/local/jdk1.8.0_161
export JRE_HOME=/usr/local/jdk1.8.0_161/jre

case "$1" in  
start)  
#startup the tomcat
#切换到tomcat bin路径    
cd /usr/local/tomcat/bin  
./startup.sh  
;;  
stop)  
# stop tomcat    
cd /usr/local/tomcat/bin
./shutdown.sh  
echo "Tomcat Stoped"  
;;  
restart)  
$0 stop  
$0 start  
;;  
*)  
echo "tomcat: usage: tomcat [start|stop|restart]"  
exit 1  
esac  
exit 0  
  • 为脚本添加执行权限
[root@localhost bin]# chmod +x /etc/init.d/tomcat
  • 快捷命令
[root@localhost bin]# service tomcat start  #启动
[root@localhost bin]# service tomcat stop  #关闭
[root@localhost bin]# service tomcat restart  #重启
  • 加入开机自启
[root@localhost bin]# vim  /etc/rc.d/rc.local

  文件末行加入 :service tomcat start

  加入权限启用:

[root@localhost bin]# chmod 777 /etc/rc.d/rc.local

你可能感兴趣的:(centos,web环境搭建)