xiang8 发表于 2015-11-14 08:03:51

Linux下安装Apache

1.   故事情节的背景
  最近要在linux上架设一个web服务器,我选择了Apache(即httpd)。我用xp+ linux虚拟机来模拟。
环境
Ip
windows xp
192.168.1.100
linux虚拟机(CentOS-5.7)
192.168.1.101
2.   在linux上安装Apache
2.1. 下载httpd源码
  官网http://www.apache.org/,我下载的httpd版本是:httpd-2.0.64.tar.gz
2.2. 安装
  按照INSTALL文件里的说明操作即可:
  # ./configure --prefix=/usr/local/apache2
  # make
  # make install
        
2.3. 开启http服务
  安装完后,输入以下命令开启服务:
  # /usr/local/apache2/bin/apachectl start
         结果出错,提示如下信息:
  Starting httpd: httpd: apr_sockaddr_info_get() failed for bogon
  httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
  解决办法:把/usr/local/apache2/conf/httpd.conf文件中以下ServerName前的#号去掉。
  #ServerName www.example.com:80
  然后运行以下命令重启服务即可。
  # /usr/local/apache2/bin/apachectl restart
3.   在linux访问http
  在文件/usr/local/apache2/conf/httpd.conff中找到如下配置,其中/icons/就是http访问的网址,在linux本机浏览器中输入http://192.168.1.101:80/icons/即可访问。其中80是端口号,http默认端口号就是80,也可以省略不写,即http://192.168.1.101/icons/
  Alias /icons/ "/var/www/icons/"
  
  <Directory &quot;/var/www/icons&quot;>
     Options Indexes MultiViews
     AllowOverride None
     Order allow,deny
     Allow from all
  </Directory>
  
4.   在windos xp访问http
  离开linux虚拟机,切换到windows xp,打开浏览器输入http://192.168.1.101:80/icons/,结果却访问不了。这是因为linux系统的防火墙搞的鬼,用以下命令把linux防火墙关掉,再试就OK了。
  # sudo service iptables stop
  为了重新开启linux防火墙来验证下。开启命令如下
  # sudo service iptables start
5.   修改默认端口
  刚才说默认端口是80,这是可以修改的,还是在/usr/local/apache2/conf/httpd.conf配置文件中,找到Listen信息,把默认端口80改为其他可用端口即可。
  #
  # Listen: Allows you to bind Apache to specific IP addresses and/or
  # ports, in addition to the default. See also the <VirtualHost>
  # directive.
  #
  # Change this to Listen on specific IP addresses as shown below to
  # prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
  #
  #Listen 12.34.56.78:80
  Listen 80
  
6.添加自己的目录
  我有想把/path/to/my/workspace/目录也添加到web服务器上,映射为http://192.168.1.101/git/,Apache配置如下:
  
  Alias /git/ &quot;/path/to/my/workspace/&quot;
  
  <Directory &quot;/path/to/my/workspace&quot;>
     Options Indexes MultiViews
     AllowOverride None
     Order allow,deny
     Allow from all
  </Directory>
  
         配置完后,输入一下命令重启web服务,
    # /usr/local/apache2/bin/apachectl restart

         在浏览器中输入http://192.168.1.101/git/,结果报错:
  Forbidden
  You don't have permission to access /git/ on this server.
         折腾很久才发现是因为在CentOS系统中SELinux的问题,你可以通过命令
  ls -Z /var/www/icons和ls –Z /path/to/my/workspace看出其权限不太一样,用以下命令设置后即可:
    chcon -R -h -t httpd_sys_content_t /path/to/my/workspace

         版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: Linux下安装Apache