WEB & WAS/PHP

PHP 7.3~7.4 sqlsrv 모듈 설치

서버엔지니어 2023. 3. 26.
728x90

OS : CentOS 7.x
PHP : 7.4  / 7.3

1.  odbc install

#RedHat Enterprise Server 6
curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo
 
#RedHat Enterprise Server 7
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
 
#RedHat Enterprise Server 8 and Oracle Linux 8
curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo
 
 
# yum remove unixODBC-utf16 unixODBC-utf16-devel #to avoid conflicts
 
# yum install msodbcsql17
# optional: for bcp and sqlcmd


# yum install mssql-tools
# echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
# echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
# source ~/.bashrc
 
# optional: for unixODBC development headers
# yum install unixODBC-devel

2. sqlsrv download & install
# wget https://pecl.php.net/get/sqlsrv-5.8.0.tgz
 
# tar xvf sqlsrv-5.8.0.tgz 
# cd sqlsrv-5.8.0
# /usr/local/php/bin/phpize 
# ./configure --with-php-config=/usr/local/php/bin/php-config 
# make
# make install
 
# ls -l /usr/local/php/lib/php/extensions/debug-zts-20180731/
-rwxr-xr-x 1 root root 1516440  8월 19  2019 opcache.so

2-1   pdo_sqlsrv install
# wget https://pecl.php.net/get/pdo_sqlsrv-5.8.0.tgz
# tar xvf pdo_sqlsrv-5.8.0.tgz 
# cd pdo_sqlsrv-5.8.0
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config 
# make
# make install
 
# ls -l /usr/local/php/lib/php/extensions/debug-zts-20180731/
-rwxr-xr-x 1 root root 3093384 10월 16 17:11 pdo_sqlsrv.so
-rwxr-xr-x 1 root root 3013752 10월 16 15:00 sqlsrv.so

# vi /usr/local/apache/conf/php.ini 

3. 웹서버 재시작
# systemctl restart httpd

4. 웹페이지에서 php info 확인

 


5. ms-sql 연동 확인

============================================================
<?php
$serverName = "192.168.0.10";
$connectionOptions = array(
    "database" => "xinet",
    "uid" => "xinet",
    "pwd" => "xinet12&*("
);
 
// Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if ($conn === false) {
    die(formatErrors(sqlsrv_errors()));
}
 
// Select Query
$tsql = "SELECT @@Version AS SQL_VERSION";
 
// Executes the query
$stmt = sqlsrv_query($conn, $tsql);
 
// Error handling
if ($stmt === false) {
    die(formatErrors(sqlsrv_errors()));
}
?>
 
<h1> Results : </h1>
 
<?php
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    echo $row['SQL_VERSION'] . PHP_EOL;
}
 
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
 
function formatErrors($errors)
{
    // Display errors
    echo "Error information: <br/>";
    foreach ($errors as $error) {
        echo "SQLSTATE: ". $error['SQLSTATE'] . "<br/>";
        echo "Code: ". $error['code'] . "<br/>";
        echo "Message: ". $error['message'] . "<br/>";
    }
}
?>

============================================================

 

6. 웹페이지서 확인

'WEB & WAS > PHP' 카테고리의 다른 글

Nginx 연동 PHP-FPM 설치 (8.2)  (0) 2023.04.09
Rocky Linux 8 PHP remi 패키지 설치  (0) 2023.03.27
PHP curl 모듈에 openssl 연동  (0) 2023.03.26
phpmysql 연동 php파일  (0) 2023.03.17
PHP 설치시 오류 모음집  (0) 2023.03.17

댓글