Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP PHP gets commented

hebrerillo

Active Coder
Hello there!

I had a local server in Apache and PHP that was working fine, but it has stopped working suddenly.

If I enter the URL "http://localhost/index.php", the PHP code gets commented out in the response:
Screenshot 2024-08-21 at 08.38.23.png

I searched in the web but found nothing. I read somewhere else that the PHP is not executed in my local server. Please, help me fix this.

I attach the file httpd.conf located in '/etc/apache2' folder.

My computer is MacOS Monterey, version 12.6.3

Thank you so much!!
 
Hello there!

I had a local server in Apache and PHP that was working fine, but it has stopped working suddenly.

If I enter the URL "http://localhost/index.php", the PHP code gets commented out in the response:
View attachment 2773

I searched in the web but found nothing. I read somewhere else that the PHP is not executed in my local server. Please, help me fix this.

I attach the file httpd.conf located in '/etc/apache2' folder.

My computer is MacOS Monterey, version 12.6.3

Thank you so much!!
Hi there.
Can you copy and paste the php code here? I want to eliminate any syntax errors
 
You said that it stopped working suddenly. I see that you're on a Mac. How did you install Apache and PHP? Did you use something like Homebrew to install, or did you install some other package?

Have you recently updated MacOS? I know updating MacOS can sometimes break packages installed for web development so it's possible the update somehow removed the associations between Apache and PHP.

Also, to rule out the obvious, your file has a .php extension, correct?
 
I don't know how macs work but, on linux you can do systemctl status apache2 in a terminal and find out if it's running.
Apache is running. I can access regular HTML files
You said that it stopped working suddenly. I see that you're on a Mac. How did you install Apache and PHP? Did you use something like Homebrew to install, or did you install some other package?

Have you recently updated MacOS? I know updating MacOS can sometimes break packages installed for web development so it's possible the update somehow removed the associations between Apache and PHP.

Also, to rule out the obvious, your file has a .php extension, correct?
Hello, sorry for the late response.

Yes, I am on a Mac. I am using the default Apache installation that comes with MAC OS.
To be honest, I do not remember how I installed PHP. It was long ago. Besides, I have another PHP installation via Homebrew that another coworker installed on my computer. So maybe they are having problems coexisting with each other.

Although they have been coexisting with each other for a long time. It's been recently that one of the PHP installations stopped working all of a sudden.

It is not a problem, I can use the Homebrew installation.

Thank you all for your help!
I
 
Apache is running. I can access regular HTML files
just because you can access HTML files does not mean Apcahe is running or correctly, Apache deals with PHP. You can run HTML without any programs 🙂

Try this:-

1. Ensure PHP is installed:
Open Terminal and run the following command to see if PHP is installed on your system:

Code:
   php -v

If it returns a version number, PHP is installed. If not, you'll need to install PHP.

2. Check Apache’s PHP module is enabled:
You need to make sure Apache is set up to handle PHP files. On a Mac, the Apache config file is typically located at /etc/apache2/httpd.conf. You can open it by running:

Code:
   sudo nano /etc/apache2/httpd.conf

Look for the following line:

Code:
   #LoadModule php_module libexec/apache2/libphp7.so

If it has a # in front of it, that means the PHP module is disabled. Remove the # to enable it, like this:

Code:
   LoadModule php_module libexec/apache2/libphp7.so

Save the file by pressing CTRL + X, then Y, and Enter to exit.

3. Restart Apache:
After making changes to httpd.conf, you need to restart Apache for the changes to take effect:

Code:
   sudo apachectl restart

4. Test again:
Now try going to http://localhost/index.php again. If everything is set up correctly, it should execute your PHP code and display the output (e.g., "hello") instead of the raw PHP code.

Let me know if this resolves the issue. 🙂
 
Hello @simong1993 , sorry for the late response

The file /etc/apache2/httpd.conf, does not contain any line like "#LoadModule php_module libexec/apache2/libphp7.so
"

I did a grep php_module and had no results. Maybe that is the problem??

Thank you so much for your help!
 
Oooo, I did some googling and found the issue!

Turns out macOS Monterey (version 12) removed the built-in PHP module from the system. That’s why your PHP code is being shown as plain text, and why the LoadModule php_module line is missing from your httpd.conf file.

Here’s how to fix it:

Step 1: Install Homebrew (If Not Already Installed)
Homebrew is a package manager that makes things super easy.

To install Homebrew, open Terminal and run:

Code:
 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Follow the instructions to finish the install.

Step 2: Install PHP Using Homebrew
For the latest PHP version, run:

Code:
 brew install php

If you need a specific version (like PHP 7.4):

Code:
 brew install [email protected]

You can verify it worked by running:

Code:
 php -v

Step 3: Configure Apache to Use Homebrew’s PHP Module
Open Apache's config file:

Code:
 sudo nano /etc/apache2/httpd.conf

Add this line to load the PHP module:

Code:
 LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so

To confirm your path, run:

Code:
 brew --prefix php

Make sure these lines exist to handle PHP files:

Code:
 <IfModule php_module>AddType application/x-httpd-php .phpDirectoryIndex index.php index.html</IfModule>

Save and exit: CTRL + O, Enter, CTRL + X.

Step 4: Restart Apache
Run:

Code:
 sudo apachectl restart

Step 5: Test Your PHP Setup
Now, go to http://localhost/index.php in your browser, and your PHP should work.

OR, try MAMP or XAMPP if you want an all-in-one package. They handle Apache, PHP, and MySQL for you.

Good luck! Let me know how it goes!
 
Hey! Sounds like an Apache configuration issue.It's often related to PHP module not being loadedCheck your httpd.conf file for this line:LoadModule php_module libexec/apache2/libphp7.so After adding or verifying it, restart ApacheThis should help Good luck!As a side note, also check if you have the latest PHP version installed and properly integrated with Apache. :blush:
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom