以下是在 Ubuntu 16.04 Xenial Xerus 安装 Apache, MySQL 及 PHP 的步骤:
开始安装前, 先更新 repositories
更新 repositories 后可以开始安装 Apache, MySQL 及 PHP.
安装 Apache
在 Ubuntu 安装 Apache, 可以很简单地透过 apt-get 完成:
完成安装后, 可以在浏览器输入服务器的位置试试, 例如 http://localhost, 如果可以看到 “Apache2 Ubuntu Default Page” 的页面, 那便表示成功安装了 Apache.
安装 MySQL Server
安装 MySQL 同样可以用 apt-get 完成, 执行以下指令安装 MySQL Server:
安装时 MySQL 会询问要设置的 root 新密码, 或者可以执行 mysql_secure_installation 修改:
安装 PHP
以下指令除了会安装 PHP 外, 还会安装一些常用的 PHP 套件:
安装好 PHP 后, 需要修改 Apache 的设定, 因为 Apache 默认会先使用 index.html 作为 index file, 将这个改为先使用 index.php, 开启 Apache 的设定档:
由面会看到类似这一行:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
把 index.php 放到最前方的位置, 即这样:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
储存盘案后离开, 然后需要重新启动 Apache 才会生效:
要测试 PHP, 可以放一个简单的 PHP Script 到 Apache DocumentRoot 目录, 这里:
然后输入以下几行程式码:
|
1 2 3 |
<?php phpinfo(); ?> |
然后在浏览器看看 http://localhost/info.php, 如果看到 PHP 的设定资料便表示安装完成了。