简介
httpd是Apache服务器的主程序
默认端口:80
systemctl {start|stop|restart|status} httpd.service
网站文件
配置文件名称 | 位置 |
服务目录 | /etc/httpd/ |
主配置文件 | /etc/httpd/conf/httpd.conf |
网站数据目录 | /var/www/html |
访问日志 | /var/log/httpd/access_log |
错误日志 | /var/log/httpd/error_log |
配置文件参数
ServerRoot "/etc/httpd" | 服务主目录 |
Listen 80 | 监听的 IP 地址与端口号 |
Include conf.modules.d/*.conf | 子配置文件 |
User apache Group apche | 运行服务的用户 运行服务的用户组 |
ServerAdmin root@localhost | 管理员邮箱 |
ServerName www.example.com:80 | 网站域名 |
DocumentRoot "/var/www/html" | 网络根目录 |
ErrorLog "logs/error_log" | 错误日志文件 |
CustomLog "logs/access_log" common | 访问记录日志文件 |
LoadModule php7 | 加载php的模块 |
LogLevel warn | |
AddDefaultCharset UTF-8 |
|
EnableSendfile on |
简单搭建
查看网络根目录位置
[root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep DocumentRoot | grep -Ev "#|$&"DocumentRoot "/var/www/html"
在网站根目录下写入一个index.html文件,内容自定义
[root@localhost ~]# echo hello >> /var/www/html/index.html
自定义修改默认文件
[root@localhost 桌面]# cat /etc/httpd/conf/httpd.conf | grep DirectoryIndex | grep -Ev "#|$&"
DirectoryIndex shell.html //默认是index.html
自定义修改默认发布目录
[root@localhost ~]# mkdir /www/html -p
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
[root@localhost ~]# cat /etc/httpd/conf/httpd.conf | grep /www/html
DocumentRoot "/www/html"
<Directory "/www/html">
<Directory "/www/html">
[root@localhost ~]# vim /www/html/index.html
[root@localhost ~]# cat /www/html/index.html
woshi index.html
[root@localhost ~]# vim /www/html/123.html
[root@localhost ~]# cat /www/html/123.html
woshi 123.html
root@localhost ~]# systemctl restart httpd.service
访问是发现没有直接读取index.html文件
提示没有访问权限,我们尝试给index.html这个文件加上一个可读可执行权限
[root@localhost ~]# chmod 777 /www/html/index.html & chmod 777 /www/html/123.html
[3] 36157
[3] 完成 chmod 777 /www/html/index.html
[root@localhost ~]# ll /www/html/
总用量 8
-rwxrwxrwx. 1 root root 15 7月 19 22:20 123.html
-rwxrwxrwx. 1 root root 17 7月 19 22:14 index.html
[root@localhost ~]# curl 10.1.82.10/index.html
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /index.html
on this server.</p>
</body></html>
加入满权限之后还是不能,这是因为centos系统有一个安全子系统(SELinux)对服务程序功能进行了限制,我们只要关掉即可
[root@localhost ~]# vim /etc/selinux/config
[root@localhost ~]# setenforce 0
[root@localhost ~]# cat /etc/selinux/config | grep -Ev "#|$&" | grep disabled
SELINUX=disabled
[root@localhost ~]# getenforce
Permissive
但是由于公司生产环境需要我们不能关闭SELinux,杜绝黑客攻击进来进行提权操作
[root@localhost ~]# vim /etc/selinux/config
[root@localhost ~]# setenforce 1
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# cat /etc/selinux/config | grep -Ev "#|$&" | grep enforcing
SELINUX=enforcing
我们把 SELinux 服务恢复到强制启用安全策略模式,然后分别查看原始网站数据的保存目录与当前网站数据的保存目录是否拥有不同的 SELinux 安全上下文值
[root@localhost ~]# ll -ldZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[root@localhost ~]# ll -ldZ /www/html/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www/html/
[root@localhost ~]# chcon -R --reference=/var/www/html/ /www/html/
[root@localhost ~]# ll -ldZ /www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /www/html/
用户段 system_u 代表系统进程的身份
角色段 object_r 代表文件目录的角色,
类型段 httpd_sys_content_t 代表网站服务的系统文件
除了chcon命名之外semanage命名也可以进行SELinux的安全上下文的修改
//修改回来
[root@localhost ~]# semanage fcontext -a -t default_t /www/html
[root@localhost ~]# ll -ldZ /www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /www/html/
[root@localhost ~]# restorecon -Rv /www/html/
restorecon reset /www/html context system_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
restorecon reset /www/html/index.html context system_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
restorecon reset /www/html/123.html context system_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
//改类型段
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /www/html
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /www/html/*
[root@localhost ~]# restorecon -Rv /www/html/
restorecon reset /www/html context system_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
[root@localhost ~]# ll -ldZ /www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /www/html/