关闭

CentOS 6.4安装jdk+tomcat+mysql+nginx

标签: CentOStomcatnginxmysqlLinux
2384人阅读 评论(0) 收藏 举报
分类:

安装配置JDK

1、下载安装

上官网下载新版本的JDK,jdk-7u45-linux-i586.tar.gz压缩包,放到Linux下任意目录,cd到该目录,执行以下命令:

mkdir –p /dev-tools/java
tar zxvfjdk-7u45-linux-i586.tar.gz -C /dev-tools/java

解压成功过后,jdk便位于/dev-tools/java下的jdk1.7.0_45目录了,如图



2、配置环境

从第一步,我们知道了jdk的解压目录就位于/dev-tools/java/jdk1.7.0_45,接着配置环境变量,通过修改.bashrc文件实现,执行以下命令:

Vi .bashrc

在文件末尾加上

export JAVA_HOME=/dev-tools/java/jdk1.7.0_45
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
exportPATH=${JAVA_HOME}/bin:$PATH

保存后退出。

执行

source~/.bashrc

修改马上生效

执行java –version能看到版本呢信息,JDK就安装配置成功了,如图


 



 

安装配置Tomcat

1、下载解压

跟JDK一样上官网http://tomcat.apache.org/download-70.cgi下载tomcat压缩包,如apache-tomcat-7.0.47.tar.gz,下载后放入任意目录cd到改目录执行以下命令解压:

mkdir –p /dev-tools/tomcat
tar zxvfapache-tomcat-7.0.47.tar.gz -C /dev-tools/tomcat

解压成功过后如图:




2、配置环境

1)打开端口

解压成功过后如果Linux有设置防火墙,就必须先在防火墙打开指定端口,tomcat默认http端口为8080,一下为打开8080端口的例子:

修改/etc/sysconfig/iptables文件,执行以下命令

vi /etc/sysconfig/iptables

增加一行

-A RH-Firewall-1-INPUT -m state--state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

如图,我把80端口也顺便打开了,以后要用到



保存退出,重启防火墙,执行命令

service iptablesrestart


2)设置开机启动

通过以上的设置,tomcat其实已经可以启动并正常访问了,只要执行

/dev-tools/tomcat/apache-tomcat-7.0.47/bin/startup.sh

就可以启动tomcat了(JDK已经正确安装)如图:


 


但是你每次启动都要输入一次未免太麻烦了,所以我们要设置开机自动启动tomcat。

a)把下面的代码保存为tomcat(蓝色字体为需要修改部分)文件,并让它成为可执行文件

chmod 755 tomcat


#!/bin/bash
#
# kenny kenny.zhou@tom.com
# /etc/rc.d/init.d/tomcat
# init script for tomcat precesses
#
# processname: tomcat
# description: tomcat is a j2se server
# 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=$?

JAVA_HOME=/dev-tools/java/jdk1.7.0_45
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME="/dev-tools/tomcat/apache-tomcat-7.0.47"

case "$1" in
start)
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
            echo $"Starting Tomcat"
            $CATALINA_HOME/bin/startup.sh
        fi
        ;;
stop)
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
            echo $"Stopping Tomcat"
            $CATALINA_HOME/bin/shutdown.sh
        fi
        ;;
*)
        echo $"Usage: $0 {start|stop}"
        exit 1
        ;;
esac

exit $RETVAL

 


b)将tomcat文件拷贝到/etc/init.d/下,并运行:

chkconfig --add tomcat

重启,tomcat就能自动启动了。

PS:

启动tomcat:

 service tomcat start

停止tomcat: 

 service tomcat stop 

 


 

安装配置nginx

1、编译安装nginx

a) 建立安装文件夹

mkdir -p /dev-tools/nginx
mkdir –p /dev-tools/pcre
cd /dev-tools

b) 安装依赖

yum install wget
yum install pcre
yum install openssl*
yum -y install gcc gcc-c++autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devellibxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-develkrb5 krb5-devel libidn libidn-devel openssl openssl-devel openldapopenldap-devel nss_ldap openldap-clients openldap-servers make
yum -y install gd gd2 gd-develgd2-devel

c) 建立用户

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
ulimit -SHn 65535
 

d) 编译安装pcre(蓝色部分为安装路径)

wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gztar zxvf pcre-8.32.tar.gzcd pcre-8.32./configure --prefix=/dev-tools/pcremake && make installcd ../


e)  编译安装nginx((蓝色部分为安装路径))

wget http://nginx.org/download/nginx-1.5.2.tar.gztar zxvf nginx-1.5.2.tar.gzcd nginx-1.5.2./configure --user=www --group=www --prefix=/dev-tools/nginx --with-http_stub_status_module --with-http_ssl_module--with-pcre=/dev-tools/pcre-8.32--with-http_realip_module --with-http_image_filter_modulemakemake installcd ../

一切顺利了,安装结束后,执行

/dev-tools/nginx/sbin/nginx

启动nginx,默认监听80端口,如何打开80端口,看tomcat配置,如图





2、配置nginx与tomcat协作

修改nginx的配置文件

vi /dev-tools/nginx/conf/nginx.conf


如下

#user  nobody;
worker_processes  1;
 
#error_log logs/error.log;
#error_log  logs/error.log  notice;
#error_log logs/error.log  info;
 
#pid       logs/nginx.pid;
 
 
events {
   worker_connections  1024;
}
 
 
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;
 
    server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root  /dev-tools/tomcat/apache-tomcat-7.0.47/webapps/ROOT;
            index  index.jsp index.html index.htm;
        }
 
        #error_page  404              /404.html;
 
        # redirect servererror pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location =/50x.html {
            root   html;
        }
 
 
        # 代理tomcat
        location ~ \.jsp${
            root          /dev-tools/tomcat/apache-tomcat-7.0.47/webapps/ROOT;
            proxy_pass http://127.0.0.1:8080;   #转向tomcat处理
	}
 
 
    }
 
}


保存配置,重新载入配置

/dev-tools/nginx/sbin/nginx -s reload

直接访问80端口下的index.jsp,如图:

 

Nginx已经可以成功将请求交给tomcat了。


3、开机自动启动nginx

vi /etc/rc.d/init.d/nginx

键入以下文本(蓝色字体按照实际修改)

#!/bin/bash

 

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has alot of features, but it's not for everyone.

 

nginxd=/dev-tools/nginx/sbin/nginx

nginx_config=/dev-tools/nginx/conf/nginx.conf

nginx_pid=/var/run/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

   echo "nginxalready running...."

   exit 1

fi

   echo -n $"Starting$prog: "

   daemon $nginxd -c${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ]&& touch /var/lock/subsys/nginx

   return $RETVAL

}

# Stop nginx daemons functions.

stop() {

        echo -n$"Stopping $prog: "

        killproc $nginxd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ]&& rm -f /var/lock/subsys/nginx /var/run/nginx.pid

}

# reload nginx service functions.

reload() {

    echo -n $"Reloading$prog: "

    #kill -HUP `cat${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo

}

# See how we were called.

case "$1" in

start)

        start

        ;;

stop)

        stop

        ;;

reload)

        reload

        ;;

restart)

        stop

        start

        ;;

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage:$prog {start|stop|restart|reload|status|help}"

        exit 1

esac

exit $RETVAL

 

保存后执行一下命令

修改权限

chmod 777 /etc/rc.d/init.d/nginx

添加开机启动

chkconfig --add nginx

chkconfig --level 2345 nginx on

 

PS:

启动mysql: service nginx start

停止mysql:  service nginx stop

 

安装配置MySQL

1、下载安装MySql

执行命令

cd /dev-tools/

wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.14-linux-glibc2.5-i686.tar.gz

tar zxvf mysql-5.6.14-linux-glibc2.5-i686.tar.gz

mv mysql-5.6.14-linux-glibc2.5-i686/ mysql

创建mysql账户与组

/usr/sbin/groupadd mysql

/usr/sbin/useradd -g mysql mysql

建立数据库文件存放的目录,安装数据库,并安装数据库文件到新建的目录

mkdir -p /home/data/mysql/data

/dev-tools/mysql/scripts/mysql_install_db --basedir=/dev-tools/mysql --datadir=/home/data/mysql/data --user=mysql

    把启动脚本中的所有“/usr/local/mysql”路径替换为我们安装目录路径“/dev-tools/mysql

    sed -i"s#/usr/local/mysql#/dev-tools/mysql#g"/dev-tools/mysql/bin/mysqld_safe

 

2、配置MySql

a)  修改/etc/my.cnf如下(蓝色字体为需要修改或者需要添加的)

[mysqld]

datadir=/home/data/mysql/data

socket=/tmp/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assortedsecurity risks

symbolic-links=0

 

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

basedir=/dev-tools/mysql

datadir=/home/data/mysql/data

b)  建立服务,开机自动启动

vi /etc/init.d/mysqld

键入以下文本(蓝色字体按照实际路径填写)

#!/bin/sh

 

# chkconfig: - 85 15

# description: mysql

 

mysql_username="root"

mysql_password=""

 

function_start_mysql()

{

    printf "StartingMySQL...\n"

    /bin/sh /dev-tools/mysql/bin/mysqld_safe 2>&1 >/dev/null &

}

 

function_stop_mysql()

{

    printf "StopingMySQL...\n"

    /dev-tools/mysql/bin/mysqladmin -u${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown

}

 

function_restart_mysql()

{

    printf"Restarting MySQL...\n"

    function_stop_mysql

    sleep 5

    function_start_mysql

}

 

if [ "$1" = "start" ]; then

    function_start_mysql

elif [ "$1" = "stop" ]; then

    function_stop_mysql

elif [ "$1" = "restart" ]; then

function_restart_mysql

else

    printf "Usage:/home/data/mysql/data{start|stop|restart|kill}\n"

fi

设置权限

chmod 777 /etc/init.d/mysqld

添加服务并让他开机自动启动

chkconfig --add mysqld

chkconfig --level 2345 mysqld on

 

PS:

启动mysql: service mysqld start

停止mysql:  service mysqld stop


 


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4720次
    • 积分:86
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    文章分类