如何将基于GlassFish的Java EE应用部署到CentOS服务器?
Hey there! Let's get your Java EE app deployed to CentOS with GlassFish smoothly—since it runs fine locally, we just need to replicate that environment on the server and get your app up. Here's a step-by-step breakdown:
First, you need a working JDK because GlassFish relies on it. Make sure to pick a version compatible with your GlassFish release (e.g., GlassFish 6 requires JDK 11 or later):
- Install OpenJDK 11 (the most common, stable choice):
sudo yum update -y sudo yum install java-11-openjdk-devel -y - Verify the installation to make sure it worked:
You should see output showing an OpenJDK 11.x.x version.java -version
It's best practice to run GlassFish as a non-root user (to avoid security risks), so let's set that up first:
- Create a dedicated user for GlassFish:
sudo useradd -m glassfish sudo passwd glassfish - Switch to the new user and download/extract GlassFish (replace the version number with the one you're using locally, e.g., 6.2.5):
sudo su - glassfish wget https://download.eclipse.org/ee4j/glassfish/glassfish-6.2.5.zip unzip glassfish-6.2.5.zip mv glassfish6 /opt/glassfish # Optional: Move to a standard system directory for easier access - Start the default GlassFish domain (
domain1):
You can check if the admin console is accessible by visiting/opt/glassfish/bin/asadmin start-domain domain1http://your-server-ip:4848(we'll open this port in the firewall shortly). The default credentials areadmin/admin—you'll be prompted to change this password on your first login.
Since your app runs locally, you probably already have a WAR/EAR package ready. If not:
- If you use Maven, run this in your project root directory on your local machine:
Your WAR file will be in themvn clean packagetargetfolder of your project. - For Eclipse/IntelliJ, right-click your project → Export → WAR File (follow the prompts to save it to your local drive).
You have two straightforward options here:
Option 1: Deploy via the Admin Console
- Open
http://your-server-ip:4848in a browser and log in with your updated admin credentials. - Navigate to Applications → Deploy.
- Under Package File to Upload, click Browse and select your WAR/EAR file from your local machine.
- Leave the default settings (or adjust the context path if you want a custom URL for your app) and click OK. Wait a few seconds, and your app should appear in the list as "Running".
Option 2: Deploy via the command line (faster for repeated deployments)
- First, transfer your WAR file to the CentOS server from your local machine using
scp:scp /path/to/your-app.war glassfish@your-server-ip:/tmp/ - Log into the CentOS server as the
glassfishuser, then run the deploy command:
You'll get a confirmation message like "Application deployed successfully."/opt/glassfish/bin/asadmin deploy /tmp/your-app.war
Now let's ensure external users can reach your app:
- Open the necessary ports in the CentOS firewall (it uses firewalld by default):
- For general app access (port 8080):
sudo firewall-cmd --add-port=8080/tcp --permanent - If you want to keep the admin console accessible (optional—only do this if you trust the network):
sudo firewall-cmd --add-port=4848/tcp --permanent - Reload the firewall to apply the changes:
sudo firewall-cmd --reload
- For general app access (port 8080):
- Test your app by visiting
http://your-server-ip:8080/your-app(replaceyour-appwith your WAR file name, unless you changed the context path during deployment).
- App fails to start: Check the GlassFish server logs for detailed error messages:
Look for exceptions like missing dependencies or misconfigured resources.tail -f /opt/glassfish/glassfish/domains/domain1/logs/server.log - Can't access the app/admin console: Double-check the firewall rules and confirm GlassFish is running with:
/opt/glassfish/bin/asadmin status-domain domain1 - Forgot admin password: Reset it quickly with:
/opt/glassfish/bin/asadmin change-admin-password
内容的提问来源于stack exchange,提问作者minustwo

