Month: March 2019

Tomcat Base Setup With PHP Support

Tomcat Base Setup With PHP Support

Tomcat is an awesome server when wanting to run local webapps or have a LAN server. But, getting it setup can be a little confusing at first go. This tutorial will show you the common things to look at as well as change to get a functional Tomcat server. In addition, at the end of this, you will be able to run PHP scripts from Tomcat too. So, first things first, we need to download a few programs to get started. They are Tomcat, PHP/Java Bridge, and PHP-CGI.

Downloads

*** Note: We need two JARs that comprise PHP/Java Bridge. Simply go to the most recent version of the application under “Binary package” and then go to the “exploded” folder. From there, right-click each JAR link and do “Open Link In New Tab”. Then go to each tab so the page will load and begin downloading the JAR.


Setup

OK, having everything we need, extract Tomcat to wherever you want to store it. I have a specific directory were I hold self-contained applications. Make sure to name the Tomcat folder to something more readable than its original name and we’re good to go. We next need to move the two JARs to their new home, edit the proper config files, and start Tomcat.

Move to the freshly extracted Tomcat folder and go to the “lib” folder. Copy or move the two JARs you downloaded for PHP/Java Bridge to this location. Then, go up one directory and go to “conf”. Edit the following files (“server.xml”, “tomcat-users.xml”, “web.xml”) with the below information.


server.xml

Find the “Connector” that has port 8080 and change it to the following:

<Connector port="1120" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443"
           address="0.0.0.0" />
*** Note: You can leave the port value as is or use your own.
*** Note2: The “address” value isn’t needed unless you want to make your server available for your LAN or the Internet. You can even set it to 127.0.0.1 which means localhost so that you can come back and change it later.


tomcat-users.xml

For the users file, add a new user outside of the commented out text but still within the “tomcat-users” tag. Mine looks like the following:

<user username="toor" password="root" roles="manager-gui, admin-gui"/>
*** Note: These roles should only be available to the admin or not included.


web.xml

For the web file we need to add the proper information for PHP to work on our Tomcat server. We are also adding a “welcome-file” entry for index.php. So, first, add the following right after the opening “web-app” part.

<!-- PHP FOR TOMCAT -->

<listener>
    <listener-class>php.java.servlet.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>PhpJavaServlet</servlet-name>
    <servlet-class>php.java.servlet.PhpJavaServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>PhpCGIServlet</servlet-name>
    <servlet-class>php.java.servlet.fastcgi.FastCGIServlet</servlet-class>
    <init-param>
        <param-name>prefer_system_php_exec</param-name>
        <param-value>On</param-value>
    </init-param>
    <init-param>
        <param-name>php_include_java</param-name>
        <param-value>Off</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>PhpJavaServlet</servlet-name>
    <url-pattern>*.phpjavabridge</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>PhpCGIServlet</servlet-name><url-pattern>*.php</url-pattern>
</servlet-mapping>
Then, scroll all the way down to where “welcome-file-list” attributes are set and add the following:
<welcome-file>index.php</welcome-file>
We are now ready to launch Tomcat and do some initial cleanup and setup.


Tomcat Start & Initial Setup

Tomcat Start

To start Tomcat, simply go up one directory and go to the “bin” folder. From there, on Linux, make sure “catalina.sh” is set with proper execute permissions. You can do it through the file manager or the command line. For the command line it is as follows:

chmod +x catalina.sh
On Windows, everything is already set for you. OK, let’s actually start Tomcat. To do this, open your terminal and move to the “bin” folder. Then execute the following based on your system:
*** Linux
./catalina.sh start

*** Windows
.\catalina.sh start
To stop it:
*** Linux
./catalina.sh stop

*** Windows
.\catalina.sh stop


Tomcat Manager & Tomcat Webapps

Now that Tomcat is running, go to localhost:<your-port> in a new Tab. It will load the Tomcat “Welcome Page” which has links to admin tools, doc pages, and example apps. Click on “Manager App” and if prompted enter the user and password you setup just a moment ago. From there, remove the examples app by clicking its “Undeploy” button if you don’t want it taking space. Keep the page open but open your file manager and go to Tomcat’s “webapps” directory. Once there, re-name the “ROOT” folder to “welcome” (without the quotations). Then, create a new folder and name it “ROOT” (again, without the quotations). Copy the “WEB-INF” folder from the “welcome” app’s folder to “ROOT”. Then, edit the “web.xml” to more appropriate entries for “display-name” and “description”. At this point, everything is setup as it should be and you are ready to test whether PHP is working on Tomcat. Though, before we do that, let’s restart Tomcat by stopping it and starting it again.


Testing PHP

To test PHP on Tomcat go to the “ROOT” folder and from there create an “index.php” file. Enter the following:

<?php 

echo "Hello World!";

?>
Go to localhost:<your-port> OR if you have manager still open press on your keyboard Ctrl+F5. This will clear the page cache and refresh the page. You should see in the “Path” column one that is “/”. Press it and it’ll do the same as opening localhost:<your-port> by opening the “ROOT” app. You should see “Hello World!” load up.


Conclusion

And you’re done! You can now use Tomcat to create webapps with Java or PHP or even both! At this point, it might be useful to setup proper SSL so certain local applications have proper security. But, this will be for another time.