ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Apache

2020-03-10 20:05:19  阅读:207  来源: 互联网

标签:httpd ## html conf Apache root


虚拟机网络配置:

[root@Apache ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens160  

[root@Apache ~]# nmcli connection down ens160

[root@Apache ~]# nmcli connection up ens160

[root@Apache ~]# ifconfig

虚拟机yum源配置:

[root@Apache ~]# df   ##查看挂载位置

[root@Apache ~]# cd /etc/yum.repos.d/

[root@Apache yum.repos.d]# vim westos.repo

#####1.Apache作用###################

在web被访问时通常使用http://的方式

https://    ##超文本传输协议

https://    超文本传输协议提供软件 如:Apache,nginx,stgw,jfe,Tengine

 

#####2.Apache的安装##################

[root@Apache ~]# dnf install httpd.x86_64 -y

#####3.Apache的启用#################

[root@Apache ~]# systemctl enable --now httpd    ##开启服务并设定服务位开机启动

[root@Apache ~]# firewall-cmd --list-all     ##查看火墙信息

[root@Apache ~]# firewall-cmd --permanent --add-service=http    ##在火墙中永久打开http访问

[root@Apache ~]# firewall-cmd --permanent --add-service=https   ##在火墙中永久打开https访问

[root@Apache ~]# firewall-cmd --reload    ##刷新火墙使设定生效

#####4.Apache的基本信息##############

服务名称:  httpd

配置文件:

/etc/httpd/conf/httpd.conf     ##主配置文件

/etc/httpd/conf.d/*.conf         ##子配置文件

默认发布目录:   /var/www/html

默认发布文件:   index.html

默认端口:      88     ##http             443   ##https

用户:   apache

日志:  /etc/httpd/logs

#####5.Apache的基本配置###############

一.Apache端口修改

[root@Apache ~]# netstat -antlupe | grep httpd    ##查看端口

[root@Apache ~]# ps aux | grep httpd     ##查看用户

[root@Apache ~]# vim /etc/httpd/conf/httpd.conf    ##修改端口为8080

[root@Apache ~]# netstat -antlupe | grep httpd

[root@Apache ~]# firewall-cmd --permanent --add-port=8080/tcp

[root@Apache ~]# firewall-cmd --reload

[root@Apache ~]# firewall-cmd --list-all

注意:

[root@Apache ~]# vim /etc/httpd/conf/httpd.conf     ##编辑端口为6666

[root@Apache ~]# systemctl restart httpd      ##无法指向6666

[root@Apache ~]# getenforce    ##selinux服务

[root@Apache ~]# setenforce 0    ##设置为警告模式,可重启

[root@Apache ~]# semanage port -l | grep http    ##查看http可使用的端口

[root@Apache ~]# semanage port -a -t http_port_t -p tcp 6666    ##添加可执行端口

[root@Apache ~]# setenforce 1   ##切换至强制模式,可执行重启

二.默认发布文件

[root@Apache ~]# cd /var/www/html/

[root@Apache html]# ls                             ##如果文件不存在访问的是apache默认测试页,文件存在访问的是文件内容

[root@Apache html]# vim index.html

[root@Apache html]# vim /etc/httpd/conf/httpd.conf    ##还原端口

[root@Apache html]# systemctl restart httpd

测试访问:

注意:

[root@Apache html]# vim westos.html

访问192.168.3.110时默认访问为index.html,访问westos时需指定文件

[root@Apache html]# vim /etc/httpd/conf/httpd.conf     ##编辑文件使默认访问为westos.html

[root@Apache html]# systemctl restart httpd

测试访问:

三.默认发布目录

[root@Apache html]# mkdir -p /westos/html  

[root@Apache html]# cd /westos/html/
[root@Apache html]# vim index.html

[root@Apache html]# vim /etc/httpd/conf/httpd.conf   ##    124行编辑默认发布目录  125-127进行授权

[root@Apache html]# systemctl restart httpd
[root@Apache html]# setenforce 0    ##修改为警告模式

测试访问:

 

[root@Apache html]# ls -Zd /var/www/

[root@Apache html]# ls -Zd /westos/html/      ##安全上下文不一样

[root@Apache html]# semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'    ##修改安全上下文

[root@Apache html]# restorecon -RvvF /westos/    ##刷新安全上下文

[root@Apache html]# setenforce 1    ##强制模式

测试访问:

#####6.Apache的访问控制##################

实验素材:

[root@Apache ~]# cd /var/www/html/

[root@Apache html]# mkdir westos

[root@Apache html]# cd westos/

[root@Apache westos]# vim index.html

[root@rhel7 ~]# firefox http://192.168.3.110/westos    ##在rhel7中测试访问

注意:

在rhel7中登陆rhel8访问页面是须加+X

[root@rhel7 ~]# ssh root@192.168.3.110 -X

一.基于客户端IP的访问控制

[root@Apache ~]# vim /etc/httpd/conf/httpd.conf    ##还原默认发布目录

<1>.ip黑名单

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

129 <Directory "/var/www/html/westos">    ##访问目录
130         Order Allow,Deny                         ##先读Allow在读Deny
131         Allow from All                               ##所有人可访问
132         Deny from 192.168.3.109             ## 109访问被拒绝
133 </Directory>

[root@Apache ~]# systemctl restart httpd

访问测试:

[root@rhel7 ~]# firefox http://192.168.3.110/westos    ##rhel7不可以

[root@Apache ~]# firefox http://192.168.3.110/westos     ##rhel8可以

注意:

范围小的时候被后读:

[root@Apache ~]# vim /etc/httpd/conf/httpd.conf     ##修改先读顺序

[root@Apache ~]# systemctl restart httpd

[root@rhel7 ~]# firefox http://192.168.3.110/westos    ##可访问

<2>.ip白名单

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

[root@Apache ~]# systemctl restart httpd

测试访问

[root@rhel7 ~]# firefox http://192.168.3.110/westos    ##可访问

[root@Apache ~]# firefox http://192.168.3.110/westos     ##拒绝访问

二.基于用户认证

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

<Directory "/var/www/html/westos">                                               
         AuthUserfile /etc/httpd/htpasswdfile                                             ##指定认证文件
         AuthName "Please input your name and password"            ##认证提示语
         AuthType basic                                                                     ##认证类型
         Require valid-user                                                                ##能够通过认证的用户
 </Directory>

[root@Apache ~]# htpasswd -cm /etc/httpd/htpasswdfile admin     ##生成认证文件(当文件haha不存在时使用)

[root@Apache ~]# systemctl restart httpd

注意:

当/etc/http/htpasswdfile存在那么在添加用户是不要加-c参数 否则会覆盖源文件内容

访问测试:

[root@rhel7 ~]# firefox http://192.168.3.110/westos

注意:切换用户是清除网页缓存历史(ctrl+shift+Delete)

[root@Apache ~]# vim /etc/httpd/conf/httpd.conf     ##指定admin通过

测试:

[root@rhel7 ~]# firefox http://192.168.3.110/westos

westos:无法跳转

#####7.Apache的虚拟主机#############

[root@Apache ~]# vim /etc/hosts     ##添加解析

[root@Apache ~]# mkdir -p /var/www/westos.com/{news,wenku}

[root@Apache ~]# cd /var/www/westos.com/news/

[root@Apache news]# vim index.html

[root@Apache news]# cd ../wenku/

[root@Apache wenku]# vim index.html

[root@Apache ~]# cd /var/www/html/

[root@Apache html]# vim index.html

[root@Apache html]# cd /etc/httpd/conf.d/

[root@Apache conf.d]# vim VirtHost.conf    ##存放主机对应关系

[root@Apache conf.d]# systemctl restart httpd

访问测试:

www.westos.com

news.westos.com

wenku.westos.com

#####8.Apache的语言支持#################

html   ##默认支持

一.php

[root@Apache conf.d]# cd /var/www/html/

[root@Apache html]# vim index.php     ##编写php测试页

[root@Apache html]# dnf install php.x86_64 -y  ##安装php

[root@Apache html]# systemctl restart httpd


测试:

二.cgi

[root@Apache html]# mkdir cgidir

[root@Apache html]# cd cgidir/

[root@Apache cgidir]# vim index.cgi

[root@Apache cgidir]# chmod +x index.cgi    ##可执行权限

[root@Apache cgidir]# ./index.cgi

[root@Apache cgidir]# cd /etc/httpd/conf.d/

[root@Apache conf.d]# vim VirtHost.conf

[root@Apache conf.d]# systemctl restart httpd


访问测试:

#####9.Apache的加密访问################

[root@Apache conf.d]# dnf install mod_ssl -y   ##安装插件

[root@Apache conf.d]# systemctl restart httpd   ##访问https://192.168.3.110时需添加安全书

 

[root@Apache ~]# openssl genrsa -out /etc/pki/tls/private/www.westos.com.key 2048   ##生成私钥

[root@Apache ~]# openssl req -new -key /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/certs/www.westos.com.csr       ##证书签证请求

[root@Apache ~]# openssl x509 -req -days 365 -in /etc/pki/tls/certs/www.westos.com.csr -signkey /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/certs/www.westos.com.crt            ##生成证书   (x509 证书格式,-req请求,-in加载签证名称)

[root@Apache ~]# vim /etc/httpd/conf.d/ssl.conf

[root@Apache ~]# systemctl restart httpd  

测试:

firefox:https://192.168.3.110    ##查看

 

[root@Apache ~]# mkdir /www/westos.com/{wenku,news,login} -p

[root@Apache ~]# echo "wenku's page" > /www/westos.com/wenku/index.html
[root@Apache ~]# echo "news's page" > /www/westos.com/news/index.html
[root@Apache ~]# echo "login's page" > /www/westos.com/login/index.html

[root@Apache ~]# semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?'   ##修改安全上下文

[root@Apache ~]# restorecon -RvvF /www/     ##刷新

[root@Apache ~]# cd /etc/httpd/conf.d/

[root@Apache conf.d]# vim vhost.conf

[root@Apache conf.d]# systemctl restart httpd

[root@Apache conf.d]# vim /etc/hosts

访问测试:

网页重写:

[root@Apache conf.d]# vim vhost.conf    ##将80端口直接转换为443端口   ^(/.*)$  客户地址栏输入的地址%{HTTP_HOST}   客户主机    $1 RewriteRule后面跟的第一串字符的值

[root@Apache conf.d]# systemctl restart httpd

访问测试:

输入:

自动跳转:

#####10.Squid+Apache######################

实验环境:

单网卡主机设定ip(装有Apache)不能上网,双网卡主机设定ip1可以连接单网卡主机,设定ip2可以上网   实验效果:   让单网卡主机不能上网但浏览器可以访问互联网页

实验环境设置:

Apache(ip1):

[root@Apache ~]# cd /etc/sysconfig/network-scripts/

[root@Apache network-scripts]# vim ifcfg-ens160     ##修改IP

[root@Apache network-scripts]# systemctl restart NetworkManager

[root@Apache network-scripts]# nmcli connection down ens160

[root@Apache network-scripts]# nmcli connection up ens160

[root@Apache network-scripts]# ifconfig

[root@Apache network-scripts]# ping www.baidu.com     ##无法上网

rhel8.0_node2(ip2):

[root@rhel8_node2 ~]# cd /etc/sysconfig/network-scripts/

[root@rhel8_node2 network-scripts]# vim ifcfg-ens160

[root@rhel8_node2 network-scripts]# systemctl restart NetworkManager

[root@rhel8_node2 network-scripts]# ping www.baidu.com    ##测试可上网

添加一块网卡:

[root@rhel8_node2 network-scripts]# vim ifcfg-ens224

[root@rhel8_node2 network-scripts]# systemctl restart NetworkManager

[root@rhel8_node2 network-scripts]# ifconfig

测试:在ip1中ping   ip2

在 ip2中:

配置yum仓库:

[root@rhel8_node2 ~]# cd /etc/yum.repos.d/

[root@rhel8_node2 yum.repos.d]# vim westos.repo

[root@rhel8_node2 yum.repos.d]# dnf install squid -y

##修改配置文件

[root@rhel8_node2 yum.repos.d]# vim /etc/squid/squid.conf

[root@rhel8_node2 yum.repos.d]# systemctl restart squid

一.squid正向代理:
在ip1中:

[root@Apache network-scripts]# ping www.baidu.com    ##不通

网页也不可以:

[root@Apache network-scripts]# firefox

                 

在ip2中添加3128端口:

[root@rhel8_node2 yum.repos.d]# firewall-cmd --permanent --add-port=3128/tcp

[root@rhel8_node2 yum.repos.d]# firewall-cmd --reload

[root@rhel8_node2 yum.repos.d]# firewall-cmd --list-all

测试:

在ip1中:

[root@Apache network-scripts]# ping www.baidu.com    ##ping不通

网页可以:

二.squid反向代理

注意:反代理的时候取消

实验环境:

192.168.3.110   Apache服务器

192.168.3.120   squid,没有数据负责缓存

[root@rhel8_node2 yum.repos.d]# vim /etc/squid/squid.conf    ##①vhost 支持虚拟域名 vport 支持虚拟端口   ②当

192.168.3.120的80端口被访问会从192.168.3.110的80端口缓存数据

[root@rhel8_node2 yum.repos.d]# systemctl restart squid

[root@rhel8_node2 ~]# firewall-cmd --permanent --add-port=80/tcp

[root@rhel8_node2 ~]# firewall-cmd --reload

[root@rhel8_node2 ~]# firewall-cmd --list-all

在ip1中测试:

 

 

标签:httpd,##,html,conf,Apache,root
来源: https://blog.csdn.net/weixin_46102303/article/details/104715687

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有