{"id":5381,"date":"2013-06-19T18:36:52","date_gmt":"2013-06-19T13:06:52","guid":{"rendered":"http:\/\/www.binarytides.com\/?p=5381"},"modified":"2014-12-09T13:14:57","modified_gmt":"2014-12-09T07:44:57","slug":"setup-apache-php-cgi-ubuntu","status":"publish","type":"post","link":"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/","title":{"rendered":"Install and configure Apache and PHP with cgi on Ubuntu or Debian"},"content":{"rendered":"<h3>CGI - Common Gateway Interface<\/h3>\n<p>Apache can be configured to invoke the php interpreter as cgi program. This means that the php-cgi binary shall be run externally and the php script shall be executed by it and the output would be passed back to apache. This is one way to run php with apache. Other methods include shared module, fastcgi etc. Read my previous post on php server apis to learn more about various options to run php with apache.<\/p>\t\t<div class=\"display-ad-unit mobile-wide bsa\" style=\"background:#fff3f3; height:315px;\">\n\n<!-- BinaryTides_S2S_InContent_ROS_Pos1 -->\n<style>\n\t@media only screen and (min-width: 0px) and (min-height: 0px) {\n\t\tdiv[id^=\"bsa-zone_1611170977806-3_123456\"] {\n\t\t\tmin-width: 300px;\n\t\t\tmin-height: 250px;\n\t\t}\n\t}\n\t@media only screen and (min-width: 640px) and (min-height: 480px) {\n\t\tdiv[id^=\"bsa-zone_1611170977806-3_123456\"] {\n\t\t\tmin-width: 300px;\n\t\t\tmin-height: 250px;\n\t\t}\n\t}\n<\/style>\n<div id=\"bsa-zone_1611170977806-3_123456\"><\/div>\n\n\n<\/div>\n<!-- Time: 1.6927719116211E-5, Pos: 457, Key: ad_unit_1 -->\n\n\n<p>Since in cgi mode, php is run separately, its possible to use a threaded apache mpm like worker. In this post we shall setup apache and php together through the cgi.<\/p>\n<h3>Install packages<\/h3>\n<p>The necessary packages to install include, apache, mpm worker, php5, php cgi. Install all of them at one go using the apt-get command<\/p>\n<pre class=\"terminal\" ># sudo apt-get install apache2 apache2-mpm-worker php5 php5-cgi<\/pre>\n<p>Enable mod actions in apache.<\/p>\n<pre class=\"terminal\" >$ sudo a2enmod actions<\/pre>\n<p>If you already had apache and php installed with mod php, then you first need to disable mod php<\/p>\n<pre class=\"terminal\" >$ sudo a2dismod php5<\/pre>\n<h3>Configure Apache<\/h3>\n<p>Now configure apache to run php as cgi. The configuration is done in the relevant vhost block.<br \/>\nThe Apache virtual host configuration files are located at the following location<\/p>\n<pre class=\"pre_text\" >\/etc\/apache2\/sites-enabled\/<\/pre>\n<p>The default vhost file is 000-default.conf which is present on most systems.<br \/>\nA few necessary directives have to be added in the right directory block in the configuration file which will tell apache to use php-cgi binary to run files ending with \".php\".<\/p>\n<p>The directives are AddHandler, Action and ScriptAlias. A minimal configuration looks like this<\/p>\n<pre class=\"pre_text\" >&lt;Directory \/var\/www\/&gt;\r\n\tOptions Indexes FollowSymLinks MultiViews\r\n\tAllowOverride None\r\n\tOrder allow,deny\r\n\tallow from all\r\n\r\n\tAddHandler cgi-handler .php\r\n\tAction cgi-handler \/local-bin\/php-cgi\r\n&lt;\/Directory&gt;\r\nScriptAlias \/local-bin \/usr\/bin<\/pre>\n<p>The AddHandler line declares a handler called cgi-handler for .php files. This can be named to anything.<\/p>\n<p>The Action line tells apache to run cgi-handler using the \/local-bin\/php-cgi program, which is the php cgi binary. Now this path is not a real one.<\/p>\n<p>The ScriptAlias line maps \/local-bin to \/usr\/bin. So the path <b>\/local-bin\/php-cgi<\/b> becomes <b>\/usr\/bin\/php-cgi<\/b> which is the actual path of the php-cgi binary on ubuntu\/debian.<\/p>\n<p>Now restart apache<\/p>\n<pre class=\"terminal\" ># service apache2 restart<\/pre>\n<p>Put a php file with phpinfo() in the web root and open from browser. The Server API shall be reported as \"CGI\/FastCGI\".<\/p>\n\n<!-- SMARTADDR: No ad unit (ad_unit_2) was added at location: 3957. Content Length: 3325 -->\n<!-- SMARTADDR: No ad unit (ad_unit_3) was added at location: 3957. Content Length: 3418 -->\n<!-- SMARTADDR: No ad unit (ad_unit_4) was added at location: 3957. Content Length: 3511 -->\n<!-- SMARTADDR: No ad unit (ad_unit_5) was added at location: 3957. Content Length: 3604 -->","protected":false},"excerpt":{"rendered":"<p>Guide on how to setup apache and php using CGI (Common Gateway Interface) on Ubuntu<\/p>\n","protected":false},"author":1,"featured_media":15958,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[798,860,307],"tags":[47,32],"class_list":["post-5381","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-debian-linux","category-http-server-2","category-ubuntu-2","tag-apache","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Install and configure Apache and PHP with cgi on Ubuntu or Debian - BinaryTides<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Silver Moon\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/\",\"url\":\"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/\",\"name\":\"Install and configure Apache and PHP with cgi on Ubuntu or Debian - BinaryTides\",\"isPartOf\":{\"@id\":\"https:\/\/www.binarytides.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg\",\"datePublished\":\"2013-06-19T13:06:52+00:00\",\"dateModified\":\"2014-12-09T07:44:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.binarytides.com\/#\/schema\/person\/ce24c6ddfa0368f9a08bcf46505884dd\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/#primaryimage\",\"url\":\"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg\",\"contentUrl\":\"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.binarytides.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Install and configure Apache and PHP with cgi on Ubuntu or Debian\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.binarytides.com\/#website\",\"url\":\"https:\/\/www.binarytides.com\/\",\"name\":\"BinaryTides\",\"description\":\"News, Technology, Entertainment and more\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.binarytides.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.binarytides.com\/#\/schema\/person\/ce24c6ddfa0368f9a08bcf46505884dd\",\"name\":\"Silver Moon\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.binarytides.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/67ac3d58b656585dc0201e900a67f4197eb0c3ef2d1f83dd8f95a0b497cd97da?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/67ac3d58b656585dc0201e900a67f4197eb0c3ef2d1f83dd8f95a0b497cd97da?s=96&r=g\",\"caption\":\"Silver Moon\"},\"description\":\"A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at binarytides@gmail.com.\",\"url\":\"https:\/\/www.binarytides.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Install and configure Apache and PHP with cgi on Ubuntu or Debian - BinaryTides","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/","twitter_misc":{"Written by":"Silver Moon","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/","url":"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/","name":"Install and configure Apache and PHP with cgi on Ubuntu or Debian - BinaryTides","isPartOf":{"@id":"https:\/\/www.binarytides.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/#primaryimage"},"image":{"@id":"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/#primaryimage"},"thumbnailUrl":"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg","datePublished":"2013-06-19T13:06:52+00:00","dateModified":"2014-12-09T07:44:57+00:00","author":{"@id":"https:\/\/www.binarytides.com\/#\/schema\/person\/ce24c6ddfa0368f9a08bcf46505884dd"},"breadcrumb":{"@id":"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/#primaryimage","url":"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg","contentUrl":"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/www.binarytides.com\/setup-apache-php-cgi-ubuntu\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.binarytides.com\/"},{"@type":"ListItem","position":2,"name":"Install and configure Apache and PHP with cgi on Ubuntu or Debian"}]},{"@type":"WebSite","@id":"https:\/\/www.binarytides.com\/#website","url":"https:\/\/www.binarytides.com\/","name":"BinaryTides","description":"News, Technology, Entertainment and more","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.binarytides.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.binarytides.com\/#\/schema\/person\/ce24c6ddfa0368f9a08bcf46505884dd","name":"Silver Moon","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.binarytides.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/67ac3d58b656585dc0201e900a67f4197eb0c3ef2d1f83dd8f95a0b497cd97da?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/67ac3d58b656585dc0201e900a67f4197eb0c3ef2d1f83dd8f95a0b497cd97da?s=96&r=g","caption":"Silver Moon"},"description":"A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at binarytides@gmail.com.","url":"https:\/\/www.binarytides.com\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/posts\/5381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/comments?post=5381"}],"version-history":[{"count":0,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/posts\/5381\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/media\/15958"}],"wp:attachment":[{"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/media?parent=5381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/categories?post=5381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/tags?post=5381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}