Home >Backend Development >PHP Tutorial >Installation tutorial for PHP and MySQL in Red Hat environment

Installation tutorial for PHP and MySQL in Red Hat environment

WBOY
WBOYOriginal
2024-03-06 15:21:05881browse

Red Hat环境下PHP和MySQL的安装教程

Installing PHP and MySQL in a Red Hat environment is one of the basic steps to build a Web development environment. This article will detail the specific steps to install PHP and MySQL on Red Hat systems and provide corresponding code examples. I hope that through the guidance of this article, you can successfully set up a PHP and MySQL environment and prepare for web development work.

1. Install PHP

  1. Log in to the Red Hat system and open the terminal.
  2. Use the following command to update the system package list:

    sudo yum update
  3. Install PHP and related plug-ins:

    sudo yum install php php-mysql
  4. After the installation is complete, restart the Apache service:

    sudo systemctl restart httpd
  5. Verify whether PHP is successfully installed:

    php -v

2. Install MySQL

  1. Install the MySQL database server:

    sudo yum install mysql-server
  2. Start the MySQL service and set the boot time:

    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. Run the MySQL security script to Improve security:

    sudo mysql_secure_installation

    Follow the prompts to set the MySQL root password, remove anonymous users, disable root remote login, etc.

  4. Verify whether MySQL is installed successfully:

    mysql -u root -p

    Enter the root password and successfully log in to mysql, which means the installation is successful.

3. Connect PHP to MySQL

  1. Create a new PHP file in the Web directory, such as test.php:

    sudo vi /var/www/html/test.php
  2. Edit test.php and enter the following code:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "your_password";
    $conn = new mysqli($servername, $username, $password);
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully!";
    ?>
  3. Save and exit the editor, then visit http://localhost/test.php to check whether the connection is successful to the MySQL database.

Through the above steps, you have successfully installed PHP and MySQL on the Red Hat system and established the connection between them. This will provide a stable environment for your web development work. Hope this article can be helpful to you.

The above is the detailed content of Installation tutorial for PHP and MySQL in Red Hat environment. 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