Ubuntu14.04下Apache2与Tomcat7配置:域名直接访问Java应用方法
Hey there! Let's get your setup sorted so users can type www.mysite.com and land directly on your Tomcat-hosted application. Below are two proven methods to achieve this using Apache as a reverse proxy:
Method 1: Reverse Proxy with HTTP (mod_proxy_http)
This is the most straightforward approach, using Apache's proxy modules to forward requests to Tomcat's 8080 port.
Step 1: Enable required Apache modules
Open your terminal and run these commands to enable the proxy and HTTP proxy modules:sudo a2enmod proxy proxy_http sudo service apache2 restartStep 2: Configure Apache Virtual Host
Edit your default Apache site configuration (or create a new one if you prefer). The file is usually located at/etc/apache2/sites-available/000-default.conf:sudo nano /etc/apache2/sites-available/000-default.confInside the
<VirtualHost *:80>block, add these lines (replaceApplication_namewith your actual app's context path):# Disable proxy requests to avoid open proxy risks ProxyRequests Off # Forward all requests to your Tomcat app ProxyPass / http://localhost:8080/Application_name/ ProxyPassReverse / http://localhost:8080/Application_name/Save the file (Ctrl+O, then Enter) and exit nano (Ctrl+X).
Step 3: Test the setup
Restart Apache to apply changes:sudo service apache2 restartNow visit
www.mysite.comin your browser—it should load your Tomcat application directly!
Method 2: AJP Protocol (mod_proxy_ajp)
AJP is an optimized protocol for communication between Apache and Tomcat, which can offer better performance than plain HTTP. Tomcat enables AJP on port 8009 by default.
Step 1: Enable AJP proxy module
Run these commands to enable the necessary modules:sudo a2enmod proxy proxy_ajp sudo service apache2 restartStep 2: Update Virtual Host configuration
Open the same virtual host file as before (/etc/apache2/sites-available/000-default.conf) and replace the HTTP proxy lines with these:ProxyRequests Off ProxyPass / ajp://localhost:8009/Application_name/ ProxyPassReverse / ajp://localhost:8009/Application_name/Step 3: Verify Tomcat's AJP connector (optional)
If you want to double-check Tomcat's AJP setup, open/etc/tomcat7/server.xmland ensure this line is uncommented (it should be by default):<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />If you made changes here, restart Tomcat:
sudo service tomcat7 restartStep 4: Restart Apache and test
sudo service apache2 restartVisit
www.mysite.com—you should see your Tomcat app just like with the first method.
Notes to Keep in Mind
- If your application uses absolute URLs or hardcoded paths, you might need to adjust its configuration to use the root path (
/) instead of/Application_name/. - Always test configurations after making changes to catch any typos or errors. You can check Apache's syntax with
sudo apache2ctl configtest.
内容的提问来源于stack exchange,提问作者SAI GIRI CHARY AOUSULA

