Linuxapache安装

Apache 介绍

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。

shell 复制代码
Apache的主程序名叫httpd。

apache安装

shell 复制代码
[root@sxw ~]# systemctl stop firewalld
[root@sxw ~]# systemctl disable firewalld
[root@sxw ~]# setenforce 0
[root@sxw ~]# yum install -y httpd
[root@sxw ~]# systemctl start httpd
[root@sxw ~]# netstat -lntp | grep 80 #查看apache端口
tcp6       0      0 :::80                   :::*                    LISTEN      2776/httpd
#端口80.可以改
shell 复制代码
index.html:默认访问网站的主页名称
默认发布网站的目录:/var/www/html
apache目录介绍
shell 复制代码
apache的工作目录:
conf   存储配置文件
conf.d 存储配置子文件
logs   存储日志 
modules 存储模块
run    存储Pid文件,存放的pid号码。是主进程号
shell 复制代码
认识主配置文件:
# vim /etc/httpd/conf/httpd.conf 
ServerRoot "/etc/httpd"             #定义工作目录
Listen 80                           #监听端口
Listen 192.168.2.8:80 指定监听的本地网卡 可以修改
User apache    					    # 子进程的用户,有可能被人改称www账户
Group apache   						# 子进程的组
ServerAdmin root@sxw  		# 设置管理员邮件地址
DocumentRoot "/var/www/html"        # 发布网站的默认目录,想改改这里。
IncludeOptional conf.d/*.conf       # 包含conf.d目录下的所有*.conf配置文件

# 设置DocumentRoot指定目录的属性
<Directory "/var/www/html">   		# 网站容器开始标识
Options Indexes FollowSymLinks   	# 找不到主页时,链接到网站目录以外,如测试页面
AllowOverride None               	# 对网站设置特殊属性:none不设置特殊属性,all允许
Require all granted                 # granted表示允许所有人访问,denied表示拒绝所有人访问
</Directory>    					# 容器结束
DirectoryIndex index.html      		# 定义主页文件,会自动访问该文件。

访问控制

1.准备测试页面

shell 复制代码
[root@sxw ~]# echo test1 > /var/www/html/index.html #编写测试文件

2.访问控制测试

可以直接编辑apache主配置文件

shell 复制代码
1.默认允许所有主机访问
[root@sxw ~]# vim /etc/httpd/conf/httpd.conf
shell 复制代码
[root@sxw ~]# systemctl restart httpd
shell 复制代码
2.只拒绝一部分客户端访问:
[root@sxw ~]# vim /etc/httpd/conf/httpd.conf

访问网站服务器反回的状态码:403 没有权限访问
200:表示访问网站成功
shell 复制代码
[root@sxw ~]# systemctl restart httpd
shell 复制代码
[root@sxw ~]# curl -I http://192.168.153.144  #用另外一台机器测试访问成功
HTTP/1.1 200 OK
Date: Thu, 06 Aug 2020 20:40:37 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 06 Aug 2020 20:12:02 GMT
ETag: "6-5ac3b1a02ac4f"
Accept-Ranges: bytes
Content-Length: 6
Content-Type: text/html; charset=UTF-8
shell 复制代码
在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,它支持文件的上传和下载,是综合传输工具,习惯称url为下载工具。
-o:指定下载路径
-I:查看服务器的响应信息
shell 复制代码
3.拒绝所有人
[root@sxw ~]# vim /etc/httpd/conf/httpd.conf
shell 复制代码
[root@sxw ~]# systemctl restart httpd
shell 复制代码
[root@sxw ~]# curl -I http://192.168.153.144
HTTP/1.1 403 Forbidden
Date: Thu, 06 Aug 2020 20:38:00 GMT
Server: Apache/2.4.6 (CentOS)
Content-Type: text/html; charset=iso-8859-1
总结
shell 复制代码
<RequireALL>
Require all granted
</RequireAll>

<RequireALL>
Require all deined
</RequireAll>

<RequireALL>
Require not ip 192.168.116.1
Require all granted
</RequireAll>

<RequireALL>
Require ip 192.168.116.1
</RequireAll>
修改默认网站发布目录
shell 复制代码
[root@sxw ~]# vim /etc/httpd/conf/httpd.conf
119  DocumentRoot "/www"            							# 修改网站根目录为/www
131  <Directory "/www">               							# 把这个也对应的修改为/www

[root@sxw ~]# mkdir /www    #创建定义的网站发布目录
[root@sxw ~]# echo "这是新修改的网站根目录/www" > /www/index.html #创建测试页面
[root@sxw ~]# systemctl restart httpd      #重启服务

虚拟主机

shell 复制代码
虚拟主机:将多个网站放在一台服务器上。web服务器都可以实现。
三种:基于域名 基于端口 基于Ip(300M/9w/1y)
shell 复制代码
1.基于域名
[root@sxw ~]# cd /etc/httpd/conf.d/
[root@sxw conf.d]# vim test.conf   #创建配置文件
<VirtualHost *:80>   #指定虚拟主机端口,*代表监听本机所有ip,也可以指定ip
DocumentRoot /soso     #指定发布网站目录,自己定义
ServerName www.soso666.com  #指定域名,可以自己定义
<Directory "/soso/">
  AllowOverride None    #设置目录的特性,不设置目录的特性
  Require all granted   #允许所有人访问
</Directory>
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /soho
ServerName test.soso666.com
<Directory "/soho/">
  AllowOverride None
  Require all granted
</Directory>
</VirtualHost>
[root@sxw ~]# mkdir /soso #创建发布目录
[root@sxw ~]# mkdir /soho
[root@sxw ~]# echo qianfen > /soso/index.html #创建测试页面
[root@sxw ~]# echo qfedu > /soho/index.html
[root@sxw ~]# systemctl restart httpd

配置域名解析:

shell 复制代码
在wind电脑上面打开C:\Windows\System32\drivers\etc\hosts文件。可以用管理员身份打开

基于端口

shell 复制代码
[root@sxw ~]# vim /etc/httpd/conf/httpd.conf  ---添加
shell 复制代码
2.基于端口
[root@sxw ~]# vim /etc/httpd/conf.d/test.conf
<VirtualHost *:80>
  DocumentRoot /soso
  ServerName www.soso666.com
<Directory "/soso/">
  AllowOverride None
  Require all granted
</Directory>
</VirtualHost>

<VirtualHost *:81>   #修改端口
  DocumentRoot /soho
  ServerName test.soso666.com
<Directory "/soho/">
  AllowOverride None
  Require all granted
</Directory>
</VirtualHost>
[root@sxw ~]# systemctl restart httpd
注意:域名解析并没有变
shell 复制代码
3.基于IP
[root@sxw ~]# ifconfig ens33:0 192.168.153.123  #添加一个临时ip
[root@sxw ~]# vim /etc/httpd/conf.d/test.conf
<VirtualHost 192.168.153.144:80>   #指定ip
  DocumentRoot /soso
  #ServerName www.soso666.com
<Directory "/soso/">
  AllowOverride None
  Require all granted
</Directory>
</VirtualHost>

<VirtualHost 192.168.153.123:80>   #指定ip
  DocumentRoot /soho
  #ServerName test.soso666.com
<Directory "/soho/">
  AllowOverride None
  Require all granted
</Directory>
</VirtualHost>
[root@sxw ~]# systemctl restart httpd

#取消添加的ip地址
#ifconfig ens33:0 192.168.153.123 down

可以配置域名解析,也可以不用配域名解析

相关推荐
wuk9984 分钟前
基于MATLAB编制的锂离子电池伪二维模型
linux·windows·github
你想考研啊2 小时前
四、jenkins自动构建和设置邮箱
运维·jenkins
Code blocks2 小时前
使用Jenkins完成springboot项目快速更新
java·运维·spring boot·后端·jenkins
snoopyfly~3 小时前
Ubuntu 24.04 LTS 服务器配置:安装 JDK、Nginx、Redis。
java·服务器·ubuntu
独行soc3 小时前
#渗透测试#批量漏洞挖掘#HSC Mailinspector 任意文件读取漏洞(CVE-2024-34470)
linux·科技·安全·网络安全·面试·渗透测试
BD_Marathon3 小时前
Ubuntu下Tomcat的配置
linux·ubuntu·tomcat
饥饿的半导体3 小时前
Linux快速入门
linux·运维
BD_Marathon3 小时前
Ubuntu:Tomcat里面的catalina.sh
linux·ubuntu·tomcat
BD_Marathon3 小时前
设置LInux环境变量的方法和区别_Ubuntu/Centos
linux·ubuntu·centos
Me4神秘4 小时前
Linux国产与国外进度对垒
linux·服务器·安全