Home  >  Article  >  Backend Development  >  Steps to install PHP and configure MSSQL connection in Ubuntu system

Steps to install PHP and configure MSSQL connection in Ubuntu system

WBOY
WBOYOriginal
2024-02-29 13:09:031107browse

Steps to install PHP and configure MSSQL connection in Ubuntu system

The steps to install PHP and configure MSSQL connection in Ubuntu system require specific code examples

Ubuntu is a popular Linux operating system that provides a powerful The development environment is conducive to building web applications. Installing PHP and configuring MSSQL connections in Ubuntu systems is one of the important skills that many developers need to master. This article will introduce how to install PHP in the Ubuntu system and configure the steps for MSSQL connection, including specific code examples.

Step 1: Install PHP

  1. Open the terminal and update the system package list:

    sudo apt-get update
  2. Install PHP and related extensions :

    sudo apt-get install php php-mysql php-xml php-mbstring
  3. Verify whether the PHP installation is successful:

    php -v
  4. Install the PHP development kit:

    sudo apt-get install php-dev

Step 2: Install the FreeTDS library

  1. Install the FreeTDS library to establish a connection with MSSQL:

    sudo apt-get install freetds-dev freetds-bin tdsodbc
  2. Configure FreeTDS:
    Open the FreeTDS configuration file:

    sudo nano /etc/freetds/freetds.conf

    Add MSSQL configuration at the end of the file:

    [MSSQL]
     host = your_mssql_host
     port = 1433
     tds version = 8.0
  3. Configure ODBC:
    Open the ODBC configuration file:

    sudo nano /etc/odbc.ini

    Add MSSQL configuration:

    [MSSQL]
     Driver = FreeTDS
     Description = MSSQL Server
     Servername = MSSQL
     Database = your_database_name
     Port = 1433
     TDS_Version = 8.0

Step 3: Install PDO_ODBC extension

  1. Install PDO_ODBC extension:

    sudo apt-get install php-pdo-odbc
  2. Edit the PHP configuration file and enable the PDO_ODBC extension:

    sudo nano /etc/php/7.x/apache2/php.ini

    Add the following content in the file:

    extension=pdo_odbc
  3. Restart the Apache service:

    sudo systemctl restart apache2

Step 4: Test MSSQL connection

  1. Create a PHP file for testing MSSQL connection, such as test-mssql.php:

    <?php
    $server = 'MSSQL';
    $database = 'your_database_name';
    $username = 'your_username';
    $password = 'your_password';
    
    $conn = new PDO("odbc:Driver=FreeTDS;Server=$server;Database=$database;", $username, $password);
    
    if ($conn) {
     echo 'Connected to MSSQL successfully.';
    } else {
     echo 'Failed to connect to MSSQL.';
    }
    ?>
  2. Access the test-mssql.php file in the browser to check whether it is successfully connected to the MSSQL database.

Summary

Through the above steps, we successfully installed PHP in the Ubuntu system and configured the MSSQL connection. During the development process, make sure to follow the above steps one by one and adjust the configuration file according to the specific situation. In this way, you can successfully develop PHP in the Ubuntu system and connect to the MSSQL database.

The above is the detailed content of Steps to install PHP and configure MSSQL connection in Ubuntu system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn