How to run PHP on Ubuntu
March 21, 2012 in Php
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:
sudo apt-get update sudo apt-get install apache2
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/
Now you have to install PHP.
To install PHP and PHP support for Apache, just write the following in console:
sudo apt-get install php
When this is done you are ready.
Let’s go and replace the default page of Apache so we can create our own.
Open a terminal and type:
sudo mv /var/www/html/index.html /var/www/html/index.back
If you want to test that PHP is installed successfully into your system you can write and run a simple PHP script that will return to you the PHP information. So open the terminal and type:
sudo gedit /var/www/html/index.php
An empty document will open in gedit now just type the following and save the document.
<?php print_r (phpinfo()); ?>
And point your web browser at: http://localhost/
You should now be able to see your first PHP page running.
You can edit this page at any time by typing:
In order to restart Apache web server just type the following command
sudo gedit /var/www/html/index.php
In this tutorial im using gedit to create my webpages but you can also install an IDE like netbeans and manage you projects from there in a way more efficient way. Check the installation process of Netbeans here.
If you also need MySQL you can find info on how to install MySQL here.
Have fun creating your web pages!
Video Tutorial
guys i love you all of you i was going crazy but you had rescue me thanks and hope it ‘ll work
Thanks for posting this information
thank you so much man, you have no idea how much you’ve just helped me
very nice tutorial.
You’re my heroes of the week. I have php courses, but they only cover Windows programs for php, and I’m highly anti-Windows. Linux works so much better and faster, so now I don’t need to conform to perform. Thank you.
How to change base directory? as a user I found the ‘html’ directory read-only
while restarting the apache i get this msg
Restarting web server apache2 AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message
I have a question. How would someone know which Linux Distribution is better for a PHP website? I have never used Ubuntu, but I have used Fedora and currently using Debian because the platform I am using for hosting my PHP app has Debian (https://www.cloudways.com/en/php-cloud-hosting.php ). I have noticed that Debian has a relatively better performance and less system related issues.
thank you dude for saving my life thankyou so much
very nice.tnx.
Well said. How to run PHP on Ubuntu concept id interesting. The Nice and Valuable information you explained in this article I loved it more, it useful for me a lot.
Unfortunately, I am not certain as to why, but this isn’t working in Linux Mint (which is based on Ubuntu). The only thing I get from navigating to the localhost/index page with apache running, is the exact same thing that was typed into the editor…
Not sure who decided to screw the pooch on it, but I can’t go back to Ubuntu since that distro decided to not keep up with certain vital tools, which works in Mint. I may have to suffer the indignity of using Xampp or some other ridiculous software to achieve what should not have been destroyed in the first place.
sir
before I created a PHP file and I want to use that.
is it possible?
After installing this,when i run html file,php file opens in gedit.why this happpens?
Worked on ubuntu 16.04.Thanks!!!
Worked on ubuntu16.04.Thanks!!!
Can i name my file something other than index.php or is it compulsary to name it index.php to run it?
Command to restart Apache2 is-
systemctl restart apache2
works on ubuntu 18.4 . thank you so much
[…] of this video that is running on latest ubuntu versions https://youtu.be/KOJkEdyeDsk more info at http://www.allaboutlinux.eu/how-to-run-php-on-ubuntu/ […]
hi my brother hope you are fine i want to know that how to run the codeigniter project in ubuntu??