Create a Java App Server on a Virtual Machine
Join the DZone community and get the full member experience.
Join For FreeCurator's note: This tutorial originally appeared at the Windows Azure Java Developer Center.
With Windows Azure, you can use a virtual machine to provide server capabilities. As an example, a virtual machine running on Windows Azure can be configured to host a Java application server, such as Apache Tomcat. On completing this guide, you will have an understanding of how to create a virtual machine running on Windows Azure and configure it to run a Java application server.
You will learn:
- How to create a virtual machine.
- How to remotely log in to your virtual machine.
- How to install a JDK on your virtual machine.
- How to install a Java application server on your virtual machine.
- How to create an endpoint for your virtual machine.
- How to open a port in the firewall for your application server.
For purposes of this tutorial, an Apache Tomcat application server will be installed on a virtual machine. The completed installation will result in a Tomcat installation such as the following.
To complete this tutorial, you need a Windows Azure account that has the Windows Azure Virtual Machines feature enabled. You can create a free trial account and enable preview features in just a couple of minutes. For details, see Create a Windows Azure account and enable preview features.
To create a virtual machine
- Log in to the Windows Azure Preview Management Portal.
- Click New.
- Click Virtual machine.
- Click Quick create.
- In the Create virtual machine screen, enter a value for DNS name.
- From the Image dropdown list, select an image, such as Windows Server 2008 R2 SP1.
- Enter a password in the New password field, and re-enter it in the Confirm field. This is the Administrator account password. Remember this password, you will use it when you remotely log in to the virtual machine.
-
From the Location drop down list, select the data center location for your virtual machine; for example, West US. Your screen will look similar to the following.
-
Click Create virtual machine. Your virtual machine will be created. You can monitor the status in the Virtual machines section of the management portal.
To remotely log in to your virtual machine
- Log in to the Preview Management Portal.
- Click Virtual Machines, and then select the MyTestVM1 virtual machine that you previously created.
-
On the command bar, click Connect.
-
Click Open to use the remote desktop protocol file that was automatically created for the virtual machine
-
Click Connect to proceed with the connection process.
-
Type the password that you specified as the password of the Administrator account when you created the virtual machine, and then click OK.
-
Click Yes to verify the identity of the virtual machine.
To install a JDK on your virtual machine
You can copy a Java Developer Kit (JDK) to your virtual machine, or install a JDK through an installer.
For purposes of this tutorial, a JDK will be installed from Oracle's site.
- Log in to your virtual machine.
- Within your browser, open http://www.oracle.com/technetwork/java/javase/downloads/index.html.
- Click the Download button for the JDK that you want to download. For purposes of this tutorial, the Download button for the Java SE 6 Update 32 JDK was used.
- Accept the license agreement.
- Click the download executable for Windows x64 (64-bit).
- Follow the prompts and respond as needed to install the JDK to your virtual machine.
To install a Java application server on your virtual machine
You can copy a Java application server to your virtual machine, or install a Java application server through an installer.
For purposes of this tutorial, a Java application server will be installed by copying a zip file from Apache's site.
- Log in to your virtual machine.
- Within your browser, open http://tomcat.apache.org/download-70.cgi.
- Double-click 64-bit Windows zip. (This tutorial used the zip for Tomcat Apache 7.0.27.)
- When prompted, choose to save the zip.
- When the zip is saved, open the folder that contains the zip and double-click the zip.
- Extract the zip. For purposes of this tutorial, the path used was C:\program files\apache-tomcat-7.0.27-windows-x64.
To run the Java application server privately on your virtual machine
The following steps show you how to run the Java application server and test it within the virtual machine's browser. It won't be usable by external computers until you create an endpoint and open a port (those steps are described later).
- Log in to your virtual machine.
- Add the JDK bin folder to the Pathenvironment variable:
- Click Windows Start.
- Right-click Computer.
- Click Properties.
- Click Advanced system settings.
- Click Advanced.
- Click Environment variables.
- In the System variables section, click the Path variable and then click Edit.
- Add a trailing ; to the Path variable value (if there is not one already) and then add c:\program files\java\jdk\bin to the end of the Path variable value (adjust the path as needed if you did not use c:\program files\java\jdk as the path for your JDK installation).
- Press OK on the opened dialogs to save your Path change.
- Set the JAVA_HOMEenvironment variable:
- Click Windows Start.
- Right-click Computer.
- Click Properties.
- Click Advanced system settings.
- Click Advanced.
- Click Environment variables.
- In the System variables section, click New.
- Create a variable named JRE_HOME and set its value to c:\program files\java\jdk\jre (adjust the path as needed if you did not use c:\program files\java\jdk as the path for your JDK installation).
- Press OK on the open dialogs to save your JRE_HOME environment variable.
- Start Tomcat:
- Open a command prompt.
- Change the current directory to the Apache Tomcat binfolder. For example:
cd c:\program files\apache-tomcat-7.0.27-windows-x64\apache-tomcat-7.0.27\bin
(Adjust the path as needed if you used a differrent installation path for Tomcat.) - Run catalina.bat start.
You should now see Tomcat running if you run the virtual machine's browser and open http://localhost:8080.
To see Tomcat running from external machines, you'll need to create an endpoint and open a port.
To create an endpoint for your virtual machine
- Log in to the Preview Management Portal.
- Click Virtual machines.
- Click the name of the virtual machine that is running your Java application server.
- Click Endpoints.
- Click Add endpoint.
- In the Add endpoint dialog, ensure Add endpoint is checked and click the Next button.
- In the New endpoint detailsdialog
- Specify a name for the endpoint; for example, HttpIn.
- Specify TCP for the protocol.
- Specify 80 for the public port.
- Specify 8080for the private port.
Your screen should look similar to the following:
- Click the Check button to close the dialog. Your endpoint will now be created.
To open a port in the firewall for your virtual machine
- Log in to your virtual machine.
- Click Windows Start.
- Click Control Panel.
- Click System and Security, click Windows Firewall, and then click Advanced Settings.
-
Click Inbound Rules and then click New Rule.
-
For the new rule, select Port for the Rule type and click Next.
-
Select TCP for the protocol and specify 8080 for the port, and click Next.
-
Choose Allow the connection and click Next.
-
Ensure Domain, Private, and Public are checked for the profile and click Next.
-
Specify a name for the rule, such as HttpIn (the rule name is not required to match the endpoint name, however), and then click Finish.
At this point, your Tomcat web site should now be viewable from an external browser, using a URL of the form http://your_DNS_name.cloudapp.net, where your_DNS_name is the DNS name you specified when you created the virtual machine.
Application lifecycle considerations
- You could create your own application web archive (WAR) and add it to the webapps folder. For example, create a basic Java Service Page (JSP) dynamic web project and export it as a WAR file, copy the WAR to the Apache Tomcat webapps folder on the virtual machine, then run it in a browser.
- This tutorial runs Tomcat through a command prompt where catalina.bat start was called. You may instead want to run Tomcat as a service, a key benefit being to have it automatically start if the virtual machine is rebooted. To run Tomcat as a service, you can install it as a service via the service.bat file in the Apache Tomcat bin folder, and then you could set it up to run automatically via the Services snap-in. You can start the Services snap-in by clicking Windows Start, Administrative Tools, and then Services. If you run service.bat install MyTomcat in the Apache Tomcat bin folder, then within the Services snap-in, your service name will appear as Apache Tomcat MyTomcat. By default when the service is installed, it will be set to start manually. To set it to start automatically, double-click the service in the Services snap-in and set Startup Type to Automatic, as shown in the following. You'll need to start the service the first time, which you can do through the Services snap-in (alternatively, you can reboot the virtual machine). Close the running occurrence of catalina.bat start if it is still running before starting the service.
Opinions expressed by DZone contributors are their own.
Comments