I started a project recently that required that I connect a Microsoft SQL Server database with a Laravel 5.5 application, so I thought I’d document how I install the Once you install the driver, you can easily configure Laravel to use a Microsoft SQL Server connection:
I use Docker with most of my PHP projects—I am releasing a book on using Docker with PHP next week—so I thought I’d show you the recipe for getting a SQL server connection with the PDO driver. Here’s the
I was having difficulties installing the necessary packages with Debian Stretch and PHP 7.2, so I am using the official PHP 7.1 Docker image as the base image that I extend from at the time of writing. I am demonstrating the apache version so it’s easy to serve up an
The first RUN instruction installs the packages necessary for installing the
These packages are needed to install the PECL module for SQL Server’s PDO driver. You can find installation instructions for various platforms in the Microsoft/msphpsql GitHub project readme. It’s also important to note that we set the The next RUN instruction installs PHP extensions using the official PHP image’s
Finally, the most important part of this RUN instruction, we are using Building and Running The ContainerThe final instruction in the
When we build the image, the Dockerfile will copy the index.php file in the location that Apache will look for it, and we can verify that the SQL Server drivers were installed and working with PHP. You can build the image with the
The image will take a few minutes depending on your connection speed. Once the build finishes, you can run it from the command line:
The After the container is running you should see some Apache logs in the console, and you can visit http://localhost:8080 in the browser to verify that the SQL Server module is enabled: You should now be able to connect to a SQL Server database in your PHP 7.1 application! I’ve kept the example simple to just focus on the steps necessary to install the SQL Server PDO drivers, but I hope you can see how Docker can help ease the hassle of connecting to a Microsoft SQL database from your PHP applications. Want to learn more about using Docker with PHP? I wrote a book, Docker for PHP Developers that is now available for sale. You can pick from one of two packages:
The links included are affiliate links which means if you decide to buy Laravel News gets a little kickback to help run this site. |