Ubuntu Linux 安装配置LAMP环境

标签: ubuntulinuxterminalapachephpmyadminmysql
3375人阅读 评论(3) 收藏 举报

p { margin-bottom: 0.08in; }h1 { margin-bottom: 0.08in; }h1.western { font-family: "Times New Roman",serif; }h1.cjk { font-family: "DejaVu Sans"; }h1.ctl { font-family: "DejaVu Sans"; }h3 { margin-bottom: 0.08in; }pre.cjk { font-family: "DejaVu Sans",monospace; }a:link { }code.cjk { font-family: "DejaVu Sans",monospace; }

ubuntu Linux 安装配置 LAMP环境


PHP开发和服务器运行环境首选 LAMP 组合,即 Linux+Apache+MySQL+Php/Perl /Python,能最优化服务器性能。如何在本地电脑 Ubuntu 中安装和配置 LAMP环境? Ubuntu10.04本身就是基于 Linux内核,所以 Linux是现成的了。使用 Ubuntu LAMP Server软件包可以很简单地实现 Linux Apache MySQL Php的统一安装和配置,也不再需要一个一个来安装配置了。

Ubuntu环境下如何安装 LAMP组件

1,使用 Ubuntu新立得管理器
系统 ->系统管理 ->新立得软件包管理器 ->编辑 ->使用任务标记分组软件包 ->LAMP Server(勾选 )->确定 ->返回到上一个窗口点击应用 ( System->Administration->Synaptic Package Manager->Edit->Mark packages by Task->LAMP Server->OK)。然后系统会自动下载安装 lamp环境软件包,几分钟就下载搞定。安装过程中会要求设置 MySQL root帐号的密码,设置好了记住。另外当 Ubuntu系统升级时 lamp环境组件也会同时更新到最新版本。

安装完毕测试 :打开 Firefox浏览器在地址栏输入,显示 It works!表明 Apache服务器已经开始工作了, LAMP安装 也就这样完成了。


当然不使用 Gnome,使用终端命令也很简单:
直接一条命令 apt-get install apache2 mysql-server mysql-client php5 php5-gd php5-mysql


Install Apache

To start off we will install Apache.

1. Open up the Terminal (Applications > Accessories > Terminal).

2. Copy/Paste the following line of code into Terminal and then press enter:

sudo apt-get install apache2

3. The Terminal will then ask you for you're password, type it and then press enter.


Testing Apache

To make sure everything installed correctly we will now test Apache to ensure it is working properly.

1. Open up any web browser and then enter the following into the web address:


You should see a folder entitled apache2-default/. Open it and you will see a message saying "It works!" , congrats to you!


Install PHP

In this part we will install PHP 5.

Step 1. Again open up the Terminal (Applications > Accessories > Terminal).

Step 2. Copy/Paste the following line into Terminal and press enter:

sudo apt-get install php5 libapache2-mod-php5

Step 3. In order for PHP to work and be compatible with Apache we must restart it. Type the following code in Terminal to do this:

sudo /etc/init.d/apache2 restart


Test PHP

To ensure there are no issues with PHP let's give it a quick test run.

Step 1. In the terminal copy/paste the following line:

sudo gedit /var/www/testphp.php

This will open up a file called phptest.php.

Step 2. Copy/Paste this line into the phptest file:

<?php phpinfo(); ?>

Step 3. Save and close the file.

Step 4. Now open you're web browser and type the following into the web address:


nstall MySQL

To finish this guide up we will install MySQL. (Note - Out of Apache and PHP, MySQL is the most difficult to set up. I will provide some great resources for anyone having trouble at the end of this guide.)

Step 1. Once again open up the amazing Terminal and then copy/paste this line:

sudo apt-get install mysql-server

Step 2 (optional). In order for other computers on your network to view the server you have created, you must first edit the "Bind Address". Begin by opening up Terminal to edit the my.cnf file.

gksudo gedit /etc/mysql/my.cnf

Change the line

bind-address =

And change the to your IP address.

Step 3. This is where things may start to get tricky. Begin by typing the following into Terminal:

mysql -u root

Following that copy/paste this line:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpassword');

(Make sure to change yourpassword to a password of your choice.)

Step 4. We are now going to install a program called phpMyAdmin which is an easy tool to edit your databases. Copy/paste the following line into Terminal:

sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin

After that is installed our next task is to get PHP to work with MySQL. To do this we will need to open a file entitled php.ini. To open it type the following:

gksudo gedit /etc/php5/apache2/php.ini

Now we are going to have to uncomment the following line by taking out the semicolon (;).

Change this line:


To look like this:


Now just restart Apache and you are all set!

sudo /etc/init.d/apache2 restart


The End

Quick note to anyone who encountered problems with setting up the MySQL password, please refer to this page: MysqlPasswordReset

I applaud everyone who has taken the time to read this guide. This guide is also my first ever so I would love to hear back from the public on what you guys think! Just don't be too harsh. ;)

If you have questions about installing any part of LAMP just drop them in the comment box and I will do my best to help you out.

设置 Ubuntu文件执行读写权限

LAMP组建安装好之后, PHP网络服务器根目录默认设置是在: /var/www。由于 Linux系统的安全性原则,改 目录下的文件读写权限是只允许 root用户操作的,所以我们不能在 www文件夹中新建 php文件,也不能修改和删除,必须要先修改 /var/www目录的 读写权限。在界面管理器中通过右键属性不能修改文件权限,得执行 root终端命令: sudo chmod 777 /var/www 。然后就可以写入 html php文件了。

如何安装 phpmyadmin-Mysql 数据库管理

系统 ->系统管理 ->新立得软件包管理器 ->搜索 phpmyadmin->右键标记安装。
或直接使用一条命令: sudo apt-get install phpmyadmin 安装开始。

在安装过程中会要求选择 Web server apache2 lighttpd,选择 apache2,按 tab键然后确定。然后会要求输入设置的 Mysql数据库密码连接密码 Password of the database’s administrative user
然后将 phpmyadmin apache2建立连接,以我的为例: www目录在 /var/www phpmyadmin /usr/share /phpmyadmin目录,所以就用命令: sudo ln -s /usr/share/phpmyadmin /var/www 建立连接。

phpmyadmin测试 :在浏览器地址栏中打开 http://localhost/phpmyadmin

可能会显然如下错误: Cannot load mcrypt extension. Please check your PHP configuration.

解决方法: sudo apt-get install php5-mcrypt

sudo /etc/init.d/apache2 restart

Ubuntu LAMP 如何配置 Apache

1. 启用 mod_rewrite 模块
终端命令: sudo a2enmod rewrite
重启 Apache服务器: sudo /etc/init.d/apache2 restart

Apache重启后我们可以测试一下,在 /var/www目录下新建文件 test.php,写入代码:  <?php phpinfo(); ?> 保存,在地址栏输入  http://localhost/test.php ,如果正确出现了 php 配置信息则表明 LAMP Apache已经正常工作了 (记得重启 Apache服务器后再测试 )

2.设置 Apache支持 .htm .html .php
sudo gedit /etc/apache2/apache2.conf
sudo gedit /etc/apache2/mods-enabled/php5.conf
AddType application/x-httpd-php .php .htm .html 即可。

LAMP配置之 Mysql测试

上面 php,Apache 都已经测试过了,下面我们再测试一下 Mysql 数据库是否已经正确启用。

/var/www目录下新建 mysql_test.php

$link = mysql_connect("localhost","root","020511");
if (!$link)
die('Could not connect: ' . mysql_error());
else echo "Mysql已经正确配置

保存退出,在地址栏输入,显示” Mysql 已经正确配置”则表示 OK了,如果不行,重启 Apache服务器后再试一下。

解决 Firefox浏览器显示中文乱码等问题

上面在 FireFox浏览器中打开 mysql_test.php phpmyadmin 测试时,如果出现了中文乱码,则是默认语言设置问题,解决方法如下:

打开 apache配置文件: udo gedit /etc/apache2/apache2.conf,在最后面加上: AddDefaultCharset UTF-8 ,如果还是乱码的,再将 UTF-8改用 gb2312
重启 Apache sudo /etc/init.d/apache2 restart  再刷新 mysql_test.php 中文乱码没有了。

如果要人工启动 mysql mysql -u root -p,根据提示输入密码。
如果重启 Apache时出现:
* Restarting web server apache2
apache2: Could not reliably determine the server’s fully qualified domain name, using for ServerName
apache2: Could not reliably determine the server’s fully qualified domain name, using for ServerName

则还是修改 apache配置文件: sudo gedit /etc/apache2/apache2.conf,在文件最后设置: ServerName


重启 apache sudo /etc/init.d/apache2 restart

重启 mysql sudo /etc/init.d/mysql restart

配置 php.ini sudo gedit /etc/php5/apache2/php.ini

配置 apache2.conf sudo gedit /etc/apache2/apache2.conf

PHP CGI sudo /var/www/cgi-bin/

Ubuntu PHP 编辑器

最后 LAMP配置 就完成了,在 Ubuntu下进行简单的 php代码编辑,用 Gedit就可以了。 Gedit支持 HTML PHP Javascsript等近几十种语言的代码高亮功能。如果是 PHP项目开发,建议使用 PHP IDE编辑器,比如 Zend Studio Eclipse。据说文本编辑 VIM也很不错。

PS:如果是 Windows XP 下要搭建 LAMP 环境,建议大家试试 xampp 快速安装配置法,使用也很方便快捷,点击前面的超级链接或 Google一下就知道怎么用了。

* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场


安装Mysql                                 sudo apt-get install mysq...
  • zwyjg
  • zwyjg
  • 2015-02-17 19:17
  • 11954

Ubuntu 16.04搭建LAMP开发环境

  • miaoqiucheng
  • miaoqiucheng
  • 2017-05-23 20:43
  • 1197


安装一. Apache安装 yum install -y httpd 启动 /etc/init.d/httpd start 备注:Apache启动之后会提示错误: 正在启...
  • u010187139
  • u010187139
  • 2016-03-14 11:48
  • 11259


搭建顺序为Apache,Mysql,PHP以及phpmyadmin 1.安装Apache 在命令行输入 sudo apt-get install apache2 输入管理员密码 并选择Y 安装完...
  • whd526
  • whd526
  • 2017-04-03 19:27
  • 1011

linux ubuntu 搭建lamp、虚拟主机配置等

sudo apt-get update //更新 sudo apt-get install apache2 //安装apache2 apache2 -v //查看apache2版本 sud...
  • u012793120
  • u012793120
  • 2016-07-21 09:07
  • 1304


LAMP是Linux、Apache、MySQL、PHP的简称。本文描述在Ubuntu 14.04 LTS Server 64位的系统中安装Apache2、MySQL5、PHP5的过程。 第一种方式:t...
  • wangnan537
  • wangnan537
  • 2015-08-20 00:49
  • 9868


  • John_rush
  • John_rush
  • 2017-11-16 11:33
  • 116


Linux服务器环境(LAMP架构)搭建 1.1 说明 LAMP(Linux+Apache+MySQL+PHP)网站架构几乎是目前国际最流行的Web框架,该框架包括:Linux操作系统(OS),A...
  • jxlicai
  • jxlicai
  • 2013-11-25 03:26
  • 2172

Ubuntu 12.04下LAMP环境搭建实录

LAMP(Linux- Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统,Apache网络服务器,MySQL数据 库,Perl、PHP或者Pytho...
  • HanTangSongMing
  • HanTangSongMing
  • 2014-04-16 20:45
  • 5967


  • u013573047
  • u013573047
  • 2016-05-17 19:22
  • 2257
    • 访问:1987501次
    • 积分:20349
    • 等级:
    • 排名:第471名
    • 原创:307篇
    • 转载:143篇
    • 译文:9篇
    • 评论:445条