Linux服务器应用实战——服务环境快速搭建

原创
2015/10/21 23:59
阅读数 479

基于CentOS操作系统如何快速搭建包括GIT、Maven、Tomcat、Java、MySQL和Nginx等服务环境,如何基于YMP架框脚手架工具快速创建独立JVM的Tomcat服务。

最近一段时间公司的Windows服务器频频中招,实在无法忍受,于是乎决定改用Linux系统! 趁现在服务器正在扫描病毒,把今天购买的阿里云服务器的配置过程整理一下,写个快速搭建环境的手册,与小伙伴们分享!

目标:

本文将描述在CentOS 7操作系统中安装以下服务:

  • Git
  • Maven
  • Tomcat
  • Java
  • MySQL
  • Nginx

安装GIT

阿里云的CentOS镜像中默认已经安装了Git v1.8.3.1,若未安装请执行以下命令:

// 通过yum安装
yum install git
// 查看Git版本
git --version

安装Maven

// 下载Maven安装包,若当前地址无效,请访问官网: http://maven.apache.org/
wget http://apache.fayea.com/maven/maven-3/3.3.3/binaries/apache-maven-3.3.3-bin.tar.gz
// 解压
tar zxvf apache-maven-3.3.3-bin.tar.gz
// 

安装Tomcat

// 下载Tomcat安装包,若当前地址无效,请访问官网: http://tomcat.apache.org/
wget http://apache.fayea.com/tomcat/tomcat-7/v7.0.64/bin/apache-tomcat-7.0.64.tar.gz
// 解压
tar zxvf apache-tomcat-7.0.64.tar.gz

安装Java

// 访问官网 http://www.oracle.com/technetwork/java/javase/downloads/index.html
// 下载Java安装包(先在本机开始下载后,再复制下载链接地址)
wget http://download.oracle.com/otn-pub/java/jdk/8u65-b17/jdk-8u65-linux-x64.tar.gz?AuthParam=1445393430_77c75c42039e71d236a2a146b48c185c
// 解压
tar zxvf jdk-8u65-linux-x64.tar.gz\?AuthParam\=1445393430_77c75c42039e71d236a2a146b48c185c

以上操作已将Git、Maven、Tomcat和Java都准备好了,接下来配置环境变量:

// 编辑.bash_profile文件
vi ~/.bash_profile

修改后文件内容如下:

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

JAVA_HOME=/root/jdk1.8.0_65
MAVEN_HOME=/root/apache-maven-3.3.3

PATH=$PATH:$HOME/bin:$JAVA_HOME/bin:$MAVEN_HOME/bin

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
export MAVEN_OPTS=-Dfile.encoding=UTF-8

export JAVA_HOME
export MAVEN_HOME

export PATH

保存文件后别忘记执行一下才能生效

// 执行source
source .bash_profile

环境变量配置完毕,可以验证一下:

// 执行命令
java -version
// 输出
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

// 执行命令
mvn -version
// 输出
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)
Maven home: /root/apache-maven-3.3.3
Java version: 1.8.0_65, vendor: Oracle Corporation
Java home: /root/jdk1.8.0_65/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-123.9.3.el7.x86_64", arch: "amd64", family: "unix"

好了,到此为止我们已经配置好了Java、Maven环境,接下来我们开始搭建Tomcat服务,大家可以会想到,下载安装包再解压就可以了,No!

今天我要阐述的是如何通过一个Tomcat搭建多个独立JVM的的Tomcat服务,让我们开始吧!

// 创建个目录来存储代码吧
mkdir ~/workspaces
// 通过Git下载YMP v1的源码
cd ~/workspaces/
git clone http://git.oschina.net/suninformation/ymateplatform.git
// 下载完毕,接着通过Maven进行编译安装
cd ymateplatform/
mvn install

好了,屏幕一阵乱滚后,接下来上正菜,创建我们的第一个Tomcat服务

// 先创建一个目录来存放我们的Tomcat服务文件
mkdir ~/servers
// 进入YMP工程目录
cd ymateplatform/
// 执行下面的命令,其中:
// catalina.home参数:指定Tomcat软件包的位置
// catalina.base参数:指定生成的Tomcat服务存放的位置
// tomcat.version参数:指定Tomcat软件包的版本,YMP目前支持6和7两个版本
// host.name参数:指定此Tomcat服务的名称,一般设置为域名
// host.alias参数:指定Tomcat服务的别名
// service.name参数:指定Windows环境下注册的服务名称
// server.port参数:指定Tomcat服务的Server端口
// connector.port参数:指定Tomcat服务的Connector端口
// redirect.port参数:指定Tomcat服务的Redirect端口
// ajp.port参数:指定Tomcat服务的AJP端口
mvn compile exec:java -Dexec.mainClass="net.ymate.platform.extra.scaffold.TomcatServScaffold" -Dcatalina.home="/root/apache-tomcat-7.0.64" -Dcatalina.base="/root/servers/demoServ" -Dtomcat.version="7" -Dhost.name="www.ymate.net" -Dhost.alias="ymate.net" -Dservice.name="demoServ" -Dserver.port="8005" -Dconnector.port="8080" -Dredirect.port="8443" -Dajp.port="8009"

又是一阵屏幕乱滚后,输出如下内容:

[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ ymate-platform ---
log4j:WARN No appenders could be found for logger (freemarker.cache).
log4j:WARN Please initialize the log4j system properly.
Output file: /root/servers/demoServ/conf/server.xml
Output file: /root/servers/demoServ/vhost.conf
Output file: /root/servers/demoServ/bin/install.bat
Output file: /root/servers/demoServ/bin/manager.bat
Output file: /root/servers/demoServ/bin/shutdown.bat
Output file: /root/servers/demoServ/bin/startup.bat
Output file: /root/servers/demoServ/bin/uninstall.bat
Output file: /root/servers/demoServ/bin/manager.sh
Output file: /root/servers/demoServ/webapps/ROOT/index.jsp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.591 s
[INFO] Finished at: 2015-10-21T22:41:02+08:00
[INFO] Final Memory: 11M/26M
[INFO] ------------------------------------------------------------------------

恭喜,我们的第一个Tomcat服务创建成功了!我们先看看生成的目录结构:

cd ~/server/demoServ
ls -al
// 输出内容:
drwxr-xr-x 8 root root 4096 10月 21 10:42 .
drwxr-xr-x 5 root root 4096 10月 21 22:41 ..
drwxr-xr-x 2 root root 4096 10月 21 10:42 bin
drwxr-xr-x 3 root root 4096 10月 21 10:52 conf
drwxr-xr-x 2 root root 4096 10月 21 10:52 logs
drwxr-xr-x 2 root root 4096 10月 21 10:42 temp
-rw-r--r-- 1 root root  560 10月 21 10:42 vhost.conf
drwxr-xr-x 3 root root 4096 10月 21 10:42 webapps
drwxr-xr-x 3 root root 4096 10月 21 10:52 work

是不是和Tomcat本身的目录结构一样呢,没错,demoServ服务的所有文件都将在存放在这里,与Tomcat软件包完成隔离开来。 这里多了个vhost.conf文件,里面已经帮你写好了Tomcat与Apache Server集成所需要的配置,:)YMP贴心不?!

好了,让我们启动demoServ服务看看效果吧,等等...还差一步哦!

// 先看一下demoServ/bin目录里的文件
cd ~/server/demoServ/bin
ls -al
drwxr-xr-x 2 root root 4096 10月 21 10:42 .
drwxr-xr-x 8 root root 4096 10月 21 10:42 ..
-rw-r--r-- 1 root root  431 10月 21 10:42 install.bat
-rw-r--r-- 1 root root  455 10月 21 10:42 manager.bat
-rwxr--r-- 1 root root  345 10月 21 10:42 manager.sh
-rw-r--r-- 1 root root  401 10月 21 10:42 shutdown.bat
-rw-r--r-- 1 root root  401 10月 21 10:42 startup.bat
-rw-r--r-- 1 root root  432 10月 21 10:42 uninstall.bat

因为我们在Linux系统里,只需要manager.sh文件就够了,其它的文件都是在Windows里才会用到。 现在,需要给manager.sh文件可执行的权限:

chmod +x manager.sh

OK,让我启动demoServ服务吧!

// 执行start命令
./manager.sh start
// 输出
Using CATALINA_BASE:   /root/servers/demoServ
Using CATALINA_HOME:   /root/apache-tomcat-7.0.64
Using CATALINA_TMPDIR: /root/servers/demoServ/temp
Using JRE_HOME:        /root/jdk1.8.0_65
Using CLASSPATH:       /root/apache-tomcat-7.0.64/bin/bootstrap.jar:/root/apache-tomcat-7.0.64/bin/tomcat-juli.jar
Using CATALINA_PID:    /root/servers/demoServ/logs/catalina.pid
Tomcat started.

启动成功,快快打开浏览器访问网址 http://你的域名或IP地址:8080/ 试试! 创建更多的Tomcat服务,就按刚才的过程再做一遍吧!

PS:manager.sh提供的命令如下:

Usage: manager.sh ( commands ... )
commands:
  debug             Start Catalina in a debugger
  debug -security   Debug Catalina with a security manager
  jpda start        Start Catalina under JPDA debugger
  run               Start Catalina in the current window
  run -security     Start in the current window with security manager
  start             Start Catalina in a separate window
  start -security   Start in a separate window with security manager
  stop              Stop Catalina, waiting up to 5 seconds for the process to end
  stop n            Stop Catalina, waiting up to n seconds for the process to end
  stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
  stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running
  configtest        Run a basic syntax check on server.xml - check exit code for result
  version           What version of tomcat are you running?

继续,还没完呢:p

安装MySQL

// 先回到老地方
cd ~
// 下载官方安装包文件
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-5.6.27-1.linux_glibc2.5.x86_64.rpm-bundle.tar
// 解压
tar xvf MySQL-5.6.27-1.linux_glibc2.5.x86_64.rpm-bundle.tar
// 开始安装文件包
rpm -ivh MySQL-client-5.6.27-1.linux_glibc2.5.x86_64.rpm
rpm -ivh MySQL-devel-5.6.27-1.linux_glibc2.5.x86_64.rpm
rpm -ivh MySQL-embedded-5.6.27-1.linux_glibc2.5.x86_64.rpm
rpm -ivh MySQL-shared-5.6.27-1.linux_glibc2.5.x86_64.rpm
rpm -ivh MySQL-shared-compat-5.6.27-1.linux_glibc2.5.x86_64.rpm
rpm -ivh MySQL-test-5.6.27-1.linux_glibc2.5.x86_64.rpm
// 由于CentOS自带的mariadb的包与MySQL的包有冲突,需要先卸载
rpm -qa |grep -i mariadb
yum remove mariadb-libs.x86_64
// 若需要则执行perl安装
yum install -y perl perl-devel
// 安装perl-Data-Dumper模组
yum install -y perl-Data-Dumper
// 继续安装
rpm -ivh MySQL-server-5.6.27-1.linux_glibc2.5.x86_64.rpm
// 安装完毕,由于我使用的是root用户,所以MySQL的用户使用的是root并生成随机密码
// 查看生成的随机密码
cat /root/.mysql_secret
// 启动MySQL服务
service mysql start
// 接下来完成MySQL服务的初始化
// 执行命令:
/usr/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

// 这里输入生成的随机密码
Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

// 提问你是否需要修改root密码,这里回车确认并输入新密码两次,
Change the root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

// 提问是否移除匿名用户访问,回车确认
Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

// 提问是否禁用root帐户远程登录,回车确认
Disallow root login remotely? [Y/n]
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

// 提问是否移除test数据库,回车确认
Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

// 重新加载,回车确认,配置完成!
Reload privilege tables now? [Y/n]
 ... Success!

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

// 快快登录MySQL试试吧!
mysql -u root -p

安装Nginx

// 查看yum上的Nginx包的版本是1.6的比较老
yum info nginx
// 根据Nginx官方的文档,安装最新的稳定版本
// 新建配置文件,命令如下:
vi /etc/yum.repos.d/nginx.repo
// 文件内容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
// :wq保存
// 再次查看Nginx版本是1.8
yum info nginx
// OK,开始安装
yum install nginx
// 启动Nginx
/usr/sbin/nginx -c /etc/nginx/nginx.conf

Nginx服务安装配置完毕,更多的Nginx命令如下:

// 重新加载配置
/usr/sbin/nginx -s reload
// 停止服务
/usr/sbin/nginx -s stop
// 验证配置文件
/usr/sbin/nginx -t -c /etc/nginx/nginx.conf

OK,现在我们的服务器上该有的服务都有了,生产环境快速搭建——分分钟搞定!本次内容介绍完毕,请小伙伴们自己尝试吧,欢迎交流!

下一篇文章将重点介绍Nginx与Tomcat服务的负载均衡体验,还有Apache Server与Tomcat的集成部署等等,敬请期待吧!

展开阅读全文
打赏
2
26 收藏
分享
加载中
现在的服务器都用centos7了么!
2015/10/22 16:14
回复
举报
更多评论
打赏
1 评论
26 收藏
2
分享
返回顶部
顶部