Help installing PHP on Ubuntu (Solved. Thank you!)

(I’m currently using Firefox 124.01 on Ubuntu 22.04.03 LTS) (My formatting is also givinge problems… I want to eat my own eyeballs out right now…)

I’m very new to Linux, and trying to get PHP installed, but it won’t render anything…

Following this guide (ubuntu.com/server/docs/programming-php), I started with:


<span style="color:#323232;">sudo apt update
</span><span style="color:#323232;">sudo apt install apache2
</span>

Test Page Success!

So I continue with:


<span style="color:#323232;">sudo apt install php libapache2-mod-php
</span><span style="color:#323232;">sudo apt install php-mysql
</span><span style="color:#323232;">sudo apt install php-cgi
</span>

Then:


<span style="color:#323232;">sudo systemctl restart apache2.service
</span>

I write the following in /var/www/html/index.php:


<span style="color:#323232;"><?php
</span><span style="color:#323232;">
</span><span style="color:#323232;">phpinfo();
</span><span style="color:#323232;">
</span><span style="color:#323232;">Test Text1
</span><span style="color:#323232;">
</span><span style="color:#323232;">?>
</span><span style="color:#323232;">
</span><span style="color:#323232;">Test Text2
</span>

I see “Test Text2” and nothing else. I see my PHP tags and their contents upon source inspection.

Then I perform the following:


<span style="color:#323232;">apt list --installed | grep php
</span><span style="color:#323232;">sudo apt purge php
</span><span style="color:#323232;">apt list --installed | grep php (0 Results)
</span>

And try again:


<span style="color:#323232;">sudo apt install php libapache2-mod-php
</span><span style="color:#323232;">sudo apt install php-mysql
</span><span style="color:#323232;">sudo apt install php-cgi
</span><span style="color:#323232;">
</span><span style="color:#323232;">sudo systemctl restart apache2.service
</span>

No luck… Any ideas what’s going on?

A little additional info:


<span style="color:#323232;">~$ which php
</span><span style="color:#323232;">/usr/bin/php
</span>

<span style="color:#323232;">~$ php -v
</span><span style="color:#323232;">PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
</span><span style="color:#323232;">Copyright (c) The PHP Group
</span><span style="color:#323232;">Zend Engine v4.1.2, Copyright (c) Zend Technologies
</span><span style="color:#323232;">    with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
</span>

<span style="color:#323232;">~$ apt list --installed | grep php
</span><span style="color:#323232;">
</span><span style="color:#323232;">WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
</span><span style="color:#323232;">
</span><span style="color:#323232;">libapache2-mod-php8.1/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.14 amd64 [installed,automatic]
</span><span style="color:#323232;">libapache2-mod-php/jammy,jammy,now 2:8.1+92ubuntu1 all [installed]
</span><span style="color:#323232;">php-common/jammy,jammy,now 2:92ubuntu1 all [installed,automatic]
</span><span style="color:#323232;">php-mysql/jammy,jammy,now 2:8.1+92ubuntu1 all [installed]
</span><span style="color:#323232;">php8.1-cli/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.14 amd64 [installed,automatic]
</span><span style="color:#323232;">php8.1-common/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.14 amd64 [installed,automatic]
</span><span style="color:#323232;">php8.1-mysql/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.14 amd64 [installed,automatic]
</span><span style="color:#323232;">php8.1-opcache/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.14 amd64 [installed,automatic]
</span><span style="color:#323232;">php8.1-readline/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.14 amd64 [installed,automatic]
</span><span style="color:#323232;">php8.1/jammy-updates,jammy-updates,jammy-security,jammy-security,now 8.1.2-1ubuntu2.14 all [installed,automatic]
</span><span style="color:#323232;">php/jammy,jammy,now 2:8.1+92ubuntu1 all [installed]
</span>

<span style="color:#323232;">/etc/apache2/mods-enabled$ ls
</span><span style="color:#323232;">access_compat.load  authz_core.load  deflate.load  mime.load         php8.1.load      status.load
</span><span style="color:#323232;">alias.conf          authz_host.load  dir.conf      mpm_prefork.conf  reqtimeout.conf
</span><span style="color:#323232;">alias.load          authz_user.load  dir.load      mpm_prefork.load  reqtimeout.load
</span><span style="color:#323232;">auth_basic.load     autoindex.conf   env.load      negotiation.conf  setenvif.conf
</span><span style="color:#323232;">authn_core.load     autoindex.load   filter.load   negotiation.load  setenvif.load
</span><span style="color:#323232;">authn_file.load     deflate.conf     mime.conf     php8.1.conf       status.conf
</span>
dbx12, (edited )

Looks like your php code is wrong, try this (and just this) for index.php


<span style="color:#323232;"><?php
</span><span style="color:#323232;">phpinfo();
</span>

You also had Test Text1 inside the php block which is a syntax error. Maybe that’s the cause why you don’t get anything.

Edit corrected the filename to lowercase extension.

GardenVarietyAnxiety,

I wiped and reinstalled everything php and apache… It still didn’t work, so I tried your suggestion and simply put

<?php phpinfo();

It works now? And reverting to my original file works too…

I don’t know how, but your idea worked. Thanks~

dbx12,

Great that it worked out, did you use apt remove or apt purge?

You might want to put your code in triple back ticks so it renders as code block :) Currently isn’t visible on some clients (boost for Lemmy in my case)

sloppy_diffuser,

I don’t have a solution, but wanted to comment this is a reason to use docker. Instead of tinkering with your host system in ways you’ll probably forget, declare all the steps in a nice containerized system that does one thing: hosts a php website as you develop.

Expose your project through a volume for a mostly seamless file sharing experience between the host and container.

GardenVarietyAnxiety,

I’ve seen a lot about docker, and it looks pretty awesome.

I’ll definitely get to it eventually, I’m just trying to learn the basics for now.

hitmyspot,

Have you tried a html file to ensure Apache is ok? Or no files and Apache should show a server page.

Also, the test file can just be

<?php phpinfo(); ?>

GardenVarietyAnxiety,

I wiped and reinstalled everything php and apache… It still didn’t work, so I tried to simply put

<?php phpinfo();

as suggested by someone else …and it works now? I reverted my .php file to what I listed in my op and that works now, too.

Oi.

hitmyspot,

Lol, turn it off and back on!

GardenVarietyAnxiety,

Basically, lol

raoul,

Maybe there is a conflict between libapache2-mod-php and php-cgi?

According to your doc, it seems to be one or the other.

GardenVarietyAnxiety,

I wiped and reinstalled everything php and apache… It still didn’t work, so I tried to simply put

<?php phpinfo();

as suggested by someone else …and it works now? I reverted my .php file to what I listed in my op and that works now, too.

Oi.

littleme,

Have you tried enabling php with the a2enmod command ? serverlab.ca/…/installing-php-for-apache-on-ubunt…

GardenVarietyAnxiety,

It returned: Module php8.1 already enabled

onlinepersona,

Was that the output of sudo a2enmod php ?

GardenVarietyAnxiety,

I wiped and reinstalled everything php and apache… It still didn’t work, so I tried to simply put

<?php phpinfo();

as suggested by someone else …and it works now? I reverted my .php file to what I listed in my op and that works now, too.

Oi.

onlinepersona,

😂 welcome to the wonderful world of computing.

If it shan’t dance to your flute, reboot

Anti Commercial AI thingyCC BY-NC-SA 4.0

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • php@programming.dev
  • meta
  • Macbeth
  • All magazines