Home >Backend Development >PHP Tutorial >Detailed introduction to the steps to install PHP and MySQL on Red Hat
"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
Use the following command to install PHP and Commonly used modules:
sudo yum install php php-mysql
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
Use the following command to install the MySQL server:
sudo yum install mysql-server
sudo service mysqld start sudo chkconfig mysqld on
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.
sudo nano /etc/php.ini
Make sure the following two lines are uncommented and configured correctly:
extension=mysqli.so
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
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!