Installing PHP on Windows and Ubuntu

Guide to Installing PHP on Windows and Ubuntu: From Zero to "Hello! It Works!"

Welcome to a PHP installation guide that's anything but ordinary! Whether you're a curious beginner dipping your toes into programming or a seasoned developer setting up a new environment, this tutorial is designed to make the process smooth, engaging, and even a little fun. We'll walk you through installing PHP on both Windows and Ubuntu, offering unique approaches like using the Windows Subsystem for Linux (WSL) for a seamless cross-platform experience. By the end of this guide, you'll not only have PHP up and running but also write your first script to proudly display "Hello! It works!" - a small yet satisfying milestone in your coding journey.

Step 1: Installing PHP on Windows

1.1 The Unconventional Way: Using the Windows Subsystem for Linux (WSL)

If you're on Windows, why not embrace the power of Linux without leaving your OS? WSL allows you to run Ubuntu (or other Linux distributions) directly on Windows. Here's how to set it up:

  • Enable WSL:
    • Open PowerShell as Administrator and run:
      wsl --install
    • This installs WSL and Ubuntu by default. Restart your computer if prompted.
  • Install PHP in Ubuntu on WSL:
    • Open Ubuntu from your Start menu.
    • Update your package list:
      sudo apt update
    • Install PHP:
      sudo apt install php
    • Verify the installation:
      php -v

1.2 The Classic Way: Installing PHP Directly on Windows

If you prefer to stick to native Windows, follow these steps:

  • Download PHP:
  • Extract the Files:
    • Extract the ZIP file to a directory, e.g., C:\php.
  • Add PHP to Your System Path:
    • Open the Start menu, search for "Environment Variables," and click "Edit the system environment variables."
    • Under the "Advanced" tab, click "Environment Variables."
    • In the "System variables" section, find the Path variable, click "Edit," and add the path to your PHP directory (e.g., C:\php).
  • Verify the Installation:
    • Open Command Prompt and type:
      php -v
    • You should see the PHP version displayed.

Step 2: Installing PHP on Ubuntu

2.1 The Quick and Easy Way

Ubuntu makes installing PHP a breeze. Here's how:

  • Update Your Package List:
    Open your terminal and run:
    sudo apt update
  • Install PHP:
    Run the following command to install PHP:
    sudo apt install php
  • Verify the Installation:
    Check the installed PHP version:
    php -v

2.2 The Alternative Way: Using a PPA for the Latest Version

If you want the latest PHP version, you can add a Personal Package Archive (PPA):

  • Add the PPA:
    sudo add-apt-repository ppa:ondrej/php
    sudo apt update
  • Install PHP:
    sudo apt install php8.x
    Replace 8.x with the desired version number.
  • Verify the Installation:
    php -v

Step 3: Writing and Running Your First PHP Script

Now that PHP is installed, let's create a simple script to print"Hello! It works!" and celebrate your success.

3.1 On Windows (Native or WSL)

  • Open a text editor (e.g., VisualStudio Code).
  • Write the following code:
    <?php
    echo "Hello! It works!";
    ?>
  • Save the file as hello.php in your PHP directory (e.g., C:\php\hello.php).
  • Run the Script:
    • Open Command Prompt (or Ubuntu terminal if using WSL).
    • Navigate to the directory where hello.php is saved.
    • Run the script:
      php hello.php
    • You should see:
      Hello! It works!

3.2 On Ubuntu

  • Open a terminal and create a new file:
    nano hello.php
  • Write the following code:
    <?php
    echo "Hello! It works!";
    ?>
  • Save and exit (Ctrl + X, then Y to confirm).
  • Run the Script:
    • Open Command Prompt (or Ubuntu terminal if using WSL).
    • Navigate to the directory where hello.php is saved.
    • Run the script:
      php hello.php
    • You should see:
      Hello! It works!

Bonus: Running PHP with a Web Server

If you want to see your script in a browser, you can set up a local web server:

  • On Ubuntu:
    Install Apache and PHP:
    sudo apt install apache2 libapache2-mod-php
    Move hello.php to /var/www/html/ and access it via http://localhost/hello.php.
  • On Windows:
    Use XAMPP or WAMP to set up a local server. Place hello.php in the htdocs directory and access it via http://localhost/hello.php.

That's it! You've not only installed PHP but also created your first script. Now go forth and build something amazing!

Related articles