Ubuntu Linux下 安装配置 LAMP开发环境

这篇文章是看到过的Ubuntu Linux下配置Lamp环境介绍最好的一篇文章,故转载过来供大家参考

 

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浏览器在地址栏输入 http://127.0.0.1,显示 It works!表明 Apache服务器已经开始工作了, LAMP安装 也就这样完成了。

2,使用命令行

当然不使用 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:

http://localhost/

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:

http://localhost/testphp.php

Install 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 = 127.0.0.1

And change the 127.0.0.1 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:

;extension=mysql.so

To look like this:

extension=mysql.so

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 安装开始。

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 建立连接。

如果没有配置好可以用以下命令重新配置:sudo dpkg-reconfigure -plow phpmyadmin

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://127.0.0.1/test.php 或 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

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

保存退出,在地址栏输入 http://127.0.0.1/mysql_test.php,显示” 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 127.0.1.1 for ServerName
apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

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

LAMP组件经常使用的几个终端命令

重启 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支持 HTMLPHP Javascsript等近几十种语言的代码高亮功能。如果是 PHP项目开发,建议使用 PHP IDE编辑器,比如Zend Studio  Eclipse。据说文本编辑 VIM也很不错。

 

如果是 Windows XP 下要搭建 LAMP 环境,建议大家试试 xampp 快速安装配置法,使用也很方便快捷,32位Linux下也可以使用XAMPP,大家可以试试。