Current Location: Home> Latest Articles> Complete Guide to SQL and PHP Configuration on CentOS | Installation and Optimization Tutorial

Complete Guide to SQL and PHP Configuration on CentOS | Installation and Optimization Tutorial

gitbox 2025-06-30

When configuring SQL and PHP on CentOS, ensuring system stability and security is crucial. This guide will explain in detail how to configure SQL and PHP in the CentOS environment, helping you quickly set up an efficient development environment.

CentOS Basic Environment Installation

First, ensure your CentOS system is installed and updated to the latest version. You can update your system using the following command:

sudo yum update -y

After updating the system, you can begin installing the necessary packages, including PHP and SQL components.

Installing PHP

Installing PHP on CentOS is straightforward. Use the following command to install PHP and its common extensions:

sudo yum install php php-mysqlnd php-xml php-mbstring php-zip -y

After installation, run the following command to verify if PHP was installed successfully:

php -v

If the PHP version information is displayed, the installation was successful.

Installing MariaDB as the SQL Database

To configure SQL in a CentOS environment, you can choose to install MariaDB, a lightweight version of MySQL. Execute the following command to install MariaDB:

sudo yum install mariadb-server -y

After installation, enable and start the MariaDB service:

sudo systemctl start mariadb

Securing MariaDB

To enhance the database security, it is recommended to run the MariaDB security script:

sudo mysql_secure_installation

Follow the prompts to complete the security setup, including setting the root password, removing anonymous users, etc.

Configuring PHP to Connect to MariaDB

In your PHP code, you need to set up the database connection. Here's a simple example showing how to connect to MariaDB in PHP:

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

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connection successful";

Using PHPMyAdmin to Manage the Database

To conveniently manage your database, you can install PHPMyAdmin. First, install the required dependencies:

sudo yum install epel-release -y

After installation, you need to configure PHPMyAdmin to allow web access. Edit the configuration file:

sudo nano /etc/httpd/conf.d/phpMyAdmin.conf

Ensure you change the following setting to allow access from your IP address:

Require all granted

Finally, restart the Apache service to apply the changes:

sudo systemctl restart httpd

Conclusion

By following this guide, you have successfully configured SQL and PHP on CentOS. With proper setup and management, you can create an efficient and secure development environment. Make sure to regularly update your system and software to avoid potential security risks.

We hope this guide helps you easily configure SQL and PHP in the CentOS environment.