PHP Database MySQL config/install
Posted on March 25, 2024 (Last modified on October 11, 2024) • 2 min read • 384 wordsVideo is in Swedish
In this article, we will guide you through the process of configuring and installing PHP with MySQL on your server or local machine.
Before starting, make sure you have:
If you haven’t already, download the latest version of PHP from the official website and follow the installation instructions for your operating system.
Once installed, open the php.ini
file in a text editor (usually located at /etc/php.ini
or C:\Windows\php.ini
) and make the following changes:
extension_dir
to the directory where your PHP extensions are located (e.g., /usr/lib/php/extensions/
).mysql.default_socket
to the path of your MySQL socket file (e.g., /var/run/mysqld/mysqld.sock
).If you haven’t already, download the latest version of MySQL from the official website and follow the installation instructions for your operating system.
Once installed, open the my.cnf
file in a text editor (usually located at /etc/mysql/my.cnf
) and make the following changes:
bind-address
to the IP address you want to use for MySQL connections.port
to the port number you want to use for MySQL connections (default is 3306).Restart your web server and MySQL service to apply the changes:
sudo service apache2 restart
sudo service nginx restart
sudo service mysql restart
Create a new file called info.php
in your web root directory (e.g., /var/www/html/
) with the following content:
<?php
phpinfo();
?>
Open this file in your web browser to verify that PHP is installed and configured correctly.
Next, create a new file called test.php
with the following content:
<?php
$conn = mysqli_connect('localhost', 'username', 'password', 'database');
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Replace 'username'
, 'password'
, and 'database'
with your actual MySQL credentials and database name. Open this file in your web browser to verify that you can connect to your MySQL database using PHP.
That’s it! You have now configured and installed PHP with MySQL on your server or local machine.
Swedish