How to execute PHP code using the command line?

PHP
By naman
How to execute PHP code using the command line?

Although initially released in 1997, PHP wasn't popular until 2005, when it gained hype as a reliable web development language. Today, it is one of the most used programming languages for website development. It is easy to comprehend and reliable. 

PHP stands for Hypertext Pre-processors, a server-side scripting language that you can use for static or dynamic web page development. It works on UNIX or Apache servers (the servers with PHP installed).

This blog will walk you through the step-by-step process of installing PHP code on your system. Continue reading to learn how to install PHP on Windows, Linux, and Mac using the command line. 

 

How do you install PHP code on the Windows operating system using a command line?

Here is a step-by-step guide on how to install PHP on the Windows operating system using a command line:

  1. Go to the official PHP site. Start by downloading PHP files from the official site. You must download the .zip file according to your system's architecture (x86 or x64).
  2. Once you download the .zip file to your preferred location, you should choose C Drive, a boot drive inside a php folder (i.e. C:\php).
  3. When you are done with steps 1 & 2, you should add the folder (C:\php) to the Environment Variable Path. It makes the file accessible from the command line.

To add the folder (C:\php) to the Environment Variable Path: Right-click on My Computer or This PC icon -> Choose Properties from the context menu-> click the Advanced system settings link -> click the Environment Variables.

In System Variables, you have to check for the PATH environment variable. Once you find it, select and Edit it. However, if the PATH environment variable doesn't exist in the system, you click on the New.

  1. Once you reach the edit system variable or new system variable window, you should specify the value of the PATH environment variable. Here, we have set it to C:\php. You should put the location of extracted php files.
  2. Now, click OK and close all remaining windows.

 

How do you install PHP code on a Linux system using a command line?

If you are a Linux user user, you can install PHP on the Linux system using the following command:

1. #apt install php-common libapache2-mod-php php-cli

It will automatically install the PHP module for Apache 2 and its dependencies. Here, all related packages may be desired, for example, libapache-mod-php to integrate with Apache 2 and php-pear for PEAR. Apache should be restarted for the changes to take place.

2. You should stop and start Apache once the PHP file is installed (restart) via the following command:

# /etc/init.d/apache2 stop

# /etc/init.d/apache2 start

Here, PHP is installed with only core modules. For better configuration, additional modules will be desired, such as MySQL, cURL, GD, etc., which you can install via the apt command.

3. For listing additional PHP packages, you can use the following commands.

# apt-cache search php

# apt search php|grep -i mysql

# aptitude search php

It will show packages like php-cgi, php-cli, php-dev, etc., and you can install the ones you need. Since Debian performs dependency checks, it'll prompt you to install MySQL and cURL.

4. To install PHP with MySQL, cURL, you should use the following command:

# apt install php-mysql php-curl

APT will automatically add the appropriate lines to the different php.ini related files (like /etc/php/7.4/php.ini, /etc/php/7.4/conf.d/*.ini, etc.). And depending on the extension, it will add entries similar to extension=foo.so. However, to reflect the changes, you should restart the web server (like Apache) so that these changes take effect.

Key tip: The above method will install PHP with an apache server; however, make sure that If an extension was seemingly installed yet the functions are undefined, recheck that the appropriate ini file is being loaded. Also, make sure that you restart the web server after installation.

How do you install PHP’s latest version in Linux using a command line?

Sometimes, your OS (operating system) might not support the latest version of PHP. In that case, you can use the ppa ondrej. Start by running the following command in the terminal:

1. # sudo apt-get update

It will allow your system to have the latest version of PHP that you want to install. 

2. Next, you should install software-properties-common. The command will allow management for additional software sources.

#sudo apt -y install software-properties-common

3. Now, install the repository ppa:ondrej/php, update apt-get again, and you are good to install PHP 7.4.

#sudo add-apt-repository ppa:ondrej/php

#sudo apt-get update

#sudo apt -y install php7.4

If you want to install additional modules, replace PACKAGE_NAME with the package you wish to install, for example:

Change # sudo apt-get install php7.4-PACKAGE_NAME to #sudo apt-get install -y php7.4-cli for command interpreter. You can also change it to php7.4-json to work with JSON data, and so on.

 

How do you install PHP code on a MAC system using a command line?

For all MAC users, you can install PHP on the MAC system with the following command:

1. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3

2. After installation of PHP on the MAC system, follow the following steps to run PHP using the standard line:

Open command prompt (Command line window)

Go to the directory location where you have stored PHP files.

Enter the following command-> php file_name.php

3. Now, restart the server for testing with the command -> php -S localhost: port -t your_folder/

Ensure that the PHP file's name inside the root folder is index.php. Also, you should hyperlink all other PHP pages through the main index page.

 

Summary

Once the installation is complete on your machine, you verify the successful installation of the language by entering php -v in your command line interface. The command will display the PHP version, build date, and copyright information.

Congratulations! You have successfully installed PHP! You can now start developing unique websites powered by a versatile programming language.

Share this blog:

Profile picture for user naman
naman
Naman Saxena, an SEO executive, excels in optimizing digital presence and driving online growth, helping businesses enhance visibility and achieve marketing goals.