index.php 在 Apache2 伺服器上顯示為列表

index.php 在 Apache2 伺服器上顯示為列表

我的作業系統是 Ubuntu 22.04

我先安裝 php 8.1

然後我安裝了 Apache/2.4.52 (Ubuntu) 伺服器

然後我添加了以下內容:

sudo apt-get install -y php php-cli php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath libapache2-mod-php

指令:php -v,產生:

PHP 8.1.2-1ubuntu2.8 (cli) (built: Nov  2 2022 13:35:25) (NTS)

然後我修改了:

/etc/apache2/mods-enabled/dir.conf: DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

我重新啟動了apache2伺服器。

我添加了一個index.php文件,但是當我嘗試訪問該網頁時,我只看到index.php的列表

<?php
echo 'This is a test';

我那裡還有一個index.html 檔:

<h1>This is a test</h1>

當我刪除index.php並返回網頁時,我看到

This is a test

正如預期的那樣,採用大粗體字體。

PHP 手冊(以及我在這個網站上找到的答案)引用了一個 httpd.conf 文件,並希望我添加:

LoadModule php_module modules/libphp.so

但我在 Ubuntu 22.04 上沒有 httpd.conf,我在 /etc/apaache2 資料夾中有一個 apache2.conf,這是一個從各個子資料夾載入所有內容的框架檔案。

當我嘗試時:

sudo a2enmod php

我有:

ERROR: Module php does not exist!

有任何想法嗎?

編輯:

我做了:

cd /etc/apache2
grep -R php

這就是我得到的:

sites-available/default-ssl.conf:       <FilesMatch "\.(cgi|shtml|phtml|php)$">
sites-available/default-ssl.conf.original:      <FilesMatch "\.(cgi|shtml|phtml|php)$">
mods-enabled/dir.conf:  DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
mods-available/dir.conf:    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

並做:

sudo a2enmod php8.1

產生相同的錯誤:

ERROR: Module php8.1 does not exist!

然後 grep 查找已安裝的軟體包,我得到:

libapache2-mod-php8.1/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed,automatic]
libapache2-mod-php/jammy,now 2:8.1+92ubuntu1 all [installed]
php-bcmath/jammy,now 2:8.1+92ubuntu1 all [installed]
php-cli/jammy,now 2:8.1+92ubuntu1 all [installed]
php-common/jammy,now 2:92ubuntu1 all [installed]
php-curl/jammy,now 2:8.1+92ubuntu1 all [installed]
php-gd/jammy,now 2:8.1+92ubuntu1 all [installed]
php-mbstring/jammy,now 2:8.1+92ubuntu1 all [installed]
php-mysql/jammy,now 2:8.1+92ubuntu1 all [installed]
php-xml/jammy,now 2:8.1+92ubuntu1 all [installed]
php-zip/jammy,now 2:8.1+92ubuntu1 all [installed]
php8.1-bcmath/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-cli/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-common/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-curl/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-gd/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-mbstring/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-mysql/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-opcache/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed,automatic]
php8.1-readline/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed,automatic]
php8.1-xml/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1-zip/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 amd64 [installed]
php8.1/jammy-updates,jammy-security,now 8.1.2-1ubuntu2.8 all [installed,automatic]
php/jammy,now 2:8.1+92ubuntu1 all [installed]

相關內容