To run a PHP application on Ubuntu, first, we have to install the latest version of PHP on our system. This can be done by the following step.
sudo apt-get install php
Now we need to install a web server to run PHP because PHP runs in the backend. Apache2 is the popular web server for this. We can install apache2 using the below command.
sudo apt-get install apache2
After the installation, type
on your browser and it returns an “Apache2 Ubuntu Default Page“.
The apache2 installation will make /var/www/html folder structure on your system. This is our system’s localhost directory. We can see an index.html file inside this directory which contains the HTML we saw before(“Apache2 Ubuntu Default Page“).
We need to paste or clone our PHP application in this directory to run. But it requires one more step. Setting the permission to write to this directory. It can be done by the below command.
sudo chmod 777 -R /var/www/html
Note that, this permission gives all users to write to this directory and is not safe. Change it after all the steps are done.
Clone/Paste PHP our application inside /var/www/html
Now we need to clone or paste our PHP application inside /var/www/html directory.
If your project is in Git, cone it with the below command.
git clone [git_repository_url]
Or, if your project is in another directory, copy and paste it to /var/www/html.
Install packages/dependencies (optional)
If our PHP application using any packages/dependencies, we have to install it. The Composer is the most popular package manager for PHP. Running the below command from the root directory of our project install the packages in the package.json file.
Running our project
After everything is set, we can access our project from our browser window. I am assuming that my_php_app is the project directory we pasted/cloned in the previous step. Now we can start our PHP app using the URL.
This will open our PHP app on our browser.
Note that, it only works if our project’s root directory contains index.php or index.html. If not, use the below URL.