help with getting php working with apache?

Really embaressed to ask this question but ive spent the last 6 hours trying to get this working without luck.

I'm new(ish) to freebsd. Primary knowledge is Windows but im also ok with Linux. Ive just installed FreeBSD 15.0 with KDE/Plasma over the top to experiment with. I've run cut down FreeBSD packages for the last several years (PFSense / XigmaNAS) but im hoping to transition to a full install as I keep hitting boundaries with those.

In short, I cant get .php files to render correctly. Apache is up and running (Apache24). I can hit basic html pages locally.

Ive installed php through package manager, as well as targetting specific versions like php85.

I've made sure both apache and php are running as services

I've added the following in httpd.conf

LoadModule php_module modules/libphp.so

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

i've tried

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

but no matter what ive tried it either renders the file as plain text, or tries to open it in an external program

1766285503644.png


Any help greatly appreciated.... going a bit nuts here. Sure ive missed something simple, but for the life of me cant work out what that is. Spent hours trying different things from google but none of them have worked.
 
Ive installed php through package manager, as well as targetting specific versions like php85.

I've made sure both apache and php are running as services

I've added the following in httpd.conf

LoadModule php_module modules/libphp.so
The package you need is not (only) lang/php85 (language itself) - you need to install www/mod_php85 (the Apache module for PHP 8.5). Installing that package explains all necessary steps to get it up and run, and also informs you:
Code:
[activating module `php' in /usr/local/etc/apache24/httpd.conf]
Now I see that line in my httpd.conf:
Code:
LoadModule php_module         libexec/apache24/libphp.so
Differs from what you've added. Don't forget to restart Apache ( service apache24 restart).
 
Apologies it was mod_php85 I installed, plus I installed all the addons i could find.

have that line in the httpd.conf (sorry I transposed it incorrectly the first time) but I still get the same thing. It just tries to load the contents of the php file.

both apache24 and php_fpm have been restarted and are running.
 
Code:
You should add the following to your Apache configuration file:

<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>
That is part of the information you get on installing www/mod_php85. Add both as suggested with quotes (and not one of it vice versa without quotes), and restart Apache. And as you're firm with Linux and new to FreeBSD: You noticed that the httpd.conf file isn't located in /etc, but in /usr/local/etc?
 
Thanks for your help, but ive given up for the moment. I had those lines in there as well but I just couldnt get it working. In the end, i ended up giving up on apache and running nginx... As soon as i switched it started loading. So i assume the problem was with the Apache side, but for the moment I dont really understand why.
 
we don't really even know what you tried... usually the reason is because apache no longer is the motor for php and you need to use php_fpm but we never saw your httpd.conf I didn't see your httpd.conf the thing about switching the problem as I see it is that I would worry about ok this failed now that works but then later on ok there's a difficulty in the next system or a limitation was it really worth saving that effort... plus I feel the struggle does help me learn, nevertheless there are things I did have to give up on and somehow now I am using like wireguard or other projects like dns name server, internet net news, some never materialized using rest apis, some are in the works voip faxing, some were working but ended up abandoning anyways, some I need to get back to ie. ampache, good luck, etc..
 
pkg install mod_php84

Consider switching to php-fpm and mod_fast_cgi as per Apache httpd project
recommendation. See https://cwiki.apache.org/confluence/display/HTTPD/PHP-FPM
Note i don't use php-fpm
An alternative is ap24-mod_fastcgi which i don't use

Changes to Apache configuration file httpd.conf :

LoadModule php_module libexec/apache24/libphp.so

<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>

------------------------------------------------------------------------------------
# service apache24 restart
# apachectl -M | grep php
------> php_module (shared)
 
pkg install mod_php84

Consider switching to php-fpm and mod_fast_cgi as per Apache httpd project
recommendation. See https://cwiki.apache.org/confluence/display/HTTPD/PHP-FPM
Note i don't use php-fpm
An alternative is ap24-mod_fastcgi which i don't use

Changes to Apache configuration file httpd.conf :

LoadModule php_module libexec/apache24/libphp.so

<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>

------------------------------------------------------------------------------------
# service apache24 restart
# apachectl -M | grep php
------> php_module (shared)
So I tried your directions and the line

LoadModule php_module libexec/apache24/libphp.so

gives an error. I put the rest in the httpd.conf file and started apache. Eh, anyway, localhost is working with php on my machine. I dunno. I have an apache24 server running on my laptop and it works very well. Perhaps OP can give your suggestion a try as well.
 
I suspect Alain De Vos was on the right track.

I'm sure it's going to be one thing missed, or it's a difference in the newer versions the old guides don't cover.

I followed numerous guides / forum posts and couldnt get Apache working. They basically all said the same things. I rolled back to fresh freebsd about a half dozen times. No luck.

Some of the guides i followed:


The guide that finally worked for me was https://docs.vultr.com/how-to-install-nginx-mysql-php-femp-stack-on-freebsd-14-0 . Literally a 5 - 10 min job. Worked first time. At this point of time I dont understand enough to know if I should be on Apache.. I'm running a simple internal facing php site (or trying to) to automate some things. Does not need to be performant, and as nginx is working I dont currently feel inspired to spend several hours trying to get apache working (though I accept I may at some stage find there is good reason to).

I suspect a guide that might work is the sister one to the one that worked above for nginx (https://docs.vultr.com/how-to-install-apache-mysql-php-famp-stack-on-freebsd-14-0). It has a section on mod_proxy and mod_proxy_fcgi the other guides didnt have. That may well have been the missing link I needed. That and the step below (which didnt appear in the other guides). This all seems to align with the fpm version requirements. Posting it here in case it helps others, though admittedly I have not verfied it personally.

1766358780256.png
 
#LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so
LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so
#LoadModule mpm_worker_module libexec/apache24/mod_mpm_worker.so

try the command service php_fpm start
 
also check the permissions on the php files make sure they are only executable and not readable to everyone... you're not giving us a lot to work with...
 
So I tried your directions and the line

LoadModule php_module libexec/apache24/libphp.so

gives an error. I put the rest in the httpd.conf file and started apache. Eh, anyway, localhost is working with php on my machine. I dunno. I have an apache24 server running on my laptop and it works very well. Perhaps OP can give your suggestion a try as well.
When using this forum try to be as specific as possible. It helps us and you wasting time. Which error ? Why ? Strange ? Explination ? There are many ways to do php on apache, we don't know your setup , we don't now your httpd.conf & subdirectories. If php works now, fine but it would be nicer to know why it works. Tomorrow you can fall into problems and have no idea how to fix it.
 
When using this forum try to be as specific as possible. It helps us and you wasting time. Which error ? Why ? Strange ? Explination ? There are many ways to do php on apache, we don't know your setup , we don't now your httpd.conf & subdirectories. If php works now, fine but it would be nicer to know why it works. Tomorrow you can fall into problems and have no idea how to fix it.
Oh, I just installed php84 and apache24 and dropped your suggested text in the conf file. Let me check the error.

Syntax error on line 547 of /usr/local/etc/apache24/httpd.conf: Cannot load libexec/apache24/libphp.so into server: Cannot open "/usr/local/libexec/apache24/libphp.so"

At that point I just removed the line and restarted the server. I didn't even try and fix it. :D

Like I said before though, I have an apache24 server configured on my laptop and I can't even remember all the changes I made or php services I have running. But I thought I would just do a simple install and copy paste of your suggestion and post the results. Next time I'll copy paste any errors as well. Sorry!

EDIT: I'm a dumb dumb sometimes. I didn't install mod_php84. I just did and the line doesn't generate and error anymore because the service is now installed. My bad.
 
ive given up for the moment. I had those lines in there as well
I see typos in your lines (that's why I pointed you to "quotes"). The next thing I would have asked you was the output of
grep -ri php /usr/local/etc/apache24/… but anyway, if you're now happy with nginx - there I would have no clue how to get PHP up and running ;)
 
In the end, i ended up giving up on apache and running nginx... As soon as i switched it started loading. So i assume the problem was with the Apache side, but for the moment I dont really understand why.
I switched from Apache after seeing how nginx had PHP separate :D (sounded better to not have webserver and PHP handling on same server for multithreading)

I can't find notes and haven't tried it on FreeBSD, but years ago had PHP working with Apache 2 on Linux without separate FPM.
 
I think if you are using mod_php then it's an install and a config file tweak. So maybe the confusion is because there are options with Apache and PHP: mod_php or php-fpm.

mod_php is definitely deprecated but simple enough (from my experience) to set-up, once you know how.

nginx I think only has php-fpm, so less to get confused about where looking for help - there's only one option.

But there are plenty of choices, so whatever works.
 
Back
Top