[linux学习](linuxprobe课程)十 使用Apache服务部署静态网站

做个apche网站

  1. 安装httpd服务(apache的服务就是httpd
  2. 加到启动项
  3. 访问localhost或者IP或者域名
  4. 完成
  5. 在documentroot路径下/htm/下加网页
  6. 访问这个路径

SELinux 某个安全子系统

  1. 在功能及文件某种类型方面对系统进行防护也叫安全上下文
  2. 功能指的就是服务的哪些参数可以开
  3. 文件某种类型指的是,文件是个人常用的,还是某个服务用到的,会有一部分服务被定义了默认的文件,还有一部分是系统自带文件也已经被配置
  4. 有三个状态,开启(enforcing)也叫强制启用安全策略模式,将拦截服务的不合法请求。关闭(disabled) 对于越权的行为不警告也不拦截。警告(permissive)遇到服务越权访问时,只发出警告而不强制拦截。
  5. 一般情况下SELinux默认打开,最好打开,还是打开算了,别纠结,有问题处理问题,但是别关他
  6. getenforce 获取selinux状态
  7. setenforce 0(/1) 临时配置selinux状态,0是关闭但是实际状态是警告,1是开启

个人用户主页

  1. 打开httpd服务的这个功能,第17行的UserDir disabled参数前面加上井号(#),表示让httpd服务程序开启个人用户主页功能;同时再把第24行的UserDir public_html参数前面的井号(#)去掉(UserDir参数表示网站数据在用户家目录中的保存目录名称,即public_html目录,等同于主配置文件里的rootdocument参数,意味着个人用户主页的文件都在这个下面,也相当于tomcat的webapps),保存并退出
[root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf 
 1 #
 2 # UserDir: The name of the directory that is appended onto a user's home
 3 # directory if a ~user request is received.
 4 #
 5 # The path to the end user account 'public_html' directory must be
 6 # accessible to the webserver userid. This usually means that ~userid
 7 # must have permissions of 711, ~userid/public_html must have permissions
 8 # of 755, and documents contained therein must be world-readable.
 9 # Otherwise, the client will only receive a "403 Forbidden" message.
 10 #
 11 <IfModule mod_userdir.c>
 12 #
 13 # UserDir is disabled by default since it can confirm the presence
 14 # of a username on the system (depending on home directory
 15 # permissions).
 16 #
 17 # UserDir disabled
 18 
 19 #
 20 # To enable requests to /~user/ to serve the user's public_html
 21 # directory, remove the "UserDir disabled" line above, and uncomment
 22 # the following line instead:
 23 # 
 24   UserDir public_html
 25 </IfModule>
 26 
 27 #
 28 # Control access to UserDir directories. The following is an example
 29 # for a site where these directories are restricted to read-only.
 30 #
 31 <Directory "/home/*/public_html">
 32 AllowOverride FileInfo AuthConfig Limit Indexes
 33 Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
 34 Require method GET POST OPTIONS
 35 </Directory>
  1. 用户自己创建文件,进入到home目录,创建public_html目录(根据用户配置文件的配置起名),在该目录下创建index.html,给自己的家目录赋权755,让其他人可以看到,很重要,刚才研究了半天,发现赋权只赋了下面的文件,没有给自几的家目录赋权,导致页面还是forbidden

[root@linuxprobe home]# su - linuxprobe
Last login: Fri May 22 13:17:37 CST 2017 on :0
[linuxprobe@linuxprobe ~]$ mkdir public_html
[linuxprobe@linuxprobe ~]$ echo “This is linuxprobe’s website” > public_html/index.html
[linuxprobe@linuxprobe ~]$ chmod -Rf 755 /home/linuxprobe

  1. 开放SELinux域中个人主页的参数
    getsebool -a|grep httpd 获取域状态(get 获取,se selinux ,bool 布尔值,即状态,off代表0,on代表1)
    setsebool -P httpd_enable_homedirs=on 设置个人用户功能可以用(-P永久生效)
  2. 浏览器访问
    localhost/~hupeng (localhost 本机,/ 网站根路径,~ 个人用户 ,hupeng 用户名 中间连着,也没有哪个网站中间带空格的吧)

个人用户主页带密码

  1. root用户
  2. 生成某个网站登陆用户的密码存放文件到某个路径下
    htpasswd -c /etc/httpd/passwd dage (ht http,passwd 密码,-c 第一次生成 ,/etc/httpd/passwd 密码文件的存放路径,dage登陆网站时用的名称)
    (输入密码)
  3. 编辑httpd的用户配置文件,让他支持密码(31行-35行)

[root@linuxprobe ~]# vim /etc/httpd/conf.d/userdir.conf
27 #
28 # Control access to UserDir directories. The following is an example
29 # for a site where these directories are restricted to read-only.
30 #
31 <Directory “/home/*/public_html”>
32 AllowOverride all
#刚刚生成出来的密码验证文件保存路径
33 authuserfile “/etc/httpd/passwd”
#当用户尝试访问个人用户网站时的提示信息
34 authname “My privately website”
35 authtype basic
#用户进行账户密码登录时需要验证的用户名称
36 require user linuxprobe
37
[root@linuxprobe ~]# systemctl restart httpd
4. 再次访问

虚拟网站主机功能

类似于个人用户主页,也是将一个主机运行多个程序,也类似于tomcat下webapps中多个服务,以及nginx转发
可以通过ip地址,域名,以及端口进行类似转发,每个ip,域名,或每个端口对应一个目录,每个目录又可以进行单独的配置,因为共用了一个httpd服务,所以叫做虚拟网站主机,并且在配置文件里是以Virtualhost节点进行配置

配置文件

# 30多行,自己找
Listen 80
Listen 666
Listen 888
 # **省略一部分内容接下来是113行左右,因为配置文件是按着从上到下的顺序读取的,目前不太了解这个顺序对服务的影响,暂时按着书上要求来做**
 <VirtualHost 192.168.10.100>
 DocumentRoot /home/www/hu
 ServerName www.hu.com
 <Directory /home/www/hu >
 AllowOverride None
 Require all granted
SetEnvIf User-Agent "Firefox" ff=1
Order deny,allow
Deny from env=ff 
Order allow,deny
Allow from 192.168.10.200
</Directory>
 </VirtualHost>
<VirtualHost 192.168.10.150>
 DocumentRoot /home/www/peng
 ServerName www.peng.com
 <Directory /home/www/peng >
 AllowOverride None
 Require all granted
 </Directory>
 </VirtualHost>
<VirtualHost 192.168.10.200>
 DocumentRoot /home/www/haha
 ServerName www.haha.com
 <Directory /home/www/haha >
 AllowOverride None
 Require all granted
 </Directory>
 </VirtualHost>
<VirtualHost 192.168.10.200:666>
 DocumentRoot /home/www/666
 ServerName www.haha.com
 <Directory /home/www/666 >
 AllowOverride None
 Require all granted
 </Directory>
 </VirtualHost>
<VirtualHost 192.168.10.100:888>
 DocumentRoot /home/www/888
 ServerName www.haha.com
 <Directory /home/wwwroot/888 >
 AllowOverride None
 Require all granted
 </Directory>
 </VirtualHost>

每个VirtualHost节点都是一个虚拟网站的配置

  1. 虚拟的IP及端口,可以是多个IP对应多个虚拟主机,也可以是多个端口,不填写默认是80,如果重启服务后访问不到,可能是端口被上下文限制做如下操作
# 查看哪些端口是允许httpd服务的
semanage port -l|grep http
# 设置新增的两个端口
semanage port -a -t http_port_t -p tcp 666
semanage port -a -t http_port_t -p tcp 888
  1. DocumentRoot /home/www/888 网站文件存放的地址,如果不能访问,看下上下文,ls -Zd /home/www/888,如果上下文不是httpd相关的就设置下,
ls -Zd /home/www/888
semanage fcontext -a -t httpd_sys_content_t /home/www
semanage fcontext -a -t httpd_sys_content_t /home/www/*
semanage fcontext -a -t httpd_sys_content_t /home/www/888
semanage fcontext -a -t httpd_sys_content_t /home/www/888/*
  1. servername www.hu.com 配置域名,需要修改host配置文件
vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.150 www.peng.com www.peng.com www.peng.com
~                                                         
  1. <Directory /home/wwwroot/888 >xxxxxx </Directory>配置具体的参数
  2. AllowOverride None 不读取.htaccess 有点复杂,单独学习吧
    Require all granted 有点复杂,单独学习吧
    SetEnvIf User-Agent “Firefox” ff=1 设置火狐作为一个用户标签
    Order deny,allow 禁止优先级高,禁止过滤掉的再允许
    Deny from env=ff 禁止了上面定义的用户标签为火狐的请求的访问
    Order allow,deny 允许优先级高
    Allow from 192.168.10.200 允许这个IP访问,其他的都禁止

总结

学到目前为止,apche建立网站相对比较简单,允许多个网站部署在一个服务器,也允许服务层面的用户登陆,基本满足一些小网站需求,但根据目前的学习仅限于静态网站,需要重点掌握几个东西,主配置文件/etc/httpd/conf/httpd.config,SeLinux的两个限制,上下文(开始以为只有文件,然后发现端口也可以限制),域(也就是参数),包含了以下几个命令

# 看文件上下文,一般用于配置更改过路径的网站地址
# 需要注意目录配置时没有最后一个斜线,并且不能递归,只能/*
ls -Zd /home/www
# 配置文件上下文
semanage fcontext -a -t httpd_sys_content_t /home/www
semanage fcontext -a -t httpd_sys_content_t /home/www/*
# 查看端口的上下文
semanage port -l|grep tomcat
# 配置端口上下文
semanage port -a -t http_port_t -p tcp 666
# 使配置生效,这个能递归
restorecon -Rv /home/www/
# 查看及配置域,主要是某些配置文件参数配置后不生效,可能是默认限制了
getsebool -a|grep http
setsebool -P httpd_enable_homedirs=on
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值