作者:indian

关键词:apache, 安装apache, freebsd下安装apache

 

 

一、准备工作

1、FreeBSD 客户端。

如果你没有自己的 DNS 服务器,那么你就要确保在“/etc/hosts”文件中有个条目指向你的服务器 IP ,这将保证你的服务器主机名能够被本地解析,否则你只能用 IP 访问服务器了。

编辑“hosts”文件:



# ee /etc/hosts


将看见大概如下内容:



::1               localhost localhost.example.com
127.0.0.1         localhost localhost.example.com
192.168.1.11      host.example.com


替换 example.com 为你的域名,替换 host.example.com 为你的主机名,替换 192.168.1.11 为你的服务器 IP 地址。

 

2、Windows 客户端。

如果是 windows 客户端,请修改 C:\WINDOWS\system32\drivers\etc 下的 hosts 文件,修改内容同上。

 

 

二、安装

运行下列命令开始安装:



# cd /usr/ports/www/apache22

# make install clean

# rehash


 

 

三、配置



# ee /usr/local/etc/apache22/httpd.conf


配置 apache 让它能正常解析主机名。将 ServerAdmin you@example.com (~138行)改为你的管理员邮箱;将 ServerName host.example.com:80(~147行)改为你的服务器主机名。保存退出。

 

 

四、测试

1、测试配置文件。



# apachectl configtest


此命令测试配置文件。如果返回“Syntax OK”,表示配置文件没有语法错误。否则,shell 会给出错误提示。

 

2、配置 apache 自动启动。



# ee /etc/rc.conf


在“/etc/rc.conf”文件中添加如下内容:



apache22_enable="YES"

apache22_http_accept_enable="YES"


apache 将随系统启动而自动启动,相当于运行了 apache 的启动命令:



# /usr/local/etc/rc.d/apache22 start


 

3、测试。

你可以用浏览器测试服务器是否运行正常,或者用下面的命令测试。



# telnet localhost 80

Trying 127.0.0.1...

Connected to localhost.

Escape character is '^]'.


按“Ctrl+]”进入 telnet 命令状态。按下回车键,并输入“GET”命令:



# telnet localhost 80

Trying 127.0.0.1...

Connected to localhost.

Escape character is '^]'.

^]

telnet>

GET / HTTP/1.0

(Enter,回车)

HTTP/1.1 200 OK

Date: Tue, 14 Oct 2008 12:04:45 GMT

Server: Apache/2.2.9 (FreeBSD) mod_ssl/2.2.9 OpenSSL/0.9.8e DAV/2

Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT

ETag: "17047-2c-3e9564c23b600"

Accept-Ranges: bytes

Content-Length: 44

Connection: close

Content-Type: text/html

<html><body><h1>It works!</h1></body></html>Connection closed by foreign host.

如果你在最后一行看见“It works!”,表明 apache 运行正常。

 

 

五、apache的相关

命令:



# /usr/local/etc/rc.d/apache22 start 启动 apache

# /usr/local/etc/rc.d/apache22 stope 停止 apache

# /usr/local/etc/rc.d/apache22 configtest 测试配置文件语法错误

# /usr/local/etc/rc.d/apache22 restart 重启 apache


配置文件:



/usr/local/etc/apache22/httpd.conf 这个是 apache 的主配置文件

/usr/local/etc/apache22/extra/httpd-ssl.conf apache的 ssl 配置文件


日志文件:



/var/log/httpd-access.log 访问日志

/var/log/httpd-error.log 错误日志

/var/log/httpd-ssl_request.log SSL 请求日志


目录结构:



/usr/local/www/apache22/data apache的 WEB 目录,所有的 web 内容都放在这儿。要更改位置,修改 httpd.conf 中的设置。