Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CedNet's avatar

sqlsrv driver on Linux?

I'm trying to move an an application that's been on a Windows (WAMP) server before over to Linux. The app has previously connected to an SQL Server (2012) using the "sqlsrv" driver. I got that setup working just fine.

How do you set up this sqlsrv driver on Linux? I get the expected "could not find driver" message.

If someone out there managed to install the SQL driver on an Ubuntu server, please let me know how. I've found some details for Debian, but not for Ubuntu. Some googling says: sudo apt-get install php5-sybase

The server in question is setup using ServerPilot which is using some special setup to run many PHP versions simultaniously.

Can someone assist here?

0 likes
57 replies
bashy's avatar

Ubuntu is based off Debian so any commands or packages normally work across both.

SQL Server is by Microsoft right? Think you have to compile it yourself on Linux.

bashy's avatar

That's the PHP side of it, not the driver? You can compile it on Linux

1 like
keevitaja's avatar

Yeah, that is obvious. He needs to refactor the code to use this freedts lib, as sqlsrv depends on windows ddl's.

Edit: lol, or perhaps not. I have not used it

CedNet's avatar

A limiting factor right now is of course that I am on ServerPilot, which handles mostly for what PHP supports. They have a list of extensions that their official builds of PHP (4,5,6) supports:

https://serverpilot.io/community/articles/available-php-extensions.html

I see "odbc" in there, but no mssql (which would be the alternative), sadly...

I really fricken want to get rid of the Windows server we have for serving this application.

keevitaja's avatar

If php.net is correct, then you cannot use sqlsrv functions on ubuntu :(

Rufhausen's avatar

I can't comment on the ServerPilot aspect of this, but I've recently had to deal with setting up Homestead to work with MSSQL Server and it came down to doing the following:

sudo apt-get install php5-mssql

Update /etc/freetds/freetds.conf to read as follows:

[global]  
# TDS protocol version
; tds version = 4.2
tds version = 8.0
client charset = UTF-8

Add to php.ini:

mssql.charset = "UTF-8"
4 likes
CedNet's avatar

Rufhausen, will that work with Laravel? I keep "sqlsrv" as driver name or change to "mssql"? Reason I ask is that using "mssql", Laravel says it's not a supported driver (as per the docs).

Rufhausen's avatar

CedNet, yes. At least it's working for me. I haven't changed anything in database.php aside from adding the credentials:

  'sqlsrv' => array(
   'driver' => 'sqlsrv',
            'host' => getenv('MS_DB_HOST'),
            'database' => getenv('MS_DB_DATABASE'),
            'username' => getenv('MS_DB_USERNAME'),
            'password' => getenv('MS_DB_PASSWORD'),
   'prefix' => '',
  ),
CedNet's avatar

Alright, then ServerPilot is out the door.

What PHP version are you using? I'd like to go up to 5.5.

Rufhausen's avatar

I'm running 5.6.2 on homestead and on our Test server (ubuntu 12.04.5).

CedNet's avatar

I just tried to install mssql on the ServerPilot handled server and it won't work as PECL stopped supporting mssql as of 5.4. Great.

When you say Homestead btw, you mean it's via Forge? With Forge I can't add a server at the location where I host my servers (in Sweden). Or can Forge add "any" custom server? (freshly installed)

Rufhausen's avatar

When I refer to Homestead, I'm just referring to my local dev environment running Laravel Homestead with some extras added: https://gist.github.com/rufhausen/b0b053ec39e9e8d90f86 I don't know anything about ServerPilot, but can you just bypass it and log in to the shell directly and install via apt-get, or do u not have root access?

CedNet's avatar

Ah, gotcha.

ServerPilot serves you a secured server ready for adding what they call "apps" (home directories for domains). It does it all pretty well.

They have their own way of serving PHP. Any app on the server can have any PHP version.

So installing php5-mssql won't make mssql available in the app directories.

1 like
CedNet's avatar

I installed it as per your previous post. Set up all the configs. Restarted php. No difference, still complaining over missing sqlsrv driver. Darnit

Rufhausen's avatar

I can only assume that ServerPilot overwrites any customizations that you are making. Bummer. Here's a screenshot of phpinfo on my homestead env to at least prove I'm not crazy. :) https://cloudup.com/cKokf-eln_I

aardalich's avatar

I'm not completely certain on Linux being more of a FreeBSD fan myself.

For my FreeBSD installs (both in production and within Vagrant/VirtualPC in dev) I install and configure something called FreeTDS which then allows usage of the normal mssql pdo driver for php/laravel.

http://www.freetds.org

For the config file (/usr/local/etc/freetds.conf in my case)

[<server name name you will use in laravel database conf>]
       host = <ip address of your sqlsrv>
       port = 14330
       tds version = 7.0 (sql 2003 in my case, may need to change for different versions)
Rufhausen's avatar

I believe FreeTDS gets installed automatically on Ubuntu when you use apt-get to install php5-mssql.

1 like
keevitaja's avatar

I tried this on my ubuntu 14.04 LTS desktop and it works!

You can't use any of the sqlsrv components. Use mssql instead.

➜ sudo apt-get install php5-mssql
➜ sudo php5enmod mssql
➜ sudo service apache2 restart
CedNet's avatar

Okay, I'm testing a new approach using an ODBC connection type. I found this Laravel package: https://github.com/ccovey/odbc-driver It's 4.1 only, but there's a package that ups it to 4.x and it loads fine.

Now the only problem is that the SQL query generated via the PDO ODBC driver is causing an error:

SQLSTATE[HY010]: Function sequence error: 0 [Microsoft][ODBC Driver Manager] Fel i funktionssekvens (SQLExecute[0] at ext\pdo_odbc\odbc_stmt.c:254) (SQL: select count(*) as aggregate from x where x)

By any chance someone here know why this occurs? There's no difference in the SQL queries. Just a driver change from sqlsrv to odbc.

1 like
CedNet's avatar

Btw, the exact generated query (by laravel debug output) works fine via SQL Server management studio. No issue at all.

CedNet's avatar

Okay, appears certain queries simply don't work. Some do. I think I must wait for official ODBC support before I can move this application over to Linux.

Thanks all for your help!

Rufhausen's avatar

This is similar to the behavior I experienced before making the charset changes (to utf-8) in freetds.conf and php.ini for mssql. Some queries worked and others just bonked. Maybe there are comparable changes you can make for your setup to remove the errors.

uxweb's avatar

Hi, i work a lot with SQL Server, and now using Laravel was pain in the butt because i started to use Vagrant to create a virtual environment with Ubuntu.

First, as @keevitaja posted, you must install php5-mssql, this makes Laravel use the dblib driver instead of sqlsrv.

You can see this in the Illuminate\Database\Connectors\SqlServerConnector class.

After installing this package, you must edit the /etc/freetds/freetds.conf file adding this under the global section:

[global]
    tds version = 8.0
    client charset = UTF-8

This lets the driver encode all the data in utf-8 and avoid the strange characters in data.

Finally you should create a locales.conf file within /etc/freetds to allow correct parsing of dates with SQL Server, i have this configuration and has worked well with DateTime, DateTime2, SmallDateTime and Date data types:

[default]
    date format = %Y-%m-%d %I:%M:%S.%z

[en_US]
    date format = %b %e %Y %I:%M:%S:%z%p
    language = us_english
    charset = iso_1

[es_ES]
    date format = %b %d %Y %I:%M%p
    language = spanish
    charset = iso_1

[pt_BR]
    date format = %d/%m/%Y %H:%M
    language = Portuguese
    charset = iso_1

[it_IT]
    date format = %d/%m/%Y %H:%M
    language = Italiano
    charset = iso_1

After that restart your php-fpm or if you are not using fpm, restart nginx or apache.

Hope this helps!.

3 likes
Woodlandtrek's avatar

@uxweb, thank you! I seriously just lost most of a day trying to figure out why my dates from sqlserver would not import directly into mysql.

@Rufhausen thanks for those installation instructions, they made life much easier

louposk's avatar

Really interesting post.

I get PDOException driver not found in production.

I local environment i have the following:

'sqlsrv' => array(
            'driver'   => 'sqlsrv',
            'host'     => 'localhost',
            'database' => 'techno',
            'username' => 'sa',
            'password' => '1234',
            'prefix'   => '',
        ),

and i use php_sqlsrv_54_ts.dll and php_sqlsrv_54.dll. But these are not suppoted in production Centos Linux VPS.

So the only thing that must me done in production is the configuration in /etc/freetds/freetds.conf file? Any ideas?

noleafclover614@gmail.com's avatar

This thread helped me get set up with SQL Server by configuring freetds.conf and locales.conf. Thanks! The only issue now is that the dates being stored in my database are always 12 hours behind. Is this a configuration issue with the timezone?

uxweb's avatar

@noleafclover614 If you are using Homestead or any *nix OS, then you can check if your timezone is set accordingly with the date command:

vagrant@homestead:~$ date

Sat Mar 21 05:28:15 UTC 2015

If you have UTC, then that's why you are getting the dates 12 hours behind.

You can set the right timezone by running this command:

sudo dpkg-reconfigure tzdata

And after that you will get the right date and time.

uxweb's avatar

@louposk Are you using windows for development and linux for production?, Can you describe with more detail your scenario?

Next

Please or to participate in this conversation.