CentOS操作系统下安装Java Web应用攻略(上)

来源:互联网 发布:剑网三不合法的脸数据 编辑:程序博客网 时间:2024/06/06 01:28


vi命令的基本操作:

 a进入编辑模式;

Ctrl+C终止编辑模式;

在非编辑模式按:wq 保存文件退出。

在非编辑模式按:q! 不保存退出。

在非编辑模式按dd,可删除鼠标所在的当前行。

 

第一章 挂载硬盘

 

  1. 购买阿里云CentOS主机

     

    注意事项:最好选择SSD硬盘。

    建议买50G系统盘+ 数据盘,例如150G数据盘,单独购买数据盘后,登录CentOS主机需要挂载数据盘。挂载过程,用SSH工具登录 CentOS

     

  1. 查看硬盘:

    fdisk -l fdisk -lu

    显示

     

    可以看到/dev/vdb的硬盘大小为161.1GB

  1. 硬盘格式化

     

       确认数据盘/dev/vdb是新购买的没有数据的硬盘,执行格式化命令:

     

       mkfs -t ext4 /dev/vdb

       ext4是文件系统格式,/dev/vdb是要格式化的硬盘

    -t ext4 表示将分区格式化成ext4文件系统类型。

     

  1. 格式化硬盘后,需要挂载此硬盘

    先用 df -l  命令查看挂载情况,显示

     

    FileSystem下未显示/dev/vdb.

     

    然后创建一个目录/root/web,这个目录以后用于存放网站程序,建目录命令:

     

    mkdir /root/web

     

    然后将格式化的/dev/vdb盘与/root/web关联。关联命令:

    mount -t ext4 /dev/vdb /root/web/

    这时再用 df命令查看:

    可看到/dev/vdb挂载到/root/web

    如果取消挂载的目录,可使用 umount /dev/vdb

     

  2. 启动系统自动挂载硬盘

     

         需要设置在启动的时候自动挂载硬盘,这样不用每次重启机器都使用mount命令,首先查看下硬盘的UUID,使用命令:  blkid 显示:

     

    复制/dev/vdbUUID,编辑/etc/fstab,使用命令:

    vim /etc/fstab

    按一下a进入插入模式,在末尾增加:

    UUID=db8cc13d-3854-4ea6-ac11-12d6b879698b /root/web   ext4    defaults    0  1

    然后按CRTL+C,再按:wq 并回车

     

  3. 测试自动挂载

       cd /root/web

       建个新目录 mkdir test

       如果重启后,cd /root/web , 然后ls查看目录内容,则应该有test目录。

       重启使用 shutdown -r now 命令重启机器。

     

  1. 安装Nginx

     

     参考http://www.linuxidc.com/Linux/2016-09/134907.htm

  1. gcc安装

yum install gcc-c++

2 PCRE pcre-devel 安装

yum install -y pcre pcre-devel

3 zlib 安装

yum install -y zlib zlib-devel

4  OpenSSL 安装

yum install -y openssl openssl-devel

 

5、官网下载

Nginx官方网站找nginx最新版本

 https://nginx.org/en/download.html

目前最新版本为nginx-1.13.3,复制这个版本的下载地址连接

https://nginx.org/download/nginx-1.13.3.tar.gz

使用wget下载:

wget https://nginx.org/download/nginx-1.13.3.tar.gz

然后解压:

tar -zxvf nginx-1.13.3.tar.gz

解压后,进入解压目录:

cd  nginx-1.13.3

 

 

 

  1. 配置

     进入了解压目录后,运行:

./configure

 

以上命令是默认安装。

 

  1. 编译安装(在解压的nginx-1.13.3目录)

make

make install

 

查找安装路径

whereis nginx

显示安装的路径为/ur/local/nginx

 

启动、停止nginx

cd /usr/local/nginx/sbin/

./nginx

./nginx -s stop

./nginx -s quit

./nginx -s reload

./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

查询nginx进程:

ps aux|grep nginx

重启 nginx

1.先停止再启动(推荐):
nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:

./nginx -s quit

./nginx

 

2.重新加载配置文件:
ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:
./nginx -s reload

启动成功后,在浏览器可以看到这样的页面:

开机自启动

即在rc.local增加启动代码就可以了。

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx
设置执行权限:

chmod 755 rc.local

 

 

然后shutdown -r now 重启测试nginx是否重启后自动运行。

重启后,ps -ef|grep nginx没有发现进程。

发现/etc/rc.local是物理连接到 /erc/rc.d/rc.local

进入rc.d目录,chmod 755 rc.local重启后,查看nginx:

 

已经有nginx进程,然后在浏览器输入这个nginx服务器的地址,显示下面的页面:

 

到此Nginx安装成功。

 

后续还要为Nginx做更多的设置,比如如何与tomcat程序关联。

 

  1. NFS安装

     

    NFS Server安装:

     

    NFS安装包括NFS Server安装和NFS Client端的安装。

    安装NFS是为了集群环境下,多台Tomcat应用可以把图片、htmlcss,js等静态资源文件统一存储,无论从tomcat那个节点访问网站都可以读取到静态资源。用户无论从那个tomcat上传图片,另一个tomcat应用也能访问到这个图片。

     

    1安装nfs

    yum -y install nfs-utils rpcbind

     

2在服务器和客户端两台机器上创建共享目录/root/web/webapps/nfs

(为了方便文件上传,直接创建到tomcat的应用目录中)

 

 

 

 

 

3在服务器上编辑/etc/exports

vi /etc/exports

 

加上下面两句:

/root/web/webapps/nfs/ 10.46.177.154(rw,no_root_squash,no_all_squash,sync)

/root/web/webapps/nfs/ 10.46.163.20(rw,no_root_squash,no_all_squash,sync)

以上10.开头的IP地址是允许NFS 客户端访问的IP地址,可以写内网地址。根据实际情况修改。

 

4 使刚才的配置生效

exportfs -r

显示 exportfs: IP:/root/web/webapps/nfs: Function not implemented

 

这个可能是因为NFS 客户端暂时未安装NFS。等客户端安装后再检查。

 

5启动

service rpcbind start

service nfs start

6查看状态

showmount -e 172.17.212.157    (NFS Server本机的IP地址)

显示:

Export list for 172.17.212.157:

/root/web/webapps/nfs 172.17.212.158

显示配置的容许连接的NFS 客户端。

 

为确保NFS Server在重启后能自动运行,在重启后可执行下showmount -e 172.17.212.157

如果显示其他信息,则需要执行:

 

service rpcbind start

service nfs start

 

 

NFS Client安装:

 

  1. 客户端安装NFS Client(Tomcat 主机)

yum -y install nfs-utils rpcbind

2. 在客户端挂载服务器端的共享目录 (先创建一个/root/web/webapps/nfs/目录)

mount  -t nfs 172.17.212.157:/root/web/webapps/nfs/  /root/web/webapps/nfs/

  1. 客户端启动nfs

    service rpcbind start

       172.17.212.157/root/web/webapps/nfs下创建一个目录,比如abc,在客户端的/root/web/webapps/nfs/目录下看下是否显示.

       重启服务器再进入/root/web/webapps/nfs/,没有发现172.17.212.157nfs目录里新增的文件夹,在/etc/fstab中末尾增加:

    172.17.212.157:/root/web/webapps/nfs /root/web/webapps/nfs  nfs rsize=8192,wsize=8192,timeo=14,intr

重启机器,shutdown -r now,

再看/root/web/webapps/nfs,可以看到172.17.212.157中对应目录的文件夹,说明重启自动mount成功.rsize=8192,wsize=8192,timeo=14,intr等参数可考虑如何优化读写速度。)

自动mount成功后可加入更多的目录映射,根据项目,例如:

172.17.212.157:/root/web/webapps/nfs/site/company  /root/web/webapps/site/company nfs rsize=8192,wsize=8192,timeo=14,intr

 

 

172.17.212.157:/root/web/webapps/nfs/images  /root/web/webapps/images nfs rsize=8192,wsize=8192,timeo=14,intr

 

 

172.17.212.157:/root/web/webapps/nfs/userfiles /root/web/webapps/userfiles nfs rsize=8192,wsize=8192,timeo=14,intr

 

 

172.17.212.157:/root/web/webapps/nfs/uploadImg  /root/web/webapps/uploadImg nfs rsize=8192,wsize=8192,timeo=14,intr

 

 

172.17.212.157:/root/web/webapps/nfs/upload  /root/web/webapps/upload nfs rsize=8192,wsize=8192,timeo=14,intr

 

 

172.17.212.157:/root/web/webapps/nfs/searchIndex  /root/web/webapps/searchIndex nfs rsize=8192,wsize=8192,timeo=14,intr

 

 

172.17.212.157:/root/web/webapps/nfs/pubinfo  /root/web/webapps/pubinfo nfs rsize=8192,wsize=8192,timeo=14,intr

 

 

172.17.212.157:/root/web/webapps/nfs/WEB-INF/luceneIndex  /root/web/webapps/WEB-INF/luceneIndex nfs rsize=8192,wsize=8192,timeo=14,intr

 

 

172.17.212.157:/root/web/webapps/nfs/WEB-INF/uploadfiles  /root/web/webapps/WEB-INF/uploadfiles nfs rsize=8192,wsize=8192,timeo=14,intr

 

  1. 服务器文件迁移

     

    当服务器进行文件迁移时(比如更换服务器,需要将原服务器的文件迁移到新的服务器),可以考虑的迁移方案:

  1. 迁移文件打包

  2. scp命令复制(从原服务器复制到目标服务器)

  3. find增量备份、增量压缩及复制。(比如6.1号开始迁移,复制了大量的文件,6.3号全部部署完毕,这期间原服务器增加了一些图片、文件,需要采用增量复制的方案)。

     

    (稍后补充文档说明)。

 

  1. 安装Java环境

     

  1. 使用 jdk-7u80-linux-x64.tar.gz版本。

  2. mkdir /usr/java

  3. 将此文件解压到/usr/java目录:

       tar -xzvf jdk-7u80-linux-x64.tar.gz -C /usr/java

     

  4. vi /etc/profile 增加以下内容(注意检查下word里不要有中文空格):

JJAVA_HOME="/usr/java/jdk1.7.0_80"

JRE_HOME="/usr/java/jdk1.7.0_80/jre"

CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

JAVA_OPTS="-server -Xss256k -Xms1024m -Xmx1024m -XX:MaxNewSize=256m -XX:SurvivorRatio=6 -XX:MaxPermSize=300m -XX:+UseParallelGC -XX:ParallelGCThreads=24 -XX:+UseParallelOldGC -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Dsun.lang.ClassLoader.allowArraySyntax=true -Dfile.encoding=UTF-8"

export JAVA_HOME

export JRE_HOME

export CLASSPATH

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC JAVA_OPTS

 

ulimit -SHn 65535

 

以上参数根据实际情况调整。

  1. source /etc/profile使配置生效。

  2. 使用java -version查看版本,同时检查java是否可用。

     

  1. 安装Tomcat

     

    1、目前选择 Tomcat版本:

    apache-tomcat-7.0.68.tar.gz

  1. 解压到/usr/local目录 :

    tar -xzvf apache-tomcat-7.0.68.tar.gz -C /usr/local

    进入/usr/local:

    cd /usr/local

    更名为tomcat7:

    mv apache-tomcat-7.0.68 tomcat7

 3、运行tomcat

 cd /usr/local/tomcat7/bin

./startup.sh

显示

Tomcat started.

然后 cd ..

cd logs

tail -200f catalina.out

检查日志输出.如果发现日志中出现

Deploying web application directory    /usr/local/tomcat7/webapps/host-manager 并长时间卡在这里不动(通过后显示INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [134,709] milliseconds.),可关掉tomcat,修改jre中的配置。具体过程:

 

Ctrl+C  中断log日志显示;

ps -ef|grep tomcat

kill -9 进程号

cd  /usr/java/jdk1.7.0_80/jre/lib/security

vi java.security

找到 securerandom.source=file:/dev/urandom

改为securerandom.source=file:/dev/./urandom

然后再启动tomcat.

为了防止针对Tomcat的网络攻击,须删除一些tomcat目录和文件,须删除的文件:

cd /usr/local/tomcat7/conf

tomcat-users.xml删除或更名。

删除: rm  tomcat-users.xml

更名  mv  tomcat-users.xml tomcat-users.old

cd /usr/local/tomcat7/webapps

删除除ROOT之外的所有的目录

rm -rf docs

rm -rf examples

rm -rf host-manager

rm -rf manager

注意删除后,重启tomcat

 

如果需要修改tomcat的服务端口:

须打开/usr/local/tomcat7/conf/server.xml

找到:

 <Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

8080改为8088,另外因为一般项目都使用UTF-8字符集,所以redirectPort="8443" 后面须增加:

 URIEncoding="UTF-8"

修改后的配置:

 <Connector port="8088" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443"  URIEncoding="UTF-8"/>

 

 

目前的tomcat没有部署项目应用,因为阿里云默认没放开8080端口,如果要查看tomcat运行的页面,需要在nginx服务器中对nginx.conf进行配置。

 

 打开Nginx主机的nginx.conf:

 

cd /usr/local/nginx/conf

vi nginx.conf

server { 上面增加一个负载均衡设置:

 

upstream  mydomain.com

{

 

server 172.17.212.158:8088 weight=1;

}

见下图:

 

下面这段每行左侧加#

改后:

 

下面增加:

 location /

       {

           # root   html;

           #  index  index.html index.htm;

           # proxy_pass http://openjweb.com/ ;

           #proxy_pass http://localhost ;

           proxy_pass http://mydomain.com/;

           proxy_set_header Host $host;

           proxy_redirect off;

           proxy_set_header X-Real-IP $remote_addr;

           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

           #proxy_connect_timeout 60;

           proxy_connect_timeout 300;

           proxy_read_timeout 600;

           proxy_send_timeout 600;

      }

 

然后重启nginx:

/usr/local/nginx/sbin/nginx -s reload

tomcat8080改为了8088,检查tomcat进程(ps -ef|grep tomcat),如果有,使用kill -9 进程号杀掉,启动tomcat:

/usr/local/tomcat7/bin/startup.sh

访问nginx对应的外网地址(不带8088端口),可看到tomcat页面:

 

 

在集群环境中为了实现tomcat的集群会话,一般需要配置Redis作为分布式缓存。配置Tomcat+Redis在后面安装完Redis再做介绍。

 

原创粉丝点击