10

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.

Christian
  • 735
  • 3
  • 10
  • 29

6 Answers6

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.

castis
  • 8,154
  • 4
  • 41
  • 63
  • 1
    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. – Christian Aug 06 '13 at 17:15
  • 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. – Christian Aug 06 '13 at 17:16
  • top? as in the `top` command? It wouldn't show php, it would show up as apache. but im glad i could help! – castis Aug 06 '13 at 17:17
  • 1
    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. – datasage Aug 06 '13 at 17:29
  • 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? – Soerendip Sep 23 '18 at 23:24
8

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.

MagicLAMP
  • 1,032
  • 11
  • 26
  • What do you mean with "if you are testing from public_html"? My files are in ´/var/www/html´ would that work? – Soerendip Sep 23 '18 at 23:27
  • 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. – MagicLAMP Oct 02 '18 at 00:54
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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Dan
  • 11
  • 1
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.

-1

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

davidgo
  • 269
  • 4
  • 10
-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);

Mic1780
  • 1,774
  • 9
  • 23
  • 5
    if a function is the only thing inside php tags, the semicolon does not matter. – castis Aug 06 '13 at 17:00
  • 1
    you are correct @castis. but it is best practice. take a look here: http://stackoverflow.com/a/2038751/1637737 so actually the semicolon is still required. php is just smart enough to fix problems for the lazy. – Mic1780 Aug 06 '13 at 17:02
  • 5
    thats nice and all, but it doesn't help him with his issue. – castis Aug 06 '13 at 17:05