Apache shows PHP code instead of executing it

前端 未结 26 1602
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 02:23

I have recently been trying to install PHP and Apache on my computer. After many hours, they\'re installed. I have modified the httpd.conf and php.ini files like everyone sa

相关标签:
26条回答
  • 2020-11-22 03:14

    This was happening to me also when running $_POST in MAMP. All of my .ini and httpd files were all set up correctly. If you are doing form handling and you have an html document and posting the info to a php formhandler running $POST, make sure that you are running the html file from localhost via your server, and not just locally.

    This was a shortcut I did to run html documents, by just clicking the html file in my directory and launching in my web browser, when in reality to check if php is being processed in your form, you must run your html through your servers. A very simple protocol that I overlooked.

    Example:

    Wrong: file:///Applications/MAMP/htdocs/form/form.html

    Right: http://localhost:your port number/form/form.html

    Now the php should be processed once you click your submit button

    0 讨论(0)
  • 2020-11-22 03:14

    This solution worked for me. I purged apache2 and reinstall. It happened after purge and install. If it is the first install, you would not face this problem.

    0 讨论(0)
  • 2020-11-22 03:17

    For PHP 7 (May apply to previous versions as well), but I had to do this:

    Add this to the bottom of /etc/apache2/apache2.conf

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

    Run this from the terminal:

    sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7
    

    Then don't forget to restart Apache so it knows you changed stuff:

    sudo service apache2 restart
    

    This is a summary from: https://www.atlantic.net/community/howto/try-php7-lamp-ubuntu-14-04/

    0 讨论(0)
  • 2020-11-22 03:17

    Thanks to others on this thread for their suggestions. Following the steps mentioned I found that the apache server was not able to start reporting a syntax error in a load file in /etc/apache2/mods-enabled. Turns out that both php7.0 and php7.1 were enabled.

    a2dismod php7.0
    systemctl restart apache2
    

    and php is rendered correctly again.

    0 讨论(0)
  • 2020-11-22 03:18

    Apache shows php code instead of executing Issue fixed

    1. Opened php5.6 conf or php7.x conf

    # following command:

    $ sudo vi /etc/apache2/mods-enabled/php5.6.conf

    2. Commented following lines

    3. Restarted the server

    $ sudo service apache2 restart

    4 Enjoy :)

    0 讨论(0)
  • 2020-11-22 03:19

    Posting what worked for me in case in helps someone down the road, though it is an unusual case.

    I had set a handler to force my web host to use a higher version of php than their default. There's was 5.1, but I wanted 5.6 so I had this:

    <FilesMatch \.php$>
        SetHandler php56-cgi
    </FilesMatch>
    

    in my .htaccess file.

    When trying to run my site locally, having that in there caused php code to be output to the browser. Removing it solved the problem.

    0 讨论(0)
提交回复
热议问题