There are two options.

  1. Access the php file through a local webserver(ie thru a local website). The web-server will deal with the requested php file. It will use either,

    • Inbuilt PHP module to interpret the php file, or
    • PHP through CGI (eg.CGI, FastCGI)

      If your apache(check if apache is running using service apache2 status!!) is set to the default configuration, this could be as simple as

      http://localhost/path/to/your.php
      

      Remember by default, the base directory for apache is /var/www/html/, so you need not include this in the url.

  2. Use the php binary directly from a terminal.

       php /path/to/your/file.php
    
Answer from sjsam on Stack Overflow
๐ŸŒ
Ubuntu
ubuntu.com โ€บ server โ€บ docs โ€บ how-to โ€บ web-services โ€บ install-php
How to install and configure PHP - Ubuntu Server documentation
December 11, 2025 - To install PHP โ€“ and the Apache ... installed if you need them for your setup. PHP-CLI You can run PHP scripts via the Command Line Interface (CLI)....
๐ŸŒ
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 1: First, We need to install PHP Package in ubuntu using Sudo apt-get install <Package-Name>, Enter the below command to install the PHP package in ubuntu. ... Step 2: Make sure the PHP package is installed successfully using the below command.
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-install-php-on-ubuntu
How to Install PHP on Ubuntu? - GeeksforGeeks
July 23, 2025 - Basically in the system, Eight ... to an Ubuntu user account that also has sudo privileges/rights. Full Access to the terminal window (Ctrl+Alt+T). Ubuntu is known as one ......
๐ŸŒ
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 - PHP (Hypertext Preprocessor) is a server-side scripting language that was created by programmer Rasmus Lerdorf in 1993 and released in 1995. In this article, weโ€™ll discuss an overview of PHP, why you need it for your Ubuntu server, and the steps on how to install, configure and test the configuration of PHP on an Ubuntu server.
Find elsewhere
๐ŸŒ
Contabo
contabo.com โ€บ home โ€บ how to install php on ubuntu vps โ€“ a comprehensive guide
How to Install PHP on Ubuntu VPS โ€“ a Comprehensive Guide | Contabo Blog
September 20, 2025 - Your server is now ready to serve PHP content through Apache. Many PHP applications require extra functionality beyond the core installation. This is where PHP extensions come in โ€” they add features such as database drivers, image processing, or encryption support. ... After installing, restart your web server or PHP-FPM so the changes take effect. You can check which extensions are active by running: ... With the right extensions in place, PHP on your Ubuntu VPS can run a wider range of applications without compatibility issues.
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ sysadmin โ€บ how to install php on ubuntu
How To Install PHP On Ubuntu 20.04 or 22.04 | phoenixNAP KB
February 13, 2025 - If you are running an Apache web server, install PHP with the Apache module. Take the following steps: 1. Update the repositories to ensure you get the latest software version: ... 2. Install software-properties-common to help you manage distributions and independent software sources: ... 3. Add the ondrej/php Personal Package Archive (PPA), which provides multiple PHP versions for Ubuntu, including versions not available in the official Ubuntu repositories.
๐ŸŒ
Medium
medium.com โ€บ @arunpragashap โ€บ install-apache-server-in-ubuntu-and-run-a-php-file-7e5630376e96
Install Apache Server in Ubuntu and Run a PHP file. | by Arunpragash | Medium
July 29, 2023 - For execute a php we need to install a php in our ubuntu so run this command ... It will install the PHP for executing the php file. If you are trying to connect the mysql in PHP then you need to install mysql cli and php8.1-mysql php package.
๐ŸŒ
Serverspace
serverspace.io โ€บ support โ€บ help โ€บ installing-and-configuring-php-on-ubuntu
Installing and Configuring PHP on Ubuntu - Serverspace.io
July 27, 2025 - Discover how to install and configure PHP on Ubuntu for dynamic web development! Step-by-step guide covers setup, Apache integration, key settings, and troubleshooting.
Top answer
1 of 1
14

This answer provides details on LAMP and PHP Install. This answer provides the details on "how to run php" on your web server.

In this answer you'll get the steps on how to install MySQL. But the details on how to connect php into mysql are too long and probably off topic right here. So I suggest you to first meet the requirements to properly run LAMP.

Additionally you may wish to have a look at this post on howtoforge, of which I place here the relevant things for your convenience:

Installing LAMP On Ubuntu For Newbies

In this guide I will show you how to install a LAMP system. LAMP stands for Linux, Apache, MySQL, PHP. The guide is intended to help those who have very little knowlegde of using Linux.

Install Apache

To start off we will install Apache.

  1. Open up the Terminal (Applications > Accessories > Terminal). (Ctrl+T also works)
  2. Copy/Paste the following line of code into Terminal and then press enter:

    sudo apt-get install apache2

  3. The Terminal will then ask you for you're password, type it and then press enter.

Testing Apache

To make sure everything installed correctly we will now test Apache to ensure it is working properly.

  1. Open up any web browser and then enter the following into the web address:

http://localhost/

You should see a folder entitled apache2-default/. Open it and you will see a message saying "It works!" , congrats to you!

Install PHP

In this part we will install PHP 5.

Step 1. Again open up the Terminal (Applications > Accessories > Terminal). Step 2. Copy/Paste the following line into Terminal and press enter:

sudo apt-get install php5 libapache2-mod-php5

Step 3. In order for PHP to work and be compatible with Apache we must restart it. Type the following code in Terminal to do this:

sudo /etc/init.d/apache2 restart

Test PHP -- To ensure there are no issues with PHP let's give it a quick test run.

Step 1. In the terminal copy/paste the following line: updated

sudo gedit /var/www/html/testphp.php

This will open up a file called phptest.php.

Step 2. Copy/Paste this line into the phptest file:

<?php phpinfo(); ?>

Step 3. Save and close the file.

Step 4. Now open you're web browser and type the following into the web address:

http://localhost/testphp.php

The page should look like this:

Good luck!

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-install-php-7-4-and-set-up-a-local-development-environment-on-ubuntu-20-04
How To Install PHP 7.4 and Set Up a Local Development Environment on Ubuntu 20.04 | DigitalOcean
2 weeks ago - Since Composer is something used across projects, itโ€™s recommended that you continue to the next portion and set Composer to run globally. You can place the Composer PHAR anywhere you wish. If you put it in a directory that is part of your $PATH, you can access it globally. You can even make it executable on Ubuntu (and other Unix systems) and invoke it without directly using the PHP interpreter.
๐ŸŒ
DEV Community
dev.to โ€บ alvinqingxing โ€บ testing-and-deploying-a-php-app-in-ubuntu-2i5h
Testing and Deploying a PHP App in Ubuntu - DEV Community
September 22, 2020 - A better solution will be to activate Apache's User Directory feature, which will allow you to run your PHP code from a new public_html directory in your home folder. The following command will activate the User Directory feature: ... Next, you will need create the User Directory (~/public_html) and set the correct directory permissions: mkdir $HOME/public_html chmod +x $HOME chmod 755 $HOME/public_html ยท You will then need to enable PHP scripting in the User Directory (by default Ubuntu disables this).
๐ŸŒ
Cherry Servers
cherryservers.com โ€บ home โ€บ blog โ€บ cloud computing โ€บ how to install php on ubuntu 22.04 | step-by-step
How to Install PHP on Ubuntu 22.04 | Cherry Servers | Cherry Servers
November 7, 2025 - Popular sites that run PHP include Facebook, Pinterest, Wikipedia, WordPress, Slack, and many others. Currently, PHP 8.2 is the latest release. It ships with numerous features and improvements. Check out the release page for a comprehensive list of new features and enhancements. In this tutorial, you will learn how to install PHP 8.2 on Ubuntu ...
๐ŸŒ
Ubuntu Manpages
manpages.ubuntu.com โ€บ manpages โ€บ trusty โ€บ en โ€บ 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 is given then this parameter is taken as the filename to parse and execute (same as with -f). If no parameter is present then the standard input is read and executed. --interactive -a Run PHP interactively.