How to setup a webserver on local system for PHP


Setting up a web server on your local system to run PHP involves choosing and configuring software like Apache, Nginx, or using a pre-packaged solution like XAMPP or WAMP. Here’s how you can do it:

Option 1: Using XAMPP (Easiest Method)

XAMPP is an easy-to-install package that includes Apache, PHP, and MySQL. It’s perfect for quickly setting up a local development environment.

1. Download XAMPP

  • Go to the XAMPP website.
  • Download the version of XAMPP that includes the PHP version you want to use.

2. Install XAMPP

  • Run the installer and follow the on-screen instructions.
  • Choose the components you need (typically, Apache, MySQL, and PHP are pre-selected).
  • Install XAMPP in the default directory (e.g., C:\xampp).

3. Start Apache

  • Open the XAMPP Control Panel.
  • Click the "Start" button next to Apache. This will start the Apache web server.

4. Test the Installation

  • Open a web browser and go to http://localhost.
  • You should see the XAMPP welcome page, confirming that the server is running.

5. Run PHP Scripts

  • Place your PHP files in the htdocs folder inside the XAMPP installation directory (e.g., C:\xampp\htdocs).
  • Access your PHP script by going to http://localhost/your-script.php in your browser.

Option 2: Manually Setting Up Apache with PHP

If you prefer to set up Apache manually, follow these steps:

1. Download Apache

  • Download the Apache binaries for Windows from Apache Lounge.
  • Extract the contents to a directory, e.g., C:\Apache24.

2. Download PHP

  • Download the PHP zip package from the PHP Downloads page.
  • Extract it to a directory, e.g., C:\php.

3. Configure Apache to Use PHP

  • Open the Apache configuration file (httpd.conf) located in the conf folder inside the Apache installation directory (C:\Apache24\conf\httpd.conf).

  • Add the following lines to the httpd.conf file to integrate PHP with Apache:

    LoadModule php_module "C:/php/php8apache2_4.dll" AddHandler application/x-httpd-php .php PHPIniDir "C:/php"

    Ensure the paths match where you extracted PHP.

  • Save and close the httpd.conf file.

4. Start Apache

  • Open Command Prompt as Administrator.
  • Navigate to the Apache bin directory:
    cd C:\Apache24\bin
  • Start Apache by typing:
    httpd.exe

5. Test PHP Integration

  • Create a PHP file named test.php in the htdocs directory (C:\Apache24\htdocs):
    <?php phpinfo(); ?>
  • Open your browser and navigate to http://localhost/test.php.
  • If PHP is correctly configured, you will see the PHP information page.

Option 3: Using Nginx with PHP

If you prefer using Nginx, follow these steps:

1. Download Nginx

2. Download PHP

  • Download the PHP zip package from the PHP Downloads page.
  • Extract it to a directory, e.g., C:\php.

3. Configure Nginx

  • Open the Nginx configuration file (nginx.conf) located in the conf directory inside the Nginx installation directory (C:\nginx\conf\nginx.conf).

  • Add or modify the server block to handle PHP files:

    server { listen 80; server_name localhost; location / { root html; index index.php index.html index.htm; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
  • Ensure the paths are correct and save the nginx.conf file.

4. Start PHP-FPM

  • Open Command Prompt and navigate to the PHP directory (C:\php).
  • Start PHP-FPM by typing:
    php-cgi.exe -b 127.0.0.1:9000

5. Start Nginx

  • Open Command Prompt and navigate to the Nginx directory (C:\nginx).
  • Start Nginx by typing:
    start nginx

6. Test PHP Integration

  • Create a test.php file in the Nginx html directory (C:\nginx\html):
    <?php phpinfo(); ?>
  • Open your browser and navigate to http://localhost/test.php.
  • You should see the PHP information page.