PHP 无法在 Ubuntu Apache 服务器上运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43604755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:52:43  来源:igfitidea点击:

PHP is not working on Ubuntu Apache server

phpapacheubuntu

提问by Huy Vo

I just installed php and apache server on Ubuntu 17.04 but my .phpfile doesn't work on my browser.

我刚刚在 Ubuntu 17.04 上安装了 php 和 apache 服务器,但我的.php文件在我的浏览器上不起作用。

index.php:

索引.php:

<!DOCTYPE html>
<html>
<body>
<h1>Welcome!</h1>
<?php
  echo "Hello World!";
?>
</body>
</html>

Only Welcome!appeared on the browser, and when I inspect the page I see php code is being commented. I have already started apache server and still have no idea what is wrong. Please help me out, thank you!

Welcome!出现在浏览器上,当我检查页面时,我看到 php 代码正在被注释。我已经启动了 apache 服务器,但仍然不知道出了什么问题。请帮帮我,谢谢!

$ php -v

$ php -v

PHP 7.0.15-1ubuntu4 (cli) (built: Feb 28 2017 21:33:59) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.15-1ubuntu4, Copyright (c) 1999-2017, by Zend Technologies

$ sudo /etc/init.d/apache2 status

$ sudo /etc/init.d/apache2 status

apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Tue 2017-04-25 14:38:55 +07; 10min ago
  Process: 11578 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
  Process: 11604 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 11619 (apache2)
    Tasks: 55 (limit: 4915)
   Memory: 9.7M
      CPU: 429ms
   CGroup: /system.slice/apache2.service
           ├─11619 /usr/sbin/apache2 -k start
           ├─11663 /usr/sbin/apache2 -k start
           └─11664 /usr/sbin/apache2 -k start

Apr 25 14:38:55 g5080 systemd[1]: Starting The Apache HTTP Server...
Apr 25 14:38:55 g5080 apachectl[11604]: AH00558: apache2: Could not reliably determin…sage
Apr 25 14:38:55 g5080 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

采纳答案by belgacemfahmi

while you are in ubuntu 17.04

当你在 ubuntu 17.04

you can follow this steps

你可以按照这个步骤

sudo apt-get install apache2 php libapache2-mod-php7.0 mysql-server php-mbstring php7.0-mbstring phpmyadmin     //install
sudo service apache2 restart    //restart

and give it another try for your file(index.php)

再试试你的文件(index.php)

回答by vinay kumar

Check the below configuration in your web server

检查您的网络服务器中的以下配置

sudo apt-get install apache2 php5 libapache2-mod-php5`

It will install everything you need and will start the apache server with support for PHP.

它将安装您需要的一切,并将启动支持 PHP 的 apache 服务器。

To verify that the php module is loaded, type:

要验证 php 模块是否已加载,请键入:

   a2query -m php5

if not enabled, then load with:

如果未启用,则加载:

   sudo a2enmod php5

and restart apache:

并重新启动apache:

   sudo service apache2 restart

回答by Hrvoje Antunovi?

Trying this command actually worked for me

尝试这个命令实际上对我有用

sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-pear php-imagick php-imap php-mcrypt php-recode php-tidy php-xmlrpc

回答by eli

Maybe just a special case:
I wanted to directly call a php file inside a subdirectory of "/var/www/vhosts/.../somePage/index.php" (after restoring from an old VM). It wasn't possible, the index.php would just be downloaded

After moving the ".../somePage/" directory from "/var/www/vhosts/..." to "/var/www/" the index.php would run correctly (calling http://localhost/somedir/index.php)

也许只是一个特例:
我想直接调用“/var/www/ vhosts/.../somePage/index.php”子目录中的php文件(从旧VM恢复后)。这是不可能的,index.php 只会

在将“.../somePage/”目录从“/var/www/vhosts/...”移动到“/var/www/”索引后下载。 php 会正确运行(调用http://localhost/somedir/index.php