You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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.html file into this directory.
  • Fix permissions to ensure GeoServer's Jetty can access the files (assuming GeoServer runs as the geoserver user; 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:

  1. Install Python (Ubuntu usually has Python3 pre-installed, but confirm with):
    sudo apt update && sudo apt install python3
    
  2. 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
    
  3. 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>
    
  4. Test a Python Script:
    Create test.py in cgi-bin:
    #!/usr/bin/env python3
    print("Content-Type: text/html")
    print()
    print("<html><body><h1>Python CGI Test Success!</h1></body></html>")
    
    Add execute permissions:
    sudo chmod +x /usr/share/geoserver/webapps/upload/cgi-bin/test.py
    
  5. 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):

  1. Install PHP and PHP-FPM:
    sudo apt update && sudo apt install php php-fpm
    
  2. Find PHP-FPM's Socket Path:
    Check the active PHP-FPM socket (replace 8.2 with your PHP version if needed):
    sudo systemctl status php8.2-fpm
    
    Look for a line like Listen: /run/php/php8.2-fpm.sock—this is your socket path.
  3. Configure Jetty for FastCGI:
    • Create /usr/share/geoserver/etc/jetty-fastcgi.xml with 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 the WEB-INF directory 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>
      
  4. Test a PHP Script:
    Create test.php in your upload directory:
    <?php
    echo "<html><body><h1>PHP FastCGI Test Success!</h1></body></html>";
    ?>
    
    Fix permissions:
    sudo chown geoserver:geoserver /usr/share/geoserver/webapps/upload/test.php
    sudo chmod 644 /usr/share/geoserver/webapps/upload/test.php
    
  5. 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 geoserver user 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 /geoserver to your existing Jetty instance, and handle /upload directly with Nginx+PHP-FPM/Apache+mod_wsgi—this is often easier for managing multiple languages and apps.

内容的提问来源于stack exchange,提问作者Harry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:56:21