设为首页 收藏本站
查看: 582|回复: 0

[经验分享] Apache简单配置实例

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-21 08:51:30 | 显示全部楼层 |阅读模式
实验环境:redhat 6.5  selinux=enforcing
实验主机:172.25.16.1
Apache web服务器配置文件:/etc/httpd/conf/httpd.conf
Web站点文件目录:/var/www/html
CGI程序文件:/var/www/cgi-bin
Apache web服务器手册:/var/www/html/manual
Apache 访问日志:/etc/httpd/logs/access.log
Apache 错误日志:/etc/httpd/logs/error.log

[iyunv@server1 ~]# yum install -y httpd
[iyunv@server1 ~]# /etc/init.d/httpd start
[iyunv@server1 ~]# netstat -antlp | grep httpd
tcp        0      0 :::80                       :::*                        LISTEN      1179/httpd

一.修改apache的默认发布目录
[iyunv@server1 ~]# cd /var/www/html/
[iyunv@server1 html]# ls
[iyunv@server1 html]# echo `hostname` > index.html
1.修改配置文件,添加要访问的站点目录
[iyunv@server1 ~]# vim /etc/httpd/conf/httpd.conf
     DocumentRoot “/apache/html”
[iyunv@server1 ~]# mkdir -p /apache/html
2.添加访问页面
[iyunv@server1 ~]# echo "apache's html" > /apache/html/index.html
[iyunv@server1 ~]# cat /apache/html/index.html
apache's html
[iyunv@server1 ~]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
#此时访问不到网页,因为web服务器的httpd进程设置了selinux的安全上下文标签
3.设置安全上下文
[iyunv@server1 httpd]# yum install setroubleshoot-server   
[iyunv@server1 httpd]# ls -Zd /var/www/html/    #查看默认发布目录的上下文
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[iyunv@server1 httpd]# ls -Zd /apache/html/     #查看新建目录的上下文
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /apache/html/
[iyunv@server1 httpd]# semanage fcontext -a -f httpd_sys_content_t '/apache(/.*)?'
#fcontext规则中用扩展正则表达式(/.*)?来随意匹配/后的任何字符,第归修改目录的安全上下文
#现在可以访问到/apache/html/index.html的页面

//出于权限和安全方面,我们可以对访问用户的身份进行设定从而避免非法用户的访问、
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from 172.25.16.1         //只允许本访问或设置为本段ip访问 172.25.16.0/24

二.设置普通用户进入自己的家目录,就像登陆博客一样进入自己的主页
1.修改配置文件,设置家目录
[iyunv@server1 ~]# vim /etc/httpd/conf/httpd.conf
  UserDir html
2.添加系统用户并建立用户访问目录
[iyunv@server1 ~]# useradd test
[iyunv@server1 ~]# cd /home/test
[iyunv@server1 test]# mkdir html      
[iyunv@server1 html]# echo "it's test" > index.html
[iyunv@server1 html]# cat index.html
it's test
2.修改bool值
[iyunv@server1 ~]# getsebool -a | grep httpd_enable_homedir
[iyunv@server1 ~]# setsebool -P httpd_enable_homedirs on  
4.修改访问目录权限,让浏览器可以访问   
[iyunv@server1 html]# ls -ld /home/test         
drwx------. 3 apache test 4096 Aug  7 18:52 /home/test
#在浏览器输入url :http://172.25.16.1/~test

三.建立虚拟主机, 虚拟主机是指在一台web服务器上同时存在多个web站点,他们可以有不同的ip地址或域名。
#apache支持的虚拟主机类型包括:1)基于不同域名的虚拟主机;2)基于不同ip地址的虚拟主机
1.新建两个不同的web站点目录并区别访问页面
[iyunv@server1 ~]# mkdir -p /virtual/westos/html/   
[iyunv@server1 html]# cat index.html
     The page is www.westos.com
[iyunv@server1 ~]# mkdir -p /virtual/qq/html/
[iyunv@server1 html]# cat index.html
     The page is www.qq.com
2.修改安全上下文
[iyunv@server1 virtual]# semanage fcontext -a -t httpd_sys_content_t '/virtual(/.*)?'
[iyunv@server1 virtual]# restorecon -FvvR /virtual/
3.修改配置文件
[iyunv@server1 logs]# vim /etc/httpd/conf/httpd.conf
     Listen 80
     NameVirtualHost *:80   //指定哪台服务器用于响应客户端虚拟主机的连接请求
     Include conf.d/*.conf     //因为有这条参数,所以可以把下面的配置文件写在这conf/*.conf目录下,为了方便也可直接在这个文件中直接添加Virtualhost的语句块
4.设置默认访问目录
[iyunv@server1 logs]# cat /etc/httpd/conf.d/default.conf   
     DocumentRoot /var/www/html
     customlog "logs/default.log" combined
5.设置虚拟主机访问目录
[iyunv@server1 logs]# cat /etc/httpd/conf.d/virtual.conf   #要与NameVirtualHost *:80一致
        Servername www.westos.com       #客户端的访问域名
    DocumentRoot /virtual/westos/html   #域名对应的访问站点目录
    customlog "logs/baidu.log" combined    #日志存放的位置
        Servername www.qq.com
    DocumentRoot /virtual/qq/html
    customlog "logs/baidu.log" combined
[iyunv@server1 logs]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[iyunv@server1 logs]# netstat -antlp | grep httpd
tcp        0      0 :::80                       :::*                        LISTEN      7194/httpd         
#在客户端浏览器的/etc/hosts文件或DNS记录文件中 写入ip与域名的对应关系即可采用域名访问测试




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-101873-1-1.html 上篇帖子: 架设CA服务器实现https通信,web服务器使用CA自签证书与https通信 下篇帖子: Apache禁止指定user_agent
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表