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
sudo yum install httpd
sudo systemctl start httpd sudo systemctl enable httpd
http://localhost
, if you see the default Apache Welcome page, the installation is successful. Step 2: Install PHP
sudo yum install php php-mysql
sudo systemctl restart httpd
sudo vi /var/www/html/test.php
Add the following code to the file:
<?php phpinfo(); ?>
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
sudo yum install mysql-server
sudo systemctl start mysqld
sudo mysql_secure_installation
mysql -u root -p
CREATE DATABASE mydatabase; CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword'; GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost'; FLUSH PRIVILEGES;
mysql -u myuser -p
Step 4: Test connection
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(); ?>
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!