Home >Backend Development >PHP Tutorial >Detailed explanation of the steps to install PHP and MySQL under Red Hat system

Detailed explanation of the steps to install PHP and MySQL under Red Hat system

PHPz
PHPzOriginal
2024-03-06 14:15:041020browse

Red Hat系统下安装PHP和MySQL的步骤详解

Detailed explanation of the steps to install PHP and MySQL on Red Hat system

Building PHP and MySQL environments on Red Hat system is one of the common needs of developers. PHP is a popular server-side scripting language, and MySQL is a widely used relational database management system. Their combination can provide powerful website development and database management functions. This article will introduce in detail the steps to install PHP and MySQL on the Red Hat system, and demonstrate the specific code operation process.

Step 1: Install Apache

  1. First, open the terminal and use the following instructions to install the Apache HTTP server:
sudo yum install httpd
  1. After the installation is completed, Start Apache and set it to start automatically at boot:
sudo systemctl start httpd
sudo systemctl enable httpd
  1. Confirm whether Apache is successfully installed, open the browser, visit http://localhost, if you see the default Apache Welcome page, the installation is successful.

Step 2: Install PHP

  1. Use the following instructions to install PHP and related extensions:
sudo yum install php php-mysql
  1. After the installation is complete, Restart the Apache service:
sudo systemctl restart httpd
  1. To create a PHP test page, you can use the following command:
sudo vi /var/www/html/test.php

Add the following code to the file:

<?php
phpinfo();
?>
  1. Save and exit, then visit http://localhost/test.php in the browser. If you can see the PHP information page, it means that PHP has been successfully installed.

Step 3: Install MySQL

  1. Use the following command to install the MySQL database:
sudo yum install mysql-server
  1. After the installation is complete, start the MySQL service :
sudo systemctl start mysqld
  1. Run the MySQL security script and set the root password:
sudo mysql_secure_installation
  1. Follow the prompts, set a new password, remove anonymous users, Disable root remote login, etc.
  2. Log in to the MySQL database using the following command:
mysql -u root -p
  1. Create the database and user in MySQL:
CREATE DATABASE mydatabase;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
  1. Confirm MySQL If it is running normally, you can use the following command:
mysql -u myuser -p

Step 4: Test connection

  1. Create a test PHP page for connecting to the MySQL database:
sudo vi /var/www/html/db_test.php

Add the following PHP code in the file:

<?php
$servername = "localhost";
$username = "myuser";
$password = "mypassword";
$dbname = "mydatabase";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

$conn->close();
?>
  1. Save and exit, then visit http://localhost/db_test.php in the browser, if If you can see "Connected successfully", it means that PHP and MySQL have been successfully connected.

Through the above steps, we successfully installed Apache, PHP and MySQL on the Red Hat system and tested the connection between them. These steps provide a detailed guide for setting up PHP and MySQL environments. I hope it will be helpful to you.

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