PHP Installation & Setup πŸ› οΈπŸ’»

beginner
17 min

PHP Installation & Setup πŸ› οΈπŸ’»

Welcome to the PHP Installation & Setup lesson! In this comprehensive guide, we'll walk you through the process of setting up PHP on your computer, creating a simple PHP file, and running it. By the end of this tutorial, you'll have a solid foundation for diving deeper into PHP development. 🎯

What is PHP? πŸ’‘

PHP (Hypertext Preprocessor) is a server-side scripting language used primarily for web development. It allows you to create dynamic and interactive websites without needing much knowledge of web design or programming.

System Requirements πŸ“

Before we start, ensure your computer meets the following requirements:

  • Operating System: Windows, macOS, or Linux
  • Web Server: Apache, Nginx, or Microsoft IIS (optional if you're using a development environment)
  • PHP: Version 8.0 or higher

Installation Methods πŸ› οΈ

There are multiple ways to install PHP, but we'll focus on two popular methods:

  1. Using a Web Server (Apache for Windows and macOS, and LAMP stack for Linux)
  2. Using a Development Environment (XAMPP, WAMP, or MAMP)

Method 1: Web Server 🌐

For this method, we'll use the example of installing PHP on a macOS with Apache.

Step 1: Install Homebrew (only for macOS) πŸ”§

Homebrew is a package manager for macOS that simplifies the installation of various software, including PHP.

bash
/bin/bash -c $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)

Step 2: Install PHP πŸ”„

bash
brew install php

Step 3: Enable PHP πŸ”¦

bash
sudo apachectl suexec -A /usr/local/php80 sudo nano /etc/apache2/httpd.conf

Add the following lines inside the <IfModule mod_apache2_dir.c> and <IfModule mod_apache2_file.c> sections:

bash
<Directory "/usr/local/www/"> AllowOverride All Require all granted AddType application/x-httpd-php .php </Directory>

Save and exit the file, then restart Apache:

bash
sudo apachectl restart

Step 4: Create a PHP File πŸ“

Create a new file named info.php inside the /usr/local/www/ directory:

bash
echo "<?php phpinfo(); ?>" > /usr/local/www/info.php

Step 5: Access the PHP File 🌐

Open your web browser and navigate to http://localhost/info.php. You should see the PHP information page, indicating that PHP is correctly installed and configured. βœ…

Method 2: Development Environment (XAMPP) πŸ’»

For this method, we'll use XAMPP as an example of a development environment that includes Apache, PHP, and MySQL.

  1. Download and Install XAMPP from the official website (https://www.apachefriends.org/download.html)
  2. Run the XAMPP installer and follow the on-screen instructions to complete the installation
  3. Start the Apache and MySQL modules from the XAMPP Control Panel
  4. Create a new file named info.php inside the htdocs folder (located within the XAMPP installation directory)
  5. Enter the same PHP code as in the Web Server method (<?php phpinfo(); ?>)
  6. Open your web browser and navigate to http://localhost/info.php. You should see the PHP information page, indicating that PHP is correctly installed and configured. βœ…

Quiz: What is the extension used for PHP files? πŸ“

  • .html
  • .php
  • .js

Correct Answer: B Explanation: PHP files have the .php extension.

By completing this lesson, you now have PHP installed and configured on your computer. In the next lessons, we'll dive deeper into PHP syntax, functions, and real-world examples. Happy coding! πŸŽ‰πŸŽ“