How to Run a PHP Application on Ubuntu

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 deploying a Laravel app. Laravel is a 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 apache server, Mysql database, FTP, etc.

In this article, we are setting up an environment on our Ubuntu system for running a simple PHP application.

Steps to run a PHP application on Ubuntu

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

1. Update and Update 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

2. Install Apache2

A PHP file requires a web server 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

3. Install PHP

PHP on Ubuntu can be installed using the below command.

sudo apt install php

4. 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 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';

5. 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.

The phpMyAdmin can be installed on our system using the command below.

sudo apt-get install phpmyadmin

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

We can access the phpmyadmin panel using the link below.


Note: if it is giving an error that “The requested URL /phpmyadmin was not found on this server”, follow the steps below.

  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 apache server

/etc/init.d/apache2 restart

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

This is an optional step and only need 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

Image is loading...

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

7. Copy/paste or clone the project to the root directory of the Apache webserver

var/www/html is the directory that the Apache web server 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.


8. Running the PHP file or project

Now its 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.


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


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.



So here in this article, we discussed the steps to run a PHP application on an Ubuntu system.

3 thoughts on “How to Run a PHP Application on Ubuntu

  1. I found a solution to my above issue

    sudo apt install php libapache2-mod-php
    sudo apt install php7.0-mbstring
    sudo a2dismod mpm_event
    sudo a2enmod mpm_prefork
    service apache2 restart
    after that

    sudo nano /etc/apache2/apache2.conf

    add the following line

    Include /etc/phpmyadmin/apache.conf

    sudo service apache2 restart

    Then run http://localhost/phpmyadmin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.