{"id":1330,"date":"2014-09-18T17:11:11","date_gmt":"2014-09-18T11:41:11","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=1330"},"modified":"2017-03-25T12:23:52","modified_gmt":"2017-03-25T06:53:52","slug":"configuring-apache-on-mac-os-x-mavericks-for-python-scripting","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/configuring-apache-on-mac-os-x-mavericks-for-python-scripting\/","title":{"rendered":"Configuring Apache on Mac OS X (Mavericks) for Python scripting"},"content":{"rendered":"<p>I spent a couple of hours today trying to figure out how to configure Apache Web Server on Mac OS X to execute Python scripts, so I thought I would document the process for my own reference.<\/p>\n<p>OS X is preinstalled with Apache and Python. Apache executable (apachectl) is at \/usr\/sbin\/apachectl and Python is at \/usr\/bin\/python. But the configurations for Apache are at \/etc\/apache2, specifically in the file httpd.conf. If you open the file and look for DocumentRoot, you will find that default document root is set to\u00a0\/Library\/WebServer\/Documents.<\/p>\n<p>I did not want to change the default doc root but at the same time did not want to store my scripts in the default folder. I could have created a virtual host or an alias. I decided to do the later because\u00a0it is simpler than creating virtual host; and I was configuring Python for development only. So just below the document root setting I added the alias &#8211;<!--more--><\/p>\n<div style=\"background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">DocumentRoot \"\/Library\/WebServer\/Documents\"\r\nAlias \/py \/Users\/Ram\/Sites\r\n<\/pre>\n<\/div>\n<p>The idea was to access my Python scripts using url localhost\/py\/*.py.<\/p>\n<p>And based on the information at &#8220;<a href=\"https:\/\/docs.python.org\/3.3\/howto\/webservers.html\" target=\"_blank\">HOWTO Use Python in the web<\/a>&#8220;, &#8220;<a href=\"http:\/\/httpd.apache.org\/docs\/2.2\/howto\/cgi.html\" target=\"_blank\">Apache Tutorial: Dynamic Content with CGI<\/a>&#8221; and some of the tutorials online, I added directory settings for the alias &#8211;<\/p>\n<div style=\"background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000; font-weight: bold;\">&lt;Directory<\/span> <span style=\"border: 1px solid #FF0000;\">\"\/Users\/Ram\/Sites\"<\/span><span style=\"color: #008000; font-weight: bold;\">&gt;<\/span>\r\n    Options Indexes FollowSymLinks MultiViews +ExecCGI\r\n    AllowOverride All\r\n    Order allow,deny\r\n    Allow from all\r\n    AddHandler cgi-script .py\r\n<span style=\"color: #008000; font-weight: bold;\">&lt;\/Directory&gt;<\/span>\r\n<\/pre>\n<\/div>\n<p>I create a simple test file test.py in \/Users\/Ram\/Sites with following content &#8211;<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">#!\/usr\/bin\/python<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">print<\/span> <span style=\"background-color: #fff0f0;\">'Content-Type: text\/html<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\n\\n<\/span><span style=\"background-color: #fff0f0;\">'<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">print<\/span> <span style=\"background-color: #fff0f0;\">\"Apache-Python config working!\"<\/span><\/pre>\n<\/div>\n<p>Set execute permission for this file (chomod +x test.py).<\/p>\n<p>I then restarted Apache &#8211; actually first stopped it (sudo apachectl stop) and then started again (sudo apachectl start). I read somewhere that restart option does not work.<br \/>\nI then browsed to localhost\/py\/test.py and expected it to display\u00a0&#8220;Apache-Python config working!&#8221;. But instead I got &#8220;403 Forbidden You don&#8217;t have permission to access &#8230;&#8221; error. When I checked the error log (at \/var\/log\/apache2) I saw &#8211;<\/p>\n<p><span style=\"color: #ff0000;\">&#8220;Permission denied: access to \/py\/test.py denied (filesystem path &#8216;\/Users\/Ram\/Sites&#8217;) because search permissions are missing on a component of the path&#8221;<\/span> error.<\/p>\n<p>The solution to fix this issue is described in the <a href=\"https:\/\/wiki.apache.org\/httpd\/13PermissionDenied\" target=\"_blank\">Apache Wiki<\/a>\u00a0. Apparently it was\u00a0not enough to \u00a0set execute permission on individual script file &#8211; I had\u00a0to provide execute permissions for the current folder (\/Users\/Ram\/Sites), and all its parents till the Users folder (\/User\/Ram and \/User).<\/p>\n<p>After above changes the Python script executed successfully.<\/p>\n<p>-Ram Kulkarni<\/p>\n<h4>Update:<\/h4>\n<p>I was working on a similar setup in Docker (Apache + Python), with one difference that the version of Apache was 2.4.25. Strangely the above solution did not work till I appended &#8216;*&#8217; to the directory path in httpd.conf &#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #333385;\">Directory<\/span> <span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">\"<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">\/<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">u<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">s<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">r<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">\/<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">l<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">o<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">c<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">a<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">l<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">\/<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">a<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">p<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">a<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">c<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">h<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">e<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">2<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">\/<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">h<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">t<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">d<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">o<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">c<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">s<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">\/<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">*<\/span><span style=\"color: #ffffff; background: #dd9999; font-weight: bold; font-style: italic;\">\"<\/span> <span style=\"color: #0057a6;\">&gt;<\/span>\r\n    Options +ExecCGI\r\n    AllowOverride All\r\n    Require all granted\r\n    AddHandler cgi-script .py\r\n<span style=\"color: #0057a6;\">&lt;\/<\/span><span style=\"color: #333385;\">Directory<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I spent a couple of hours today trying to figure out how to configure Apache Web Server on Mac OS X to execute Python scripts, so I thought I would document the process for my own reference. OS X is preinstalled with Apache and Python. Apache executable (apachectl) is at \/usr\/sbin\/apachectl and Python is at &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/configuring-apache-on-mac-os-x-mavericks-for-python-scripting\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Configuring Apache on Mac OS X (Mavericks) for Python scripting&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[115,1],"tags":[113,114,116],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-ls","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/1330"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=1330"}],"version-history":[{"count":2,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/1330\/revisions"}],"predecessor-version":[{"id":1887,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/1330\/revisions\/1887"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=1330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=1330"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=1330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}