一、描述
CentOS安装Apache服务可以有两种方式,一种是用yum安装,一种是从官网下载源代码进行安装。
二、使用yum安装Apache服务
步骤1:在命令行输入如下语句可以使用yum在线更新方式进行安装:
[[email protected] /]$ sudo yum install httpd -y
注意:采用该方式进行安装配置文件的默认路径为:/etc/httpd/conf/httpd.conf
步骤二:配置防火墙,添加下面红色字体的一行,表示开启80端口供客户浏览。
[[email protected] /]$ sudo vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
步骤三:配置完防火墙之后需要重新启动防火墙
[[email protected] /]$ sudo service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
步骤四;Apache配置完成后,则可启动服务
[[email protected] /]$ sudo service httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for tong
httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
注意:虽然这里可以启动但是报错,这是因为没有在Apache服务器的配置文件中添加ServerName参数,所以我们要在配置文件中配置这个参数
步骤5:我们查看我们的hostname主机名,并将该主机名添加到Apache配置文件的ServerName参数中
[[email protected] /]$ hostname
tong
[[email protected] /]$ cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=tong
由上述的hostname为tong,则编辑Apache配置文件
[[email protected] conf]$ sudo vi /etc/httpd/conf/httpd.conf
在其中加入一行:ServerName tong:80
步骤5:重新启动Apache服务,则不会报错
[[email protected] conf]$ sudo service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
步骤6:测试范例网页
在本机浏览器输入:http://本机IP地址,我的IP为:192.168.200.5,,我在浏览器上输入:http://192.168.200.5
结果显示:
三、使用源代码安装Apache服务
步骤1:下载源代码文件
[[email protected] tongSoftware]$ sudo wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.12.tar.gz
步骤2;解压并安装
[[email protected] tongSoftware]$ sudo tar xzvf httpd-2.4.12.tar.gz
进入该解压后的目录,输入如下命令:
[[email protected] httpd-2.2.29]$ sudo ./configure
configure: error: in `/usr/local/tongSoftware/httpd-2.2.29/srclib/apr‘:
configure: error: no acceptable C compiler found in $PATH
See `config.log‘ for more details
configure failed for srclib/apr
系统提示未安装gcc编译器,则安装该编译器
[[email protected] httpd-2.2.29]$ sudo yum install gcc
安装成功后重新编译源文件:
[[email protected] httpd-2.2.29]$ sudo ./configure //检查安装平台是否支持安装
[[email protected] httpd-2.2.29]$ sudomake
//根据安装平台进行编译
[[email protected] httpd-2.2.29]$ sudomake install //安装软件
然后按照之前用yum安装的方法配置防火墙,启动Apache服务。