How do I install PHP in my computer ?

Decode Web
6 min readJun 25, 2019

--

Source: https://decodeweb.in/php/how-do-i-install-php-in-my-computer/

My previous post shown you an overview of PHP and its working mechanism. Today I am going to show you how you can install PHP and other assisting softwares in your machine locally. You would be working on your own web server soon. Cheers..!!

How do I install PHP on Windows 10 ?

For Windows machines, there is one tool called as XAMPP which is a complete set of softwares and tools required for web development. XAMPP includes Apache Server, MySQL, PHP and Perl (eerl…we will talk about it later)

X: [cross platform operating systems] meaning it can run on any OS Mac OX , Windows , Linux etc.

Apache: a local server to deploy you development to test applications

MySQL: PHP always comes with MySQL

PHP: You already know about it, right..?

Perl: Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages.

So let’s download XAMPP from our Friends here.

  • Download PHP 7 (its latest version as I am writing this post) and according to your system configuration select 64 bit or 32 bit
  • Installation XAMPP is just like installing any other windows program. There are but obvious, a few things that we must note.
  • After you have downloaded XAMPP, run the setup. The warning message dialog window shown below appears.
  • Then in following screen just click Next> set Installation path which is by default set at c:\xampp
  • Next > Install MySql and Apache services and you are done.

XAMPP creates a directory structure in c:\xampp path similar to this:

  • htdocs: This is the folder where you put all your php, html, image files i.e the source code.
  • Mysql and php : core files of mysql and php respectively, do not mess with it, we will see those directory in another post.

— What is PHP CGI exe?

php-cgi is intended for a webserver. Among other things it handles HTTP headers for you.

How do I install PHP on Linux-Ubuntu 16.04/18.04 ?

To start with PHP installation on ubuntu we have some prerequisites like:

  • Logged in with sudo in system with ubuntu 16.04 or 18.04
  • Apache2 is installed.

How to install Apache ?

Run the below commands one by one:

sudo apt update
sudo apt upgrade
sudo apt install apache2

That’s it, Apache2 is installed and started automatically. You can check the Apache2 service status by using the following command:

sudo systemctl status apache2

● apache2.service — The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019–04–16 00:35:04 UTC; 1h 30min ago
Process: 1376 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
Process: 1381 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 1396 (apache2)
Tasks: 11 (limit: 502)
Memory: 84.1M
CGroup: /system.slice/apache2.service
├─1396 /usr/sbin/apache2 -k start
├─2159 /usr/sbin/apache2 -k start
├─2160 /usr/sbin/apache2 -k start
├─2161 /usr/sbin/apache2 -k start
├─2162 /usr/sbin/apache2 -k start
├─2163 /usr/sbin/apache2 -k start
├─2164 /usr/sbin/apache2 -k start
├─2165 /usr/sbin/apache2 -k start
├─2166 /usr/sbin/apache2 -k start
├─2167 /usr/sbin/apache2 -k start
└─2198 /usr/sbin/apache2 -k start

Apr 16 00:35:04 stocklot.pk systemd[1]: Starting The Apache HTTP Server…
Apr 16 00:35:04 stocklot.pk systemd[1]: Started The Apache HTTP Server.

The other following commands can be used to stop, start and enable Apache2 service.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

So now you have installed Apache server on your ubuntu, now we are installing PHP 7, 7.2 to be exact,

Step 1: Install Ondřej Surý’s PPA

sudo apt-get install software-properties-common

sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update

Step 2: Install PHP 7.2 & PHP Extensions

sudo apt-get install php7.2 libapache2-mod-php7.2 php7.2-common php7.2-curl php7.2-mbstring php7.2-xmlrpc php7.2-mysql php7.2-gd php7.2-xml php7.2-intl php7.2-ldap php7.2-imagick php7.2-json php7.2-cli

Step 3: Check the PHP installation

php -v

You should see this:

PHP 7.2.17–1+ubuntu18.10.1+deb.sury.org+3 (cli) (built: Apr 10 2019 10:51:33) ( NTS )
Copyright © 1997–2018 The PHP Group
Zend Engine v3.2.0, Copyright © 1998–2018 Zend Technologies
with Zend OPcache v7.2.17–1+ubuntu18.10.1+deb.sury.org+3, Copyright © 1999–2018, by Zend Technologies

Step 4: Restart Apache2

After installing PHP and related modules, all you have to do is restart Apache2 to reload PHP configurations.

To restart Apache2, run the commands below

sudo systemctl restart apache2.service

To test PHP 7.2 settings with Apache2, create a phpinfo.php file in Apache2 root directory by running the following command:

sudo nano /var/www/html/phpinfo.php

Then type the content below and save the file.

<?php phpinfo(); ?>

Save the file and browse to your server hostname followed by /phpinfo.php. You should see PHP default test page.

— How to switch between 2 PHP versions in the same machine ?

May be you might have an old PHP version like PHP 5.6 in your system and you installed PHP 7.2 too so thats multiple PHP in your machine. There are some applications which were developed when older PHP 5.6 was latest version, they are still live and you working on those applications, You might be working on Laravel simultaneously but Laravel requires PHP 7+ to get started. Getting the picture ?

In that case you can switch between the PHP versions to suit your requirements.

Switch From PHP 5.6 => PHP 7.2

Apache:-

sudo a2dismod php5.6
sudo a2enmod php7.2
sudo service apache2 restart

Command Line:-

sudo update-alternatives — set php /usr/bin/php7.2
sudo update-alternatives — set phar /usr/bin/phar7.2
sudo update-alternatives — set phar.phar /usr/bin/phar.phar7.2
sudo update-alternatives — set phpize /usr/bin/phpize7.2
sudo update-alternatives — set php-config /usr/bin/php-config7.2

And vice-versa, Switch From PHP 7.2 => PHP 5.6

Apache:-

sudo a2dismod php7.2
sudo a2enmod php5.6
sudo service apache2 restart

Command Line:-

sudo update-alternatives — set php /usr/bin/php5.6
sudo update-alternatives — set phar /usr/bin/phar5.6
sudo update-alternatives — set phar.phar /usr/bin/phar.phar5.6
sudo update-alternatives — set phpize /usr/bin/phpize5.6
sudo update-alternatives — set php-config /usr/bin/php-config5.6

How do I install PHP on Apple-Mac ?

Prerequisites:

Terminal: You must have Mac Terminal access and little knowledge about working with the terminal application. To login to your Mac system and open terminal

Homebrew: Homebrew is a popular package manager for the Mac operating systems. It is useful for installing most open source software like Node.

Open the terminal and run the below command:

curl -s http://php-osx.liip.ch/install.sh | bash -s 7.2

Verify PHP Installation

The PHP versions for macOS are maintained by php-osx and doesn’t overwrite the current php binaries installed on your system. The installs everything in /usr/local/php5. The new php binary is therefore in /usr/local/php5/bin/php.

export PATH=/usr/local/php5/bin:$PATH

Rest is the same process as mentioned for Ubuntu. :)

Do I need to install other softwares too after PHP installation ?

Yeah, there are. Where would you write your code ? You definitely need a good editor for it.

Below are my recommended editor you can choose from:

  • PHPStorm (My favourite)
  • Visual Studio Code / VS Code / VS (whatever it is.)
  • Atom
  • Notepad++.

So guys, I hope I have given you a complete tutorial on how to install PHP on your system. Somethin’ not working ? Getting error ? Do comment below, I will try to resolve your queries ASAP.

--

--