Current Location: Home> Latest Articles> Detailed Guide for MySQL and PHP Configuration on FreeBSD | Efficient Web Development Setup

Detailed Guide for MySQL and PHP Configuration on FreeBSD | Efficient Web Development Setup

gitbox 2025-07-01

In modern web development, setting up an efficient and stable development environment is crucial. FreeBSD, as a high-performance Unix-like operating system, combined with MySQL and PHP, offers developers a solid foundation. This article will walk you through how to configure MySQL and PHP in a FreeBSD environment for smooth web development.

Environment Preparation

Before starting, make sure your FreeBSD system is updated to the latest version. You can update your system with the following command:

<span class="fun">pkg update && pkg upgrade</span>

Next, install the necessary packages to prepare for MySQL and PHP installation.

Installing MySQL

Installing MySQL on FreeBSD is straightforward. Use the pkg command to install the latest version of MySQL:

<span class="fun">pkg install mysql80-server</span>

After installation, you need to enable the MySQL service. Run the following command in the terminal:

<span class="fun">sysrc mysql_enable=YES</span>

Start the MySQL service:

<span class="fun">service mysql-server start</span>

Configuring MySQL

Next, run the MySQL initialization script to create the default database, user, and permissions:

<span class="fun">mysql_install_db --user=mysql --basedir=/usr/local/mysql</span>

Once completed, you can log into the MySQL console using the following command:

<span class="fun">mysql -u root -p</span>

Installing PHP

After MySQL is installed and configured, you can proceed with installing PHP. Use the following command to install PHP and related extensions:

<span class="fun">pkg install php80 php80-mysqli php80-mysqlnd php80-gd php80-xml</span>

After installation, enable the PHP service:

<span class="fun">sysrc php_fpm_enable=YES</span>

Start the PHP FPM service:

<span class="fun">service php-fpm start</span>

Configuring PHP and MySQL Connection

To enable PHP to connect successfully with MySQL, you need to make changes in the PHP configuration file (php.ini). Edit the php.ini file using the following command:

<span class="fun">vi /usr/local/etc/php.ini</span>

Ensure the following configuration is correctly set:

<span class="fun">mysqli.default_socket=/tmp/mysql.sock</span>

Testing the Configuration

Finally, create a simple PHP file to test the connection between PHP and MySQL. Create a test.php file in your web directory and write the following code:

$link = mysqli_connect("localhost", "root", "your_password");
if (!$link) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
mysqli_close($link);

Visit the test.php file in your browser. If everything is set up correctly, you should see the message “Connected successfully,” indicating that PHP and MySQL are connected.

Conclusion

By following the steps above, you will successfully configure MySQL and PHP in a FreeBSD environment. This configuration is not only efficient but also stable, providing strong support for your web development. If you encounter any issues during the setup, feel free to reach out for help, and happy coding!