0

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?

serezha93
  • 31
  • 1
  • 2
  • 6
  • It should be sudo apt-get install lamp-server^. Maybe there are packages you left out.. – Parto Apr 14 '14 at 18:25
  • 2
    Sorry if this insults your intelligence: Is your code wrapped in php tags <?php ...code... ?> – Dan Apr 14 '14 at 18:34
  • @dan08 sigh! how did I miss that. That's not an insult. That's another confirmation to proceed further by assisting.. :) – AzkerM Apr 14 '14 at 18:38
  • yeah, these tags are there. – serezha93 Apr 14 '14 at 18:41
  • 1
    Can you visit this link and copy the php script shown in the first example, save it test.php or whatever you like with the .php extension, then open that file and let us know whether the output Hello World can be seen?? – AzkerM Apr 14 '14 at 18:48
  • Hello World is work – serezha93 Apr 14 '14 at 18:50
  • Its must be something about your file, it is not getting passed to the php interpreter. The most common causes are missing tags and incorrect extensions. – Dan Apr 14 '14 at 18:55
  • I hope you're familiar with php scripting and how it works. By the look of your comment and the picture you've posted. Its not the php which is troubling, its your script. – AzkerM Apr 14 '14 at 18:56
  • But why this file good working on other server in Internet? Yeah, I know php is not so bad. – serezha93 Apr 14 '14 at 18:58
  • Try to install xampp it might work i am pasting a link here which explains about its installation and running php script on it too. Hope this might help you......just give it a try..... 1) To install XAMPP http://www.codingdevil.com/2014/02/how-to-install-xampp-on-ubuntulinux.html 2)To run PHP script http://www.codingdevil.com/2014/02/how-to-run-xampp-on-ubuntu.html – BeginnersSake Apr 14 '14 at 22:02

4 Answers4

4

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!

AzkerM
  • 10,270
  • 6
  • 32
  • 51
  • I renamed file in index.php and got same result. – serezha93 Apr 14 '14 at 18:30
  • what happens when you navigate to http://localhost using one of your web browser?? Could you please edit your question and update the output for sudo /etc/init.d/apache2 status and php -v – AzkerM Apr 14 '14 at 18:34
  • I think my server does not see the file .htaccess. Because if I delete it on the production server - the result is the same. Why did he ignore .htaccess? – serezha93 Apr 14 '14 at 19:06
  • A directive in your Virtual host file. Add/Change the line AllowOverride All – Dan Apr 14 '14 at 20:20
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.

Nathan C
  • 163
  • 5
2

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.

Dan
  • 6,753
  • 5
  • 26
  • 43
1

I just had the same issue. You need to check which are your PHP version first.

php -v

After install libapache2-mod-phpX

X= your php version number. It will ask for a few dependencies, say yes for all.

FabioB
  • 63
  • 1
  • 8