0

I bought a server online and installed LAMP on in (with 1-click, I didn't configure anything).

I now need to create a dynamic page using php, but if a create a file test.php with the following code:

<?php
echo "Hello!";
?>

When I connect to http://serverIP/test.php instead of showing me an empty page which says Hello! it keeps downloading test.php on my computer as a file!

Please note:

  1. test.php is in /var/www/html next to index.html and many others .html files that work correctly.

  2. If I type php -v in the command-line it says that

    php 7.0.22-0ubuntu0.16.04.1 is installed.

  3. When I type a2enmod php7.0 it says

    Module php7.0 already enabled

  4. With LAMP i got installed Ubuntu 16.04 and Apache 2.4.18 (Ubuntu).

  5. can't see a folder named "modules" in /etc/apache2

  6. When I modify any file I also restart apache (service apache2 restart)

This is how my /etc/apache2/apache2.conf looks like (without all the comments):

Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
Include ports.conf
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
    <IfModule sapi_apache2.c>
        php_admin_flag engine on
    </IfModule>
    <IfModule mod_php5.c>
        php_admin_flag engine on
    </IfModule>
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf


#the following lines are what I added
LoadModule php7_module modules/libphp7.so ### in "etc/apache2" there isn't a folder named "modules" ###
AddType application/x-httpd-php .php
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>
  • 2
    did you install libapache2-mod-php7.0 sudo apt install libapache2-mod-php7.0 – Panther Nov 8 '17 at 18:34
  • Yes, I found it installed by default (just like php7.0) – MauroFari Nov 8 '17 at 18:36
  • Yes, php7.0 is enabled (as I wrote). Removing those lines doesn't solve the problem, I just tried... – MauroFari Nov 8 '17 at 18:42
  • 1
    can you post the output of sudo lsof -i :80 and sudo netstat -tulpn | grep LISTEN – Panther Nov 8 '17 at 19:28
  • 1
    Well there you go, you are not running Apache =) You can either stop nginx and start apache or we can configure nginx, your choice. sudo service stop nginx && sudo start apache2 – Panther Nov 8 '17 at 19:38
1

Thanks to @Panther it turned out I was using nginx, not apache... so problem solved. Thanks again @Panther!

You probably want to remove or disable nginx

sudo systemctl disable nginx

or

sudo apt-get remove nginx
0

Check /etc/php/7.0/ to make sure you have php7.0-cgi installed. You should have an apache2 folder in there, if all you see is a cli folder you will need to sudo apt-get install php7.0-cgi, then restart Apache.

  • In my apache2 folder I have 3 folders: cli, apache2 and mods-available. I did apt-get install php7.0-cgi (and it wasn't installed!), restarted Apache, but I still see those 3 folders, and it still downloads test.php on my computer – MauroFari Nov 8 '17 at 18:49
  • EDIT: a folder named "cgi" appeared, but it keeps downloading test.php instead of displaying it ( I also did a2enmod cgi and restarted Apache...) – MauroFari Nov 8 '17 at 19:12
  • @MauroFari did you nuke your browser cache? Browsers are notorious for 'caching' how to handle things. – Thomas Ward Nov 8 '17 at 19:24
  • Yes, I cleared the cache a couple times. I also try from my smartphone using FireFox Focus, which doesn't save any data (cache included) – MauroFari Nov 8 '17 at 19:28
  • Can you cut and paste the lists that you get when you type a2enmod and a2dismod. Nothing after the commands will give you a list for both enabled and disabled modules. Ctrl+C to exit out of the module entry prompt. Also have you edited any .htaccess files or have you entered any specific rewrite variables into the apache2.conf file? NVM: See your NGINX update now. – m_krsic Nov 8 '17 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.