Apache shows PHP code instead of executing it

前端 未结 26 1573
爱一瞬间的悲伤
爱一瞬间的悲伤 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:07

    For PHP7 and Apache2.4, this is all you need to do:

    sudo vim /etc/httpd/conf/httpd.conf
    

    Go to the very bottom and insert the following (all by itself):

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

    Then, restart Apache to load the new configuration:

    sudo service httpd restart
    

    Apache will now execute all HTTP/S requests with PHP. If all you have are html, css, and php files, then this will work fine for you. If you have other languages running, then you'll need to tailor the file matching to your situation. I've tested this with phpMyAdmin and it works fine.

    None of that other stuff all the people are talking about here was necessary for me. When I put the "AddType Application....." thing in the correct spot, Apache just told me that that module was already loaded and skipped it.

    More information can be found here: https://httpd.apache.org/docs/2.4/mod/core.html#filesmatch

    My install was done as follows:

    sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd
    

    You should also read this, if you haven't: https://httpd.apache.org/docs/current/howto/htaccess.html#when

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

    What worked for me:

    In active httpd.conf, find

    <IfModule mime_module>
    ...
    </IfModule>
    

    It was missing the following

    AddType application/x-httpd-php .php
    AddHandler application/x-httpd-php .php
    

    After restarting apache, .php files are correctly parsed.

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

    Run Xampp (apache) as administrator. In google chrome type:

    localhost/<insert folder name here>/<insert file name>

    i.e. if folder you created is "LearnPhp", file is "chapter1.php" then type

    localhost/LearnPhp/chapter1.php

    I created this folder in the xampp folder in the htdocs folder which gets created when you download xampp.

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

    You must enable php! Check the folder mods-enabled in the Apache directory (default: /etc/apache2/) to see if you find a file named php. I don't remember the extension but I think it's .so.

    Also check in /var/log/apache2/error.log to see if you have any other errors.

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

    If you are a ubuntu user, after installing apache must run the following command in fresh installation

    sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
    

    In my case works fine.

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

    Add following configuration to /etc/apache2/apache2.conf

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

    And restart the apache via sudo service apache2 restart

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