To start working with PHP, you need to install it on your system. PHP can be installed in several ways:

  1. Windows:

    • Download the PHP binaries from the official PHP website.
    • Extract the files to a directory (e.g., C:\php).
    • Add the directory to the system’s PATH environment variable.
  2. macOS:

    • Use Homebrew to install PHP:
      brew install php
      
    • Alternatively, download the PHP package from the official PHP website.
  3. Linux:

    • Most Linux distributions include PHP in their package manager. For example, on Ubuntu:
      sudo apt-get update
    sudo apt-get install php
      

Setting Up a Local Development Environment

Using a local development environment like XAMPP, WAMP, or MAMP can simplify the process of setting up PHP, Apache, and MySQL.

  1. XAMPP (Cross-Platform):

    • Download XAMPP from the official website.
    • Install it by following the on-screen instructions.
    • Start the Apache and MySQL services through the XAMPP control panel.
  2. WAMP (Windows):

    • Download WAMP from the official website.
    • Install WAMP and start the services using the WAMP control panel.
  3. MAMP (macOS):

    • Download MAMP from the official website.
    • Install it and start the Apache and MySQL servers using the MAMP control panel.

Using PHP with Apache/Nginx

  1. Apache:

    • PHP is often used with Apache, and if you installed XAMPP, WAMP, or MAMP, Apache is already configured.
    • To use PHP with Apache, ensure the following lines are in your Apache configuration file (httpd.conf):
      LoadModule php_module modules/libphp.so
    AddHandler application/x-httpd-php .php
      
    • Restart Apache to apply changes.
  2. Nginx:

    • To use PHP with Nginx, you need to configure Nginx to pass PHP requests to PHP-FPM.
    • Edit your Nginx configuration file (usually located in /etc/nginx/sites-available/default):
      server {
        listen 80;
        server_name yourdomain.com;
    
        root /var/www/html;
        index index.php index.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
    }
      
    • Restart Nginx to apply the configuration.

Configuring PHP (php.ini)

The php.ini file is the configuration file for PHP. It allows you to modify PHP settings such as error reporting, memory limits, and upload sizes.

  • Locating php.ini:

    • On Windows, it’s typically found in the PHP installation directory.
    • On Linux/macOS, it might be in /etc/php/7.x/apache2/php.ini or /etc/php.ini.
  • Common Configurations:

    • Error Reporting:
      error_reporting = E_ALL
    display_errors = On
      
    • Memory Limit:
      memory_limit = 128M
      
    • Upload Max File Size:
      upload_max_filesize = 20M
    post_max_size = 20M
      

After editing php.ini, restart your web server (Apache/Nginx) to apply the changes.