apache的基础信息
主配置目录 | /etc/httpd/conf |
---|---|
主配置文件 | /etc/httpd/conf/httpd.conf |
子配置目录 | /etc/httpd/conf.d/ |
子配置文件 | /etc/httpd/conf.d/*.conf |
默认发布目录 | /var/www/html |
默认发布文件 | index.html |
默认端口 | 80 |
默认安全上下文 | httpd_sys_content_t |
程序开启默认用户 | apache |
apache默认日志 | /etc/httpd/logs/* |
更改访问端口
默认访问80,当希望由其他端口(8080)进行访问时
将主配置文件中的默认访问端口
listen 80
改为
listen 8080
重启服务
systemctl restart httpd
查看apache使用的端口
netstat -antlupe |grep httpd
将8080端口加入火墙tcp端口
firewall-cmd --permanent --add-port=8080/tcp 加入8080端口
firewall-cmd --reload 重新加载
firewall-cmd --list-all 列出
public (default, active)
interfaces: eth0
sources:
services: dhcpv6-client dns ssh
ports: 8080/tcp
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
编辑默认访问文件
cd /var/www/html/
vim index.html 输入想要显示的内容
关闭selinux
getenforce 查看selinux的状态
setenforce 0 改为警告状态
getenforce 查看是否更改成功
systemctl restart httpd.service
测试
在浏览器输入访问ip地址+端口,查看能否访问默认文件中写入的信息
http://172.25.254.101:8080/
还原
使用不为默认可用端口的端口(6666)进行访问
加入端口
semanage port -a -t http_port_t -p tcp 6666 将6666端口加入可用端口
semanage port -l | grep http 查看
systemctl restart httpd.service 重启apache服务
主配置文件中的默认访问端口
listen 80
改为
listen 6666
重启服务
systemctl restart httpd
查看apache使用的端口
netstat -antlupe |grep httpd
将6666端口加入火墙tcp端口
firewall-cmd --permanent --add-port=6666/tcp 加入8080端口
firewall-cmd --reload 重新加载
firewall-cmd --list-all 列出
测试
由于上一个实验已经将selinux改为警告状态,直接进行访问
http://172.25.254.101:6666/
还原
修改默认发布文件
注:必须在默认发布目录下
主配置文件中的默认访问文件项进行修改
vim /etc/httpd/conf/httpd.conf
163 <IfModule dir_module>
164 DirectoryIndex test.html
165 </IfModule>
创建访问的指定文件
vim text.html
重启服务
systemctl restart httpd
测试
访问网页
http://172.25.254.101/
失败
可能是火墙没有允许http导致的
允许服务加入http
firewall-cmd --permanent --add-service=http
再次访问
成功
修改访问文件的顺序
访问文件允许多个存在,默认访问第一个
分别改为
163 <IfModule dir_module>
164 DirectoryIndex index.html test.html
165 </IfModule>
重启
systemctl restart httpd
测试
改为
163 <IfModule dir_module>
164 DirectoryIndex test.html index.html
165 </IfModule>
重启
systemctl restart httpd
测试
修改默认发布目录
主配置文件中的默认发布目录项进行修改
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/westos/html"
<Directory "/westos/html">
Require all granted
</Directory>
建立指定默认发布目录
mkdir -p /westos/html/
编辑发布文件(主配置文件中指定的默认发布文件)
vim /westos/html/index.html
为保证成功修改安全上下文
查看默认文件与新建文件的安全上下文
[root@desktop html]# ls -Zd /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[root@desktop html]# ls -Zd /westos/html/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/html/
发现安全上下文不同
修改新建文件的安全上下文
setenforce 1
semanage fcontext -a -t httpd_sys_content_t '/westos/html(/.*)?'
刷新
restorecon -RvvF /westos/html/
再次查看
[root@desktop html]# ls -Zd /westos/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /westos/html/
修改成功
重启
systemctl restart httpd
测试
访问显示新编辑内容(每个文件内容最好有所不同以做区分)