1

I wrote a simple script and I executed it on localhost but it wasn't executed.

I am used to use Ubuntu operation system.

I've checked apache2 web server which to be run by following command :

sudo service apache2 start

After checking the service to be run I wrote simple script such a :

<head>
<title>THE PRACTICING FILE</title> 
</head>
<body>
<?php
echo "HELLO";
?>
</body>

After running the script, I found out that the resulting page just shows the tages which I used in the script such as < title > tag at the top of page. But the php codes weren't executed, In this case echo "Hello"; wasn't executed.

What's wrong ? why such a thing have been occurred ? and finally how can i resolve the problem ?

6
  • What is the name of the file? Commented Sep 11, 2014 at 20:32
  • 1
    have you checked your php.ini? Commented Sep 11, 2014 at 20:35
  • 1
    Does the file end with .php and not .html? Commented Sep 11, 2014 at 20:37
  • the file name is index.php but only html tags were executed. Commented Sep 11, 2014 at 20:47
  • the only thing is be running apache, php is not configured with apache, so has nothing to do with "php.ini", if you view the source code by the browser will notice that appears the <?php echo "HELLO"; ?>, in other words, apache ok, but php not installed or configured in Apache. Commented Sep 11, 2014 at 20:47

1 Answer 1

2

Apache is a web server and by default serves static content, meaning HTML documents and any other kind of text or binary files as is. The way you get it to process php content and send its output is by using an Apache PHP module to invoke the PHP processor.

In order to do achieve that, make sure that:

  • You have installed php and you have a php module configured for apache: sudo apt-get install php5 libapache2-mod-php5

  • Your script has a .php extension, such as myscript.php, which is one of the default extensions to be handled by the php module on Apache.

  • You restarted apache after installing the php module: sudo service apache2 restart

  • You are hitting the proper page on your machine's browser, such as http://localhost/myscript.php, and not something like file:///var/www/html/myscript.php

If it still doesn't work, you can also check the logs on /var/log/httpd/error_log to see what went wrong.

For more info on Ubuntu with Apache and PHP (+MySQL= LAMP), follow this tutorial

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much :) the problem is referred to the packages which you mentioned. after installing the packages apache web server was restarted automatically and after refreshing the page the string was showed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.