
まず、私の仮想ホストの例は<? php echo 'Hello World'; ?>
に埋め込まなくても動作していますindex.html
。
Apache
私が使用しているのと同じマシン上にあります。PHP
このマシンでは 2 回使用できます。
$ 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"; ?>
内にこの行がなくても動作します。ただし、この行を埋め込むと、php の echo はエコーしません。index.html
次のようなものが必要ですか
LoadModule php7_module modules/libphp7.so
私の/etc/apache2/apache2.conf
?
この問題を解決するためのヒントがあれば歓迎します。
答え1
ファイルに埋め込まれた PHP コードを実行するには、そのファイルを実際に PHP インタープリタで実行する必要があります。デフォルトでは、拡張子が で終わるファイル.php
(および他のいくつかのファイル拡張子) のみが PHP インタープリタで送信されます。これは、デフォルトですべてのファイルを PHP インタープリタで送信するとパフォーマンスが低下し、場合によってはセキュリティや機能の問題が発生する可能性があるためです。
.html
ウェブサーバーに、 で終わるすべてのファイルをPHP インタープリター経由で送信するように指示するには、vhost 構成に次の行を追加する必要があります。
AddHandler application/x-httpd-php70 .html