如何在 Ubuntu 上开始使用 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5212877/
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-25 20:39:11  来源:igfitidea点击:

How to start with PHP on Ubuntu

phpubuntuide

提问by user130934

I am newbie in PHP. I have successfully installed PHP on Ubuntu, now I want start my first program. I am using gPHPEdit as IDE. Where should I save .php files that I create? And how to run/test them?

我是 PHP 新手。我已经在 Ubuntu 上成功安装了 PHP,现在我想启动我的第一个程序。我使用 gPHPEdit 作为 IDE。我应该在哪里保存我创建的 .php 文件?以及如何运行/测试它们?

采纳答案by Munim

You should pick up a book or start following some good tutorials on the web.
If you are just scripting using php, you can save them anywhere and run the php on the terminal using the php command line interpreter.
If you are trying write web scripts (and I think you are), you need to install and configure a web server (typically apache) and save your scripts in the server's document root (typically /var/www). Also, I highly recommend you to read up a little about servers and HTTP and figure out how all this works on the inside before learning to building websites in php.

您应该拿起一本书或开始学习网络上的一些优秀教程。
如果您只是使用 php 编写脚本,则可以将它们保存在任何位置并使用 php 命令行解释器在终端上运行 php。
如果您正在尝试编写 Web 脚本(我认为您是),则需要安装和配置 Web 服务器(通常是 apache)并将脚本保存在服务器的文档根目录(通常是 /var/www)中。另外,我强烈建议您在学习用 php 构建网站之前,先阅读一些关于服务器和 HTTP 的内容,并弄清楚所有这些在内部是如何工作的。

回答by rook

Make sure you have LAMPinstalled. Do a sudo taskseland select lamp then hit enter, its gotta be the most simple *amp install ever made. Its a good idea to install phpmyadmin: sudo apt-get install phpmyadmin. After that just copy the files to /var/www/and then they will show up on http://localhost. I recommended using Eclipse PDT or the Netbeans build for PHP.

确保您已LAMP安装。做一个sudo tasksel并选择灯然后按回车键,它必须是有史以来最简单的 *amp 安装。安装 phpmyadmin: 是个好主意sudo apt-get install phpmyadmin。之后只需将文件复制到/var/www/,然后它们就会显示在http://localhost. 我建议使用 Eclipse PDT 或 Netbeans 为 PHP 构建。

回答by favourworks

If you cannot save or copy to var/www/html, to run your php scripts on your browser. If you are using Ubuntu 14.04. I followed these steps and it worked for me.

如果您无法保存或复制到 var/www/html,请在浏览器上运行您的 php 脚本。如果您使用的是 Ubuntu 14.04。我遵循了这些步骤,它对我有用。

  1. Execute sudo suon the terminal.
  2. Enter your password
  3. Execute sudo subl /etc/apache2/sites-available/000-default.confon your terminal to open this file. Note you can change the subl to any text editor to open the file e.g sudo nano /etc/apache2/sites-available/000-default.conf.
  4. Change DocumentRoot /var/www/htmlto /home/user/yoursubdir
  5. Save the file and close it.
  6. Execute sudo subl /etc/apache2/apache2.confon your terminal to open this file.
  7. Add the following to end of the file

    <Directory /home/user/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory> 
    
  8. Save and Close the file.

  9. Execute sudo service apache2 restart
  10. Go to your browser, type the URL of your script e.g 127.0.1.1/directory/document.php.
  1. sudo su在终端上执行。
  2. 输入您的密码
  3. sudo subl /etc/apache2/sites-available/000-default.conf在终端上执行以打开此文件。请注意,您可以将 subl 更改为任何文本编辑器以打开文件,例如sudo nano /etc/apache2/sites-available/000-default.conf.
  4. 更改DocumentRoot /var/www/html/home/user/yoursubdir
  5. 保存文件并关闭它。
  6. sudo subl /etc/apache2/apache2.conf在终端上执行以打开此文件。
  7. 将以下内容添加到文件末尾

    <Directory /home/user/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory> 
    
  8. 保存并关闭文件。

  9. 执行 sudo service apache2 restart
  10. 转到您的浏览器,输入您的脚本的 URL,例如 127.0.1.1/directory/document.php.

I hope this helps.

我希望这有帮助。

回答by vinit kantrod

remove the index.html file from /var/www/

从中删除 index.html 文件 /var/www/

$ sudo rm index.html

create a new php file there:

在那里创建一个新的 php 文件:

$ sudo gedit /var/www/index.php

write in it:

写在里面:

<?php
print_r(phpinfo());
?>

Restart your Apache2 Server :

重启你的 Apache2 服务器:

$ sudo service apache2 restart

OR

或者

$ sudo /etc/init.d/apace2 restart

and point to yout localhost and /index.php

并指向你的本地主机和 /index.php

if err arises visit : http://www.allaboutlinux.eu/how-to-run-php-on-ubuntu/

如果出现错误,请访问:http: //www.allaboutlinux.eu/how-to-run-php-on-ubuntu/