ASPCode.net logo
  • Home 
  • Blogs 
  • Tags 
Blog
  1. Home
  2. Articles
  3. PHP Database MySQL config/install

PHP Database MySQL config/install

Posted on March 25, 2024  (Last modified on October 11, 2024) • 2 min read • 384 words
Php
 
Php
 
Share via
ASPCode.net
Link copied to clipboard

Video is in Swedish

On this page
  • Configuring and Installing PHP with MySQL
  • Prerequisites
  • Step 1: Install PHP
  • Step 2: Configure PHP
  • Step 3: Install MySQL
  • Step 4: Configure MySQL
  • Step 5: Restart Services
  • Step 6: Test PHP and MySQL Connection
  • Video
  • Sourcecode

Configuring and Installing PHP with MySQL  

In this article, we will guide you through the process of configuring and installing PHP with MySQL on your server or local machine.

Prerequisites  

Before starting, make sure you have:

  1. A web server (e.g., Apache or Nginx)
  2. PHP installed
  3. MySQL installed

Step 1: Install PHP  

If you haven’t already, download the latest version of PHP from the official website and follow the installation instructions for your operating system.

Step 2: Configure PHP  

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:

  1. Set extension_dir to the directory where your PHP extensions are located (e.g., /usr/lib/php/extensions/).
  2. Set mysql.default_socket to the path of your MySQL socket file (e.g., /var/run/mysqld/mysqld.sock).
  3. Save and close the file.

Step 3: Install MySQL  

If you haven’t already, download the latest version of MySQL from the official website and follow the installation instructions for your operating system.

Step 4: Configure MySQL  

Once installed, open the my.cnf file in a text editor (usually located at /etc/mysql/my.cnf) and make the following changes:

  1. Set bind-address to the IP address you want to use for MySQL connections.
  2. Set port to the port number you want to use for MySQL connections (default is 3306).
  3. Save and close the file.

Step 5: Restart Services  

Restart your web server and MySQL service to apply the changes:

  • For Apache: sudo service apache2 restart
  • For Nginx: sudo service nginx restart
  • For MySQL: sudo service mysql restart

Step 6: Test PHP and MySQL Connection  

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.

Video  

Swedish

Sourcecode  

Sourcecode
 
 Using Dotenv in PHP
PHP Databasedriven applications 
On this page:
  • Configuring and Installing PHP with MySQL
  • Prerequisites
  • Step 1: Install PHP
  • Step 2: Configure PHP
  • Step 3: Install MySQL
  • Step 4: Configure MySQL
  • Step 5: Restart Services
  • Step 6: Test PHP and MySQL Connection
  • Video
  • Sourcecode
Follow me

I code in Java, C#, Golang, SQL and more

     
© 2024 Systementor AB
ASPCode.net
Code copied to clipboard