Apache + PHP + MSSQL (Windows Server using Command Line)

Katherine Petalio-Amar
9 min readJul 23, 2021

Here I will show you how to set up Apache, PHP, and MSSQL on a Windows server. I will discuss the following:

  • Download and Install SQL Server (MSSQL)
  • Download and Install SQL Server Management Studio (SSMS)
  • Set SQL Credentials
  • Download and Install Apache on Windows
  • Download and Install PHP
  • Download and Add PHP SQLSRV Extension

Prerequisites

  • You must have a Windows Server (What I used here is Windows Server 2019.)
  • A bit of knowledge in Command Line (Basic will do)

Note: I install most of them using the command line, it might be a little different if you are using Windows Server with UI or Windows Desktop Version.

Install SQL Server

1. Download SQL Server, you can download it here or download it via cmd use the command below and press enter:

C:\> curl https://mycodesnippet.com/downloads/windows/SQL2019-SSEI-Dev.exe -O SQL2019-SSEI-Dev.exe

2. Run and Install, just click the installer or via cmd use the command below and press enter:

C:\> SQL2019-SSEI-Dev.exe

3. Select Installation Type, since it is my first time using MSSQL, I choose the Basic installation, but most tutorials I’ve read recommend using Custom for them to tweak the installation.

4. Select Installation Path. You can also change the installation location if you prefer to.

5. Installing and downloading the package. Wait patiently 🙂

6. Successfully Installed SQL Server.

Once installation is successful you can just close it. But if you want to explore SQLCMD you can click on the Connect Now button.

Install SQL Server Management Studio (SSMS)

1. Download SQL Server Management Studio (SSMS) you can download it here or via cmd use the command below and press enter:

C:\> curl https://mycodesnippet.com/downloads/windows/SSMS-Setup-ENU.exe -O SSMS-Setup-ENU.exe

2. Run and Install, just click the installer or via cmd use the command below and press enter:

C:\> SSMS-Setup-ENU.exe

Just click install, or if you want to change the installation location you can do so.

3. Installing and downloading the package. Wait patiently 🙂

4. Successfully Installed SQL Server.

Once successfully installed or setup completed just close it.

Setup SQL Server Credentials

This will let you connect from PHP to SQL Server.

1. Launch SSMS and log in using Windows Authentication.

Launch SSMS using cmd use the command below and press enter:

C:\> cd C:/Program Files (x86)/Microsoft SQL Server Management Studio 18/Common7/IDEC:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE> ssms.exe

2. Enable SQL Server Authentication, this will allow you to connect using SQL Credentials.

  • Right Click on Server Name then go to Properties
  • On Properties Page go to Security
  • Under Server Authentication select SQL Server and Windows Authentication Mode

3. Enable Login for SA User.

  • Click on Security then Logins, you will see SA user with (x) this indicates that login is disabled for this user.
    SA user is a default user that enables to grant permission for new users.
  • Double Click SA user, then select status under Login select enabled.

4. Create SQL Server Login

  • Right-click Security, then select New->Login
  • Supply the Login Name, and select SQL Server authentication then supply your preferred password, just uncheck the other options if you don’t wish to use it.
  • Under Sercurables, check if connect to SQL is granted.

Just keep the credentials we will use them later on.

Install Apache

1. Download Apache for windows here or via cmd use the command below:

C:\> curl https://www.apachelounge.com/download/VS16/binaries/httpd-2.4.48-win64-VS16.zip -O httpd-2.4.48-win64-VS16.zip

2. Extract Apache zip package, recommended extract location should be on your root drive (ex. C:/Apache24). You can use the following command to extract the installer.

C:\> unzip httpd-2.4.48-win64-VS16.zip

If unzip function is not available, you need to download it, just use the command below:

C:\> curl https://mycodesnippet.com/downloads/windows/unzip.exe -O unzip.exe

Once unzip is successfully download, try to unzip again the Apache installer. Once the installer is successfully extracted Apache24 folder will be seen on your root directory.

3. Run Apache, use the following commands:

C:\> C:/Apache24/bin/httpd.exe

4. Test Installation, use the command below to test if your installation is successful.

C:\> curl http://localhost

This should return the following output otherwise, there is a problem with your installation.

<html><body><h1>It works!</h1></body></html>

Just close your command line to stop Apache Service. Ctrl+C

Install Apache Service

Once we confirmed that apache is successfully working, we can now install Apache as a system service. By doing this we can set to auto start the service every time we turn on our service.

To install Apache Service use the command below:

C:\> C:/Apache24/bin/httpd.exe -k install -n "Apache HTTP Server"

Output:

Installing the 'Apache HTTP Server' service
The 'Apache HTTP Server' service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.

Now that we are done installing the Apache as System Service, let set it to Automatic Start, just use the following command:

C:\> sc config ApacheHTTPServer start=auto

This will automatically start your Apache once you restart your service.

Install PHP

Let’s now install PHP I will be installing PHP 8, but before we proceed we need to install Microsoft Visual C++ Redistributable for Visual Studio 2019. I have encountered issues due to this redistributable app, unfortunately, I’ve downloaded an outdated installer which gives me compatibility issues in PHP.

You can download here under the heading Other Tools and Frameworks for updated VC Redistributable. You can also download it using cmd just use the command below:

C:\> curl https://aka.ms/vs/16/release/VC_redist.x64.exe -O VC_redist.x64.exeC:\> VC_redist.x64.exe

You need to restart your server after your installation.

After you successfully install Visual C++ Redistributable, let’s now proceed with PHP installation.

1. Download PHP 8.0 zip package (VS16 x64 Thread Safe ) here. (I’ll be using Php8, but you can choose whichever version you want, just used the Thread Safe Version). You can also download it using the command below:

Create PHP folder, then go to the created folder

C:\> mkdir phpC:\> cd php

Download PHP 8.0 zip package

C:\php> curl https://windows.php.net/downloads/releases/php-8.0.8-Win32-vs16-x64.zip -O php-8.0.8-Win32-vs16-x64.zip

Extract the PHP Package inside PHP Folder

C:\php> unzip php-8.0.8-Win32-vs16-x64.zip

Note: If unzip does not work, just download again the unzip.exe inside the PHP folder.

2. Configure PHP. The configuration file of PHP is named php.ini. This file does not exist on the PHP zip package, but you will notice php.ini-development and php.ini-production files on extracted files. We just need to copy this configuration file and rename it to php.ini. To do it you can use the following command:

C:\php> copy php.ini-development php.ini

I’ll be using php.ini-development this default configuration provides a development setup that reports all PHP errors and warnings.

3. Enable any required extensions. Extension provides you additional library or plugin that you can use within the PHP Application. For now, we will just enable the common extension used. We just need to remove the leading semicolon(;) to uncomment and enable the extension.

I’ll be using the following extensions below:

extension=curl
extension=gd
extension=mbstring
extension=pdo_mysql

To update the php.ini just use the command below:

C:\php> notepad php.ini

This will open the php.ini file just locate those extensions you wish to enabled or to use.

Don’t forget to your save changes.

4. Add C:\php to the PATH environment variable. Adding PHP to the PATH environment variable will ensure that Windows can find the PHP executable. Just use the command below to add PHP Path.

C:\> set PATH=%PATH%;C:\php;

5. Configure PHP as Apache Module. For our PHP to run our Apache server we need to add a PHP Module on the Apache configuration file.

First, we need to stop our Apache Service.

C:\> net stop ApacheHTTPServer

Once Apache has stopped running, we can now edit httpd.conf. Just follow the following command:

C:\> cd Apache24/confC:\Apache24\conf> notepad httpd.conf

Once httpd.conf open, just add the following at the bottom of the file.

# PHP8 module
PHPIniDir "C:/php"
LoadModule php_module "C:/php/php8apache2_4.dll"
AddType application/x-httpd-php .php

If you want to prioritize PHP files over other files, you need to update DirectoryIndex. Just locate the following on httpd.conf file.

<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

Just add index.php before index.html. It should look like below:

<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>

When you are done, don’t forget to save changes.

Let’s check if the apache configuration is correct. Use the command below:

C:\> cd Apache24/binC:\Apache24\bin> httpd -t

The output should be the one below otherwise, you have errors in your configuration.

Syntax OK

Now let’s start Apache.

C:\> net start ApacheHTTPServer

6. Test a PHP file. Let’s create index.php file inside C:\> Apache24/htdocs, and add some script. Use the following command:

C:\> cd Apache24/htdocsC:\Apache24\htdocs> notepad index.php

This will ask if you want to create the file, just click yes it will open the notepad application, then add the following script:

<?php 
echo "HELLO Running PHP!";
?>

Save the file. Then test if it is working.

C:\> curl http://localhost/index.php

This should give you an output:

HELLO Running PHP!

Install PHP SQLSRV Extension

1. Download SQLSRV Driver. You can download it here or via cmd use the command below and press enter:

C:\> curl https://mycodesnippet.com/downloads/windows/SQLSRV59.exe -O SQLSRV59.exe

2. Install SQLRV Driver via cms use the command below and press enter:

C:\> SQLSRV59.exe

3. Browse the PHP extension folder. In our case, it is located at C:\php\ext.

Just click OK, then it will extract all extension files inside the ext folder.

4. Update your php.ini file. We need to add SQLSRV Extensions. Use the commands below:

C:\> notepad C:/php/php.ini

Then add the following extensions: (Please take note that we are using PHP8 so we will also add extensions suitable for PHP8).

[PHP_SQLSRV] 
extension=php_pdo_sqlsrv_80_nts_x64.dll extension=php_pdo_sqlsrv_80_ts_x64.dll
extension=php_sqlsrv_80_nts_x64.dll
extension=php_sqlsrv_80_ts_x64.dll
mssql.textlimit = 2147483647
mssql.textsize = 2147483647

5. Check if SQLSRV is enabled. Create info.php file, use the following command:

C:\> notepad C:/Apache24/htdocs/info.php

Then add the following script:

<?php
phpinfo();
?>

Save the file, then visit a browser this link: http://localhost/info.php (Note: Visiting through a browser using this link will only work if your Windows Server is Desktop Version)

You should be able to see the following on your PHP info. If PDO_SQLSRV does not exist it means it is not successfully enabled or installed.

Checking Issues/Error

If ever you encounter an issue, check on the error logs.

C:\>notepad C:/Apache24/logs/error.log

This will give you a hint of what’s wrong with your server.

Bonus Windows Commands

dir //show files and directory
del <filename> //delete file
rmdir <directory name> //delete directory
rmdir /s <directory name> //delete directory including its content
sc queryex type=service state=all //show all Windows Service

Good Luck! Happy Coding! 🙂

References:

--

--

Katherine Petalio-Amar

I'm a Software Developer with experience in Web Development and Mobile App Development and a bit knowledgeable in Server Management.