GNU/Linux >> Linux の 問題 >  >> Cent OS

MsSQL ServerCentos8に接続するためのphpをインストールしてセットアップします

Microsoft SQL Serverは、Microsoftによって開発されたリレーショナルデータベース管理システムです。データベースサーバーとして、他のソフトウェアアプリケーション(ネットワーク上の同じコンピューターまたは別のコンピューターのいずれかで実行される場合があります)からの要求に応じてデータを保存および取得する主な機能を備えたソフトウェア製品です。

また、チェックしてください:

  • DockerおよびDocker-Composeを使用してMsSQLServer2019を実行する方法
  • Ubuntu20.04にMsSQLServer2019をインストールする方法

前提条件

  • Centos8サーバーを更新
  • サーバーまたはsudoアクセス権を持つユーザーへのrootアクセス

目次

  1. php7.4のインストールと設定
  2. MsSqlサーバーのインストールとセットアップ
  3. phpmssqlドライバーのセットアップ
  4. nginxを使用したシンプルなサイトの提供

続行する前に、OSが最新であることを確認してください。

sudo dnf -y update

1。 PHPのインストールと設定

デフォルトのCentos8リポジトリには、古いバージョンのPHPが含まれています。 Remiは、CentosLinuxおよびRedHatLinux派生ディストリビューションのphp用の最新のリポジトリを提供します。インストールしましょう。

最初にdnf-utilsをインストールします

sudo dnf install -y dnf-utils

次に、このパッケージをインストールして、レミリリースを有効にします。

dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm

他のバージョンのphpがインストールされていないことを確認するには、phpモジュールをリセットします。

dnf module reset php -y

次に、インストールするphpのバージョンを有効にする必要があります。このガイドでは7.4を使用するので、次のコマンドで有効にします。

dnf module enable -y php:remi-7.4

php 7.4が有効になっていることを確認します:

# dnf module list php

Last metadata expiration check: 0:00:34 ago on Fri 10 Sep 2021 05:13:49 AM UTC.
CentOS Linux 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name Stream Profiles Summary
php remi-7.2 common [d], devel, minimal PHP scripting language
php remi-7.3 common [d], devel, minimal PHP scripting language
php remi-7.4 [e] common [d], devel, minimal PHP scripting language
php remi-8.0 common [d], devel, minimal PHP scripting language
php remi-8.1 common [d], devel, minimal PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

必要なバージョンのphpがインストールされたので、phpと必要なパッケージをインストールしましょう:

dnf install -y \
    php \
    php-fpm \
    php-pdo \
    php-pear \
    php-devel

インストールが完了したら、次のコマンドを使用してphpのバージョンを確認します。

# php -v

PHP 7.4.23 (cli) (built: Aug 24 2021 16:33:30) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.23, Copyright (c), by Zend Technologies

mssqlサーバーのインストールとセットアップ

このセクションでは、データベースサーバーをセットアップします。

MsSQLパッケージがデフォルトのリポジトリに見つかりません。次のコマンドを使用してリポジトリを追加しましょう:

sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/8/mssql-server-2019.repo

次に、次のコマンドを使用してmssqlをインストールします。

sudo dnf install -y mssql-server

次のステップは、サービスを使用できるように構成することです。このコマンドを使用してプロンプトに応答し、デフォルトのユーザー(sa)を作成します )パスワード:

sudo /opt/mssql/bin/mssql-conf setup

出力:

# sudo /opt/mssql/bin/mssql-conf setup

usermod: no changes
Choose an edition of SQL Server:
1) Evaluation (free, no production use rights, 180-day limit)
2) Developer (free, no production use rights)
3) Express (free)
4) Web (PAID)
5) Standard (PAID)
6) Enterprise (PAID) - CPU Core utilization restricted to 20 physical/40 hyperthreaded
7) Enterprise Core (PAID) - CPU Core utilization up to Operating System Maximum
8) I bought a license through a retail sales channel and have a product key to enter.

Details about editions can be found at https://go.microsoft.com/fwlink/?LinkId=2109348&clcid=0x409

Use of PAID editions of this software requires separate licensing through a Microsoft Volume Licensing program.
By choosing a PAID edition, you are verifying that you have the appropriate number of licenses in place to install and run this software.

Enter your edition(1-8): 2
The license terms for this product can be found in
/usr/share/doc/mssql-server or downloaded from:
https://go.microsoft.com/fwlink/?LinkId=2104294&clcid=0x409

The privacy statement can be viewed at:

https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x409

Do you accept the license terms? [Yes/No]:Yes
Enter the SQL Server system administrator password:
Confirm the SQL Server system administrator password:
Configuring SQL Server...

ForceFlush is enabled for this instance.
ForceFlush feature is enabled for log durability.

Created symlink /etc/systemd/system/multi-user.target.wants/mssql-server.service → /usr/lib/systemd/system/mssql-server.service.

Setup has completed successfully. SQL Server is now starting.

mssqlサーバーを起動して有効にします

systemctl start mssql-server
systemctl enable mssql-server

systemctl status mssql-server

Active: active (running)が表示されます。 すべてが正しく設定されている場合。

# systemctl status mssql-server

● mssql-server.service - Microsoft SQL Server Database Engine
Loaded: loaded (/usr/lib/systemd/system/mssql-server.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-09-10 05:24:51 UTC; 1min 22s ago
Docs: https://docs.microsoft.com/en-us/sql/linux
Main PID: 264891 (sqlservr)
Tasks: 136
Memory: 703.6M
CGroup: /system.slice/mssql-server.service
├─264891 /opt/mssql/bin/sqlservr
└─264918 /opt/mssql/bin/sqlservr

Sep 10 05:24:56 test-db-server sqlservr[264891]: [158B blob data]

3。 phpmssqlドライバーのセットアップ

phpとmssqlサーバーがインストールされ、期待どおりにセットアップされたので、ドライバーを認識できるようにシステムを構成する必要があります。

これを行うには、mssqlリポジトリを追加し、いくつかの依存関係をインストールする必要があります。

curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo
sudo ACCEPT_EULA=Y dnf -y install msodbcsql17

# Install this for unixODBC development headers
sudo dnf -y install unixODBC-devel

sqlcmdをインストールします コマンドラインクライアント

sudo dnf install -y mssql-tools

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc

source ~/.bashrc

データベースの作成

接続するテストデータベースを作成しましょう。

まず、次のコマンドを使用してmssqlサーバーにログインします。

sqlcmd -S localhost -U SA -P 'goK7GBU4Cp4UPYz'

次に、これを使用してデータベースを作成します。

CREATE DATABASE TestDB
GO

データベースを一覧表示して、データベースが作成されたことを確認しましょう:

1> SELECT Name from sys.Databases
2> GO

Name
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
TestDB

(5 rows affected)
1>

ドライバーのセットアップ

SQLサーバーの依存関係をインストールしましょう。 pecl コマンドはphp-pearによって提供されます

sudo pecl install sqlsrv
pecl install pdo_sqlsrv

次に、拡張子をiniファイルに追加します

echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-pdo_sqlsrv.ini
echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/20-sqlsrv.ini

最後にphp-fpmを再起動します:

systemctl restart php-fpm
systemctl status php-fpm

出力:

# systemctl status php-fpm

● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-09-10 05:39:41 UTC; 884ms ago
Main PID: 275734 (php-fpm
Status: "Ready to handle connections"
Tasks: 6 (limit: 23800)
Memory: 29.7M
CGroup: /system.slice/php-fpm.service
├─275734 php-fpm: master process (/etc/php-fpm.conf)
├─275735 php-fpm: pool www
├─275736 php-fpm: pool www
├─275737 php-fpm: pool www
├─275738 php-fpm: pool www
└─275739 php-fpm: pool www

Sep 10 05:39:41 test-db-server systemd[1]: Starting The PHP FastCGI Process Manager...
Sep 10 05:39:41 test-db-server systemd[1]: Started The PHP FastCGI Process Manager.

4。 nginxでシンプルなサイトを提供する

すべてがセットアップされたので、セットアップが機能していることをテストするための簡単なphpスクリプトを作成しましょう。

nginxをインストール

このコマンドを使用してnginxをインストールします:

sudo dnf install -y nginx

nginxサービスを開始して有効にします:

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx

表示されるはずです

# sudo systemctl status nginx

● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/nginx.service.d
└─php-fpm.conf
Active: active (running) since Fri 2021-09-10 05:52:17 UTC; 323ms ago
Main PID: 277633 (nginx)
Tasks: 3 (limit: 23800)
Memory: 8.0M
CGroup: /system.slice/nginx.service
├─277633 nginx: master process /usr/sbin/nginx
├─277634 nginx: worker process
└─277635 nginx: worker process

Sep 10 05:52:17 test-db-server systemd[1]: Starting The nginx HTTP and reverse proxy server...
Sep 10 05:52:17 test-db-server nginx[277630]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Sep 10 05:52:17 test-db-server nginx[277630]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Sep 10 05:52:17 test-db-server systemd[1]: Started The nginx HTTP and reverse proxy server.

簡単なサイトの作成

nginxがインストールされたので、db接続をテストするための簡単なスクリプトを作成しましょう。

ディレクトリ構造を作成する

sudo mkdir /var/www/site1
cd /var/www/site1
chown nginx.nginx -R /var/www/site1

このファイルを作成しますindex.php このコンテンツで作成したディレクトリ内:

<?php
$serverName = "127.0.0.1,1433";
$dbUser = 'sa';
$pwd = "goK7GBU4Cp4UPYz";
$dbName = "TestDB";

$connInfo = array("Database"=>$dbName, "UID"=>$dbUser, "PWD"=>$pwd);

$conn = sqlsrv_connect( $serverName, $connInfo);

if( $conn ) {
    echo "Connection established.<br />";
} else {
    echo "Connection could not be established to $serverName.<br />";
    die( print_r( sqlsrv_errors(), true));
}

/* Close the connection. */
sqlsrv_close( $conn);
?>

Nginx構成ファイルを作成します:

sudo vim /etc/nginx/conf.d/php-site.conf

このコンテンツを追加します:

    server {
    listen 80;
    server_name php.citizix.com;
    server_tokens off;
    client_max_body_size 10M;

    root /var/www/site1;
    index index.php;
    charset utf-8;

    location / {
        index index.php;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_intercept_errors on;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass php-fpm;
    }
}

次に、次のコマンドを使用して構成をテストします。

sudo nginx -t

すべて問題がない場合は、nginxサーバーを再起動します。

systemctl restart nginx

ドメインのDNS名をサーバーにマップします。 digコマンドで確認します:

➜ dig -t A php.citizix.com

; <<>> DiG 9.10.6 <<>> -t A php.citizix.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 16605
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1280
;; QUESTION SECTION:
;php.citizix.com. IN A

;; ANSWER SECTION:
php.citizix.com. 300 IN A 172.67.197.111
php.citizix.com. 300 IN A 104.21.82.63

;; Query time: 225 msec
;; SERVER: 192.168.16.175#53(192.168.16.175)
;; WHEN: Fri Sep 10 09:42:52 EAT 2021
;; MSG SIZE rcvd: 76

次に、php fpm configを更新して、nginxがphpスクリプトを提供できるようにします。ファイル/etc/php-fpm.d/www.conf ユーザーとグループをNginxに更新します:

user = nginx
group = nginx

次に、すべてが機能していることをテストするには、次のコマンドを使用してカールを実行します。

➜ curl -iL http://php.citizix.com/

Connection established.<br />

結論

このガイドでは、PHPをインストールし、Mssqlをインストールしてから、msssqlデータベースに接続するphpコンテンツをサーバー化するためのドライバーをインストールしました。


Cent OS
  1. CentOS7LinuxサーバーにPHP7をインストールまたはアップグレードする方法

  2. CentOS7にTeamSpeakサーバーをインストールして使用する方法

  3. CentOS 7にApache、PHP、MariaDBを使用してZenarioをインストールします

  1. CentOS8にPHP7.4をインストールします

  2. Centos8でのPhp7xおよびmssqlサーバーの操作

  3. CentOS7にApacheとPHPをインストールする

  1. CentOS7にPHPComposerをインストールして使用する方法

  2. CentOS7にPHP7をインストールします

  3. CentOS7にVNCをインストールして設定する方法