412

I'm trying to execute some PHP code on a project (using Dreamweaver) but the code isn't being run.

When I check the source code, the PHP code appears as HTML tags (I can see it in the source code). Apache is running properly (I'm working with XAMPP), the PHP pages are being opened properly but the PHP code isn't being executed.

Does someone have a suggestion about what is happening?

Note: The file is already named as filename.php

Edit: The Code..:

<?
include_once("/code/configs.php");
?>

The print

TylerH
  • 20,799
  • 66
  • 75
  • 101
Gui
  • 9,555
  • 10
  • 42
  • 54
  • 58
    Are you using short tags `` instead of ` – Dan Grossman Feb 25 '11 at 19:09
  • Do you get any results from phpinfo?(See example 1, http://php.net/manual/en/function.phpinfo.php) If you don't, you probably need to reconfigure apache. – amccormack Feb 25 '11 at 19:12
  • 41
    Don't EVER use short tags. ( `` ). They are deprecated, don't really work in a lot of places, and are otherwise completely unneccessary. Saving three keystrokes is not a valid reason to allow potential for your code to fail on probably half of the servers it may run on. – mopsyd Feb 11 '15 at 19:53
  • 6
    I am aware that short tags are not short echo tags, which is why I specified which I was talking about in the comment. At the time of writing, short tags were flagged for deprecation for the php 6 release, though that has apparently changed since. The problem still exists that a lot of servers have them disabled, which makes your code significantly less portable. This does not apply to short echo tags (`=`), which should run fine on php 5.4+ regardless of server settings. – mopsyd Mar 18 '16 at 00:44
  • Also taking a look at `tail -f -n 50 /var/log/apache2/error.log` helped me find the issue. – aderchox Sep 07 '20 at 09:28

36 Answers36

500

Sounds like there is something wrong with your configuration, here are a few things you can check:

  1. Make sure that PHP is installed and running correctly. This may sound silly, but you never know. An easy way to check is to run php -v from a command line and see if returns version information or any errors.

  2. Make sure that the PHP module is listed and uncommented inside of your Apache's httpd.conf This should be something like LoadModule php5_module "c:/php/php5apache2_2.dll" in the file. Search for LoadModule php, and make sure that there is no comment (;) in front of it.

  3. Make sure that Apache's httpd.conf file has the PHP MIME type in it. This should be something like AddType application/x-httpd-php .php. This tells Apache to run .php files as PHP. Search for AddType, and then make sure there is an entry for PHP, and that it is uncommented.

  4. Make sure your file has the .php extension on it, or whichever extension specified in the MIME definition in point #3, otherwise it will not be executed as PHP.

  5. Make sure you are not using short tags in the PHP file (<?), these are not enabled on all servers by default and their use is discouraged. Use <?php instead (or enable short tags in your php.ini with short_open_tag=On if you have code that relies on them).

  6. Make sure you are accessing your file over your webserver using an URL like http://localhost/file.php not via local file access file://localhost/www/file.php

And lastly check the PHP manual for further setup tips.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
shmeeps
  • 7,725
  • 2
  • 27
  • 35
  • 21
    Don't know why, but installer of PHP 5.3.15 doesn't add the line `AddType application/x-httpd-php .php`. Thanks a lot!! – MatuDuke Jul 25 '12 at 02:03
  • I need to enter the `AddType` mentioned but the file is read only. How can I overcome this and write a new line into it? – WebDevDanno May 22 '13 at 14:25
  • 8
    @shmeeps: Where have you seen that short tags are deprecated? I've done some searching and can only find recommendations against them, nothing about deprecation. Granted for applications which are to be distributed it is better to use – ClarkeyBoy Oct 28 '13 at 16:35
  • 3
    point no 6 worked for me.. local access doesn't use the apache server – KawaiKx Jul 06 '14 at 15:24
  • 9
    point 2 worked for me the safe way - `apt-get purge apache2; apt-get install apache2 libapache-mod-php5` – ulkas Sep 16 '14 at 20:41
  • @ClarkeyBoy you're correct. Short tags were not deprecated, I fixed the answer. – fregante Oct 04 '14 at 14:52
  • short tags weren't working here, (server 2008), need to research why, but at least I know – dangel Dec 27 '14 at 02:22
  • I checked `httpd.conf` and the line `LoadModule php5_module "c:/php/php5apache2_2.dll"` is missing. Isn't that weird or not? Anyway, should I added myself ? I also saw in other sites that you should uncomment line `LoadModule php5_module modules/libphp5.so`, is this something different? Windows user here. – giannis christofakis Dec 21 '15 at 21:11
  • Point #5 did the trick. Already had a complete website using short tags. – dnep Feb 17 '16 at 19:11
  • 3
    @full_prog_full Try uploading a file with a `.php` extension with the contents `` and see if accessing the file through a web browser results in "Success" being displayed without the associated PHP code. – shmeeps Apr 18 '16 at 03:29
  • There is no file called `httpd.conf` on my whole system. I searched with `find / -name "httpd.conf"` – Black Mar 07 '17 at 10:38
  • 2
    @ulkas in ubuntu 16.04, for apache2 and php7, I installed `sudo apt-get install libapache2-mod-php7.0` and it automatically configured my mods-enabled folder for php use. – coderatchet Mar 09 '17 at 00:07
  • Given the command line and web server installs of PHP are often independent, I don't think point 1 is very useful. Also, given the reactions of people who ask this FAQ, point 6 should probably be upgraded to point 1. – Quentin Aug 11 '17 at 09:32
  • What should I do if there is no `Apache's httpd.conf` @shmeeps – alper May 16 '18 at 17:10
  • I would clarify point #5, and mention that users should ensure their code starts with ` – Blue Mar 13 '19 at 13:26
  • Why is AddType set to "application/x-httpd-php" and not to "text/html"? The docs say AddType is to set the mime type for browser; so "text/html" would be right. – user2345998 Jul 03 '19 at 14:29
  • You could add `.html` text after `AddType application/x-httpd-php .php`. It would be `AddType application/x-httpd-php .php .html`. So we can run ` – Andi S. Nov 21 '19 at 03:34
  • point 3 was the missing component I needed. I recently upgraded to Mac OS Catalina, and I noticed that the `AddType application/x-httpd-php .php` line was not included in the `httpd.conf` file. – EnigmaTech Jun 22 '20 at 15:06
  • After upgrading from PHP 7.0 to 7.4 my PHP files were shown as text. Running `sudo apt purge libapache2-mod-php7.4` and re-installing the package fixed it for me. – Heinrich Ulbricht Oct 11 '20 at 12:35
  • In my case, I was passing to the cloud (Azure VM) an old code in PHP. While some of it was rendering correctly, some of it was not (I didn't get to see this until I read your post). The Bitnami LAMP installation runner does not include by default in the php.ini `short_open_tag=On`. I included it and that's all. Thank you very much. – Hugo L.M Dec 28 '20 at 17:37
  • Saved my day! Thanks! My problem was the missing MIME type – Konstantin Jan 09 '21 at 10:48
102

php7 :

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.0-fpm
sudo service apache2 restart
sj59
  • 2,072
  • 3
  • 22
  • 23
  • 41
    While this code snippet may solve the question, including an explanation of *how* and *why* this solves the problem [would really help](//meta.stackexchange.com/q/114762) to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Feb 06 '17 at 18:31
  • 7
    This answer is linux related the topicstarter is clearly using windows. – Raymond Nijland Apr 24 '17 at 11:53
  • 6
    @RaymondNijland true but this is canonical and I found this question and it saved me after over an hour of troubleshooting. – Goose May 05 '17 at 20:57
  • 1
    How and Why: This happens when PHP7 doesn't configure the default fast-CGI service in the make script. `sudo a2enmod proxy_fcgi setenvif` creates three new mods `/mods-available/proxy.conf proxy_fcgi.load proxy.load` Likewise, `sudo a2enconf php7.0-fpm` creates a config file `/conf-available/php7.0-fpm.conf` Once this is done and Apache2 can then run the PHP7 module as a fast-CGI service. – Talvi Watia Nov 21 '17 at 12:49
  • Please add an explaination – tmath Oct 12 '20 at 00:12
  • should be the best answer! – Richard Aguirre Mar 03 '22 at 22:04
  • Nobody cares about the explanation. Thanks pal. – Snake Jun 22 '22 at 22:41
  • the first line did it for me since I already did the equivalent of the 2nd line earlier in my attempts. The fact that this page is still useful after 11 years is a testament to to the most useful technical (or any other) website on the internet. – mooncaptain Jan 24 '23 at 23:49
39

I'm running Apache on Ubuntu and my issue was that the /etc/apache2/mods-available/php5.conf file was missing this:

<FilesMatch ".+\.ph(p[345]?|t|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>

I added it back in and php was parsing php files correctly.

Flow
  • 23,572
  • 15
  • 99
  • 156
Matt McDonald
  • 530
  • 6
  • 5
39

note for php 7 users, add this to your httpd.conf file:

# PHP 7 specific configuration
<IfModule php7_module>
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    <IfModule dir_module>
        DirectoryIndex index.html index.php
    </IfModule>
</IfModule>
EdC
  • 1,145
  • 10
  • 8
  • 6
    Just to help other people with this issue. The `httpd.conf` file is located in `/etc/apache2/httpd.conf` If you're using Ubuntu, it would be `apache2.conf` in the same directory that needs to be modified for PHP7. – Ahmed Sagarwala Aug 14 '18 at 19:07
23

I found another problem causing this issue and already solved it. I accidentally saved my script in UTF-16 encoding. It seems that PHP5 can't recognize <?php tag in 16 bit encoding by default.

Arrvi
  • 539
  • 6
  • 13
23

You're just opening your php file into browser. You have to open it using localhost url. if you open a file directly from your directory it will not execute the php code in any case.

use: http://locahost/index.php or http:127.0.0.1/index.php

Enable php short code. In your case, you are using <? which is php short code for <?php. By default php short codes are disabled.

Also use: sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt if you are a ubuntu user.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Hasan Baig
  • 491
  • 6
  • 17
  • 2
    Why is mcrypt required when i'm just including a file? You should noticed that this question is old and the short tags issue was mentioned 16 times, also in the top comment of my question. – Gui Dec 06 '16 at 16:20
  • 1
    I just share my solution, I was also facing the same error and after executing this command of mcrypt my issue solved. you may also try. – Hasan Baig Dec 07 '16 at 07:43
  • by default php short codes are enabled. short_open_tag=On (Default Value=ON) in php 5.6 – zond Jan 26 '17 at 13:38
  • This was my problem, my virtualmin/apache configuration was for 127.0.0.2 but i was putting my external ip address in the browser, i just updated my v/a config and now its working – Hayden Thring Jan 15 '19 at 20:14
  • I was just missing the libapache2-mod-php7.3 . – dxvargas Dec 07 '19 at 00:00
  • My code started working on Xampp on Ubuntu after I changed short_open_tag=On in php.ini. Excellent.... – Ashish Kumar Mondal May 07 '22 at 22:33
19

I'm posting this answer because my Virtualmin/Webmin admin interface decided it was a good idea to disable my PHP engine.. took me a while to find the solution, so I thought I'd share it with you guys:

Also, be sure to check that none of your website config files related to this specific host or virtualhost have any php_admin_value's in them that turn off PHP, like this:

php_admin_value engine Off

When in doubt, comment it...

# php_admin_value engine Off

And restart your webserver.

Henry van Megen
  • 2,159
  • 2
  • 23
  • 35
  • 1
    Where are these config settings exactly? – cronoklee Jul 27 '16 at 14:43
  • 2
    @cronoklee In vHost settings of httpd.conf. For existing hosts, you need to edit apache config in webmin. For future hosts, go to virtualmin > server template > apache website. Eitherway, this is not as secure as fastcgi - virtualmin's default. So the correct approach should be to troubleshoot why fcgi doesn't work, for example it may be caused by file permissions. – Sheepy Sep 09 '16 at 03:43
  • If you're using a version of Apache that doesn't have httpd.conf, look in `/etc/apache2/mods-enabled/php7.conf` or `php5.conf` – Sam Malayek Oct 20 '16 at 22:53
  • When in doubt about the location of a certain configuration setting, you can always use grep to find what you're looking for by (for example in this case) typing: `sudo grep php_admin_value /etc/* -R` – Henry van Megen May 23 '18 at 11:05
9

This just happened to me again, along with the server downloading html files, rather than processing. I had not use the webserver apache for some time on the computer and meanwhile Ubuntu updated like two more versions from originally installed LTS. Now it is

$ cat /etc/issue
Ubuntu 16.04 LTS

So the php worked after like so:

$ sudo apt-get install lamp-server^
$ sudo a2enmod php7.0
$ sudo service apache2 restart 

The webserver was now parsing the php. Maybe now got to update some webs since php7.0 now running where as it was before running php5. Oh well.

ndasusers
  • 727
  • 6
  • 12
8

I know it should sound silly... but seldom it happens.

Check if you are trying to execute php from

**http://localhost/info.php**

and not from

file:///var/www/info.php

ps> you can notice that if you write from shell

php info.php 

it answer with the code (it means php functions)..

user3257072
  • 91
  • 1
  • 1
6

I've solved this by uninstalling XAMPP, and installing WAMP. Thanks for the help.

Dummy00001
  • 16,630
  • 5
  • 41
  • 63
Gui
  • 9,555
  • 10
  • 42
  • 54
6

In case we are in the same page do following

sudo apt-get install php -y sudo apt-get install php-{bcmath,bz2,intl,gd,mbstring,mysql,zip,fpm} -y

To enable PHP 7.2 FPM in Apache2 do:

a2enmod proxy_fcgi setenvif

a2enconf php7.2-fpm

update 2: Apache downloads .php file instead of rendering

After that, I faced above issue. There are similar questions like this.

I don't know why but it only happened for my .php files in /var/www/html/ root folder. everything was ok for sub-directories. (for example wordpress and phpmyadmin worked fine)

So here is my solution. I decided to enable php module. so I ran this command:

a2enmod php7.2

but I got this errors:

Considering dependency mpm_prefork for php7.2: Considering conflict mpm_event for mpm_prefork: ERROR: Module mpm_event is enabled - cannot proceed due to conflicts. It needs to be disabled first! Considering conflict mpm_worker for mpm_prefork: ERROR: Could not enable dependency mpm_prefork for php7.2, aborting

so I decided to disable mpm by running following commands:

sudo a2dismod mpm_prefork
sudo a2dismod mpm_worker
sudo a2dismod mpm_event

then restart apache:

systemctl restart apache2

then enable php7.2 (my installed version):

sudo a2enmod php7.2

and right now everything works fine.

AmirRezaM75
  • 1,030
  • 14
  • 17
5

Make sure the script contains

<?php

before the code that should be executed. There should be no space between <? and php in this.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Solutions like this are SO important! Sure, it's basic, but some of us only dip into php occasionally and forget stuff like this. And the Big Dogs (in my case, the people who write the Google Sheets API docs) sometimes omit this in their "quickstart" demos. THANK YOU, @Barmar! – Tim Erickson Mar 30 '22 at 18:55
5

I faced this issue on php 7.1 that comes with High Sierra (OS X 10.13.5), editing /etc/apache2/httpd.conf with following changes helped:

  1. Uncomment this line

    LoadModule php7_module libexec/apache2/libphp7.so
    
  2. Paste following at the end

    <IfModule php7_module>
        AddType application/x-httpd-php .php
        AddType application/x-httpd-php-source .phps
    
        <IfModule dir_module>
            DirectoryIndex index.html index.php
        </IfModule>
    </IfModule>
    
AamirR
  • 11,672
  • 4
  • 59
  • 73
  • Like same can you pls suggest Wamp 3 version same error. There is no /etc/apache2/httpd.conf folder path in wamp 3. @AamirR – Gem Jan 09 '19 at 07:00
  • This worked for me!!! Mine was a more strange issue. Some files rendered the PHP and others did not and just displayed the code. – sjw0525 Oct 14 '19 at 17:40
4

on my ubuntu 14.04, apache 2.4, php 5.5.9 install, I tried with a sample.php on /var/www/html (default document root) and it worked ok. So the problem was on my Virtual Servers config. The solution was to include, on the Directory def containing the .php, this line:

    php_admin_flag engine on
Rogelio Triviño
  • 5,961
  • 3
  • 22
  • 17
4

Another possible cause of this problem could be that you are trying to run the script in a "user directory" from the UserDir module. Running PHP scripts in user directories is disabled by default. You will run into this problem if the script is in the public_html directory in your home folder and you are trying to access it from http://localhost/~your_username.

To fix this, open up /etc/apache2/mods-enabled/php7.2.conf. You must comment or delete the tag block at the bottom that reads

<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
mevers303
  • 442
  • 3
  • 10
3

Check all the packages you have installed for php using:

yum list installed | grep remi

Install all relevant php packages, especially php-devel on your machine.

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
3

i had similar problem but in my case solution was different. my file that held php code was called "somename.html" changed it to "somename.php" worked fine

Netrus
  • 79
  • 10
  • This also solved it for me. Does anyone know if the sever will execute php in a .html file or does the file ending always have to be changed to .php when there's a line of php to be executed? – full_prog_full Nov 10 '16 at 14:42
  • depends upon the server settings, for most cases php code that existing in anyfile.html will execute. and if it does not, it can be changed, although idk how :P – Netrus Dec 04 '16 at 16:49
3

For fresh setup of LAMP running php 7 edit the file /etc/httpd/conf/httpd.conf Note: make sure to make backup for it before changing anything.

Paste this at the very bottom of the file:

<IfModule php7_module>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

Then, search for LoadModule and paste the following line:

LoadModule php7_module modules/libphp7.so

This line will simply ask httpd to load the php 7 module

Then restart httpd

Eric Libay
  • 101
  • 8
3

Add AddType application/x-httpd-php .php to your httpd.conf file if you are using Apache 2.4

Ashahmali
  • 113
  • 7
3

In my case the php module was not loaded. Try this:

  1. Check which modules are loaded: apache2ctl -M. Look for module like php7_module (shared)
  2. If no php module is listed, then try to load the module that corresponds to your php version. In my case the php packet is libapache2-mod-php7.3. So I did: a2enmod php7.3 and the problem was solved.
Pekov
  • 527
  • 1
  • 9
  • 23
3

Easiest way to install Apache + php7 tested using Debian 10:

apt-get update -y
apt-get install apache2 php7.0 libapache2-mod-php  -y
sudo service apache2 restart
Leonardo Rivera
  • 174
  • 1
  • 5
2

This was in my .htaccess

DirectoryIndex index.html index.htm

index.html contained PHP code. By default, PHP won't process files with extentions like htm* as PHP code.

You can override this, by adding the following to .htaccess:

<FilesMatch ".+\.html$">
    SetHandler application/x-httpd-php
</FilesMatch>
James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
2

If you have configuration like this:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot "/var/www/example.com"

    <FilesMatch "\.php$">
        SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</VirtualHost>

Uncomment next lines in your httpd.conf

LoadModule proxy_module lib/httpd/modules/mod_proxy.so
LoadModule proxy_fcgi_module lib/httpd/modules/mod_proxy_fcgi.so

It works for me

user3890355
  • 1,010
  • 13
  • 13
2

Try restarting the apache server. This was the mistake I had made - forgetting to restart the server after installing php!

service httpd restart
Wilfredo
  • 137
  • 1
  • 9
2

For php7.3.* you could try to install these modules. It worked for me.

sudo apt-get install libapache2-mod-php7.3

sudo service apache2 restart
surazzarus
  • 772
  • 5
  • 17
  • 32
1

I think the problem that it is showing code instead of the result is that it is not going to local host . recheck what address u r going in. are u going to a local file directory or to the local host.

from the screenshot u sent it is going to ur computer not to the localhost.

"file:/// " it should be "localhost/"

Nav
  • 71
  • 9
1

Reinstalling the mcrypt module worked for me.

$sudo apt-get install php5-mcrypt
$sudo php5enmod mcrypt
b.g
  • 411
  • 1
  • 3
  • 19
  • It honestly did, maybe because I hadn't configured the PHP properly on my system.But, since the question I asked regarding the same issue has been marked as a duplicate to this question, I only could answer it here. I wonder why the down vote. – b.g Oct 17 '16 at 13:06
  • This answer is linux related the topicstarter is clearly using windows – Raymond Nijland Apr 24 '17 at 11:55
1

I had a case that I accidentally started untaring my files directory in root. It added the .htaccess file from my files folder that would block all php

# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
  php_flag engine off
</IfModule>

Bottom line check the .htaccess file on root.

albertski
  • 2,428
  • 1
  • 25
  • 44
1

Just spent hours of trying to get PHP 5 to run with Apache 2.4 on Windows 10. Finally for me it was a typo in httpd.conf LoadModule. Drew attention to writing and exact module path through the last answer in this apachelounge thread of denny2018. Thank you!

After two nights I discovered... My directory was written c: (lower case)

I had LoadModule php5_module "c:/php/php5apache2.dll" but correct for apache 2.4 is:

LoadModule php5_module "C:/php/php5apache2_4.dll"

So I also missed the _4 before (for apache 2.4). The full snippet that worked for me:

# PHP
LoadModule php5_module "C:/php/php5apache2_4.dll"
AddHandler application/x-httpd-php .php
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Just tried PHP 7. There the LoadModule line in httpd.conf for Apache 2.4 reads

LoadModule php7_module "C:/php/php7apache2_4.dll"

Currently php manual shows up c:/php/php5apache2.dll which of course needs to be adjusted.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
1

in my case (Apache/2.4.34),

after uncommenting the specific module

"LoadModule php7_module libexec/apache2/libphp7.so"

from

"/etc/apache2/httpd.conf"

my problem was gone.

sh6210
  • 4,190
  • 1
  • 37
  • 27
0

After trying everything above and nothing works, consider restarting your pc. Solved mine.

emmaakachukwu
  • 515
  • 1
  • 7
  • 16
0

It is possible to install phpmyadmin on raspberry 64 bit and it even was not difficult in my case.
I am running several hp-laptops with openSuse Tumbleweed and some Raspberries.
On the latest Raspberry 3 B + I 64bit raspbian buster light, kde plasma Desktop.
Linux raspi10 5.10.60-v8+ #1449 SMP PREEMPT Wed Aug 25 15:01:33 BST 2021 aarch64 GNU/Linux
I am using nginx 1.14.2 with fastcgi (not using 'sock') and php 8.0.10 (php-fpm).
I downloaded 'composer' first. (Use apt or aptitude for that). After that go to https://docs.phpmyadmin.net/en/latest/setup.html
Look for the shell command

composer create-project phpmyadmin/phpmyadmin

It will install a complete directory 'phpmyadmin' in the current directory.
Move that to /var/lib/ and set the user:group permissions according to your settings in nginx.conf
In my case:
chown -R www-data:www-data /var/lib/phpmyadmin

Create a symlink to /srv/www/public or /var/www/html or wherever your server is looking for stuff.
You can add a second symlink named phpMyAdmin to phpmyadmin
Make sure that the root instruction in nginx.conf is set accordingly.

My nginx configuration is (nearly) identical on all my machines. It is very compact right now.
To get started this is the easiest way. All further servers and features can be added later.
No entry in /etc/nginx/sites-enabled and nothing in /etc/nginx/vhosts.d
Start with just one pool in php.ini. To configure that, google a litlle and test by checking the status of php-fpm.

MikeOffenbach
  • 109
  • 1
  • 5
-1

PHP 8, Centos 7, Apache 2, Remi Repo

I've recently upgraded a centos server to use php 8.

After the upgrade php had stopped working and apache started serving me the php code as text.

Having spent a good while looking for which config setting had not updated I tracked it down to this:

in /etc/httpd/conf.d

<IfModule  mod_php7.c>

needed changing to:

<IfModule  mod_php.c>

There are other places that you may see the mod_php7 check that could probably be updated to the mod_php variant but this one definitely fixed it for me.

Don't forget to restart the server afterwards.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
DazBaldwin
  • 4,125
  • 3
  • 39
  • 43
-1

After trying everything I came up to this solution


This error appears when the PHP 7.0 FPM is not enabled by default.

To enable PHP 7.0 FPM in Apache2, run:

 sudo 2enmod proxy_fcgi setenvif
 sudo a2enconf php7.0-fpm
 systemctl restart apache2

If you have php7.4, enter 7.4 instead of 7.0 in the above commands.

ouflak
  • 2,458
  • 10
  • 44
  • 49
-1

Today, PHP comes with a built in web server, so no further installation should be needed, therefore one of the easiest way to render a PHP file for development usage would be:

  1. Open a terminal and change to the source directory which contains your web app (its main file is usually called index.php).
  2. Inside this directory, type php -S localhost:7777 (the port number is chosen arbitrary).
  3. Open a web browser an type in localhost:7777 in the address bar and the desired web page should be rendered.

If you have finished your visit, return to your terminal and press CTRL-C to shut down the development server.

This information is also given by invoking php --help in the terminal, further stuff is given at https://www.php.net/manual/en/features.commandline.webserver.php

Andi Hafner
  • 311
  • 2
  • 9
-2
sudo apt-get install apache2 php7.x libapache2-mod-php7.x
a2query -m php7.x
sudo a2enmod php7.x
sudo service apache2 restart

x- your php7 subversion

Almir
  • 5
  • 2
  • php7 is discontinued – Your Common Sense May 06 '23 at 17:42
  • 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 May 06 '23 at 19:52