Home  >  Article  >  Backend Development  >  How to configure PHP and MySQL on Red Hat server

How to configure PHP and MySQL on Red Hat server

WBOY
WBOYOriginal
2024-03-06 18:12:041167browse

在 Red Hat服务器上配置PHP和MySQL的方法分享

Share how to configure PHP and MySQL on Red Hat server

Configuring PHP and MySQL on Red Hat server is one of the key steps in building a web application. PHP and MySQL are commonly used technologies in web development, so configuring them is very important. This article will detail the steps to configure PHP and MySQL on a Red Hat server and provide specific code examples for reference.

1. Install PHP

First, make sure that PHP is installed on the server. You can check the installation of PHP through the following command:

php -v

If PHP has been successfully installed, the version information of PHP will be displayed.

If PHP is not installed on the server, you can use the following command to install PHP on Red Hat:

sudo yum install php

After the installation is complete, you can create a PHP file for testing. Create a file named test.php on the server with the following content:

<?php
    phpinfo();
?>

After saving the file, you can access the file through the browser to view PHP information.

2. Install MySQL

Next, you need to install the MySQL database. You can use the following command to install MySQL on Red Hat:

sudo yum install mysql-server

After the installation is complete, you need to start the MySQL service and set the password:

sudo service mysqld start
sudo /usr/bin/mysql_secure_installation

Follow the prompts to set the MySQL password and other options.

3. Connect PHP and MySQL

To enable PHP to connect to the MySQL database, you need to install the PHP and MySQL connection libraries. You can use the following command to install the connection library for PHP and MySQL:

sudo yum install php-mysql

After the installation is complete, you can use the following code example to test the connection between PHP and MySQL:

<?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 the above code as test_mysql.php file, after modifying the corresponding database information, access the file through the browser to test whether the connection between PHP and MySQL database is successful.

Summary:

Configuring PHP and MySQL on the Red Hat server is an important step in building a web application. Through the above steps, you can install and configure PHP and MySQL, and use code samples to test the connection between PHP and MySQL. I hope this article can help you successfully configure PHP and MySQL on a Red Hat server.

The above is the detailed content of How to configure PHP and MySQL on Red Hat server. 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