Ubuntu: Php doesn't work on Apache server



Question:

I installed LAMP

sudo tasksel install lamp-server  

I can run my site, but the php code is just displayed as text.

I have tried this solution, but it didn't help me.

When I try to run php script with terminal:

user@pc:~/ooo$ php index.htm  

I get just php code.

When

sudo /etc/init.d/apache2 status  php -v  

I get

 * apache2 is running  PHP 5.5.9-1ubuntu4 (cli) (built: Apr  9 2014 17:11:57)   Copyright (c) 1997-2014 The PHP Group  Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies      with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies  

How to solve this?


Solution:1

It would appear that your script is not getting passed to the php interpreter, and is simply outputting the text of the file. This is a common error and is almost always due:

  1. Using a file ending other than php (e.x. htm or html)
  2. Not using php tag <?php ... ?> or as Nathan mentioned using improper tags.

You may see other servers that use file extensions other than .php or use shorthand tags.

Both of these can be configured to be permitted:

Allowing php short tags

Allowing additional file extensions

Short tags used to be allowed by default, but have fallen out of favour. Its best to use the full tags <?php ?> if your starting something new or just learning php.


Solution:2

By looking at your last updated edit, I see the file is not saved as index.php. You see, php scripts will not work when you save it on an .htm or .html extension. In order for the php file to take effect, you will need rename index.htm to index.php.

EDIT

As per my last comment with a testing php script link, it seems there's no problem with your php pack or the lamp-server. It is something to do with your php script itself. Better have a look into the script properly and fix it have a proper outcome.

Hope this helps!


Solution:3

It looks like the code is using short tags <? ?> instead of the normal <?php ?>. You need to enable short tags in php.ini.

Find this in php.ini: short_open_tag

Set it to 1 and restart Apache.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »