LAMP (简体中文)
From ArchWiki
| i18n |
|---|
| English |
| Italiano |
| Русский |
| Česky |
| 简体中文 |
Contents |
[edit] Apache, PHP, and MySQL
这个文档描述了怎样在Archlinux系统上安装设置Apache网页服务器。以及选择安装PHP和MySQL并集成到Apache服务器中。
[edit] 安装软件包
# pacman -Sy apache php mysql
你可以只单独安装apache,Apache和php,或所有包。这个文档假设你安装全部,当然你可以忽略任何部分。
Note: 新默认用户和用户组: 取代了原先的用户组group "nobody" ,现在默认以user/group "http" 来运行Apache。根据这个变化,需要调整httpd.conf,虽然仍然能够用nobody来运行httpd。
[edit] 配置Apache
- 添加新用户http
useradd http
- 添加以下行到文件
/etc/hosts(如果文件不存在,则创建一个新的):
127.0.0.1 localhost.localdomain localhost
注意: 如果你想要不同的域名,在末尾追加以下行:
127.0.0.1 localhost.localdomain localhost myhostname
- 编辑
/etc/rc.conf: 如果第一步你设置了域名,那么HOSTNAME 变量应该一致,否则使用"localhost":
# # Networking # HOSTNAME="localhost"
- 在Apache配置文件注释一个模块
# nano /etc/httpd/conf/httpd.conf
把
LoadModule unique_id_module modules/mod_unique_id.so
注释为
#LoadModule unique_id_module modules/mod_unique_id.so
- 在终端以root用户运行:
# /etc/rc.d/httpd start
- 那么现在Apache应该在运行了。在网页浏览器里测试下
http://localhost/。应该显示一个简单的Apache测试页面。
- 编辑
/etc/rc.conf(设置Apache为开机运行):
DAEMONS=(... some daemons ... httpd)
或者 添加以下行到 /etc/rc.local:
/etc/rc.d/httpd start
- 如果你想使某用户目录(比如
~/public_html在机器上通过http://localhost/~user/访问) 可以被访问,请在/etc/httpd/conf/extra/httpd-userdir.conf取消注释以下行:
UserDir public_html
和
<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch ExecCGI
<Limit GET POST OPTIONS PROPFIND>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS PROPFIND>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
请确认你的用户目录权限正确设置使apache可以访问,你的用户目录和~/public_html/必有要有可执行权限。这样就可以了:
$ chmod o+x ~ $ chmod o+x ~/public_html
如果你对安全问题很偏执的话,还有其它更安全的方法设置权限,可以创建一个只允许apache和你可以访问特别组。
[edit] 另外的参数
下面这些参数可以在/etc/httpd/conf/httpd.conf 中看到,有些可能你会感兴趣:
# Listen 80
这是Apache监听的端口。 For Internet-access with router, you have to forward the port.
# ServerAdmin sample@sample.com
这是管理员admin的email地址,可以在出错页面error-pages找到,比如.
# DocumentRoot "/srv/http"
这是你网页存放的目录,可以改变它,改了之后,还要把这也改成相应的目录
<Directory "/srv/http">
[edit] 配置PHP
PHP基本上可以使用的了.
- 在
/etc/httpd/conf/httpd.conf中把下面这行注释去掉(uncomment)
#LoadModule php5_module modules/libphp5.so
改为to
LoadModule php5_module modules/libphp5.so
- PHP5文件解析已经默认设置了:
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
<IfModule mod_php5.c>
DirectoryIndex index.php index.html
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
</IfModule>
DirectoryIndex index.html
</IfModule>
- 在Apache配置文件里不存在LoadModule php5_module modules/libphp5.so这行,正确的方法应该在/etc/httpd/conf/httpd.conf结束部分加上
# PHP support Include conf/extra/php5_module.conf
- 如果有需要默认解析.phtml文件,请记住添加.phtml到这行:
DirectoryIndex index.php index.phtml index.html
- 如果你要libGD模块请把
/etc/php/php.ini里的
;extension=gd.so
取消注释为
extension=gd.so
- 如果你的
DocumentRoot是在/home/以外地方,在/etc/php/php.ini添加这个open_basedir成:
open_basedir = /home/:/tmp/:/usr/share/pear/:/path/to/documentroot
- 重启Apache服务使修改生效(以root用户):
# /etc/rc.d/httpd restart
- 用非常简单而又强大的脚本测试:
<html> <head> <title>PHP Test Page</title> </head> <body> This is Arch Linux, running PHP. <?php phpinfo(); ?> </p> </body> </html>
保存此文件为test.php 复制到/home/httpd/html/ 或 ~/public_html如果你允许此权限。当然请把它设置为可执行文件(chmod o+x test.php).
[edit] 设置支持MySQL数据库
如果只要设置支持MySQL,做以下步骤就行了,至于具体配置MySQL请参考: MySQL
- 编辑
/etc/php/php.ini(旧系统在/usr/etc里) to 取消注释如下行(移除;即可):
;extension=mysql.so
- You can add minor privileged users for your web scripts by editing the tables found in the
mysqldatabase. You have to restart MySQL for changes to take effect. Don't forget to check themysql/userstable. If there's a second entry for root and your hostname is left with no password set, everybody from your host probably could gain full access. Perhaps see next section for these jobs.
- You can get the "
error no. 2013: Lost Connection to mysql server during query" message instantly whenever you try to connect to the MySQL daemon by TCP/IP. This is the TCP wrappers system (tcpd), which uses thehosts_access(5)system to allow or disallow connections.
- 如果你出现这个问题,请在/etc/hosts.allow 添加以下行:
# mysqld : ALL : ALLOW # mysqld-max : ALL : ALLOW # and similar for the other MySQL daemons.
- 注意: The examples above are the simplest case, telling tcpd to allow connections from anywhere. You may wish to use a more-appropriate choice of permissible sources instead of ALL. Just make sure that localhost and the IP address (numeric or DNS) of the interface by which you connect are specified.
- 你可能需要编辑
/etc/my.cnf把skip-networking注释掉:
skip-networking
修改为
#skip-networking