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?

10
  • It should be sudo apt-get install lamp-server^. Maybe there are packages you left out..
    – Parto
    Apr 14, 2014 at 18:25
  • 2
    Sorry if this insults your intelligence: Is your code wrapped in php tags <?php ...code... ?>
    – Dan
    Apr 14, 2014 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, 2014 at 18:38
  • yeah, these tags are there.
    – serezha93
    Apr 14, 2014 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, 2014 at 18:48

4 Answers 4

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!

4
  • I renamed file in index.php and got same result.
    – serezha93
    Apr 14, 2014 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, 2014 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, 2014 at 19:06
  • A directive in your Virtual host file. Add/Change the line AllowOverride All
    – Dan
    Apr 14, 2014 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.

2
  • 1
    Or better yet, use the normal tags.
    – Dan
    Apr 14, 2014 at 19:34
  • True enough, but for something like a web application this can be a daunting task :)
    – Nathan C
    Apr 14, 2014 at 19:45
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.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .