How do i run php on ubuntu?

PHP - Scripting Language

PHP is a general-purpose scripting language suited for Web development. PHP scripts can be embedded into HTML. This section explains how to install and configure PHP in an Ubuntu System with Apache2 and MySQL.

This section assumes you have installed and configured Apache2 Web Server and MySQL Database Server. You can refer to the Apache2 and MySQL sections in this document to install and configure Apache2 and MySQL respectively.

Installation

PHP is available in Ubuntu Linux. Unlike Python, which is installed in the base system, PHP must be added.

To install PHP and the Apache PHP module you can enter the following command at a terminal prompt:

sudo apt install php libapache2-mod-php

You can run PHP scripts at a terminal prompt. To run PHP scripts at a terminal prompt you should install the php-cli package. To install php-cli you can enter the following command:

sudo apt install php-cli

You can also execute PHP scripts without installing the Apache PHP module. To accomplish this, you should install the php-cgi package via this command:

sudo apt install php-cgi

To use MySQL with PHP you should install the php-mysql package, like so:

sudo apt install php-mysql

Similarly, to use PostgreSQL with PHP you should install the php-pgsql package:

sudo apt install php-pgsql

Configuration

If you have installed the libapache2-mod-php or php-cgi packages, you can run PHP scripts from your web browser. 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 Apache 2 Web server is configured to run PHP scripts using this module. Please verify if the files /etc/apache2/mods-enabled/php8.*.conf and /etc/apache2/mods-enabled/php8.*.load exist. If they do not exist, you can enable the module using the a2enmod command.

Once you have installed the PHP related packages and enabled the Apache PHP module, you should restart the Apache2 Web server to run PHP scripts, by running the following command:

sudo systemctl restart apache2.service 

Testing

To verify your installation, you can run the following PHP phpinfo script:


You can save the content in a file phpinfo.php and place it under the DocumentRoot directory of the Apache2 Web server. Pointing your browser to http://hostname/phpinfo.php will display the values of various PHP configuration parameters.

References

  • For more in depth information see the php.net documentation.

  • There are a plethora of books on PHP 7 and 8. A good book from O’Reilly is Learning PHP, which includes an exploration of PHP 7’s enhancements to the language.

  • Also, see the Apache MySQL PHP Ubuntu Wiki page for more information.

To run a simple PHP file, we need to set up a server because it is a backend language. Let’s discuss the steps to run a PHP application on an Ubuntu system. Note that, we are running a simple PHP file on a local Ubuntu system.

To configure an Ubuntu server for running a PHP application, it’s better to follow the article Deploy Laravel 5.7 App On VULTR VC2. This covers all the steps to configure an Ubuntu system to run PHP and deploy a Laravel app. Laravel is a framework of PHP.

Running a PHP file or application on the Windows operating system is much simpler using XAMPP software. XAMPP is bundled with an apache server, Mysql database, FTP, etc.

Prerequisites

Before continuing this article, I assume that the reader is already working with Linux operating system and Unix/Linux commands.

We are using the following technologies to run a PHP on Ubuntu and you should be aware of it.

  • Apache2
  • PHP
  • MySQL
  • phpMyAdmin

What we will learn

In this article, we will run a PHP application on an Ubuntu system:-

  • Setup an Apache2 server
  • Install PHP
  • Setup MySQL
  • Install phpMyAdmin

Let’s dig into the steps to run a PHP app on Ubuntu. We need to set up a web server, PHP, and MySQL first to run a PHP app.

Update and Upgrade Packages

Before starting the installation of packages, it’s better to update and upgrade the packages we have on our system.

  • apt-get update updates the list of available packages and their versions, but it does not install or upgrade any packages.
  • apt-get upgrade actually installs newer versions of the packages you have. After updating the lists, the package manager knows about available updates for the software you have installed.
sudo apt-get update
sudo apt-get upgrade

Install Apache2

A PHP file requires a webserver to run. The commonly used web servers today are, Apache2 and NGINX. In this guide, we are using Apache2. It can be installed with the below command.

sudo apt-get install apache2

Install PHP

We really need PHP on our Ubuntu system to run a PHP app. Install the latest version of PHP with the below command.

sudo apt install php

Install MySQL

If our PHP project is using a database, it requires having a database server on our system. The most using database with PHP is MySQL. This can be set up using the below steps

sudo apt-get install mysql-server mysql-client
sudo mysql_secure_installation

When prompted, answer the questions using the guide below.

Enter current password for root (enter for none): (Press Enter)
Set root password? [Y/n]: Y
New password: (Enter password for mysql DB)
Re-enter new password: (Repeat password)
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]:  Y
Reload privilege tables now? [Y/n]:  Y

In the latest version of MySQL, it rollbacks the support for the root user. So, you can not migrate the database with the root user. So that we need to create a new user and grant all privileges to the user.

Note: Change the username and password as we need in the below commands.

sudo mysql --user=root mysql

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;
EXIT;

Install phpMyAdmin

phpMyAdmin is a free software tool written in PHP, intended to handle the administration of MySql over the Web. In simple, we can describe it as, a Graphical User Interface(GUI) for managing MySQL database.

To install phpMyAdmin using the below command.

sudo apt-get install phpmyadmin

It will ask for your MySQL credentials and you have to give them properly.

We can access the phpMyAdmin panel using the link below.

http://localhost/phpmyadmin

The phpMyAdmin not found error

Sometimes accessing the phpMyAdmin from the browser may return an error,

“The requested URL /phpmyadmin was not found on this server”.

How do i run php on ubuntu?

To solve this error, execute the following steps:-

  1. Open apache2.conf file using nano editor.
sudo -H nano /etc/apache2/apache2.conf

2. Paste the following line to the end of the file.

Include /etc/phpmyadmin/apache.conf

3. Restart the apache server

/etc/init.d/apache2 restart

Create a Database(Only if our PHP app needs a database to run)

This is an optional step and is only needed if our PHP project needs a database. Creating a MySQL database can be easily done using phpMyAdmin.

  • Open the link below. http://localhost/phpmyadmin  
  • Now Enter username and password(As you gave in step 4).
  • Click on the New tab
  • Enter a database name

Press Create

How do i run php on ubuntu?

It also needs to configure the database details in our PHP project.

Paste or clone the project to the root directory of the Apache webserver

var/www/html is the directory that the Apache webserver looks for files to serve on our domain by default. This location can be changed later to whatever value we want.

But now, we have to paste/clone our PHP file or project to this directory. As a default, this directory location can be seen in the location shown below.

/var/www/html

Running the PHP file or project

Now it’s time to run our PHP file. https://localhost direct to our apache2 home page. http://localhost/MyApp.php runs the MyApp.php file inside /var/www/html directory.

Note: As an example, I have chosen MyApp.php as a sample PHP file and MyPHPProject as a sample PHP project directory.

http://localhost/MyApp.php

If it’s a PHP project inside a directory, change the URL path.

http://localhost/MyPHPProject/MyApp.php

If our PHP project contains an index.php file in its root, It does not need to enter the file name. The below URL runs our PHP project.

http://localhost/MyPHPProject

Summary

So here in this article, we discussed the steps to run a PHP application on an Ubuntu system. First, we are setting up a LAMP environment and running a PHP application.

How do I start PHP in Ubuntu?

I followed these steps and it worked for me..
Execute sudo su on the terminal..
Enter your password..
Execute sudo subl /etc/apache2/sites-available/000-default. ... .
Change DocumentRoot /var/www/html to /home/user/yoursubdir..
Save the file and close it..
Execute sudo subl /etc/apache2/apache2..

How do I run a PHP script in Ubuntu terminal?

You just follow the steps to run PHP program using command line..
Open terminal or command line window..
Goto the specified folder or directory where php files are present..
Then we can run php code using the following command: php file_name.php..

Do I need to install PHP on Ubuntu?

PHP is a programming language used for developing web applications. You must install PHP packages on a Ubuntu system to run the application written on it. Generally, it is used to create e-commerce websites, blogs, and API applications. If you're looking for an easy way to install PHP on Ubuntu 22.04, look no further.

Where is PHP in Ubuntu?

The default location for the php. ini file is: Ubuntu 16.04: /etc/php/7.0/apache2.