How to open PHP files in Ubuntu instead of downloading.

Read {count} times since 2020

No one can open PHP files in web browsers without a server. Page types such as HTML, Javascript and CSS can be understand by the browsers. But browsers can’t understand PHP. So the file will download.
There are two types of file that will run : Client-side and Server-side

Client Side : The program runs in browser itself. The client can easily access the code. The browser can understand the language in which the client program is written.

examples : HTML, CSS, JavaScript, jQuery, AngularJS

Server Side : The program runs in server and the finished page is sent to the client’s browser. The returned page won’t contain any source code of the language ie the client won’t see the code in page source unlike in Client Side. The returned page won’t contain any server language at all.

So now you got the reason why the PHP file won’t open in web browsers. 
You can only open PHP file with a server that supports PHP language. So here’s how to install Apache server and configure the server in Ubuntu

Open Terminal [CTRL + ALT + T]

Install Apache


sudo apt-get install apache2

in terminal. You will be asked for your password. Type your password and press enter key.

Restart Server


sudo /etc/init.d/apache2 restart

in terminal and press Enter Key.

To check whether you installed Apache correctly open your web browser and go to http://localhost/

If the browser doesn’t say “This webpage is not available”, You installed Apache correctly.

Install PHP


sudo apt-get install libapache2-mod-php5

and press Enter Key.

Enable this module by doing the command :

sudo a2enmod php5

and relaunch Apache by entering the command:

sudo service apache2 restart

To Open the PHP files now, copy the files and paste it on /var/www folder. The go to http://localhost/file-name.php to see your PHP file in action. See this tutorial to see how to create a localhost website.

Show Comments