如何使用Ubuntu 15.04中使用Java 8设置GlassFish 4.1

时间:2020-03-05 15:32:42  来源:igfitidea点击:

GlassFish是一个开源应用服务器,用于开发和部署Java平台,企业版(Java EE平台)应用程序和基于Java技术的Web技术。
它支持不同的基于Java的技术,如Enterprise JavaBeans,JPA,JavaServer Faces,JMS,RMI,JavaServer页面,Servlet等。
GlassFish根据具有Web容器的OSGI联盟标准提供轻量级和可扩展的核心。
有关配置和管理,它具有一个非常好的易于使用的管理控制台,更新工具以进行更新和添加组件。
GlassFish对高可用性聚类和负载平衡有良好的支持。

现在,我们将在Ubuntu 15.04中安装Glassfish,非常简单。

1.添加Java PPA

首先,我们需要安装Oracle JDK 8.由于Ubuntu的存储库中不可用Oracle Java,我们需要为Oracle Java 8安装程序添加PPA。
因此,我们将首先安装Python-Software-Properties,如果未安装并将PPA添加到我们的Ubuntu 15.04计算机中。

# apt-get install python-software-properties
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-apt python-minimal
python-pycurl python2.7 python2.7-minimal
Suggested packages:
python-doc python-tk python-apt-dbg python-gtk2 python-vte python-apt-doc
libcurl4-gnutls-dev python-pycurl-dbg python-pycurl-doc python2.7-doc binutils
binfmt-support
The following NEW packages will be installed:
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-apt python-minimal
python-pycurl python-software-properties python2.7 python2.7-minimal
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,126 kB of archives.
After this operation, 17.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

现在,我们将使用add-apt-repository命令添加java的PPA,如下所示。

# add-apt-repository ppa:webupd8team/java
Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK7/JDK8/JDK9). There are no actual Java files in this PPA.
More info (and Ubuntu installation instructions):
- for Oracle Java 7: http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
- for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html
Debian installation instructions:
- Oracle Java 7: http://www.webupd8.org/2012/06/how-to-install-oracle-java-7-in-debian.html
- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html
Important!!! For now, you should continue to use Java 8 because Oracle Java 9 is available as an early access release (it should be released in 2015)! You should only use Oracle Java 9 if you explicitly need it, because it Jan contain bugs and it might not include the latest security patches! Also, some Java options were removed in JDK9, so you Jan encounter issues with various Java apps. More information and installation instructions (Ubuntu/Linux Mint/Debian): http://www.webupd8.org/2014/02/install-oracle-java-9-in-ubuntu-linux.html
More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Press [ENTER] to continue or ctrl-c to cancel adding it
gpg: keyring `/tmp/tmpahw0r1nh/secring.gpg' created
gpg: keyring `/tmp/tmpahw0r1nh/pubring.gpg' created
gpg: requesting key EEA14886 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpahw0r1nh/trustdb.gpg: trustdb created
gpg: key EEA14886: public key "Launchpad VLC" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
OK

添加PPA存储库后,我们希望更新本地包存储库索引。
为此,我们需要运行以下命令。

# apt-get update

2.安装Oracle JDK 8

更新存储库索引后,我们希望通过运行以下命令来安装Oracle JDK 8.

# apt-get install oracle-java8-installer

3.设置"java_home"变量

现在,在安装Oracle JDK 8之后,我们现在要将环境变量"java_home"设置为新安装的Oracle JDK 8的路径。
要设置变量,我们需要编辑/etc /环境文件使用我们最喜欢的文本编辑器。

# nano /etc/environment

使用文本编辑器打开后,我们需要将以下行添加到文件的底部。

JAVA_HOME="/usr/lib/jvm/java-8-oracle"

一旦添加该行,我们需要重新加载文件。

# source /etc/environment

安装和设置Oracle JDK 8后,我们将运行以下命令来检查和确认。

# java -version
java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

如果我们看到如上所示的输出,则确认我们在我们的机器中安装了Java 8.

4.安装Glassfish 4.1

在我们的Java正确安装之后,我们现在将在安装GlassFish 4.1之前,这是最新版本到达日期。
我们甚至可以从GlassFish官方下载下载旧版本。

# cd /tmp
# wget 'http://download.java.net/glassfish/4.1/release/glassfish-4.1.zip'

现在,我们希望提取最新GlassFish 4.1的下载的Zip包。
为此,我们需要安装解压缩,然后将包解压缩到/opt目录中。

# apt-get install unzip # unzip glassfish-4.1.zip -d /opt

5.设置玻璃鱼路径

现在,我们希望为GlassFish设置Path变量,以便直接从任何目录访问GlassFish可执行文件。
为此,我们将编辑~/.profile文件并添加提取GlassFish的目录的路径。

# nano ~/.profile

然后将以下行添加到其中。

export PATH=/opt/glassfish4/bin:$PATH
# source ~/.profile

6.启动GlassFish服务器

最后,在我们的Ubuntu 15.04机器中安装Oracle Java 8和GlassFish 4.1之后。
我们要启动GlassFish服务器。
为此,我们将以如下方式运行Asadmin。

# asadmin start-domain
Waiting for domain1 to start ............ Successfully started the domain : domain1 domain Location: /opt/glassfish4/glassfish/domains/domain1 Log File: /opt/glassfish4/glassfish/domains/domain1/logs/server.log Admin Port: 4848 Command start-domain executed successfully.

域是由一个管理服务器管理的一组一个或者多个GlassFish服务器实例。
默认的GlassFish服务器的端口是8080,管理服务器的端口为4848,管理用户名为admin,没有密码。
我们可以访问http://ip-address:8080 /检查glassfish服务器的主页和http://ip-address:4848 /以在我们的Web浏览器中获取管理员登录页面。

7.启用安全管理员

现在,InOrder通过网页远程访问管理面板,我们需要通过运行以下命令使用ASADMIN启用安全管理。

# asadmin enable-secure-admin
Enter admin user name> admin
Enter admin password for user "admin">
You must restart all running servers for the change in secure admin to take effect.
Command enable-secure-admin executed successfully.

这将向我们询问我们要设置的用户名和密码。

注意:如果我们收到此错误"远程故障:至少一个管理员用户具有空密码,安全管理员不允许。
使用Change-Admin-password命令或者admin控制台为管理帐户创建非空密码。
"我们需要运行作为管理员更改管理员密码,然后为admin输入新密码,然后重试上面的命令。

# asadmin change-admin-password
Enter admin user name [default: admin]>admin
Enter the admin password>
Enter the new admin password>
Enter the new admin password again>
Authentication failed for user: admin (Usually, this means invalid user name and/or password)
Command change-admin-password failed.

设置后,我们需要重新启动域。

# asadmin restart-domain

成功重新启动域命令重新启动域已成功执行。

启用安全管理员后,我们可以通过将我们的Web浏览器指向HTTP://IP地址:4848来访问管理面板。
然后,通过输入上面输入的凭据来访问管理员面板。

8.在Glassfish上部署war

现在,在我们成功安装GlassFish并运行服务器后,我们希望将WAR应用程序部署到Glasfish中。
在此,在本教程中,我们将为服务器进行部署Hello.war。
因此,首先,我们将从使用wget命令从GlassFish的官方示例页面下载Hello.war。

# wget https://glassfish.java.net/downloads/quickstart/hello.war

下载WAR文件后,我们现在将使用asadmin命令部署WAR文件。

# asadmin deploy hello.war
Enter admin user name> admin
Enter admin password for user "admin">
Application deployed with name hello.
Command deploy executed successfully.

这将要求我们输入应用程序部署的用户名和密码。

随着WAR应用程序的部署,我们可以通过访问http://ip-address:8080/Hello使用我们的Web浏览器来检查它。

9.取消部署和停止服务器

现在,如果我们使用GlassFish服务器和已部署的应用程序完成了任务,我们可以简单地找出应用程序并停止GlassFish服务器。

要取消部署运行应用程序,我们可以简单地使用我们要取消部署的应用程序名称运行ASADMIN OPS;

# asadmin undeploy hello
Enter admin user name> admin
Enter admin password for user "admin">
Command undeploy executed successfully.

要停止运行的GlassFish域,我们可以简单地运行ASADMIN停止域。

# asadmin stop-domain
Waiting for the domain to stop .
Command stop-domain executed successfully.

创建密码文件

如果我们厌倦了在部署或者取消部署应用程序时输入用户名和密码,我们只需使用文本编辑器创建名为pwdfile的文件并将以下行添加到其中。

# nano pwdfile
AS_ADMIN_PASSWORD=your_admin_password

现在,在创建该文件之后,我们可以只添加--PasswordFile标志指向PWDFile,然后按如下所示部署WAR应用程序。

# asadmin --passwordfile pwdfile deploy hello.war
Application deployed with name hello. Command deploy executed successfully.

现在,用户名和密码的提示将不会进一步出现。