{"id":148,"date":"2012-11-07T22:39:59","date_gmt":"2012-11-07T22:39:59","guid":{"rendered":"https:\/\/www.nicktailor.com\/?p=148"},"modified":"2022-10-21T11:59:54","modified_gmt":"2022-10-21T11:59:54","slug":"how-to-compile-php-and-run-it-as-a-cgi-binary","status":"publish","type":"post","link":"https:\/\/nicktailor.com\/tech-blog\/how-to-compile-php-and-run-it-as-a-cgi-binary\/","title":{"rendered":"How to compile PHP and run it as a CGI binary"},"content":{"rendered":"<div><span style=\"text-decoration: underline;\"><strong>How to compile php as a cgi binary<\/strong><\/span><\/div>\n<div><\/div>\n<div>\n<ul>\n<li><strong><em>This blog post was written using RedHat enterprise server, however the principles apply to just about any linux distro out there, it should work for them all.<\/em><\/strong><\/li>\n<\/ul>\n<\/div>\n<div><\/div>\n<ol>\n<li>Generally what I would do is check the OS repository and see what is the next upgraded version of php its going to do, when finally you decide to do a server wide upgrade of php, and then I would go and download the source files of that version. So in this case we are going to do php 5.3.17<\/li>\n<li>Download from\u00a0<a href=\"http:\/\/www.php.net\/releases\/\">http:\/\/www.php.net\/releases\/<\/a><\/li>\n<li>Log into your server, and created a directory, then cd into it, next run the following below.<\/li>\n<\/ol>\n<p><em>wget http:\/\/www.php.net\/get\/php-5.3.17.tar.gz\/from\/a\/mirror<\/em><\/p>\n<ol>\n<li>Next you want to untar the file tar \u2013zxvf &lt;filename you downloaded&gt;<\/li>\n<li>Next we need to get the configure flags that php is currently using, the easiest way to get this is to find a domain that has php running and setup a phpinfo.php that contains the following<\/li>\n<\/ol>\n<p><em>&lt;?php phpinfo() ?&gt;<\/em><\/p>\n<p>Save that file and then view it through your browser\u00a0<a href=\"http:\/\/domain.com\/phpinfo.php\">http:\/\/domain.com\/phpinfo.php<\/a><\/p>\n<p>You should see a php info page. If you do not see it, it probably means your owner permissions are incorrect.<\/p>\n<p>Example<\/p>\n<p>-rw-r&#8211;r&#8211; 1 root\u00a0\u00a0\u00a0 root\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 19 Nov\u00a0 7 14:32 phpinfo.php (incorrect)<\/p>\n<p>-rw-r&#8211;r&#8211; 1 tailor\u00a0 tailor\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 19 Nov\u00a0 7 14:32 phpinfo.php (correct)<\/p>\n<p>6.\u00a0\u00a0 So at the top of that phpinfo page you should see a section called \u201cConfigure Command\u201d\u00a0 Looks like what is below here.<\/p>\n<p>&nbsp;<\/p>\n<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"bottom\"><strong>Configure Command<\/strong><\/td>\n<td valign=\"bottom\">&#8216;.\/configure&#8217; &#8216;&#8211;disable-fileinfo&#8217; &#8216;&#8211;disable-pdo&#8217; &#8216;&#8211;enable-bcmath&#8217; &#8216;&#8211;enable-calendar&#8217; &#8216;&#8211;enable-ftp&#8217; &#8216;&#8211;enable-gd-native-ttf&#8217; &#8216;&#8211;enable-libxml&#8217; &#8216;&#8211;enable-magic-quotes&#8217; &#8216;&#8211;enable-mbstring&#8217; &#8216;&#8211;enable-soap&#8217; &#8216;&#8211;enable-sockets&#8217; &#8216;&#8211;enable-zend-multibyte&#8217; &#8216;&#8211;prefix=\/usr&#8217; &#8216;&#8211;with-bz2&#8217; &#8216;&#8211;with-curl=\/opt\/curlssl\/&#8217; &#8216;&#8211;with-freetype-dir=\/usr&#8217; &#8216;&#8211;with-gd&#8217; &#8216;&#8211;with-gettext&#8217; &#8216;&#8211;with-imap=\/opt\/php_with_imap_client\/&#8217; &#8216;&#8211;with-imap-ssl=\/usr&#8217; &#8216;&#8211;with-jpeg-dir=\/usr&#8217; &#8216;&#8211;with-kerberos&#8217; &#8216;&#8211;with-libdir=lib64&#8217; &#8216;&#8211;with-libxml-dir=\/opt\/xml2&#8217; &#8216;&#8211;with-libxml-dir=\/opt\/xml2\/&#8217; &#8216;&#8211;with-mcrypt=\/opt\/libmcrypt\/&#8217; &#8216;&#8211;with-mysql=\/usr&#8217; &#8216;&#8211;with-mysql-sock=\/var\/lib\/mysql\/mysql.sock&#8217; &#8216;&#8211;with-mysqli=\/usr\/bin\/mysql_config&#8217; &#8216;&#8211;with-openssl=\/usr&#8217; &#8216;&#8211;with-openssl-dir=\/usr&#8217; &#8216;&#8211;with-pcre-regex=\/opt\/pcre&#8217; &#8216;&#8211;with-pic&#8217; &#8216;&#8211;with-png-dir=\/usr&#8217; &#8216;&#8211;with-xpm-dir=\/usr&#8217; &#8216;&#8211;with-zlib&#8217; &#8216;&#8211;with-zlib-dir=\/usr&#8217;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>You want to copy and inside the \u2018.\/configure&#8212;<\/p>\n<p><strong>From above example<\/strong><\/p>\n<p>&#8216;.\/configure&#8217; &#8216;&#8211;disable-fileinfo&#8217; &#8216;&#8211;disable-pdo&#8217; &#8216;&#8211;enable-bcmath&#8217; &#8216;&#8211;enable-calendar&#8217; &#8216;&#8211;enable-ftp&#8217; &#8216;&#8211;enable-gd-native-ttf&#8217; &#8216;&#8211;enable-libxml&#8217; &#8216;&#8211;enable-magic-quotes&#8217; &#8216;&#8211;enable-mbstring&#8217; &#8216;&#8211;enable-soap&#8217; &#8216;&#8211;enable-sockets&#8217; &#8216;&#8211;enable-zend-multibyte&#8217; &#8216;&#8211;prefix=\/usr&#8217; &#8216;&#8211;with-bz2&#8217; &#8216;&#8211;with-curl=\/opt\/curlssl\/&#8217; &#8216;&#8211;with-freetype-dir=\/usr&#8217; &#8216;&#8211;with-gd&#8217; &#8216;&#8211;with-gettext&#8217; &#8216;&#8211;with-imap=\/opt\/php_with_imap_client\/&#8217; &#8216;&#8211;with-imap-ssl=\/usr&#8217; &#8216;&#8211;with-jpeg-dir=\/usr&#8217; &#8216;&#8211;with-kerberos&#8217; &#8216;&#8211;with-libdir=lib64&#8217; &#8216;&#8211;with-libxml-dir=\/opt\/xml2&#8217; &#8216;&#8211;with-libxml-dir=\/opt\/xml2\/&#8217; &#8216;&#8211;with-mcrypt=\/opt\/libmcrypt\/&#8217; &#8216;&#8211;with-mysql=\/usr&#8217; &#8216;&#8211;with-mysql-sock=\/var\/lib\/mysql\/mysql.sock&#8217; &#8216;&#8211;with-mysqli=\/usr\/bin\/mysql_config&#8217; &#8216;&#8211;with-openssl=\/usr&#8217; &#8216;&#8211;with-openssl-dir=\/usr&#8217; &#8216;&#8211;with-pcre-regex=\/opt\/pcre&#8217; &#8216;&#8211;with-pic&#8217; &#8216;&#8211;with-png-dir=\/usr&#8217; &#8216;&#8211;with-xpm-dir=\/usr&#8217; &#8216;&#8211;with-zlib&#8217; &#8216;&#8211;with-zlib-dir=\/usr&#8217;<\/p>\n<p>7. Now you will need to make some modifications to this. I am running my php as a cgi already, but if you are running it as a module you will see in the configure flags that mysql is disabled even though it is enabled, the reason why this is because when php is run as a module, you are also loading the various flags as module extenstions to run with php and the configure flags will not reflect that on phpinfo page.<\/p>\n<p>So these will be the primary flags you need to ensure are working<\/p>\n<p>Note- these flags I am using are for a 64bit OS, the flags are different for a 32bit OS<\/p>\n<p><em><strong>This is my Example of flag functions in php I wanted to work. I copied this into a text editor and made the changes I needed accordingly, outlined below.<\/strong><\/em><\/p>\n<p>&#8216;.\/configure&#8217; &#8216;-enable-yum&#8217; &#8216;&#8211;build=x86_64-redhat-linux-gnu&#8217; &#8216;&#8211;host=x86_64-redhat-linux-gnu&#8217; &#8216;&#8211;target=x86_64-redhat-linux-gnu&#8217; &#8216;&#8211;program-prefix=&#8217; &#8216;&#8211;prefix=\/usr&#8217; &#8216;&#8211;exec-prefix=\/usr&#8217; &#8216;&#8211;bindir=\/usr\/bin&#8217; &#8216;&#8211;sbindir=\/usr\/sbin&#8217; &#8216;&#8211;sysconfdir=\/etc&#8217; &#8216;&#8211;datadir=\/usr\/share&#8217; &#8216;&#8211;includedir=\/usr\/include&#8217; &#8216;&#8211;libdir=\/usr\/lib&#8217; &#8216;&#8211;libexecdir=\/usr\/libexec&#8217; &#8216;&#8211;localstatedir=\/var&#8217; &#8216;&#8211;sharedstatedir=\/usr\/com&#8217; &#8216;&#8211;mandir=\/usr\/share\/man&#8217; &#8216;&#8211;infodir=\/usr\/share\/info&#8217; &#8216;&#8211;cache-file=..\/config.cache&#8217; &#8216;&#8211;with-config-file-path=\/etc&#8217; &#8216;&#8211;without-config-file-scan-dir&#8217; &#8216;&#8211;enable-force-cgi-redirect&#8217; &#8216;&#8211;disable-debug&#8217; &#8216;&#8211;enable-pic&#8217; &#8216;&#8211;disable-rpath&#8217; &#8216;&#8211;enable-inline-optimization&#8217; &#8216;&#8211;with-bz2&#8217; &#8216;&#8211;with-curl&#8217; &#8216;&#8211;with-exec-dir=\/usr\/bin&#8217; &#8216;&#8211;with-freetype-dir=\/usr&#8217; &#8216;&#8211;with-png-dir=\/usr&#8217; &#8216;&#8211;with-gd&#8217; &#8216;&#8211;enable-gd-native-ttf&#8217; &#8216;&#8211;with-gettext&#8217; &#8216;&#8211;with-ncurses=shared&#8217; &#8216;&#8211;with-gmp&#8217; &#8216;&#8211;with-iconv&#8217; &#8216;&#8211;with-jpeg-dir=\/usr&#8217; &#8216;&#8211;with-png&#8217; &#8216;&#8211;with-xml&#8217; &#8216;&#8211;with-libxml-dir=\/usr&#8221;&#8211;with-expat-dir=\/usr&#8217; &#8216;&#8211;with-dom=shared,\/usr&#8217; &#8216;&#8211;with-dom-xslt=\/usr&#8217; &#8216;&#8211;with-dom-exslt=\/usr&#8217; &#8216;&#8211;with-xmlrpc=shared&#8217; &#8216;&#8211;with-pcre-regex=\/usr\/include&#8217; &#8216;&#8211;with-zlib&#8217; &#8216;&#8211;with-layout=GNU&#8217; &#8216;&#8211;enable-bcmath&#8217; &#8216;&#8211;enable-exif&#8217; &#8216;&#8211;enable-ftp&#8217; &#8216;&#8211;enable-magic-quotes&#8217; &#8216;&#8211;enable-sockets&#8217; &#8216;&#8211;enable-sysvsem&#8217; &#8216;&#8211;enable-sysvshm&#8217; &#8216;&#8211;enable-track-vars&#8217; &#8216;&#8211;enable-trans-sid&#8217; &#8216;&#8211;enable-yp&#8217; &#8216;&#8211;enable-wddx&#8217; &#8216;&#8211;with-pear=\/usr\/share\/pear&#8217; &#8216;&#8211;with-imap=shared&#8217; &#8216;&#8211;with-imap-ssl&#8217; &#8216;&#8211;with-kerberos&#8217;\u00a0 &#8216;&#8211;with-mysql=\/usr&#8217; &#8216;&#8211;with-unixODBC=shared,\/usr&#8217; &#8216;&#8211;enable-memory-limit&#8217; &#8216;&#8211;enable-shmop&#8217; &#8216;&#8211;enable-calendar&#8217; &#8216;&#8211;enable-mbstring&#8217; &#8216;&#8211;enable-mbstr-enc-trans&#8217; &#8216;&#8211;enable-mbregex&#8217; &#8216;&#8211;with-mime-magic=\/usr\/share\/file\/magic.mime&#8217; &#8216;&#8211;enable-dba&#8217; &#8216;&#8211;enable-db4&#8217; &#8216;&#8211;enable-gdbm&#8217; &#8216;&#8211;enable-static&#8217; &#8216;&#8211;with-openssl&#8217;<\/p>\n<ul>\n<li>You will notice the following key thing<\/li>\n<li>You want to remove \u201c&#8211;with-apxs2=\/usr\/sbin\/apxs\u201d &#8211; you can only compile one SAPI + cli at the same time. You can&#8217;t both compile CGI and the apache2 SAPI at the same time.\u00a0<a href=\"http:\/\/bugs.php.net\/bug.php?id=30682&amp;edit=1\">http:\/\/bugs.php.net\/bug.php?id=30682&amp;edit=1<\/a><\/li>\n<li>You want to ensure that &#8220;&#8211;without-mysql&#8221; is changed to \u201c&#8211;with-mysql\u201d<\/li>\n<li>You want to ensure that you have &#8216;&#8211;enable-static&#8217; (this will enable the static libraries)<\/li>\n<li>Enable any other flags you may want to use.<\/li>\n<\/ul>\n<p>8. Create a file called config.sh on the server in the directory where you untarred the source php files<\/p>\n<p>9. Copy the configure flags all on one line in that files and save it<\/p>\n<p>10. Now run \u201csh config.sh\u201d (This is will test the configure flags to see if the server can support them and it will fail on any that need dependancies installed. When you get an error on a package you generally just want to search for the development packages of the failed packages. Use Yum to install them and then run \u201csh config.sh\u201d again and keep going until it finishes properly. I have listed a few common packages people run into below.<\/p>\n<p><em>Note: sometimes you will run into path issues, like it cant find something in \/usr\/bin etc, what I do is do a locate of the file its looking for, usually they are .so files and simply create a simlink to where its attempting for the missing file so the configure test can complete.<\/em><\/p>\n<p>yum install\u00a0 gcc4-c++<br \/>\nyum install\u00a0 gdbm-devel<br \/>\nyum install\u00a0 libjpeg-devel<br \/>\nyum install\u00a0 libpng-devel<br \/>\nyum install\u00a0 freetype-devel<br \/>\nyum install\u00a0 gmp-devel<br \/>\nyum install\u00a0 libc-client-devel<br \/>\nyum install\u00a0 openldap-devel<br \/>\nyum install\u00a0 mysql-devel<br \/>\nyum install\u00a0 ncurses-devel<br \/>\nyum install\u00a0 unixODBC-devel<br \/>\nyum install\u00a0 postgresql-devel<br \/>\nyum install\u00a0 net-snmp-devel<br \/>\nyum install\u00a0 bzip2-devel<br \/>\nyum install\u00a0 curl-devel<\/p>\n<p>11. Once you have completed the test of the configure flags you, it will generate files at the end.<\/p>\n<p>12. Now you want to run\u00a0<em><strong>\u201cMake\u201d<\/strong><\/em>\u00a0&lt;&#8211;<strong>(DO NOT DO \u201cMAKE INSTALL\u201d)\u00a0<\/strong>This will take some time to complete, if it completes successfully, you will have created a cgi binary file that will be located in \u201csapi\/cgi\/php-cgi\u201d<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Phase 2 Running Your new php cgi on your vhost<\/strong><\/span><\/p>\n<ol>\n<li>Copy your new php-cgi\u00a0 binary to the cgi-bin directory or script-alias directory within your domain or vhost. This is usually \u201c\/home\/username\/www\/cgi-bin\u201d<\/li>\n<\/ol>\n<p>Fix the permissions on the new cgi bin so that its running as the apache user or suexec user your are using for your vhost.<\/p>\n<p>Ie. -rw-r&#8211;r&#8211; 1 apache:apache\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a019 Nov\u00a0 7 14:32 php-cgi(correct)<br \/>\n-rw-r&#8211;r&#8211; 1 tailor:tailor \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a019 Nov\u00a0 7 14:32 php-cgi(correct)<\/p>\n<ol>\n<li>Next ensure the file has executable permissions \u201c<em>chmod +x php-cgi<\/em>&#8220;<\/li>\n<li>Now go back into the document root folder of the domain or vhost and create a .htaccess file with the following lines below and save the file.<\/li>\n<\/ol>\n<p>AddHandler php-cgi \u00a0.php \u00a0.htm<br \/>\nAction php-cgi \/cgi-bin\/php-cgi<\/p>\n<ol>\n<li>As soon as you do the above the site will be using the new cgi php. If you reload the phpinfo.php page now you should see the server API read as:<\/li>\n<\/ol>\n<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"bottom\"><strong>Server API<\/strong><\/td>\n<td valign=\"bottom\">CGI\/FastCGI<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Note: if you want to disabled the cgi php simply comment out the lines in the .htaccess file. The cool thing about this is you don\u2019t need to reload apache for php changes anymore, since its running as a cgi. You should check the phpinfo.php page and ensure that all the flags you wanted are listed on that page, if they are not, you either missed the flag in your configure or did not compile fully.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to compile php as a cgi binary This blog post was written using RedHat enterprise server, however the principles apply to just about any linux distro out there, it should work for them all. Generally what I would do is check the OS repository and see what is the next upgraded version of php its going to do, when<a href=\"https:\/\/nicktailor.com\/tech-blog\/how-to-compile-php-and-run-it-as-a-cgi-binary\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58,138],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-centos","category-linux"],"_links":{"self":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":11,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":260,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/148\/revisions\/260"}],"wp:attachment":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}