个人工具

UbuntuHelp:ApacheMySQLPHP/zh

来自Ubuntu中文

跳转至: 导航, 搜索




简介

Parent page: Programming Applications This is to help people setup and install a LAMP (Linux-Apache-MySQL-PHP) server in Ubuntu, including Apache 2, PHP 4 or 5, and MySQL 4.1 or 5.0. When installing from the Ubuntu 6.06 (Dapper Drake) "Server cd", you have the option of choosing to install a LAMP setup at the inital Ubuntu installation screen. That will install apache2, php5 and mysql 5.0.

这是帮助大家在Ubuntu里安装并设置一个LAMP(Linux-Apache-MySQL-PHP)服务。其中包括Apache 2, PHP 4/5 和 MySQL 4.1/ 5.0。当你从Ubuntu 6.06 (Dapper Drake)“Server CD”里安装系统, 在Ubuntu安装程序的开始时有一个让你选择安装LAMP的选项。选择后将安装apache2, php5 and mysql 5.0.

检查需要

Some applications require php4 while others will work with php5. Be sure to install the version of php and the corresponding apache2 module for it. You cannot have both php4 and php5 modules running on the same instance of apache2 at the same time. Installing one may remove the other. If you have both php4 and php5 installed, be aware of which version of the apache2 php module you have. If libapache2-mod-php5 is already installed, the php4 package will install libapache-mod-php4 and not libapache2-mod-php4 package. If libapache2-mod-php5 is not installed, installing php4 will install the apache2 php module (libapache2-mod-php4). See this example. Most web applications will use Apache2, php5 and mysql5.0. If no specific versions are mentioned in your web application's documentation, use those.


有些应用程序用PHP5工作,有些则需要PHP4。确定应用程序安装的PHP版本和对应的apache2模块。你不能把PHP4和php5同时安装在同一个apache2实例中。安装了其中的一个会删除另一个。如果你都安了php4和php5,了解哪个版本适的apache2 php模块是你要用的。如果libapache2-mod-php5已经安装了,php4的软件包应该安装libapache-mod-php4而不是libapache2-mod-php4。如果libapache2-mod-php5 软件包没有被安装,则php4就要安apache2 php模块(libapache2-mod-php4)。看这个例子,大部分的web程序都用Apache2,php5和mysql5.0.如果你的web程序的文档里没有提到要特定的版本,就安装它们。

在Ubuntu 6.06 LTS (Dapper Drake) 里安装 默认LAMP集群


如果你没有用服务器版CD里的LAMP安装程序安装它,但又不想重装系统来安装同样的软件包的话。用任何方式(any method)来安装下列软件包

apache2 php5-mysql libapache2-mod-php5 mysql-server

(比如 sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server)
这些包全都可以Ubuntu 6.06 LTS (Dapper Drake)的 main软件库里找到。一旦LAMP安装完毕後,你需要为mysql设置一个root密码,然後根据你的web程序创建一个数据库,用户和密码。就这些!

在 Ubuntu 7.04 (Feisty Fawn) 里安装 默认LAMP集。

在Feisty里,ubuntu基本系统包含了 Tasksel。你可以用tasksel或者像上面描述的那样来安装LAMP。

sudo tasksel install lamp-server

See Tasksel

启用完后,怎么删除LAMP集群

To also remove the debconf data, use the purge option when removing. To get rid of any configurations you may have made to apache, manually remove the /etc/apache2 directory once the packages have been removed.


删除以下软件包来删除LAMP集:

apache2 apache2-mpm-prefork apache2-utils apache2.2-common libapache2-mod-php5 libapr1 libaprutil1 libdbd-mysql-perl libdbi-perl libmysqlclient15off libnet-daemon-perl libplrpc-perl libpq5 mysql-client-5.0 mysql-common mysql-server mysql-server-5.0 php5-common php5-mysql

可以用 purge参数来同时删除配置日志。当你要完全删除apache的配置,在你删除以上软件包后,你要手动删除
/etc/apache2 这个文件夹。

安装 Apache2

任何方式安装Apache2网页服务器:

apache2(例如 sudo apt-get install apache2)

故障检修

If you get this error: apache2: Could not determine the server's fully qualified domain name, using 127.0.0.1 for ServerName then use a text editor such as "sudo nano" at the command line or "gksudo gedit" on the desktop to edit


如果你出现这个错误信息:apache2:不能找到完全有效的域名,现用127.0.0.1作为服务器域名,然后在命令行用文本编辑器比如“sudo nano”或者在桌面用“gksudo gedit”来编辑。

/etc/apache2/httpd.conf

然后在文件尾行加入

ServerName localhost


虚拟主机

Apache2 has the concept of sites, which are separate configuration files that Apache2 will read. These are available in /etc/apache2/sites-available. By default, there is one site available called default this is what you will see when you browse to http://localhost or http://127.0.0.1. You can have many different site configurations available, and activate only those that you need. As an example, we want the default site to be /home/user/public_html/. To do this, we must create a new site and then enable it in Apache2. To create a new site:


  • * Change the DocumentRoot to point to the new location. For example, /home/user/public_html/
    * Change the Directory directive, replace <Directory /var/www/> to <Directory /home/user/public_html/
    * You can also set separate logs for each site. To do this, change the ErrorLog and CustomLog directives. This is optional, but handy if you have many sites
    * Save the file

Now, we must deactivate the old site, and activate our new one. Ubuntu provides two small utilities that take care of this: a2ensite (apache2enable site) and a2dissite (apache2disable site).

sudo a2dissite default && sudo a2ensite mysite

Finally, we restart Apache2:

sudo /etc/init.d/apache2 restart

If you have not created /home/user/public_html/, you will receive an warning message To test the new site, create a file in /home/user/public_html/:

echo '<b>Hello! It is working!</b>' > /home/user/public_html/index.html

Finally, browse to http://localhost/


apache2 有一站点的概念,这些站点分配配置文件供Apache2读取。这些站点可以在 /etc/apache2/sites-available 找到。默认情况下,当你浏览[1] 或者 [2]时,只有一个命名为 ‘default’的有效站点可以显示出来。你可以拥有许多不同的可用的站点配置,并且可以激活并启用你需要的那个。举个例子,我们想要把 /home/user/public_html/ 作为默认站点,我们就必须创建一个新的站点并且在apache2里开启它。新建一个站点:

  • 复制默认网络站点作为起始页:
  • sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mysite
  • 用命令行里的文本编辑器“ sudo nano”或者“gksudo gedit”编辑一个新配置文件。例如:
  • gksudo gedit /etc/apache2/sites-available/mysite
  • 更改 ROOT文档 来指向新的域。例如,/home/user/public_html/
  • 更改目录指令,将 <Directory /var/www/> 更改为 <Directory /home/user/public_html/ >。这时可选的,但如果你有很多个站点,用它会很方便。
  • 保存文件

现在,我们要使旧的站点无效,并且激活新的站点。Ubuntu提供了两个小工具:a2ensite (apache2enable site) and a2dissite (apache2disable site).

sudo a2dissite default && sudo a2ensite mysite

最后,重启 Apache2:

sudo /etc/init.d/apache2 restart

如果你没有创建 /home/user/public_html/,在测试新站点的时候你会收到一个错误信息。
在 /home/user/public_html/ 里创建一个文件:

echo '<b>Hello! It is working!</b>' > /home/user/public_html/index.html

最后浏览到 [3]

安装 PHP5

用(任何方式)单独安装PHP5

php5(例如: sudo apt-get install php5)

如果你的系统里也安装了PHP4,还需要安装:

libapache2-mod-php5

故障检修


Does your browser ask if you want to download the php file instead of displaying it? If Apache is not actually parsing the php after you restarted it, install libapache2-mod-php5. It is installed when you install the php5 package, but may have been removed inadvertently by packages which need to run a different version of php. You may also need to actually enable it, by doing sudo a2enmod php5 followed by sudo /etc/init.d/apache2 restart. Be sure to clear your browser's cache before testing your site again.


你的浏览器有没有问你是否想要下载php而不运行它?如果Apache重启后确实没有解析php,安装libapache2-mod-php5。这个包已经在你安装PHP5的时候就安装了的,但会在你安装了其他版本的PHP后被无意中删除了。在执行完
sudo /etc/init.d/apache2 restart后,你也许还要执行“sudo a2enmod php5”来激活它。
在你测试你的站点之前请确认已经清除掉浏览器的缓存。


安装PHP4

任何方式安装PHP4:

php4

If PHP5 is present on your system, installing php4 will install the php module for apache (version 1.3) and not apache2. To use php4 with apache2, install

如果你的系统已经安装了PHP5,在安装php4时还要安装apache(version1.3)的php模块而不是apache2.若要在 apache2里使用php4,请安装:

libapache2-mod-php4

故障检测


你的浏览器有没有问你是否想要下载php而不运行它?如果Apache重启后确实没有解析php,安装libapache2-mod-php4。这个包已经在你安装PHP5的时候就安装了的,但会在你安装了其他版本的PHP后被无意中删除了。在执行完
sudo /etc/init.d/apache2 restart后,你也许还要执行“sudo a2enmod php4”来激活它。
在你测试你的站点之前请确认已经清除掉浏览器的缓存。


Installing MYSQL with PHP 5

使用任何方式安装:

mysql-server libapache2-mod-auth-mysql php5-mysq


Installing MYSQL with PHP 4

First enable the universe repository since the packages are not in main

因为软件包不在main源里,所以先激活 universe 源
我正在使用Ubuntu服务器版(命令行)
我正在用桌面版
使用任何方式安装:

mysql-server libapache2-mod-auth-mysql php4-mysql


安装PHP后

You may need to increase the memory limit that PHP imposes on a script. Edit the /etc/php5/apache2/php.ini file and increase the memory_limit value.

你也许需要提升存储限制来让PHP利用script(小程序)。编辑/etc/php5/apache2/php.ini文件并且提升存储限制的值。


安装MySQL后

Before you can access the database from other computers in your network, you have to change its bind address. Note that this can be a security problem, because your database can be accessed by others computers than your own. Skip this step if the applications which require mysql are running on the same machine. type:

设置mysql协议地址
在你可以从其他电脑通过网络访问到数据库之前,你必须更改它的协议地址。注意,这会是个安全问题,因为除了你之外的其他电脑也可以访问到数据库。如果需要用到的程序在同一个机器上运行,跳过这个步骤。输入:

nano /etc/mysql/my.cnf

and change the line:

然后更改行

bind-address = localhost

to your own internal ip address e.g. 192.168.1.20

成你自己的内部 ip 地址 比如: 192.168.1.20

bind-address = 192.168.1.20

If your ip address is dynamic you can also comment out the bind-address line and it will default to your current ip. If you try to connect without changing the bind-address you will recieve a "Can not connect to mysql error 10061".

如果你的是动态IP地址,你也可以把 blind-adress 这一行给注释掉。它会默认为你当前的IP。如果你没有更改 bind-address的地址而尝试链接,你会收到这样一个错误信息:‘Can not connect to mysql error 10061’。


Set mysql root password

在用控制台访问数据库之前,你需要输入:

mysql -u root

在mysql控制台输入:

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

正确输入mysql命令后会显示:Query OK, 0 rows affected (0.00 sec) Mysql commands can span several lines

别忘了在你结束没意思起来命令的时候在结尾加上分号。注意:如果你已经设置了root密码,你将需要用到:

mysql -u root -p

(你忘记了 mysql-root 密码吗?查看 MysqlPasswordReset。)


创建一个mysql数据库

mysql> CREATE DATABASE database1;


创建一个mysql用户:

创建一个拥有所有权限的新用户(只用于检测故障),在mysql prompt(字符控制台)里输入:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'yourusername'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;

创建一个拥有少许权限的新用户(用于在大部分web程序上工作),这个用户只可以使用命名为“database1”的数据库。在mysql prompt里输入:

mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON database1.* TO 'yourusername'@'localhost' IDENTIFIED BY 'yourpassword';

yourusername 和 yourpassword 可以是你喜欢的任何东西。database1 是用户可以访问的一个数据库名称。localhost是你访问数据库的域名,为了可以让所有的域名链接(或者特定的域名)到你的数据库,你可以把它改成“%”(或者改成主机名和ip地址)。注意,这会是一个安全问题而且只能用于测试目的!推出mysql prompt,输入:

mysql> \q

因为你已经设置了root密码,如果你要重新使用mysql(以mysql root身份),你需要使用:

mysql -u root -p

然后在prompt里输入密码。

备份设置

Please, let's say something in which directories MySql stores the database information and how to configure a backup

接着,我们来说一下MySql是在哪个位置存储数据库信息的和如何去设置一个备份

可选

有不止一种方式来设置mysql root密码和创建数据库。例如 mysqladmin可以这样使用:

mysqladmin -u root -p password yourpassword


mysqladmin -u root -p create database1

mysqladmin 是LAMP默认安装方式里提供的一个命令行工具。

Phpmyadmin and mysql-admin


通过Phpmyadmin 和mysql-admin这两个图形界面工具可以完成 所有的mysql任务,包括设置root密码和创建可用的数据库。在安装其中一个或者两个都安装之前,先激活 universe 源

我正在使用Ubuntu服务器版(命令行)
我正在使用桌面版
任何方式安装:

phpmyadmin

故障检测

'If you get blowfish_secret error: Choose and set a phrase for cryptography in the file /etc/phpmyadmin/blowfish_secret.inc.php and copy the line (not the php tags) into the file /etc/phpmyadmin/config.inc.php or you will receive an error.

如果你收到了短信密码(或者密码字,网上有两个名字)错误信息
在/etc/phpmyadmin/blowfish_secret.inc.php里用密码学选择并设置一个短语 ,并且把该行(没有PHP后缀)复制到文档/etc/phpmyadmin/config.inc.php里,不然你就会收到错误信息。


可选:源码安装phpMyAdmin

查看phpMyAdmin说明页面: 如何源码安装 phpmyadm

Mysql-admin

在桌面版系统限制运行Mysql-admin。用any method 安装

mysql-admin

更多信息

你真的应该阅读 2.9.3. Securing the Initial MySQL Accountsfrom the MySQL Reference Manual.


编辑apache配置

如果你想使当前用户成为PHP页面的管理员。你需要编辑apache的配置文档:

$ gksudo "gedit /etc/apache2/apache2.conf"

Search both the strings starting by "User" and "Group", and change the names by the current username and groupname you are using. Then you'll need to restart Apache. (look at the next chapter concerning apache commands) Configuration options relating specifically to user websites (accessed through localhost/~username) are in /etc/apache2/mods-enabled/userdir.conf.

搜索所有以“User”和“Group”开头的行,然后把名字更改成你当前的用户名和你正在使用的组名。接着你需要重启apache。(请看下一章,关于apache命令行)具体于用户站点(通过 localhost/~username访问)相关的配置选项在:/etc/apache2/mods-enabled/userdir.conf。

编辑PHP配置以便与mysql共同工作(Ubuntu Dapper)

In Dapper Drake, "extension=mysql.so" and "extension=mysqli.so" are enabled in the php.ini file out-of-the-box. However, sometimes php is not looking for those files in the right directory. You have then to move your files or modify the php.ini configuration.:

在Dapper Drake里,"extension=mysql.so" 和 "extension=mysqli.so"在php.ini文件里预设启动的。但是,php有时候没用通过正确目录查查看这些文件。所以你必须移动你的文件或者修改php.ini配置:

第一个解决方法

locate the directory where the extension files are placed: locate mysql.so (change mysql.so in mysqli.so if you want to install the mysqli functions) -then modify the php.ini file to indicate the right place for the extension directory:

找出扩展文件放置的所在目录:找出 mysql.so(如果你想安装mysqli functions,把 mysql.so改成 mysqli.so),然后修改php.ini文件以指示出扩展目录正确的放置位置:

$ gksudo "gedit /etc/php4/apache2/php.ini"

或者你用的是php5:

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

查看 “extension_dir”的属性,然后把它设置成mysql(i).so(你找到的)的所在目录:

extension_dir= "/usr/lib/php5/20051025/"

重启apache,接着测试你的mysql(i)functions是否工作。


第二个解决方法

locate the directory where the extension files are placed: locate mysql.so (change mysql.so in mysqli.so if you want to install the mysqli functions) Let's say that you found the file in '/usr/lib/php5/20051025/' -then check in the php.ini file for the extension directory

找出扩展文件放置的所在目录:找出 mysql.so(如果你想安装mysqli functions,把 mysql.so改成 mysqli.so),假设你找到的文件在 : '/usr/lib/php5/20051025/',作为扩展目录把它登记到php.ini文件里。

$ gksudo "gedit /etc/php4/apache2/php.ini"

或者你用的是php5

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

Look for the 'extension_dir' property. It should be by default '/usr/lib/php5/ext'. If it's not, change it for this value. -Now create the default directory for extensions:

查看(locate)'extension_dir'的属性。它应该默认为 ‘/usr/lib/php5/ext’。如果不是,把它改成这个值。--现在为扩展文件创建一个默认的目录:

$ sudo mkdir /usr/lib/php5/ext

Copy the extension file to the new directory:

--复制扩展文件到新建的目录:

$ sudo cp /usr/lib/php5/20051025/mysql.so /usr/lib/php5/ext/mysql.so

Change the first path to the one you found with the locate function, and change mysql.so into mysqli.so if you want to use mysqli functions. -Restart apache (see below), and test if your mysql(i) functions are working.

把第一个路径改成你用 locate功能 找到的那个,如果你想要使用 mysqli功能,把mysql.so 改成 mysqli.so。--重启apache(看下面),然后测试看你的 mysql(i)功能 是否在工作。

运行,停止,测试和重启 Apache

使用下列命令来运行 Apache:

$ sudo /usr/sbin/apache2ctl start

停止:

$ sudo /usr/sbin/apache2ctl stop

To test configuration changes, use :

测试配置的更改:

$ sudo /usr/sbin/apache2ctl configtest

最後,重启使用:

$ sudo /usr/sbin/apache2ctl restart


使用 Apache

You can access apache by typing 127.0.0.1 or http://localhost (by default it will be listening on port 80) in your browser address bar. By default the directory for apache server pages is /var/www . It needs root access in order to put files in. A way to do it is just starting the file browser as root in a terminal:

你可以通过在浏览器的地址栏里键入127.0.0.1 或者   http://localhost (默认监视端口为80) 来访问apache。apache服务器页面的默认目录是 /var/www 。为了把文件放进去,要用root用户访问。用root用户登录的一个方法:只要以root身份在终端里启动文件浏览程序:

$ sudo nautilus

or if you want to make /var/www your own. (Use only for non-production web servers - this is not the most secure way to do things.)

或者如果你想要使 /var/www 成为你自己的(更改权限)。(只用于未出示的web服务器-这不是最安全的做法)

$ sudo chown -R $USER:$USER /var/www

状态

检查你的PHP装置的状态:

$ gksudo "gedit /var/www/testphp.php"

然后插入这行:

<?php phpinfo(); ?>

用web浏览器在 http://yourserveripaddress/testphp.php 或者 http://localhost/testphp.php 访问这个页面。

加固Apache

If you just want to run your Apache install as a development server and want to prevent it from listening for incoming connection attempts, this is easy to do.

如果你只是想让apache以开发服务器运行并且阻止被企图连接进来的连接所监听。这是容易实现的。

$ gksudo "gedit /etc/apache2/ports.conf"
$ password:

修改 ports.conf是之包含:

Listen 127.0.0.1:80

保存文件,然後重启Apache(看上文)。现在Apache想只服务于你的本地域名,http://127.0.0.1 或者 http://localhost。

密码保护一个目录(Password-Protect a Directory)

有两个方法来密码保护一个特殊的目录。推荐的方法有:
编辑 /etc/apache2/apache2.conf。(你需要用root用户访问来完成这个工作)。
另一个方法是:编辑目录里的 a .htaccess 文件来实现保护。(你需要用root用户访问来完成这个工作)。

(用 .htaccess 密码保护一个目录)Password-Protect a Directory With .htaccess

阅读 EnablingUseOfApacheHtaccessFiles 提示:在很少的一些Ubuntu版本里,.htaccess文件是默认不工作的。.

阅读EnablingUseOfApacheHtaccessFiles 来帮助启用它们。


略图(thumbnails)

thumbnails

If you direct your web browser to a directory (rather than a specific file), and there is no "index.html" file in that directory, Apache will generate an index file on-the-fly listing all the files and folders in that directory. Each folder has a little icon of a folder next to it. To put a thumbnail of that specific image (rather than the generic "image icon") next to each image file (.jpg, .png, etc.): ... todo: add instructions on how to do thumbnails here, perhaps usingApache::AutoIndex 0.08 or Apache::Album 0.95 ...


如果你让你的浏览器访问一个没有“index.html”文件的目录。Apache将会产生一个 index文件,这个文件会即时把该目录中所有的文件和文件夹列出来。每个文件夹都有一个相应小图标在其旁边。发送一个略图当做特殊图像(不是通常的“图标”)放在每个图像文件(.jpg,.png,等等)的旁边:...要做的有:在这里,也许能附加使用 Apache::AutoIndex 0.08 或者 Apache::Album 0.95 指令来实现 略图。


已知问题

与Skype不兼容

[4]Skype uses port 80 for incoming calls, and thus, may block Apache.

Skype 的来访呼叫使用端口80,因此会于Apache冲突。解决的方法是改变其中一个程序的端口。通常端口81是空闲并且工作良好的。在Skype里打开 ‘目录>选项’,然後点击 ‘高级’ ,然後在来访呼叫(incoming calls)的方框里写入你选的端口。

其他Apache选项


更多信息

  • 建议阅读 StrongPasswords
  • 如果你想运行一个开放的web服务器,同样建议阅读 BastilleLinux
  • 你同样可以编译 Self:PHP5FromSource,就如同 Self:MYSQL5FromSource一样。
  • Self:PHPOracle 将使你能连接到 Oracle(甲骨文)数据库。
  • PhpPear:PHP扩展和应用程序仓库。