Can't get my local server to work

Hey good folks, I’m in the very early stages of learning PHP and am stuck already.

I’m running Ubuntu 12.04. Here are more details:

jorge@Rhoxanne:~$ php -v
PHP 5.3.10-1ubuntu3.21 with Suhosin-Patch (cli) (built: Oct 28 2015 01:47:12)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

jorge@Rhoxanne:~$ apache2 -v
Server version: Apache/2.2.22 (Ubuntu)
Server built: Jul 24 2015 17:25:42

The phpinfo() script works as does the localserver URL. So I seem to have everything in place to test my scripts on the local server, but I haven’t been able to get even the simplest one to work.

Below please find a sample .html file and its corresponding .php file. The .html page seems to work just fine, but when the submit button is pressed the browser just opens the php file (Firefox) or downloads it (Chromium}. I’m sure the problem is easy to solve, I just can’t figure out how.

<html>
 <head>
  <link href="/home/jorge/css/phpMM.css" rel="stylesheet" type="text/css" />
 </head>

 <body>
  <div id="header"><h1>PHP & MySQL: The Missing Manual</h1></div>
  <div id="example">Example 1-1</div>

  <div id="content">
    <h1>Welcome!</h1>
    <p>Hello there. So I hear you're learning to be a PHP programmer!</p>
    <p>Why don't you type in your name for me:</p>
    <form action="/home/jorge/scripts/sayHelloWeb.php" method="POST">
      <p><i>Enter your name:</i> <input type="text" name="name" size="20" /></p>
      <p><input type="submit" value="Say Hello" /></p>
    </form>
  </div>

  <div id="footer"></div>
 </body>
</html>


<html>

  <head>

    <link href = "/home/jorge/css/phpMM.css" rel = "stylesheet" type = "text/css" />

  </head>

  <body>

    <div id = "header"> <h1> PHP & MySQL: The Missing Manual </h1> </div>

    <div id = "example"> Example 1-1 </div>

    <div id = "content">

      <h2> Hello, <?php echo $_REQUEST['name']; ?> </h2>

      <p> Great to meet you!  Welcome to the beginning of your PHP programming odyssey. </p>

    </div>

    <div id = "footer"> </div>

  </body>

</html>

It sounds like you need to add PHP as a handler. I don’t recall how you do this in Linux, but I’m pretty sure you can do it via .htaccess

However, it doesn’t sound right because when you install PHP, it should of made the addition to your Apache configuration to add PHP as a handler.

With such an old Ubuntu version and PHP version, I can’t help you, but tell you it could be because PHP isn’t being accessed properly because it could not have been added as a handler.

I think the .htaccess file may be relevant to my actual domain server but not to my local server (on my machine).

You can still use .htaccess in a local server and do the same thing.

Try running just local host and you should get the default Apache index.html.

Find the directory where the index.html is located and make a new 'hello-world.php" file in the same directory.

Try changing the default path and in php.ini

On the server, search for a file called httpd.conf once you’ve found that file, search it for a line that reads:

AddType application/x-httpd-php .php

If it’s there it might be commented out with a # at the start of the line, if it has remove the # then save and reboot the server

That’s what I was talking about. However, since it’s Linux, you shouldn’t need to add that line into httpd.conf because when you install PHP, you should be installing Apache first and then PHP. PHP will re-configure Apache and add PHP as a handler.

I’m looking at my Ubuntu 15.10 httpd.conf and there isn’t that line, but it doesn’t need to be added if you properly installed PHP.

The main problem is how did OP install PHP.

@jcmcobra could you post a link to the tutorial that you’re working from?

I’m using the most recent book I could find at the local library, not an online tutorial SpacePhoenix. I don’t think it’s one of Sitepoint’s texts, so I should probably not hype it here.

Thanks to everyone for the suggestions. I’ll look into them.

You may have something here. I’m not sure that I installed Apache first.
Maybe if I uninstall PHP then reinstall it, the configuration will be corrected.

Your problem is here:
/home/jorge/scripts/sayHelloWeb.php

You can still run php scripts via CLI from anywhere but by default apache2 will create a Virutal host for you ( /var/www/html).

Your files will need to go here. You can change that by going to /etc/apache/sites-enables/000-default.conf if needed but all site files will need to be under that folder in order for the webserver (apache) to see it.

It has nothing to do with where the file is placed. If the file isn’t placed in the right spot. Apache will return a 404 error page. OP’s original issue is why is PHP being downloaded instead of being rendered as it should be. And I have answered that already. It’s because it isn’t properly installed. There are only one of two ways why PHP is being downloaded rather than being output to the screen.

  1. The PHP file in question has a download header.
  2. PHP isn’t added as a handler.

By default, when you manually install it via Windows, PHP isn’t the default handler. It’s HTML. You have to add PHP as a handler so that Apache knows that .php files should be executed via PHP and not HTML. I’m not sure if this is true with Ubuntu because I am using Ubuntu 15.10 with PHP Version 5.6. So I don’t remember if I had to add PHP as a handler or if you had to manually do that on old Ubuntu versions.

The versions are different and there might be a slight change in the two. So I can’t replicate the issue or I would of done so already.

Whatever. Really though to OP just try it.

No, OP shouldn’t. They’ll just get a 404 error page. Wasting time on something that is already confirmed not the problem makes no sense.


@jcmcobra

While attempting to re-create your issue with my Ubuntu 15.10 & PHP 5.6. I found that this file could be the issue.

Here are the steps to debugging the issue.

  1. Open your favorite IDE editor.
  2. Click on File > Open
  3. Browse to /etc/apache2/mods-available/php5.conf
  4. Check if the first few lines have this.

<FilesMatch ".+\.ph(p[345]?|t|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>

5. If it does, but SetHandler application/x-httpd-php is commented out like this #SetHandler application/x-httpd-php or # SetHandler application/x-httpd-php. Un-comment it by removing the hash (#) sign.
6. Also, check to see if these few lines exists in the same file.

<FilesMatch ".+\.phps$">
    SetHandler application/x-httpd-php-source
    # Deny access to raw php sources by default
    # To re-enable it's recommended to enable access to the files
    # only in specific virtual host or directory
    Require all denied
</FilesMatch>

7. If they don’t. Add them in after the first </FilesMatch>.
8. Save your work.
9. Restart Apache.
10. Close your current windows/ tabs.
11. Re-open the path location.
12. Try it.

If this still doesn’t work.

13. Clear browser caches. Then close the browser. And restart it again.

Let us know what happens after that.

Why don’t you just use vagrant or more precisely puphpet.com. That is what I use for my local set-up. If your site runs on 5.3 it will probably run fine on 5.4. I’m working on a project right now that runs on production server with 5.3 but locally I’m running a 5.4 vm just fine.

I already had triple booting configured long before (precisely 1 year) we talked about VM in that one topic. I also had Ubuntu 14.04 installed on a partition and I had upgraded to 15.10 when it was released. I couldn’t just turn back and erase my whole hard-drive as there are millions of files on all 3 partitions.

I wasn’t talking about you. I was addressing the thread owner.

1 Like

Carry on then.

Thanks for the suggestions sstrooper. The lines are in the file and not commented out, though they don’t exactly match. Actually, here’s the file:

<IfModule mod_php5.c>
    <FilesMatch "\.ph(p3?|tml)$">
	SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
	SetHandler application/x-httpd-php-source
    </FilesMatch>
    # 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_value engine Off
        </Directory>
    </IfModule>
</IfModule>

Notice that the lines you mention are present and not commented out, though they don’t match yours precisely. Also notice the comments around the middle. I’ve tried commenting out the five lines mentioned, but the only effect that has is that now the browser wants to save or open the file rather than download it.

In the meantime I’ve tried to uninstall everything and reinstalling, but nothing has changed. The truth is, I even doubt that I’ve successfully uninstalled and I’ve tried more than one method. Ubuntu can be trying.