Current Location: Home> Latest Articles> Complete Guide to Configuring PHP Remote Connection on CentOS Server

Complete Guide to Configuring PHP Remote Connection on CentOS Server

gitbox 2025-08-08

Essential Preparations for Configuring PHP Remote Connection on CentOS

Proper configuration of PHP remote connection is a crucial step to ensure stable application operation, especially when deploying on a CentOS server. This article will guide you through the complete configuration process step by step.

Verify PHP and MySQL Installation

Before starting, make sure PHP and MySQL are installed on your server. Check their versions with the following commands:

php -v
mysql -V

Install Missing Packages

If not installed, use the command below to install PHP and its MySQL extension quickly:

<span class="fun">sudo yum install php php-mysql</span>

Enable MySQL Remote Access

By default, MySQL only allows local connections. You need to modify the configuration to allow remote access.

Edit MySQL Configuration File

Open the MySQL configuration file, usually located at /etc/my.cnf. Under the [mysqld] section, comment out or remove the bind-address setting to remove binding restrictions:

<span class="fun"># bind-address = 127.0.0.1</span>

Create a Remote Access User

Log into MySQL CLI and create a user that can access the database remotely:

<span class="fun">CREATE USER 'remote_user'@'%' IDENTIFIED BY 'password';</span>

Grant this user privileges on the specific database:

<span class="fun">GRANT ALL PRIVILEGES ON database_name.* TO 'remote_user'@'%';</span>

Flush privileges to apply changes:

<span class="fun">FLUSH PRIVILEGES;</span>

Configure Firewall to Allow Remote Connections

Ensure the firewall allows traffic through MySQL’s default port 3306 by running:

sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload

Test PHP Remote Connection

Use the following PHP script to test if the remote database connection works:

<?php
$servername = "your_remote_host";
$username = "remote_user";
$password = "password";
$database = "database_name";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connection successful!";
?>

Upload this file to your server and access it. If you see “Connection successful!”, your remote connection setup is complete.

Conclusion

By following these steps, you have successfully configured PHP to remotely connect to MySQL on your CentOS server. This setup facilitates remote database management and improves system flexibility and scalability. Hopefully, this guide helps you achieve efficient remote database connections with ease.