ab520025520025 发表于 2018-11-21 11:00:24

apache安装配置文档

  Apache安装与配置
  配置文件说明官方文档http://httpd.apache.org/docs/2.2/
  帮助手册http://apache.jz123.cn/
下载与安装过程
cd /root/tools  
wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.29.tar.gz   (官方网站下载)
  
tar -jxvf http*
  
cd httpd-2.2.27
  
./configure \
  
--prefix=/application/apache2.2.29 \
  
--enable-deflate \
  
--enable-expires \
  
--enable-headers \
  
--enable-modules=most \
  
--enable-so \
  
--with-mpm=worker \
  
--enable-rewrite
  --enable-mpms-shared=all \可以随意切换mpm类型
  --with-mpm=event            2.4以上支持
  参数说明:
--prefix=PREFIX   install architecture-independent files inPREFIX  
                  指定安装路径,默认是这个。
  
--enable-deflateDeflate transfer encoding support提供对内容压缩编码传输支持。是apache
  调优的重要选项
  创建链接文件
  ln -s/application/apache2.2.27 /application/apache
  方便生产环境中切换
/application/apache/bin/apachectl-t 检查语法  
/application/apache/bin/apachectl-l 列出编译的模块
  启动apache:
  /application/apache/bin/apachectl-start
  在浏览器输入127.0.0.1 看是否有 It works!!
  无法启动原因分析:
  1.iptables 和 selinux是否关闭
iptables -F   或者是  
iptables-I input -p tcp --dport 80 -j ACCEPT
  
setenforce 0 暂时关闭方法
  
cat /etc/selinux/config | grep SELINUX=disable #永久关闭地点
  2.查看端口是否存在
netstat -lnt | grep 80  3.查看进程是否存在
ps -ef | grep http  3.本地wget 测试,模拟用户检查
wget http://127.0.0.1  
说明本身配置没问题
  
# wget http://127.0.0.1
  
--2015-03-30 09:55:15-- http://127.0.0.1/
  
正在连接 127.0.0.1:80... 已连接。
  
已发出 HTTP 请求,正在等待回应... 200 OK
  
长度:44
  
正在保存至: “index.html”
  

  
100%[===========================================================================================>]44          --.-K/s   in 0s
  

  
2015-03-30 09:55:16 (1.52 MB/s) - 已保存“index.html” )
  4.查看apache错误日志
# cat /application/apache/logs/error_log  
Digest: generating secretfor digest authentication ...
  
Digest: done
  
Apache/2.2.27 (Unix) DAV/2 configured-- resuming normal operations
  
File doesnot exist: /application/apache2.2.27/htdocs/favicon.ico
虚拟主机配置
基于域名的虚拟主机配置
  例子:增加一个www.zjx.com的域名
  前提:一定要开启 NameVirtualHost *:80不论是在主配置文件还是extra中(但前提是关闭主配置文件中的DocumentRoot)
  修改主配置文件中包含httpd-vhosts文件的配置(先备份)
  vim/application/apache/conf/httpd.conf
  …………………
  #Virtual hosts
  #Include conf/extra/httpd-vhosts.conf去掉#号
  ……………………….
  修改虚拟主机配置文件
  vim/application/apache/conf/extra/httpd-vhosts.conf
  NameVirtualHost *:80   注意这里监听所有地址,如果访问127.0.0.1会返回www.zjx.com的主页
  
  ServerAdminwebmaster@dummy-host.example.com
  DocumentRoot "/var/www/zjx"
  ServerName www.zjx.com
  ServerAlias zjx.com
  ErrorLog "logs/zjx.error_log"
  CustomLog "logs/zjx.access_log"common
  
  注意另一个标签要删除否则会报错
  如果本机测试还要修改hosts文件。
  创建虚拟主机的站点目录,并在目录下新建一个文件,输入些内容
  mkdir -p /var/www/zjx
  echo "i am zjx ,my qq is 282297681">/var/www/zjx/index.html
  此时会报403错误。原因是主配置文件中没有对此站点目录进行许可所以要加入一下配置
  不能有空格!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  vim/application/apache/conf/httpd.conf
  
  AllowOverride None
  Options None
  Order allow,deny
  Allow from all
  
  测试一下是否成功
  # wgetwww.zjx.com
  --2015-04-02 20:28:15-- http://www.zjx.com/
  正在解析主机 www.zjx.com... 127.0.0.1
  正在连接 www.zjx.com|127.0.0.1|:80... 已连接。
  已发出 HTTP 请求,正在等待回应... 200 OK
  长度:30
  正在保存至: “index.html.2”
  100%[============================================================>]30          --.-K/s   in 0s
  2015-04-02 20:28:15 (1.79 MB/s) - 已保存“index.html.2” )
基于IP地址的虚拟主机配置
  环境:yum安装的apache
  # apachectl -v
  Server version: Apache/2.2.15 (Unix)
  Server built:   Oct 162014 14:48:21
  先在主配置文件中注释掉中心主机配置
  #DocumentRoot "/var/www/html"
  在主配置文件最后加入
  
    DocumentRoot /www/hehe
  
    ServerNamewww.hehe.com
  

  

  
    DocumentRoot /www/haha
  
    ServerNamewww.haha.com
  

  建立目录输入内容
# echo "i am hehe ">/www/hehe/index.html  
# echo "i am haha ">/www/haha/index.html
  
#
  
# tree /www
  
/www
  
├── haha
  
│   └── index.html
  
└── hehe
  
    └── index.html
  

  
2 directories, 2 files
  
# httpd -t
  
Syntax OK
  添加虚拟网卡
  # ip addr add 192.168.80.10/24 dev eth0
  # ip addr show
  1: lo:mtu 16436 qdisc noqueuestate UNKNOWN
  link/loopback00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scopehost lo
  inet6 ::1/128 scopehost
  valid_lft foreverpreferred_lft forever
  2: eth0:mtu 1500 qdiscpfifo_fast state UP qlen 1000
  link/ether00:0c:29:68:75:9c brd ff:ff:ff:ff:ff:ff
  inet 192.168.80.9/24 brd 192.168.80.255 scope global eth0
  inet 192.168.80.10/24 scopeglobal secondary eth0
  inet6fe80::20c:29ff:fe68:759c/64 scope link
  valid_lft foreverpreferred_lft forever
  重启服务测试
  # apachectl graceful
  # wget 192.168.80.9
  --2015-04-16 23:32:18-- http://192.168.80.9/
  正在连接 192.168.80.9:80... 已连接。
  已发出 HTTP 请求,正在等待回应... 200 OK
  长度:11
  正在保存至: “index.html”
  100%[===========================================================================================>]11          --.-K/s   in 0s
  2015-04-16 23:32:18 (1.06 MB/s) - 已保存“index.html” )
  # cat index.html
  i am hehe
  #
  # wget 192.168.80.10
  --2015-04-16 23:32:33--http://192.168.80.10/
  正在连接 192.168.80.10:80... 已连接。
  已发出 HTTP 请求,正在等待回应... 200 OK
  长度:11
  正在保存至: “index.html.1”
  100%[===========================================================================================>]11          --.-K/s   in 0s
  2015-04-16 23:32:33 (1.84 MB/s) - 已保存“index.html.1” )
  # cat index.html.1
  i am haha
  #
基于端口的虚拟主机
  同上面的步骤在配置文件最后加入
  
  DocumentRoot /www/hehe
  ServerNamewww.hehe.com
  
  
  DocumentRoot /www/lala
  ServerNamewww.lala.com
  
  修改监听端口
  Listen 80
  listen 8080
  重启服务
  # apachectl graceful
  #
  # wget 192.168.80.9:8080
  --2015-04-16 23:42:20--http://192.168.80.9:8080/
  正在连接 192.168.80.9:8080... 已连接。
  已发出 HTTP 请求,正在等待回应... 200 OK
  长度:11
  正在保存至: “index.html”
  100%[===========================================================================================>]11          --.-K/s   in 0s
  2015-04-16 23:42:20 (1.93 MB/s) - 已保存“index.html” )
  # cat index.html
  i am lala
基于域名的再配置
  NameVirtualHost 192.168.80.9:80   注意此处要去掉注释并指明监听什么地址什么端口的虚拟主机
  
  DocumentRoot /www/hehe
  ServerName www.hehe.com
  
  
  DocumentRoot /www/fuck
  ServerName www.fuck.com
  
  域名不同,IP和端口都相同。
  hosts文件加入以下内容
  192.168.80.9 www.fuck.com
  192.168.80.9 www.hehe.com
  测试
  # wget www.fuck.com
  --2015-04-16 23:57:21-- http://www.fuck.com/
  正在解析主机 www.fuck.com... 192.168.80.9
  正在连接 www.fuck.com|192.168.80.9|:80... 已连接。
  已发出 HTTP 请求,正在等待回应... 200 OK
  长度:11
  正在保存至: “index.html”
  100%[===========================================================================================>]11          --.-K/s   in 0s
  2015-04-16 23:57:21 (2.00 MB/s) - 已保存“index.html” )
  # cat index.html
  i am fuck
  之前的疑惑解决了:
  在原来的192.168.80.8也就是编译安装的apache中配置虚拟主机,虽然生效了但是,当访问127.0.0.1是返回的页面仍然是虚拟主机的页面,这是因为在配置虚拟主机时默认监听的了所有ip所有当有请求过来时会默认返回第一个虚拟主机中的页面。如果把配置文件中两个虚拟主机中的位置对换一下就会发现,返回的是第二个虚拟主机的主页,由此证明,在配置基于域名的虚拟主机时最好指定监听的地址
apache2.4版本以后虚拟主机的配置问题
  参考博客http://blog.csdn.net/lilanfei/article/details/40377503
总结:出现403错误的几种可能原因
  1.apache配置文件中没有对站点目录进行许可,即在httpd.conf中缺少如下配置
  vim/application/apache/conf/httpd.conf
  
  AllowOverride None
  Options None
  Order allow,deny
  Allow from all
  
  2.站点目录权限问题,apache无法访问
  注意:以源码编译安装的默认appache主进程用户是daemon
  vim /application/apache/conf/httpd.conf
  
  AllowOverride None
  Options IndexesFollowSymlinks
  Order allow,deny
  #Allow from all
  
  3.站点目录下无首页文件,而apache的配置文件又禁止了首页浏览
  vim /application/apache/conf/httpd.conf
  
  AllowOverride None
  Options -Indexes FollowSymlinks
  Order allow,deny
  Allow from all
  
  rm -rf/var/www/zjx/index.html
  #indexes前面加-可以,也可以去掉Indexes 或者改为 Options None
  4.默认的配置文件提供的用户没有访问站点目录的权限。
  vim /application/apache/conf/httpd.conf
  # If you wish httpd to run as a different user or group, youmust run
  # httpd as root initially and it will switch.
  #
  # User/Group: The name (or #number) of the user/group to runhttpd as.
  # It is usually good practice to create a dedicated user andgroup for
  # running httpd, as with most system services.
  #
  User daemon
  Group daemon
  编译的apache默认的用户:daemon(同rsync)
  yum安装的apache默认是:
  默认创建目录权限755,apache可以读取网页信息,但不能上传。
  目录755 文件644 对于上传的目录在设置成apache用户的权限。


页: [1]
查看完整版本: apache安装配置文档