6

I just installed php and apache server on Ubuntu 17.04 but my .php file doesn't work on my browser.

index.php:

<!DOCTYPE html>
<html>
<body>
<h1>Welcome!</h1>
<?php
  echo "Hello World!";
?>
</body>
</html>

Only Welcome! appeared on the browser, and when I inspect the page I see php code is being commented. I have already started apache server and still have no idea what is wrong. Please help me out, thank you!

$ php -v

PHP 7.0.15-1ubuntu4 (cli) (built: Feb 28 2017 21:33:59) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.15-1ubuntu4, Copyright (c) 1999-2017, by Zend Technologies

$ sudo /etc/init.d/apache2 status

apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Tue 2017-04-25 14:38:55 +07; 10min ago
  Process: 11578 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
  Process: 11604 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 11619 (apache2)
    Tasks: 55 (limit: 4915)
   Memory: 9.7M
      CPU: 429ms
   CGroup: /system.slice/apache2.service
           ├─11619 /usr/sbin/apache2 -k start
           ├─11663 /usr/sbin/apache2 -k start
           └─11664 /usr/sbin/apache2 -k start

Apr 25 14:38:55 g5080 systemd[1]: Starting The Apache HTTP Server...
Apr 25 14:38:55 g5080 apachectl[11604]: AH00558: apache2: Could not reliably determin…sage
Apr 25 14:38:55 g5080 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
Huy Vo
  • 2,418
  • 6
  • 22
  • 43

7 Answers7

8

while you are in ubuntu 17.04

you can follow this steps

sudo apt-get install apache2 php libapache2-mod-php7.0 mysql-server php-mbstring php7.0-mbstring phpmyadmin     //install
sudo service apache2 restart    //restart

and give it another try for your file(index.php)

Fahmi B.
  • 758
  • 2
  • 10
  • 23
6

Check the below configuration in your web server

sudo apt-get install apache2 php5 libapache2-mod-php5`

It will install everything you need and will start the apache server with support for PHP.

To verify that the php module is loaded, type:

   a2query -m php5

if not enabled, then load with:

   sudo a2enmod php5

and restart apache:

   sudo service apache2 restart
vinay kumar
  • 111
  • 9
3

Trying this command actually worked for me

sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-pear php-imagick php-imap php-mcrypt php-recode php-tidy php-xmlrpc
2

Tested Fixture for AWS Ubuntu 18.04

Step 1 - In AWS Server, have created Ubuntu 18.04, connect via putty, search for apache2.conf file. Step 2 - With apache2.conf open in the directory [$ sudo vi apache2.conf], press I for editing file, all you have to do is add the following to the bottom of the file:

<FilesMatch \.php$>
​SetHandler application/x-httpd-php
​</FilesMatch>

Save and close apache2.conf. Step 3 (usually not needed) - Enable/disable modules In order to get PHP to function properly, you have to disable the mpm_event module and enable the mpm_prefork and php7 modules. To do this, go back to your terminal window and issue the command:

sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7.2

Step 4 - Restart Apache 2

sudo service apache2 restart
Abdul Rehman
  • 689
  • 6
  • 11
0

Maybe just a special case:
I wanted to directly call a php file inside a subdirectory of "/var/www/vhosts/.../somePage/index.php" (after restoring from an old VM). It wasn't possible, the index.php would just be downloaded

After moving the ".../somePage/" directory from "/var/www/vhosts/..." to "/var/www/" the index.php would run correctly (calling http://localhost/somedir/index.php)

MacMartin
  • 2,366
  • 1
  • 24
  • 27
0

Package php-mcrypt is not available, but is referred to by another package. this error comes after giving the command sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-pear php-imagick php-imap php-mcrypt php-recode php-tidy php-xmlrpc

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 29 '22 at 11:09
0

EDIT 2023: Works with Rasbian Debian Bullseye and Debian Bookworm.

You need to edit file /etc/apache2/apache2.conf and add to the end of file:

SetHandler application/x-httpd-php

Then reload apache2.service with:

sudo systemctl restart apache2.service

Then check if service is correctly loaded with:

sudo systemctl status apache2.service
Wongjn
  • 8,544
  • 2
  • 8
  • 24
hokuto
  • 1