Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Configuring Jetty for FastCGI - and pass env. vars to PHP scripts

Hello and good evening,

on a CentOS Linux server I run 3 instances of Jetty on 3 different IP addresses, serving a servlet and  Wordpress in 3 different languages: English, German, Russian.

The Wordpress installations are configured exactly as described at https://www.eclipse.org/jetty/documentation/current/configuring-fastcgi.html

And the Jetty instances are started by /etc/systemd/system/jetty-en.service (and jetty-de.service at port 8081, jetty-ru.service at 8080):

[Unit]
Description=Jetty
After=network-online.target

[Service]
Environment=COUNTRY=en
Environment=DOMAIN=wordsbyfarber.com
Environment=DATABASE_URL=jdbc:postgresql://127.0.0.1:6432/my_database_en?user=my_user_en&password=my_user_password
Environment=FCM_SECRET=<FCM_SECRET_FOR_ANDROID>
Environment=AMAZON_ID=<AMAZON_ID>
Environment=AMAZON_SECRET=<AMAZON_SECRET>
Environment=FACEBOOK_ID=<FACEBOOK_ID>
Environment=FACEBOOK_SECRET=<FACEBOOK_SECRET>
Type=simple
User=jetty
Group=jetty
ExecStart=/usr/bin/java -Djdbc.drivers=org.postgresql.Driver -jar /usr/share/java/jetty-distribution-9.4.27.v20200227/start.jar jetty.home=/usr/share/java/jetty-distribution-9.4.27.v20200227 jetty.base=/var/www/jetty-base-en jetty.http.host=127.0.0.1 jetty.http.port=8082
SuccessExitStatus=143
Restart=always
RestartSec=180
PrivateTmp=true

[Install]
WantedBy=multi-user.target

As you can see above I pass some information through the env. variables to the custom Java servlet of my word game.

The above stuff works well, but now I am also trying to pass some configuration information to the few PHP scripts I am also running - and unfortunately this does not work - when I print($_ENV['COUNTRY'); in the PHP scripts, the env. variable is not there.

Is there please a way to pass the env. vars from Jetty process to the PHP scripts run through FastCGI?

The reason why I am asking is that I am trying to reuse as much source code (Java, PHP, _javascript_) while serving my word game in 3 different languages.

Thank you 
Alex

PS: Below is my Jetty config xml file:

# cat /var/www/jetty-base-en/webapps/wordsbyfarber.com.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
    "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.servlet.ServletContextHandler">

    <New id="root" class="java.lang.String">
        <Arg>/var/www/html/wordsbyfarber.com</Arg>
    </New>

    <Set name="contextPath">/</Set>

    <Set name="resourceBase"><Ref refid="root" /></Set>

    <Set name="virtualHosts">
            <Array type="java.lang.String">
                    <Item>wordsbyfarber.com</Item>
                    <Item>www.wordsbyfarber.com</Item>
            </Array>
    </Set>

    <Set name="welcomeFiles">
        <Array type="string">
                <Item>index.php</Item>
                <Item>index.html</Item>
        </Array>
    </Set>

    <Call name="addFilter">
        <Arg>org.eclipse.jetty.fcgi.server.proxy.TryFilesFilter</Arg>
        <Arg>/*</Arg>
        <Arg>
            <Call name="of" class="java.util.EnumSet">
                <Arg><Get name="REQUEST" class="javax.servlet.DispatcherType" /></Arg>
            </Call>
        </Arg>
        <Call name="setInitParameter">
            <Arg>files</Arg>
            <Arg>$path /index.php?p=$path</Arg>
        </Call>
    </Call>

    <Call name="addServlet">
        <Arg>
            <New class="org.eclipse.jetty.servlet.ServletHolder">
                <Arg>default</Arg>
                <Arg>
                    <Call name="forName" class="java.lang.Class">
                        <Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
                    </Call>
                </Arg>
                <Call name="setInitParameter">
                    <Arg>dirAllowed</Arg>
                    <Arg>false</Arg>
                </Call>
                <Call name="setInitParameter">
                    <Arg>gzip</Arg>
                    <Arg>true</Arg>
                </Call>
            </New>
        </Arg>
        <Arg>/</Arg>
    </Call>

    <Call name="addServlet">
        <Arg>org.eclipse.jetty.fcgi.server.proxy.FastCGIProxyServlet</Arg>
        <Arg>*.php</Arg>
        <Call name="setInitParameter">
            <Arg>proxyTo</Arg>
            <Arg>http://localhost:9000</Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>prefix</Arg>
            <Arg>/</Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>scriptRoot</Arg>
            <Arg><Ref refid="root" /></Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>scriptPattern</Arg>
            <Arg>(.+?\\.php)</Arg>
        </Call>
    </Call>
</Configure>



Back to the top