To open an interactive php shell, just type in a terminal:

php -a

As for opening a file, just:

php filename.php
Answer from animaletdesequia on askubuntu.com
🌐
Allaboutlinux
allaboutlinux.eu › how-to-run-php-on-ubuntu
How to run PHP on Ubuntu | allaboutlinux.eu
How to run PHP on Ubuntu. First of all you will need a web-server to host our PHP pages. In this tutorial I ll use Apache2. You can install Apache2 from the Terminal by using: ... when the installation is finished, Apache is up and running and you can test that by pointing your web browser at http://localhost/ The default page will come up with a message “it works” and some more info.This html file is located under your local file system in folder /var/www/html/
🌐
Ubuntu
manpages.ubuntu.com › manpages › trusty › man1 › php5.1.html
Ubuntu Manpage: php - PHP Command Line Interface 'CLI'
By using the -S option where addr:port point to a local address and port PHP will listen to HTTP requests on that address and port and serve files from the current working directory or the docroot passed by the -t option. If none of -r -f -B -R -F -E or -S is present but a single parameter ...
Top answer
1 of 3
192

You should check your server configuration files. Look for lines that start with LoadModule php... There probably are configuration files/directories named mods or something like that. Start from there.

You could also check output from php -r 'phpinfo();' | grep php and compare lines to phpinfo(); from web server.

To run php interactively:

(So you can paste/write code in the console.)

php -a

To make it parse a file and output to the console:

php -f file.php

Parse a file and output to another file:

php -f file.php > results.html

Do you need something else?

To run only a small part, one line or like, you can use:

php -r '$x = "Hello World"; echo "$x\n";'

If you are running Linux then do man php at the console.

If you need/want to run PHP through fpm (FastCGI Process Manager), use cli fcgi:

SCRIPT_NAME="file.php" SCRIPT_FILENAME="file.php" REQUEST_METHOD="GET" cgi-fcgi -bind -connect "/var/run/php-fpm/php-fpm.sock"

Where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.

2 of 3
3

On SUSE Linux, there are two different configuration files for PHP: one for Apache, and one for CLI (command line interface). In the /etc/php5/ directory, you will find an "apache2" directory and a "cli" directory. Each has a "php.ini" file. The files are for the same purpose (PHP configuration), but apply to the two different ways of running PHP. These files, among other things, load the modules PHP uses.

If your OS is similar, then these two files are probably not the same. Your Apache php.ini is probably loading the German module, while the the CLI php.ini isn't. When the module was installed (auto or manual), it probably only updated the Apache php.ini file.

You could simply copy the Apache php.ini file over into the cli directory to make the CLI environment exactly like the Apache environment.

Or, you could find the line that loads the German module in the Apache file and copy/paste just it to the CLI file.

🌐
TecMint
tecmint.com › home › php › how to use and execute php codes in linux command line – part 1
How to Use and Execute PHP Codes in Linux Command Line - Part 1
July 13, 2015 - 4. You can run a PHP script simply as, if it is a shell script. First Create a PHP sample script in your current working directory. # echo -e '#!/usr/bin/php\n<?php phpinfo(); ?>' > phpscript.php · Notice we used #!/usr/bin/php in the first line of this PHP script as we use to do in shell script (/bin/bash). The first line #!/usr/bin/php tells the Linux Command-Line to parse this script file to PHP Interpreter.
Find elsewhere
🌐
PHP
php.net › manual › en › features.commandline.usage.php
PHP: Usage - Manual
To run the above script on Unix, it must be made executable, and called simply as script.php echothis or script.php -h. On Windows, a batch file similar to the following can be created for this task:
🌐
Ubuntu
ubuntu.com › server › docs › how-to › web-services › install-php
How to install and configure PHP - Ubuntu Server documentation
December 11, 2025 - If you have installed the php-cli package, you can run PHP scripts at a terminal prompt. By default, when libapache2-mod-php is installed, the Apache2 web server is configured to run PHP scripts using this module.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-run-a-php-application-on-ubuntu-localhost
How to Run a PHP Application on Ubuntu Localhost? - GeeksforGeeks
July 23, 2025 - Step 3: Now, we can execute the PHP program on ubuntu, We need to replace the default apache page with our PHP page. To do that run the below command. sudo mv /var/www/html/index.html /var/www/html/index.back · Step 4: Now write your PHP Script to execute using the below command. ... After entering the above command the notepad will be opened with the file name index.php, Write your script in that file.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-execute-php-code-using-command-line
How to Execute PHP Code using Command Line? - GeeksforGeeks
... To run PHP code interactively, use the -a option. ... If you need to run a PHP script in the background (e.g., for long-running processes), append & to the command: ... This will run the script in the background and free up the terminal.
Published   July 12, 2025
🌐
Sololearn
sololearn.com › en › Discuss › 120711 › how-to-run-php-in-linux-ubuntu
How to run php in linux ubuntu? | Sololearn: Learn to code for FREE!
Use xampp. it's free, and easy to install in Ubuntu. after installation, type this command in terminal: sudo /opt/lampp/lampp start now you can start appache and MySQL using GUI. you have to save your PHP files inside: /opt/lampp/htdocs then ...
Top answer
1 of 2
83

To run PHP commands immediately on Terminal you can pass -a option to your installed PHP:

php -a

Details:

php -a opens an interactive shell which let you type directly PHP commands and view the result on your terminal, As an example, after typing php -a in terminal you can type echo 'Hello World'; and after Press Enter Hello World! will be printed on the screen.

Windows Solution

On windows there is no interactive mode same as Linux but still you can use interactive like mode!, So, open PHP on place you installed it for example if you use XAMPP then your PHP should be is on C:\xampp\php (or add the binary directory to environment variables) and then type php -a, At the end of each line you can view the results by pressing Ctrl+Z and then Enter.

php -a
echo 'hello world!';
^Z
2 of 2
52

Escape the inside double quotes (") that you are using to delimit your string.

php -r "Print \"Hello, World!\";"

Alternatively, use single quotes (') for the PHP string or for the quoting of the PHP code.

If you run php --help you can see a list of commands that the php program accepts.

  -a               Run as interactive shell
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse and execute <file>.
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -r <code>        Run PHP <code> without using script tags <?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
  -R <code>        Run PHP <code> for every input line
  -F <file>        Parse and execute <file> for every input line
  -E <end_code>    Run PHP <end_code> after processing all input lines
  -H               Hide any passed arguments from external tools.
  -S <addr>:<port> Run with built-in web server.
  -t <docroot>     Specify document root <docroot> for built-in web server.
  -s               Output HTML syntax highlighted source.
  -v               Version number
  -w               Output source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.

  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin

  --ini            Show configuration file names

  --rf <name>      Show information about function <name>.
  --rc <name>      Show information about class <name>.
  --re <name>      Show information about extension <name>.
  --rz <name>      Show information about Zend extension <name>.
  --ri <name>      Show configuration for extension <name>.
🌐
Ubuntu
ubuntu.com › server › docs › how-to-install-and-configure-php
How to install and configure PHP | Ubuntu
If you have installed the php-cli package, you can run PHP scripts at a terminal prompt. By default, when libapache2-mod-php is installed, the Apache2 web server is configured to run PHP scripts using this module. First, verify if the files /etc/apache2/mods-enabled/php8.*.conf and ...
🌐
Educative
educative.io › answers › how-to-execute-php-scripts-on-the-command-line
How to execute PHP scripts on the command line
To check if it is installed and ... on your terminal or cmd: ... If you don’t have PHP installed, you can install it as shown below. Download it from the official site. Extract the file and store it in a folder, preferably named php, in your C just like so: c:/php · Next, place this folder in your environment variable path so that it can be recognized by the command line. ... You can change the php5 to your version of choice. You can run the command ...
🌐
IONOS
ionos.com › help › hosting › using-php-for-web-projects › executing-php-files-via-the-command-line-php-cli
Executing PHP files via the command line (PHP CLI) - IONOS Help
Execute the command cd in the terminal ... execute the command. ... To execute a PHP file, enter the path to the PHP CLI program followed by the name of the PHP file in the terminal....
🌐
ServerHub
serverhub.com › home › learn how to install, configure, & run php on ubuntu server
Learn How to Install, Configure, & Run PHP on Ubuntu Server
November 1, 2023 - Before doing the PHP install on Ubuntu task, you need to update package lists on your server. Open a terminal and enter the following command: ... To modify the PHP settings, you can edit the php.ini file located in the /etc/php/[PHP_VERSION}apache2/directory.