11

I've seen this question answered many times, but most end either unanswered or by telling the asker to put this:

<?php phpinfo() ?> 

in a test file. Obviously, if that produced what was expected, I wouldn't be here. Instead, I get a 404 error.

I'm using an ubuntu 12.04 server with Amazon. Apache is installed, php5 is installed, and apache was restarted. I followed the following sequence:

sudo apt-get install apache2 sudo apt-get install php5 sudo apt-get install libapache2-mod-php5 sudo /etc/init.d/apache2 restart 

Each one of the first three commands now gives me "apache2 is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded" Obviously, replace apache2 with php5 and libapache2-mod-php5 for the other two.

This is a sure way to tell me it's installed, correct? Well, when I use the command "top", php is not one of the services that are running, which tells me it's not running, correct?

Navigating to the IP address gives me Amazon's "It Works!" page, but navigating to any other page on the server produces a 404 error.

Any help is much appreciated.

18
  • 2
    phpinfo is a function. use phpinfo(). Commented Aug 6, 2013 at 16:54
  • What do you mean by any other pages Commented Aug 6, 2013 at 16:54
  • if you are getting 404 errors then that means the other pages don't exist. Commented Aug 6, 2013 at 16:55
  • sorry, i did use phpinfo() Commented Aug 6, 2013 at 16:55
  • Navigating to any other files I created. Commented Aug 6, 2013 at 16:55

6 Answers 6

12

Check out the apache config files. For Debian/Ubuntu theyre in /etc/apache2/sites-available/ for RedHat/CentOS/etc they're in /etc/httpd/conf.d/. If you've just installed it, the file in there is probably named default.

Make sure that the config file in there is pointing to the correct folder and then make sure your scripts are located there.

The line you're looking for in those files is DocumentRoot /path/to/directory.

For a blank install, your php files most likely needs to be in /var/www/.

What you'll also need to do is find your php.ini file, probably located at /etc/php5/apache2/php.ini or /etc/php.ini and find the entry for display_errors and switch it to On.

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

5 Comments

Ahh! Thanks so much! I did suspect that I was in the wrong place, but I was told it was the correct directory. I'll have to write this one down for future reference.
I'm still not sure why "top" didn't show php, but I created a php file in that folder and it works just fine.
top? as in the top command? It wouldn't show php, it would show up as apache. but im glad i could help!
Top will only show php if php is configured to run as an fcgi deamon. Default config will set it up as an apache module.
Would be nice to add, how to switch it on. Is it sufficient to change the Production value to On, or do I also need to remove the semicolon in the beginning of the line?
9

One big gotcha is that PHP is disabled in user home directories by default, so if you are testing from ~/public_html it doesn't work. Check /etc/apache2/mods-enabled/php5.conf

# Running PHP scripts in user directories is disabled by default # # To re-enable PHP in user directories comment the following lines # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it # prevents .htaccess files from disabling it. #<IfModule mod_userdir.c> # <Directory /home/*/public_html> # php_admin_flag engine Off # </Directory> #</IfModule> 

Other than that installing in Ubuntu is real easy, as all the stuff you used to have to put in httpd.conf is done automatically.

2 Comments

What do you mean with "if you are testing from public_html"? My files are in ´/var/www/html´ would that work?
By convention, when getting apache to serve web pages from everyone's home directory, you put a default public_html folder in everyone's home, and apache can be set up to serve from there. My answer does not affect /var/www/html sites, so if you are having issues serving from that directory, you need to look at DirectoryRoot or something.
1

To answer the original question "Why is php not running?" The file your browser is asking for must have the .php extension. If the file has the .html extension, php will not be executed.

Comments

0

Type in browser localhost:80//test5.php[where 80 is your port and test.php is your file name] instead of c://xampp/htdocs/test.php.

Comments

-1

When installing Apache and PHP under Ubuntu 14.04, I needed to specifically enable php configs by issuing a2enmod php5-cgi

1 Comment

What does that mean?
-6

You need to add the semicolon to the end of all php things like echo, functions, etc.

change <?php phpinfo() ?> to <?php phpinfo(); ?>

If that does not work, use php's function ini_set to show errors: ini_set('display_errors', 1);

3 Comments

if a function is the only thing inside php tags, the semicolon does not matter.
you are correct @castis. but it is best practice. take a look here: stackoverflow.com/a/2038751/1637737 so actually the semicolon is still required. php is just smart enough to fix problems for the lazy.
thats nice and all, but it doesn't help him with his issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.