用 Apache 建立一个 HTTP 服务器

2013年5月15日 分类: Linux (76 个脚步)

1. 写在前面

Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。

2. 安装apache 服务器组件,使用yum来进行安装。

[root@shiniv ~]# yum -y install httpd  ← 在线安装httpd

Setting up Install Process
Setting up repositories
dag 100% |=========================| 1.1 kB 00:00
update 100% |=========================| 951 B 00:00
base 100% |=========================| 1.1 kB 00:00
addons 100% |=========================| 951 B 00:00
extras 100% |=========================| 1.1 kB 00:00
Reading repository metadata in from local files
Reducing Dag RPM Repository for Red Hat Enterprise Linux to included packages only
Finished
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for httpd to pack into transaction set.
httpd-2.0.52-28.ent.cento 100% |=========================| 62 kB 00:00
---> Package httpd.i386 0:2.0.52-28.ent.centos4 set to be updated
--> Running transaction check
--> Processing Dependency: libaprutil-0.so.0 for package: httpd
--> Processing Dependency: apr >= 0.9.4-24.2 for package: httpd
--> Processing Dependency: libapr-0.so.0 for package: httpd
--> Processing Dependency: httpd-suexec for package: httpd
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for apr to pack into transaction set.
apr-0.9.4-24.5.c4.1.i386. 100% |=========================| 7.5 kB 00:00
---> Package apr.i386 0:0.9.4-24.5.c4.1 set to be updated
---> Downloading header for httpd-suexec to pack into transaction set.
httpd-suexec-2.0.52-28.en 100% |=========================| 22 kB 00:00
---> Package httpd-suexec.i386 0:2.0.52-28.ent.centos4 set to be updated
---> Downloading header for apr-util to pack into transaction set.
apr-util-0.9.4-21.i386.rp 100% |=========================| 5.2 kB 00:00
---> Package apr-util.i386 0:0.9.4-21 set to be updated
--> Running transaction check
Dependencies Resolved

=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
httpd i386 2.0.52-28.ent.centos4 update 888 k
Installing for dependencies:
apr i386 0.9.4-24.5.c4.1 base 88 k
apr-util i386 0.9.4-21 base 51 k
httpd-suexec i386 2.0.52-28.ent.centos4 update 28 k

Transaction Summary
=============================================================================
Install 4 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 1.0 M
Downloading Packages:
(1/4): apr-0.9.4-24.5.c4. 100% |=========================| 88 kB 00:00
(2/4): httpd-suexec-2.0.5 100% |=========================| 28 kB 00:00
(3/4): httpd-2.0.52-28.en 100% |=========================| 888 kB 00:02
(4/4): apr-util-0.9.4-21. 100% |=========================| 51 kB 00:00
warning: rpmts_HdrFromFdno: V3 DSA signature: NOKEY, key ID 443e1821
Public key for apr-0.9.4-24.5.c4.1.i386.rpm is not installed
Retrieving GPG key from http://mirror.centos.org/centos/RPM-GPG-KEY-centos4
Importing GPG key 0x443E1821 "CentOS-4 key <centos-4key@centos.org>"
Key imported successfully
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: apr ######################### [1/4]
Installing: apr-util ######################### [2/4]
Installing: httpd ######################### [3/4]
Installing: httpd-suexec ######################### [4/4]

Installed: httpd.i386 0:2.0.52-28.ent.centos4
Dependency Installed: apr.i386 0:0.9.4-24.5.c4.1 apr-util.i386 0:0.9.4-21 httpd-suexec.i386 0:2.0.52-28.ent.centos4
Complete!

显示是这样的话 就证明了安装apache成功了

3. 安装PHP

<pre class=”brush: shell; ” lang=”shell”>[root@shiniv ~]# yum -y install php  ← 在线安装PHP

Setting up Install Process
Setting up repositories
Reading repository metadata in from local files
Reducing Dag RPM Repository for Red Hat Enterprise Linux to included packages only
Finished
Parsing package install arguments
Resolving Dependencies
–&gt; Populating transaction set with selected packages. Please wait.
—&gt; Downloading header for php to pack into transaction set.
php-4.3.9-3.15.i386.rpm 100% |=========================| 23 kB 00:00
—&gt; Package php.i386 0:4.3.9-3.15 set to be updated
–&gt; Running transaction check
–&gt; Processing Dependency: php-pear for package: php
–&gt; Restarting Dependency Resolution with new changes.
–&gt; Populating transaction set with selected packages. Please wait.
—&gt; Downloading header for php-pear to pack into transaction set.
php-pear-4.3.9-3.15.i386. 100% |=========================| 33 kB 00:00
—&gt; Package php-pear.i386 0:4.3.9-3.15 set to be updated
–&gt; Running transaction check

Dependencies Resolved

=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
php i386 4.3.9-3.15 update 1.3 M
Installing for dependencies:
php-pear i386 4.3.9-3.15 update 266 k

Transaction Summary
=============================================================================
Install 2 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 1.6 M
Downloading Packages:
(1/2): php-pear-4.3.9-3.1 100% |=========================| 266 kB 00:00
(2/2): php-4.3.9-3.15.i38 100% |=========================| 1.3 MB 00:01
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: php ######################### [1/2]
Installing: php-pear ######################### [2/2]

Installed: php.i386 0:4.3.9-3.15
Dependency Installed: php-pear.i386 0:4.3.9-3.15
Complete!</pre>

安装PHP成功

4. 配置apache服务器

[root@shiniv ~]# vi /etc/httpd/conf/httpd.conf  ← 编辑Apache的配置文件

#
# Don't give away too much information about all the subcomponents
# we are running. Comment out this line if you don't mind remote sites
# finding out what major optional modules you are running
ServerTokens OS  ← 找到这一行,将“OS”改为“Prod”(在出现错误页的时候不显示服务器操作系统的名称)
 ↓
ServerTokens Prod   ← 变为此状态

# 
# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory
# listings, mod_status and mod_info output etc., but not CGI generated
# documents or custom error documents).
# Set to "EMail" to also include a mailto: link to the ServerAdmin.
# Set to one of: On | Off | EMail
#
ServerSignature On  ← 找到这一行,将“On”改为“Off”
 ↓
ServerSignature Off  ← 在错误页中不显示Apache的版本

#
# ServerAdmin: Your address, where problems with the server should be
# e-mailed. This address appears on some server-generated pages, such
# as error documents. e.g. admin@your-domain.com
#
ServerAdmin root@localhost  ← 将管理员邮箱设置为自己常用的邮箱 
 ↓
ServerAdmin yourname@yourserver.com  ← 根据实际情况修改默认设置

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If this is not set to valid DNS name for your host, server-generated
# redirections will not work. See also the UseCanonicalName directive.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make
# redirections work in a sensible way.
#
#ServerName new.host.name:80  ← 修改主机名
 ↓
ServerName www.centospub.com:80  ← 根据实际情况修改,端口号保持默认的80

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs-2.0/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks  ← 找到这一行,删除“Indexes”,并添加“Includes”、“ExecCGI”
 ↓
Options Includes ExecCGI FollowSymLinks  ← 允许服务器执行CGI及SSI

#
# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
#AddHandler cgi-script .cgi  ← 找到这一行,去掉行首的“#”,并在行尾添加“.pl”
 ↓
AddHandler cgi-script .cgi .pl  ← 允许扩展名为.pl的CGI脚本运行

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None  ← 找到这一行,将“None”改为“All”
 ↓
AllowOverride All  ← 变为此状态,允许.htaccess

# 
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined  ← 找到这一行
 ↓
LogFormat "%h %l %u %t \"%!414r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined  ← 改为此状态(添加“!414”到规则中,对于过长的日志不记录)

#
# Specify a default charset for all pages sent out. This is
# always a good idea and opens the door for future internationalisation
# of your web site, should you ever want it. Specifying it as
# a default does little harm; as the standard dictates that a page
# is in iso-8859-1 (latin1) unless specified otherwise i.e. you
# are merely stating the obvious. There are also some security
# reasons in browsers, related to javascript and URL parsing
# which encourage you to always set a default char set.
#
AddDefaultCharset UTF-8  ← 找到这一行,在行首添加“#”
 ↓
#AddDefaultCharset UTF-8  ← 不使用UTF-8作为网页的默认编码
AddDefaultCharset GB2312  ← 并接着添加这一行(添加GB2312为默认编码)

<Directory "/var/www/icons">  ← 找到这一个标签,并在标签中更改相应选项
  Options Indexes MultiViews  ← 找到这一行,将“Indexes”删除
    ↓
  Options MultiViews   ← 变为此状态(不在浏览器上显示树状目录结构)
AllowOverride None
Order allow,deny
Allow from all
</Directory>

[root@shiniv ~]# rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html  ← 删除测试页

5. 启动服务

 

[root@sample ~]# chkconfig httpd on  ← 设置HTTP服务自启动

[root@sample ~]# chkconfig --list httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off   ← 确认2--5为on的状态就OK

[root@sample ~]# /etc/rc.d/init.d/httpd start  ← 启动HTTP服务
Starting httpd:              [ OK ] ← 启动成功会出现OK

如果启动失败的话,会出现错误信息。原因可能是因为httpd.conf文件编辑过程中的失误,请检查httpd.conf。

6. 测试

在站点根目录下面建立一个php文件 内容如下:

<?php 
phpinfo();
?>

打开浏览器,输入地址http://localhost ,如看到当前服务器的详细信息,就证明了apache已经配置完成了

 

 

用 Apache 建立一个 HTTP 服务器 【声明】本文 用 Apache 建立一个 HTTP 服务器 为柠之漠然原创文章,转载请注明出自 枫之落叶
并保留本文有效链接:https://blog.shiniv.com/2013/05/apache-http-server/ , 转载请保留本声明!

标签:
2 条评论

2 trackbacks

  1. google Trackback | 2013-08-31
  2. CdTBJCzU Trackback | 2013-08-31
你必须要启用 Javascript