Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Enable CGI Mode in Apache Tomcat

DZone's Guide to

How to Enable CGI Mode in Apache Tomcat

Having trouble enabling the CGI mode in your Apache Tomcat? Check out this tutorial to learn more!

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Hello Coders, I hope you are all doing well.

Today, I had to enable the CGI mode in Tomcat, while facing lots of issue in the same. So, I thought that, after successful deployment, I would write an article in the same simplified steps. Here it is!

  • First, download the Apache Tomcat (choose the version that is compatible with your application; in my case, I am using the latest one).
  • Go to the CATALINA_HOME/conf and modify the following files:
    • web.xml (Here, we will enable the CGI support in Tomcat)
      • To enable CGI support in Tomcat, we have to uncomment servlet and servlet-mapping of CGI servlet in web.xml.
      • After uncommenting the servlet and servlet-mapping of CGI, we have to add one param value in that servlet mapping to execute our CGI.
    • context.xml
      • We have to set the attribute "privileged" to true for Context.
<!-- web.xml -->

<servlet>
  <servlet-name>cgi</servlet-name>
  <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
  <init-param>
    <param-name>executable</param-name>
    <param-value></param-value>
  </init-param>
  <init-param>
    <param-name>cgiPathPrefix</param-name>
    <param-value>WEB-INF/cgi</param-value>
  </init-param>
  <load-on-startup>5</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>cgi</servlet-name>
	<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>


<!-- context.xml -->

<Context privileged="true">
</Context>


  • Go to the CATALINA_HOME/webapps/ROOT/  directory.
    • Check weather WEB-INF  directory is exist or not, if not create the directory  WEB-INF .
    • Create another directory named as cgi under  WEB-INF.
  • Now, your Tomcat is CGI enabled. You can copy any CGI file under this directory, and it will run successfully.
  • Your base URL will be <<request protocol>>>://<<server-ip>>:<<server-port>>/cgi-bin/<<name of cgi>>, in my case i.e. http://localhost:8080/cgi-bin/test.cgi
  • For more understanding, you can look for CGI-howto section in Tomcat

Let me know if you face any issue after this deployment. Happy Coding!

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Topics:
tomcat ,tomcat 8 ,tomcat 7 ,cgi server ,java ,tutorial ,cgi mode

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}