Skip to main content

How to set up Tomcat 7 and run Python CGI scripts on Linux

What is Apache Tomcat :

Apache Tomcat is an open-source web server and servlet system developed by the Apache Software Foundation (ASF).

How to Download Tomcat 7.0.68 For Linux : 


wget https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.68/bin/apache-tomcat-7.0.68.tar.gz

 
 How to Install Tomcat : The Installation is easy and one step process . 

Run the below command to install tomcat :
tar -xvzf apache-tomcat-7.0.68.tar.gz


All the key config files can be found in the conf directory underneath the home directory of  Tomcat , and some of the most important ones are:
  • server.xml - This is the main config file for Tomcat and contains the settings for most of the key Tomcat components.
  • context.xml - This is a global config file used by all web apps running on Tomcat. If you wish to override the global settings you should create your own context.xml file and place it in the META-INF directory of your web app.
  • web.xml - This is the global deployment descriptor used by all web apps running on Tomcat. If you wish to override the global settings you should create your own web.xml file and place it in the WEB-INF directory of your web app.
  • tomcat-users.xml - This file contains details on users and their roles

Let's start configuring the Tomcat Server :


1. modify the web.xml file in the  <TOMCAT_HOME>\conf\folder 

Changes : 

 a)uncomment the cgi servlet and its mapping

<servlet>
 <servlet-name>cgi</servlet-name>
 <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
 <init-param>
 <param-name>debug</param-name>
 <param-value>0</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>


b)  Add an servlet parameter "passShellEnvironment" and set it to “true” (
 "force" the environment variables to be passed) 
<init-param>
          <param-name>passShellEnvironment</param-name>
          <param-value>true</param-value>

</init-param>
c)Add an servlet parameter "executable"
<init-param>
          <param-name>executable</param-name>
          <param-value>C:\Python27\python.exe</param-value>
 </init-param> 


2. Modify <TOMCAT_HOME>\conf\context.xml  to add a property on <Context>: 
 <Context privileged="true">
...
</Context>



3. Create a folder say "test" in <TOMCAT_HOME>\webapps directory . This would be the root Folder of your application. 

4. Create a WEB-INF folder inside the root folder (test) and create a cgi folder inside WEB-INF folder


5. Create a python  script  hello.py and put it under  <TOMCAT_HOME>\webapps\test\WEB-INF\cgi\

6. cat hello.py : 

  print("Hello 2017")

7. Now start the tomcat server .

cd apache-tomcat-7.0.68/bin
sh startup.sh

8.Browse the below URL to verify your script and the  set up . 

http://localhost:8080/test/cgi-bin/hello.py 



Comments

  1. How can you write "C:\Python27\python.exe" if the title of your page is "......ON LINUX"?

    ReplyDelete
  2. Explore unparalleled excellence with Supportfly, your go-to Google Cloud managed services provider. Elevate your cloud experience with top-notch solutions tailored to your needs. Trust us to optimize, secure, and streamline your operations, ensuring peak performance and peace of mind. Unleash the full potential of Google Cloud with Supportfly – your key to efficient, reliable, and cutting-edge managed services.

    ReplyDelete

Post a Comment