{"id":242,"date":"2011-10-31T18:00:36","date_gmt":"2011-11-01T01:00:36","guid":{"rendered":"http:\/\/35.225.155.113\/blog\/index.php\/2011\/10\/31\/adding_sql_server_support_in_php_on_linux\/"},"modified":"2019-10-09T18:23:49","modified_gmt":"2019-10-10T01:23:49","slug":"adding-sql-server-support-in-php-on-linux","status":"publish","type":"post","link":"https:\/\/www.weinstein.org\/blog\/index.php\/2011\/10\/adding-sql-server-support-in-php-on-linux.html","title":{"rendered":"Adding SQL Server Support in PHP on Linux"},"content":{"rendered":"<p>Back in July I outlined a method for <a href=\"http:\/\/pdw.weinstein.org\/2011\/07\/establish-and-maintain-an-ssh-tunnel-between-linux-and-windows.html\">establishing a SSH tunnel between Linux and Windows<\/a> machines.  The goal of the connection was to enable a PHP script on a front-end Linux web server access to information stored on the back-end private Windows server running SQL Server.<\/p>\n<p>What I didn&#8217;t mention at the time was how I enabled PHP support for Microsoft&#8217;s SQL Server.<\/p>\n<p>The most common deployments of PHP on Linux include support for MySQL or Postgres, depending largely on other factors such has the organization&#8217;s preference, experience and<br \/>\nrequirements. Since PHP can be deployed on Windows, there is support for Microsoft&#8217;s SQL Server. Such support is nontrivial to enable in PHP on Linux.&nbsp;It is however possible:<\/p>\n<p>To enabled SQL Server support in PHP on Linux, the PHP extension that provides said support requires the <a href=\"http:\/\/www.freetds.org\/\"> FreeTDS <\/a> library to build against. FreeTDS is an open source<br \/>\nimplementation of C libraries originally marketed by Sybase and Microsoft to enable access to their database servers.<\/p>\n<p>Downloading the source code, building and installing FreeTDS is straightforward:<\/p>\n<div style=\"padding: 10px; background-color: rgb(0, 0, 0); color: rgb(23, 240, 23);\">\n$ wget \\<br \/>\nftp:\/\/ftp.ibiblio.org\/pub\/Linux\/ALPHA\/freetds\/stable\/freetds-stable.tgz<br \/>\n$ gunzip freetds-stable.tgz<br \/>\n$ tar xf freetds-stable<br \/>\n$ cd freetds<br \/>\n$ .\/configure<br \/>\n$ make<br \/>\n$ make install<\/div>\n<p>The next step is to build the PHP source code against the FreeTDS libraries to include SQL Server support. This can be done one of two ways; build PHP from scratch or build the specific PHP extension. Since I was working on a server with a preexisting install of PHP, I opted for door number two:<\/p>\n<p>Locate or download the source code for the preexisting version of PHP. Next, copy the mssql extension source code from the PHP source code into a separate <i>php_mssql<\/i><br \/>\ndirectory:<\/p>\n<div style=\"padding: 10px; background-color: rgb(0, 0, 0); color: rgb(23, 240, 23);\">\n$ cp ext\/mssql\/config.m4 ~\/src\/php_mssql<br \/>\n$ cp ext\/mssql\/php_mssql.c ~\/src\/php_mssql<br \/>\n$ cp ext\/mssql\/php_mssql.h ~\/src\/php_mssql<\/p>\n<\/div>\n<p>Now build the source code, pointing it to where FreeTDS has been installed:<\/p>\n<div style=\"padding:10px; background-color: rgb(0, 0, 0); color: rgb(23, 240, 23);\">\n$ phpize<br \/>\n$ .\/configure &#8211;with-mssql=\/usr\/local\/freetds<br \/>\n$ make<\/div>\n<p>There should now be a mssql.so file in <i>~\/src\/php_mssql\/modules\/<\/i> that can be copied into the existing PHP install. Once copied the last remaining steps are to enable the extension by modify the <i>php.ini<\/i> file and restarting the Apache HTTP Server.<\/p>\n<p>Additional Information can be found here: <a href=\"http:\/\/www.robert-gonzalez.com\/2008\/03\/31\/connecting-php-on-linux-to-mssql-on-windows\/\">Connecting PHP on Linux to MSQL on Windows <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back in July I outlined a method for establishing a SSH tunnel between Linux and Windows machines. The goal of the connection was to enable a PHP script on a front-end Linux web server access to information stored on the back-end private Windows server running SQL Server. What I didn&#8217;t mention at the time was [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,92,82],"tags":[12,338,6,181,7,287,43,10],"_links":{"self":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/242"}],"collection":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=242"}],"version-history":[{"count":1,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/242\/revisions"}],"predecessor-version":[{"id":318,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/242\/revisions\/318"}],"wp:attachment":[{"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weinstein.org\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}