{"id":3163,"date":"2018-07-22T11:14:06","date_gmt":"2018-07-21T23:14:06","guid":{"rendered":"http:\/\/www.zoyinc.com\/?p=3163"},"modified":"2019-06-09T22:22:21","modified_gmt":"2019-06-09T10:22:21","slug":"enable-python-scripts-in-apache","status":"publish","type":"post","link":"http:\/\/www.zoyinc.com\/?p=3163","title":{"rendered":"Enable python scripts in Apache"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3178\" src=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2018\/07\/EnablePythonScriptsInApache01.jpg\" alt=\"\" width=\"750\" height=\"168\" srcset=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2018\/07\/EnablePythonScriptsInApache01.jpg 750w, http:\/\/www.zoyinc.com\/wp-content\/uploads\/2018\/07\/EnablePythonScriptsInApache01-150x34.jpg 150w, http:\/\/www.zoyinc.com\/wp-content\/uploads\/2018\/07\/EnablePythonScriptsInApache01-300x67.jpg 300w, http:\/\/www.zoyinc.com\/wp-content\/uploads\/2018\/07\/EnablePythonScriptsInApache01-500x112.jpg 500w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/p>\n<p>Want to run Python from Apache 2.4, this is what I did to get it working. For this exercise I am creating a web hook script and want to run them from the custom folder &#8220;\/srv\/www\/webhooks\/&#8221;.<\/p>\n<h2>Apache config changes<\/h2>\n<p>First backup and then open up &#8220;\/etc\/httpd\/conf\/httpd.conf&#8221; in your favorite editor..<\/p>\n<p>Search for &#8220;#AddHandler cgi-script .cgi&#8221; and change this to:<\/p>\n<p style=\"padding-left: 30px;\">AddHandler cgi-script .cgi <span style=\"background-color: #ffff00;\">.py<\/span><\/p>\n<p>Search for &#8220;ScriptAlias \/cgi-bin\/&#8221;. This will return a list of folders which run cgi scripts, typically something like:<\/p>\n<p style=\"padding-left: 30px;\">ScriptAlias \/cgi-bin\/ &#8220;\/var\/www\/cgi-bin\/&#8221;<\/p>\n<p>Because we want to run from a different folder I needed to add a new entry directly below:<\/p>\n<p style=\"padding-left: 30px;\">ScriptAlias \/webhooks\/ &#8220;\/srv\/www\/webhooks\/&#8221;<\/p>\n<p>Next look for a line &#8220;&lt;Directory &#8220;\/var\/www\/cgi-bin&#8221;&gt;&#8221;. This defines the settings of the cgi-bin folder. At the end it has a &#8220;&lt;\/Directory&gt;&#8221;. Below this add a section for the web hooks folder:<\/p>\n<pre class=\"crayon:false;\">#\r\n# Script alias folder for web hook scripts\r\n#\r\n&lt;Directory \"\/srv\/www\/webhooks\"&gt;\r\n\u00a0\u00a0 AllowOverride None\r\n\u00a0\u00a0 Options None\r\n\u00a0\u00a0 Require all granted\r\n&lt;\/Directory&gt;<\/pre>\n<p>Now restart Apache to enable your changes. For RedHat use:<\/p>\n<p style=\"padding-left: 30px;\">systemctl restart httpd.service<\/p>\n<h2>Create the Python script<\/h2>\n<p>Create a folder for your scripts, in my case my web files are under &#8220;\/srv\/www&#8221; so create a subfolder &#8220;webhooks&#8221;:<\/p>\n<pre class=\"crayon:false;\">mkdir -p \/srv\/www\/webhooks\r\nchown apache.apache \/srv\/www\/webhooks\r\nchmod 2770 \/srv\/www\/webhooks<\/pre>\n<p>Create a sample Python file &#8220;\/srv\/www\/webhooks\/test.py&#8221;<\/p>\n<pre class=\"lang:python decode:true \">#!\/usr\/bin\/env python\r\nimport cgi;\r\nimport cgitb;cgitb.enable()\r\n\r\n# Print headers\r\nprint \"Content-Type: text\/html\"\r\nprint \"\"\r\n\r\n# Print page\r\nprint(\"&lt;html&gt;\")\r\nprint(\"&lt;title&gt;Webhook Test Page&lt;\/title&gt;\")\r\nprint(\"&lt;body&gt;Test webhook page by Zoyinc.&lt;\/body&gt;\")\r\nprint(\"&lt;\/html&gt;\")\r\n<\/pre>\n<p>Set the correct permissions:<\/p>\n<pre class=\"crayon:false;\">chmod 770 \/srv\/www\/webhooks\/test.py<\/pre>\n<p>Go to your favorite browser and see if it works, the url for me is <a  href=\"http:\/\/www.zoyinc.com\/webhooks\/test.py\">http:\/\/www.zoyinc.com\/webhooks\/test.py<\/a><\/p>\n<p>You should see the following:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3180\" src=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2018\/07\/EnablePythonScriptsInApache02.jpg\" alt=\"\" width=\"463\" height=\"212\" srcset=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2018\/07\/EnablePythonScriptsInApache02.jpg 463w, http:\/\/www.zoyinc.com\/wp-content\/uploads\/2018\/07\/EnablePythonScriptsInApache02-150x69.jpg 150w, http:\/\/www.zoyinc.com\/wp-content\/uploads\/2018\/07\/EnablePythonScriptsInApache02-300x137.jpg 300w\" sizes=\"auto, (max-width: 463px) 100vw, 463px\" \/><\/p>\n<h2>Problems<\/h2>\n<h3>Running a virtual host<\/h3>\n<p>If you are running in a virtual host you may need to add the details there:<\/p>\n<pre class=\"lang:python decode:true\">&lt;VirtualHost *:80&gt;\r\n    ServerAdmin studentsonline@zoyinc.com\r\n    DocumentRoot \/srv\/www\/studentsonline\r\n    ServerName studentsonline.zoyinc.com\r\n    ErrorLog logs\/studentsonline.zoyinc.com-error_log\r\n    CustomLog logs\/studentsonline.zoyinc.com-access_log combined\r\n    DirectoryIndex \"Students OnlineM.html\"\r\n    ScriptAlias \/studentsonline\/ \"\/srv\/www\/studentsonline\/\"\r\n    #\r\n    # Script alias folder for students online scripts\r\n    #\r\n    &lt;Directory \"\/srv\/www\/studentsonline\"&gt;\r\n       AllowOverride None\r\n       Options None\r\n       Require all granted\r\n       AddHandler cgi-script .py\r\n    &lt;\/Directory&gt;\r\n&lt;\/VirtualHost&gt;\r\n<\/pre>\n<h3>\nLogs<\/h3>\n<p>It is good to tail the apache log, so something like:<\/p>\n<p style=\"padding-left: 30px;\">tail -f \/var\/log\/httpd\/www.zoyinc.com-error_log<\/p>\n<p>Remember Apache has a different log for each virtual host &#8211; which is why the above is named www.zoyinc.com.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Want to run Python from Apache 2.4, this is what I did to get it working. For this exercise I am creating a web hook script and want to run them from the custom folder &#8220;\/srv\/www\/webhooks\/&#8221;. Apache config changes First backup and then open up &#8220;\/etc\/httpd\/conf\/httpd.conf&#8221; in your favorite editor.. Search for &#8220;#AddHandler cgi-script .cgi&#8221; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3182,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85,433],"tags":[305,431,410,430,432,358],"class_list":["post-3163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache","category-python","tag-apache","tag-cgi","tag-configuration","tag-python","tag-web-hooks","tag-web-server"],"_links":{"self":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/3163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3163"}],"version-history":[{"count":11,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/3163\/revisions"}],"predecessor-version":[{"id":3587,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/3163\/revisions\/3587"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/media\/3182"}],"wp:attachment":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3163"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}