Ubuntu 安装 Linux, Apache, MySQL,PHP 指南

  1. 更新 apg-get 包列表
sudo apt-get update
  1. 安装 Apache
    在终端输入以下命令
sudo apt-get install apache2

安装完成后,可以访问 IP 查看,一般是默认的 Apache 页面,如下图所示:


Ubuntu 安装 Linux, Apache, MySQL,PHP 指南_第1张图片
apache-ubuntu.png
  1. 安装 MySQL
    接下来使用 apt 命令安装 MySQL,这次我们还会顺便安装一些有用的包
sudo apt-get install mysql-server php5-mysql

当弹出如下图所示的提示框,输入并确认 MySQL root 账户的密码


Ubuntu 安装 Linux, Apache, MySQL,PHP 指南_第2张图片
set-password-for-mysql-root.png
  • 创建 MySQL 数据库目录结构
sudo mysql_install_db
  • 运行基本 MySQL 安全脚本
sudo mysql_secure_installation

出现如下提示时,输入 MySQL 密码

MySQL root password prompt:
Enter current password for root (enter for none):
OK, successfully used password, moving on...

接下来会询问是否要更改当前 root 账户的密码,这里选择的是 n,即不更改 root 账户的密码

MySQL root password prompt:
Change the root password? [Y/n] n

接下来的弹窗都可以选择默认值,直接输入回车即可。最后会提示配置成功

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
  1. 安装 PHP
    我们可以再次使用 apt 命令获取 PHP。
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

安装完成后需要配置 Apache。
首先打开 Apache 的 dir.conf,输入下面的命令即可

sudo nano /etc/apache2/mods-enabled/dir.conf

dir.conf 看起来是下面这样的:

 
        DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

我们想把 PHP index 文件移到 DirectoryIndex 之后的第一个位置,这样 dir.conf 文件就变成了下面这样

 
        DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

修改完成后保存并关闭文件。然后需要重启 Apache Web Server 确保刚才的改动生效。

sudo service apache2 restart
  1. 在 Web Server 上测试 PHP 程序
    使用如下命令在 /var/www/html 目录下创建一个基本的 PHP 测试脚本
echo '' | sudo tee /var/www/html/info.php

接下来可以在 Web 浏览器里打开 PHP 脚本,将 your_server_IP_address 替换为自己的公网 IP

Visit in a web browser:
http://your_server_IP_address/info.php

如果你看到的页面如下图所示,说明 PHP 程序可以正常工作。


Ubuntu 安装 Linux, Apache, MySQL,PHP 指南_第3张图片
default_php.png

最后删除测试用的 PHP 脚本

sudo rm /var/www/html/info.php

参考链接:
https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-14-04

你可能感兴趣的:(Ubuntu 安装 Linux, Apache, MySQL,PHP 指南)