0

I just started working on Ubuntu 16.04 Mini, and I just star working on PHP. When I create php file and put in directory /var/www/ubunturock/dummy.php and run the apache server and when I go to my address ubunturock/dummy.php I get my PHP code file. Any idea?

Here is my PHP code as well:

<?php
$servername='localhost';
$username='root';
$password='root';

$schema='demir';

$conn=mysql_connect($servername, $username, $password) or
    die('Connection failed : ' . $conn-> connect_error);

mysql_select_db($schema);

$result=mysql_query('SELECT * FROM topics');

echo '<h1>All Topics</h1><br>';
echo '<table>';
echo '<tr><td>ID</td><td>Name</td></tr>';
while($row = mysql_fetch_assoc($result)){
    echo '<tr>';
    echo '<td>' .$row['id'].'</td>';
    echo '<td>' .$row['name'].'</td>';
    echo '</tr>';
}
echo '</table>';
?>
David Foerster
  • 36,264
  • 56
  • 94
  • 147
yimy
  • 3
  • Apache doesn't interpret or execute PHP code, that's the PHP interpreter's job. Did you install the PHP interpreter and integrate it with Apache, for example with mod_php, cgi, fcgi, php-fpm? – Henning Kockerbeck Nov 08 '16 at 00:43
  • @HenningKockerbeck No. Just install sudo apt-get install php sudo apt-get install mysql-php. That's what I installed – yimy Nov 08 '16 at 00:44

1 Answers1

0

Apache is a webserver, so it basically does what webservers are there for: You (or your browser) asks for a file, and it sends you that file. Apache on its own doesn't "know" that this file contains code that you wanted to be executed beforehand.

PHP gets executed by the PHP interpreter. You seemed to have installed that interpreter, but you need to integrate it with Apache. The lifecycle of a PHP script basically looks like this (a bit anthromorphized, but it's easier to get the gist that way)

  • A client requests a file with ending .php.
  • Apache knows, such files need to be handed to the PHP interpreter. So it does.
  • The PHP interpreter executes the PHP code and generates output, which it hands back to Apache.
  • Apache sends that output to the client.

In your current setting, Apache doesn't "know" that .php files are in any way special or how to handle them. So it just does as it would do with any other file and sends the file as-is.

There are several ways to integrate the PHP interpeter with Apache. You can use the Apache module mod_php, you can use the common gateway interface cgi or its newer versions like fcgi or php-fpm. They differ in aspects like

  • how easy are they to set up
  • what performance they offer
  • how extensively you can configure them
  • how well you can secure your setup

If you just want a server to toy around and experiment, it should be sufficient to use mod_php by simply installing the package libapache2-mod-php and restarting Apache. But that SAPI (server API) is quite bad performance-wise, nearly without configuration options and not very secure (mostly because by default every PHP process is run by the same system user). It would be not a good idea to use that in production or on a server that's connected to the internet (which means, that everybody from Oslo to Timbuktu can have a go at it).

Two more things: Ubuntu 16.04 comes with PHP 7. The old mysql functions like the ones you're using, mysql_connect, mysql_query, mysql_fetch_assoc and so on, have been deprecated since PHP 5.0 (meaning, since twelve years ago). In PHP 7 they have been removed, so you can't use them anymore. You should use the mysqli or pdo modules. Tutorials about those are easy to find.

And secondly, the php package contains the basic interpreter. Many additional modules are packaged in their own packages, like the php-mysql you already installed. If you want to use those, you have to install the respective package.

  • I am really noob for Ubuntu, and just confusing there what you wrote, and try to understand step-by-step :) Which step do I need to do first ? – yimy Nov 08 '16 at 15:48
  • @yimy First, install a PHP SAPI. For a playground server that can't be accessed from all of the internet, mod_php is good enough. To do that, install the package libphp2-mod-php and restart Apache. Then you should basically be able to execute PHP code from Apache. – Henning Kockerbeck Nov 08 '16 at 16:23
  • @yimy As the next step, replace any old mysql functions in your code, because they're not available in PHP 7 any more. Use mysqli or the not MySQL-specific PDO instead. For both, there are many tutorials available. – Henning Kockerbeck Nov 08 '16 at 16:24