如何在ubuntu 16.04下的虛擬主機上為apache2啟用php7.0?

如何在ubuntu 16.04下的虛擬主機上為apache2啟用php7.0?

首先,我的虛擬主機範例無需<? php echo 'Hello World'; ?>嵌入index.html.

Apache與我使用的同一台機器上。PHP在這台機器上可以使用兩次。

$ which php
/usr/bin/php
$ which php7.0 
/usr/bin/php7.0

另外:libapache2-mod-php7.0已經安裝。

到目前為止我所做的:

$ sudo mkdir /var/www/www.virtualhost.com/
$ cd /var/www/www.virtualhost.com/
$ sudo cat > index.html
<html>
    <body>
        <h1>My virtualhost</h1>
        PHP Test:<br>
        <?php echo "PHP"; ?>
    </body>
</html>
ctrl+d
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-availble/virtualhost.com.conf

編輯我的virtualhost.com.conf文件後,它看起來像:

<VirtualHost *:80>

    ServerName www.virtualhost.com
    ServerAlias virtualhost.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/www.virtualhost.com
    DirectoryIndex index.html index.php

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined


    <Directory /var/www/www.virtualhost.com/>
            Options +Indexes -FollowSymlinks
            AllowOverride None
    </Directory>

    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

然後我啟用了我的網站,在我的/etc/hosts文件中創建了一個條目並apache2使用以下命令重新啟動:

$ sudo a2ensite virtualhost.com.conf
$ sudo apache2ctl restart

我的新條目/etc/hosts是:

127.0.0.1       www.testhost.com    
127.0.0.1       www.virtualhost.com

以下模組啟動:

$ a2query -m
authz_host (enabled by maintainer script)
proxy_fcgi (enabled by site administrator)
socache_shmcb (enabled by site administrator)
negotiation (enabled by maintainer script)
filter (enabled by maintainer script)
access_compat (enabled by maintainer script)
authz_core (enabled by maintainer script)
deflate (enabled by maintainer script)
authn_file (enabled by maintainer script)
php7.0 (enabled by maintainer script)
authz_user (enabled by maintainer script)
authnz_fcgi (enabled by site administrator)
mime (enabled by maintainer script)
proxy (enabled by site administrator)
mpm_prefork (enabled by site administrator)
dir (enabled by maintainer script)
alias (enabled by maintainer script)
auth_basic (enabled by maintainer script)
setenvif (enabled by maintainer script)
cgi (enabled by site administrator)
env (enabled by maintainer script)
autoindex (enabled by maintainer script)
authn_core (enabled by maintainer script)
status (enabled by maintainer script)
ssl (enabled by site administrator)

站點<?php echo "PHP"; ?>內沒有線路即可運作。index.html但是如果我嵌入這一行,php 的 echo 就不會回顯。

我需要類似的東西嗎

LoadModule php7_module modules/libphp7.so

在我的/etc/apache2/apache2.conf

歡迎任何解決此問題的提示。

答案1

為了執行檔案中嵌入的 PHP 程式碼,需要透過 PHP 解譯器實際執行該檔案。預設情況下,僅透過 PHP 解釋器發送以 結尾的檔案.php(以及一些其他檔案副檔名),因為預設會透過PHP 解釋器發送所有檔案會降低效能,並且在某些情況下可能會導致安全性或功能問題。

為了告訴您的網頁伺服器.html透過 PHP 解釋器發送所有以 結尾的文件,您應該將以下行添加到您的虛擬主機配置中:

AddHandler application/x-httpd-php70 .html

相關內容