Apache静态网页的部署

16 篇文章 0 订阅

Apache 静态网站的配置

一、安装apache

yum直接安装apache

[root@TSL5 ~]# yum install httpd -y

启动服务,并加入到开机启动

[root@TSL5 ~]# systemctl start httpd
[root@TSL5 ~]# systemctl enable httpd
ln -s ‘/usr/lib/systemd/system/httpd.service’ ‘/etc/systemd/system/multi-user.target.wants/httpd.service’

在网页http://192.168.43.214查看:

在这里插入图片描述

二、配置服务文件

在网站数据目录中写入文件

[root@TSL5 html]# cat index.html
Welcome to Apache!!!

刷新页面

在这里插入图片描述

如果你想把保存网址的数据的改为 xxxxxx

mkdir xxxxxxx

[root@TSL5 ~]# vim /etc/httpd/conf/httpd.conf

在这里插入图片描述

在DocumentRoot 后面修改和<Directory “/home/wwwroot”>修改

[root@TSL5 ~]# mkdir /home/wwwroot
[root@TSL5 ~]# echo “Hello Apache” > /home/wwwroot/index.html

[root@TSL5 ~]# systemctl restart httpd

刷新网页:

在这里插入图片描述

三、SElinux安全子系统

为什么会出现默认界面呢?

原因是:selinux在调皮

查看selinux配置文件

[root@TSL5 ~]# vim /etc/selinux/config

在这里插入图片描述

enforcing:强制启动安全策略模式,将拦截服务的不合法请求。

permissive:遇到服务越权访问时,只发出警告而不强制拦截。

disabled:对于越权的行为不警告也不拦截。

[root@TSL5 ~]# getenforce
Enforcing

[root@TSL5 ~]# setenforce 0
[root@TSL5 ~]# getenforce
Permissive

在selinux中,getenforce查看当前selinux运行状态,setenforce可以临时修改[0|1]

再次刷新网页:

在这里插入图片描述

[root@TSL5 ~]# setenforce 1

[root@TSL5 ~]# ls -Zd /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
[root@TSL5 ~]# ls -Zd /home/wwwroot
drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot

system_u:代表用户进程身份

object_r:代表文件目录

httpd_sys_content_t:代表网站服务文件系统

针对上诉情况,需要semanage命令,把当前的网站目录/home/wwwroot的SElinux上下文修改为跟原始网站目录一样就ok了。

semanage命令

semanage命令是用于管理SElinux策略,格式为 semanage 选项 文件l

常用参数及作用:

-l:查询

-a:添加

-m:修改

-d:删除

向新的网站数据目录新添加一条SElinux安全上下文

[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*

restorecon命令使SElinux安全上下文立即生效,加-Rv参数指定目录进行递归操作

[root@TSL5 ~]# restorecon -Rv /home/wwwroot
restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

[root@TSL5 ~]# getenforce
Enforcing

在这里插入图片描述
在这里是不是觉得特别神奇!

四、个人用户主页功能

在httpd服务中,默认没有开启个人用户主页功能,我们需要编辑配置文件,进行修改

在这里插入图片描述
进行如下修改:

在这里插入图片描述

在家目录中创建用于保存网站数据的目录及首页面文件,再给家目录755权限,保证其他人可以读取到里面的内容

[root@TSL5 ~]# su - tsl
Last login: Fri Mar 27 18:32:57 CST 2020 on pts/5

[tsl@TSL5 ~]$ mkdir public_html
[tsl@TSL5 ~]$ echo " This is tsl’s websites" > public_html/index.html
[tsl@TSL5 ~]$ chmod -Rf 755 /home/tsl

重启服务,并再浏览器的地址栏输入网址:格式为 “网址/~用户名”

[root@TSL5 ~]# systemctl restart httpd

在网址栏输入:http://192.168.43.214/~tsl

在这里插入图片描述
为什么会被禁止呢?

这里应该是linux域的概念。

使用getsebool命令查询并过滤出所有HTTP协议相关的安全策略

[root@TSL5 ~]# getsebool -a | grep http
httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_graceful_shutdown --> on
httpd_manage_ipa --> off
httpd_mod_auth_ntlm_winbind --> off
httpd_mod_auth_pam --> off
httpd_read_user_content --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off
httpd_tmp_exec --> off
httpd_tty_comm --> off
httpd_unified --> off
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_use_sasl --> off
httpd_verify_dns --> off
named_tcp_bind_http_port --> off
prosody_bind_http_port --> off

这里的httpd_enable_homedirs --> off,默认是关闭的,我们需要改为on

setsebool命令加上-P 让修改后的SElinux安全策略永久生效

[root@TSL5 ~]# setsebool -P httpd_enable_homedirs=on

再次在网址栏输入:http://192.168.43.214/~tsl

在这里插入图片描述
有时候网址主不想直接将网页的内容显示出来,需要通过身份验证的用户才能访问里面的内容,例如公司内部就是这样的。

首先使用htppasswd命令生成密码数据库,-c参数表示一次生成,后面分别添加密码数据库存放的文件,以及需要验证的用户名称 。【注】用户不一定是系统的已经有的本地账户!

[root@TSL5 ~]# htpasswd -c /etc/httpd/passwd tsl
New password: #输入用于网页验证的密码
Re-type new password: #再次输入进行确认
Adding password for user tsl

在这里插入图片描述

进行如下修改:

31 <Directory “/home/*/public_html”>
32 AllowOverride all
33 authuserfile " /etc/httpd/passwd" #密码验证保存路径
34 authname " My Personal Website" #进行密码验证的提示信息
35 authtype basic
36 require user tsl #需要进行验证的用户名称
37

刷新页面:

在这里插入图片描述

五、虚拟主机功能

一般有几种模式:

1.基于ip地址

2.基于主机域名

3.基于端口号

下面进行基于主机域名的配置,这个是比较常用的。

我们定义IP地址与域名之间的对应关系

[root@TSL5 ~]# vim /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.43.214 www.baidu.com bbs.baidu.com tech.baidu.com

保证他们都能ping通

在这里插入图片描述

分别在/home/wwwroot中创建用于保存不同网站数据的三个目录,并向其中写入头文件

[root@TSL5 ~]# mkdir -p /home/wwwroot/www
[root@TSL5 ~]# mkdir -p /home/wwwroot/bbs
[root@TSL5 ~]# mkdir -p /home/wwwroot/tech
[root@TSL5 ~]# echo " WWW.baidu.com" > /home/wwwroot/www/index.html
[root@TSL5 ~]# echo " BBS.baidu.com> /home/wwwroot/bbs/index.html
[root@TSL5 ~]# echo " TECH.baidu.com> /home/wwwroot/tech/index.html

在httpd服务的配置文件中,大约113行处,分别追加写入三个基于主机的虚拟网站参数

[root@TSL5 ~]# vim /etc/httpd/conf/httpd.conf

113 <VirtualHost 192.168.43.214>
114 DocumentRoot “/home/wwwroot/www”
115 ServerName “www.baidu.com”
116 <Directory “/home/wwwroot/www”>
117 AllowOverride None
118 Require all granted
119
120
121 <VirtualHost 192.168.43.214>
122 DocumentRoot “/home/wwwroot/bbs”
123 ServerName “bbs.baidu.com”
124 <Directory “/home/wwwroot/bbs”>
125 AllowOverride None
126 Require all granted
127
128
129 <VirtualHost 192.168.43.214>
130 DocumentRoot “/home/wwwroot/tech”
131 ServerName “tech.baidu.com”
132 <Directory “/home/wwwroot/tech”>
133 AllowOverride None
134 Require all granted
135
136

这里需要注意的是,当前的网站数据目录还在/home/wwwroot目录中,因此我们还需要正确的设置网站数据目录的上下文

[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www/*
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs/*
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech/*

[root@TSL5 ~]# restorecon -Rv /home/wwwroot

[root@TSL5 ~]#systemctl restart httpd
在这里插入图片描述

基于端口号:

[root@TSL5 ~]# mkdir -p /home/wwwroot/520

[root@TSL5 ~]# mkdir -p /home/wwwroot/521

[root@TSL5 wwwroot]# echo “port:520” > /home/wwwroot/520/index.html
[root@TSL5 wwwroot]# echo “port:521” > /home/wwwroot/521/index.html

在httpd服务配置文件中,写入如下:

[root@TSL5 ~]# vim /etc/httpd/conf/httpd.conf

41 #Listen 12.34.56.78:80
42 Listen 80
43 Listen 520
44 Listen 521

[root@TSL5 ~]# vim /etc/httpd/conf/httpd.conf

114 <VirtualHost 192.168.43.214:520>
115 DocumentRoot “/home/wwwroot/520”
116 ServerName “www.baidu.com”
117 <Directory “/home/wwwroot/520”>
118 AllowOverride None
119 Require all granted
120
121
122 <VirtualHost 192.168.43.214:521>
123 DocumentRoot “/home/wwwroot/521”
124 ServerName “bbs.baidu.com”
125 <Directory “/home/wwwroot/521”>
126 AllowOverride None
127 Require all granted
128
129

[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/520
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/520/*
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/521
[root@TSL5 ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/521/*
[root@TSL5 ~]# restorecon -Rv /home/wwwroot
[root@TSL5 ~]# systemctl restart httpd

在这里插入图片描述

如果重启服务报错,进行如下操作:

[root@TSL5 ~]# semanage port -l | grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989

[root@TSL5 ~]# semanage port -a -t http_port_t -p tcp 520

[root@TSL5 ~]# semanage port -a -t http_port_t -p tcp 521

[root@TSL5 ~]# semanage port -l | grep http

[root@TSL5 ~]# systemctl restart httpd

六、Apache的访问控制

[root@TSL5 ~]# mkdir /var/www/html/server
[root@TSL5 ~]# echo “Successful” > /var/www/html/server/index.html

大概在httpd服务配置文件129行左右,添加如下:

[root@TSL5 ~]# vim /etc/httpd/conf/httpd.conf

<Directory “/var/www/html/server”>
Order allow,deny
Allow from 192.168.43.214

521

[root@TSL5 ~]# semanage port -l | grep http

[root@TSL5 ~]# systemctl restart httpd
下面由于电脑配置问题,卡住了,有兴趣的可以了解一下,我们共同探讨。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_44902227

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值