如何在 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
How to start with PHP on Ubuntu
提问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 LAMP
installed. Do a sudo tasksel
and 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。我遵循了这些步骤,它对我有用。
- Execute
sudo su
on the terminal. - Enter your password
- Execute
sudo subl /etc/apache2/sites-available/000-default.conf
on your terminal to open this file. Note you can change the subl to any text editor to open the file e.gsudo nano /etc/apache2/sites-available/000-default.conf.
- Change
DocumentRoot /var/www/html
to/home/user/yoursubdir
- Save the file and close it.
- Execute
sudo subl /etc/apache2/apache2.conf
on your terminal to open this file. Add the following to end of the file
<Directory /home/user/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
Save and Close the file.
- Execute
sudo service apache2 restart
- Go to your browser, type the URL of your script e.g
127.0.1.1/directory/document.php.
sudo su
在终端上执行。- 输入您的密码
sudo subl /etc/apache2/sites-available/000-default.conf
在终端上执行以打开此文件。请注意,您可以将 subl 更改为任何文本编辑器以打开文件,例如sudo nano /etc/apache2/sites-available/000-default.conf.
- 更改
DocumentRoot /var/www/html
为/home/user/yoursubdir
- 保存文件并关闭它。
sudo subl /etc/apache2/apache2.conf
在终端上执行以打开此文件。将以下内容添加到文件末尾
<Directory /home/user/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
保存并关闭文件。
- 执行
sudo service apache2 restart
- 转到您的浏览器,输入您的脚本的 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/