如何在GeoServer自带Jetty中部署第二个Web应用?
Got it, let's break down how to set up your /upload web app with Python and PHP support using the Jetty instance bundled with your GeoServer installation on Ubuntu. Here's a step-by-step guide:
1. Set Up the Basic Static Web App Context
First, we'll create the directory structure for your /upload app—Jetty automatically scans the webapps folder for deployable contexts:
- Create the app directory:
sudo mkdir -p /usr/share/geoserver/webapps/upload - Place your
index.htmlfile into this directory. - Fix permissions to ensure GeoServer's Jetty can access the files (assuming GeoServer runs as the
geoserveruser; adjust if your setup uses a different user):sudo chown -R geoserver:geoserver /usr/share/geoserver/webapps/upload sudo chmod -R 755 /usr/share/geoserver/webapps/upload
Restart GeoServer with /usr/share/geoserver/bin/shutdown.sh followed by /usr/share/geoserver/bin/startup.sh, then test accessing http://localhost:8080/upload/index.html—your static page should load.
2. Add Python Support via CGI
Jetty doesn't natively run Python, but we can use its CGI servlet to execute Python scripts:
- Install Python (Ubuntu usually has Python3 pre-installed, but confirm with):
sudo apt update && sudo apt install python3 - Create a CGI directory in your upload app:
sudo mkdir /usr/share/geoserver/webapps/upload/cgi-bin sudo chmod -R 755 /usr/share/geoserver/webapps/upload/cgi-bin - Enable Jetty's CGI Servlet:
Edit/usr/share/geoserver/etc/webdefault.xml, find the CGI-related section (or add this block if it's missing):<servlet> <servlet-name>CGI</servlet-name> <servlet-class>org.eclipse.jetty.servlet.CGIServlet</servlet-class> <init-param> <param-name>commandPrefix</param-name> <param-value>/usr/bin/python3</param-value> </init-param> <init-param> <param-name>pathInfoOnly</param-name> <param-value>true</param-value> </init-param> <load-on-startup>5</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CGI</servlet-name> <url-pattern>/cgi-bin/*.py</url-pattern> </servlet-mapping> - Test a Python Script:
Createtest.pyincgi-bin:
Add execute permissions:#!/usr/bin/env python3 print("Content-Type: text/html") print() print("<html><body><h1>Python CGI Test Success!</h1></body></html>")sudo chmod +x /usr/share/geoserver/webapps/upload/cgi-bin/test.py - Restart GeoServer and test
http://localhost:8080/upload/cgi-bin/test.py—you should see the Python-generated page.
3. Add PHP Support via FastCGI
For PHP, we'll use Jetty's FastCGI servlet to connect to PHP-FPM (the standard PHP process manager):
- Install PHP and PHP-FPM:
sudo apt update && sudo apt install php php-fpm - Find PHP-FPM's Socket Path:
Check the active PHP-FPM socket (replace8.2with your PHP version if needed):
Look for a line likesudo systemctl status php8.2-fpmListen: /run/php/php8.2-fpm.sock—this is your socket path. - Configure Jetty for FastCGI:
- Create
/usr/share/geoserver/etc/jetty-fastcgi.xmlwith this content:<?xml version="1.0"?> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <New id="fcgiPool" class="org.eclipse.jetty.util.thread.QueuedThreadPool"> <Set name="minThreads">1</Set> <Set name="maxThreads">10</Set> </New> <New id="fcgiClient" class="org.eclipse.jetty.fcgi.client.http.HttpClientTransportOverFCGI"> <Arg><Ref refid="fcgiPool"/></Arg> </New> <New id="fcgiHttpClient" class="org.eclipse.jetty.client.HttpClient"> <Arg><Ref refid="fcgiClient"/></Arg> <Set name="maxConnectionsPerDestination">10</Set> <Set name="idleTimeout">30000</Set> </New> <Call name="addBean"> <Arg><Ref refid="fcgiHttpClient"/></Arg> </Call> </Configure> - Create
/usr/share/geoserver/webapps/upload/WEB-INF/web.xml(make theWEB-INFdirectory first if needed) with:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="4.0"> <servlet> <servlet-name>fastcgi</servlet-name> <servlet-class>org.eclipse.jetty.fcgi.server.FastCGIServlet</servlet-class> <init-param> <param-name>proxyTo</param-name> <param-value>unix:///run/php/php8.2-fpm.sock</param-value> <!-- Replace with your socket path --> </init-param> <init-param> <param-name>scriptRoot</param-name> <param-value>/usr/share/geoserver/webapps/upload</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>fastcgi</servlet-name> <url-pattern>*.php</url-pattern> </servlet-mapping> </web-app>
- Create
- Test a PHP Script:
Createtest.phpin your upload directory:
Fix permissions:<?php echo "<html><body><h1>PHP FastCGI Test Success!</h1></body></html>"; ?>sudo chown geoserver:geoserver /usr/share/geoserver/webapps/upload/test.php sudo chmod 644 /usr/share/geoserver/webapps/upload/test.php - Restart GeoServer and test
http://localhost:8080/upload/test.php—the PHP page should load.
4. Key Notes & Alternatives
- Permissions: Always double-check that the
geoserveruser has read/execute access to your app files and directories—permission issues are the most common cause of 403/500 errors. - Jetty Version Compatibility: GeoServer's bundled Jetty version might vary. If you get class-not-found errors for the CGI/FastCGI servlets, confirm your Jetty version supports these modules (avoid upgrading Jetty unless necessary to prevent conflicts with GeoServer).
- Simpler Alternative: If configuring Jetty feels cumbersome, consider installing Nginx or Apache as a reverse proxy. You can proxy
/geoserverto your existing Jetty instance, and handle/uploaddirectly with Nginx+PHP-FPM/Apache+mod_wsgi—this is often easier for managing multiple languages and apps.
内容的提问来源于stack exchange,提问作者Harry

