Home  >  Article  >  Backend Development  >  Detailed introduction to the steps to install PHP and MySQL on Red Hat

Detailed introduction to the steps to install PHP and MySQL on Red Hat

王林
王林Original
2024-03-06 22:30:051170browse

详细介绍Red Hat上安装PHP和MySQL的步骤

"Detailed steps for installing PHP and MySQL on Red Hat"

Red Hat is one of the commonly used Linux distributions. In order to build Web applications or Websites usually require PHP and MySQL to be installed. This article will detail the steps to install PHP and MySQL on a Red Hat system and provide specific code examples.

Step One: Update the System

Before installing any new software, you first need to update the system to ensure that all software packages are up to date. Use the following command to update the system:

sudo yum update

Step 2: Install PHP

  1. Install PHP and related modules

Use the following command to install PHP and Commonly used modules:

sudo yum install php php-mysql
  1. Verify PHP installation

After the installation is complete, you can use the following command to verify whether PHP is installed successfully:

php -v

If the installation is successful, PHP version information will be displayed.

Step 3: Install MySQL

  1. Install MySQL server

Use the following command to install the MySQL server:

sudo yum install mysql-server
  1. Start the MySQL service and set it to start automatically at boot
sudo service mysqld start
sudo chkconfig mysqld on
  1. Set the MySQL root password

When you install MySQL for the first time, you need to set the password of the root user:

sudo /usr/bin/mysql_secure_installation

Follow the prompts to set the root password, delete the test database, etc.

Step 4: Connect PHP and MySQL

After installing PHP and MySQL, you need to ensure that they can be connected correctly. You can use PHP's mysqli extension to connect to a MySQL database.

  1. Edit PHP configuration file
sudo nano /etc/php.ini

Make sure the following two lines are uncommented and configured correctly:

extension=mysqli.so
  1. Create a simple PHP script to test the connection.

Write a PHP file (such as test.php):

<?php
$servername = "localhost";
$username = "root";
$password = "your_password";
$dbname = "your_database";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功!";
$conn->close();
?>

Save and exit the editor, then move the file to the root directory of the web server:

sudo mv test.php /var/www/html/test.php
  1. Access this file in your browser to test the connection.

Enter the IP address or domain name of the server in the browser, plus "/test.php", such as http://your_server_ip/test.php. If you see "Connection successful!" Indicates that the connection between PHP and MySQL is normal.

Through the above steps, you have successfully installed PHP and MySQL on the Red Hat system, and the connection is normal. This lays the foundation for you to build a web application or website later. Hope this article helps you!

The above is the detailed content of Detailed introduction to the steps to install PHP and MySQL on Red Hat. 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