Apache的运用与实战

WEB服务器

1、WEB服务简介

shell 复制代码
# 目前最主流的三个Web服务器是Apache、Nginx、 IIS。
- WEB服务器一般指网站服务器,可以向浏览器等Web客户端提供网站的访问,让全世界浏览。
- WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务。
- WEB服务器是一种被动程序只有当Internet上运行其他计算机中的浏览器发出的请求时,服务器才会响应

2、WEB 服务协议

shell 复制代码
# WEB 服务应用层使用HTTP协议。
# HTML(标准通用标记语言)格式的文件。
# 浏览器通过统一资源定位器(URL)去访问web服务。
# 为了解决HTTP协议的这一缺陷,需要使用另一种协议:安全套接字层超文本传输协议HTTPS。为了数据传输的安全,HTTPS在HTTP的基础上加入了SSL协议,SSL依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密。
# WEB服务采用的是浏览器/服务器结构
shell 复制代码
web服务器只能解析静态页面。 动态页面:只要和数据库进行连接的都属于动态页面,比如java写的代码,PHP的代码,python的代码。
shell 复制代码
web服务器:apache nginx IIS  #端口全部为80!https为443端口
shell 复制代码
前端页面:静态元素: .html .img js css swf 配合:apache、nginx.
后端页面:动态元素:根据不同的开发语言: .php .jsp .py  配合:java、php、python


专门解析php代码的web中间件(web容器)--php-fpm端口9000
专门解析java代码的web中间件--tomcat(8080).
专门解析python代码的web中间件 ---uwsgi(5000)

SQL
数据库:mysql、mariadb

Apache 服务的搭建与配置

Apache 介绍

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

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

一、apache安装

shell 复制代码
[root@qfedu.com ~]# systemctl stop firewalld
[root@qfedu.com ~]# systemctl disable firewalld
[root@qfedu.com ~]# setenforce 0
[root@qfedu.com ~]# yum install -y httpd
[root@qfedu.com ~]# systemctl start httpd
[root@qfedu.com ~]# netstat -lntp | grep 80 #查看apache端口
tcp6       0      0 :::80                   :::*                    LISTEN      2776/httpd
#端口80.可以改
shell 复制代码
index.html:默认访问网站的主页名称
默认发布网站的目录:/var/www/html

1.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@localhost  		# 设置管理员邮件地址
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@qfedu.com ~]# echo test1 > /var/www/html/index.html #编写测试文件

2.访问控制测试

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

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

访问网站服务器反回的状态码:403 没有权限访问
200:表示访问网站成功
shell 复制代码
[root@qfedu.com ~]# systemctl restart httpd
shell 复制代码
[root@test ~]# 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@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf
shell 复制代码
[root@qfedu.com ~]# systemctl restart httpd
shell 复制代码
[root@test ~]# 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 复制代码
[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf
119  DocumentRoot "/www"            							# 修改网站根目录为/www
131  <Directory "/www">               							# 把这个也对应的修改为/www

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

三、虚拟主机

shell 复制代码
虚拟主机:将多个网站放在一台服务器上。web服务器都可以实现。
三种:基于域名 基于端口 基于Ip
shell 复制代码
1.基于域名
[root@qfedu.com ~]# cd /etc/httpd/conf.d/
[root@qfedu.com 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@qfedu.com ~]# mkdir /soso #创建发布目录
[root@qfedu.com ~]# mkdir /soho
[root@qfedu.com ~]# echo qianfen > /soso/index.html #创建测试页面
[root@qfedu.com ~]# echo qfedu > /soho/index.html
[root@qfedu.com ~]# systemctl restart httpd

配置域名解析:

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

测试访问

基于端口

shell 复制代码
[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf  ---添加
shell 复制代码
2.基于端口
[root@qfedu.com ~]# 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@qfedu.com ~]# systemctl restart httpd
注意:域名解析并没有变

访问:www.soso666.com

访问: test.soso666.com:81

shell 复制代码
3.基于IP
[root@qfedu.com ~]# ifconfig ens33:0 192.168.153.123  #添加一个临时ip
[root@qfedu.com ~]# 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@qfedu.com ~]# systemctl restart httpd

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

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

Nginx 服务的搭建与配置

Nginx介绍

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,由俄罗斯的程序设计师Igor Sysoev所开发,其特点是占有内存少,并发能力强。事实上nginx的并发能力确实在同类型的网页服务器中表现较好。

安装 Nginx

1.2Nginx基本使用

shell 复制代码
获取Nginx
Nginx的官方主页: http://nginx.org
shell 复制代码
关闭防火墙关闭selinux
[root@qfedu.com ~]# systemctl stop firewalld  #关闭防火墙
[root@qfedu.com ~]# systemctl disable firewalld #开机关闭防火墙
[root@qfedu.com ~]# setenforce 0  #临时关闭selinux
[root@qfedu.com ~]# getenforce   #查看selinux状态

Nginx安装:
Yum方式:
[root@qfedu.com ~]# cd /etc/yum.repos.d/
[root@qfedu.com yum.repos.d]# vi nginx.repo  #编写nginx的yum源
[nginx]
name=nginx
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@qfedu.com yum.repos.d]# yum clean all
[root@qfedu.com yum.repos.d]# yum makecache
[root@qfedu.com ~]# yum install -y nginx  #安装nginx
shell 复制代码
[root@qfedu.com ~]# systemctl start nginx  #启动
[root@qfedu.com ~]# systemctl restart nginx #重启
[root@qfedu.com ~]# systemctl enable nginx  #开机启动
[root@qfedu.com ~]# systemctl stop nginx  #关闭
shell 复制代码
1.查看nginx状态
[root@qfedu.com ~]# ps aux | grep nginx 
root       3927  0.0  0.0  46384   968 ?        Ss   18:46   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      3928  0.0  0.1  46792  1932 ?        S    18:46   0:00 nginx: worker process
root       3932  0.0  0.0 112660   968 pts/1    R+   18:47   0:00 grep --color=auto nginx
2.查看nginx端口
[root@qfedu.com ~]# netstat -lntp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3927/nginx: master
#注意:nginx默认端口为80
3.测试主页是否可以访问:
[root@qfedu.com ~]# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Sat, 16 Nov 2019 10:49:48 GMT
Content-Type: text/html
Content-Length: 635
Last-Modified: Fri, 11 Oct 2019 06:45:33 GMT
Connection: keep-alive
ETag: "5da0250d-27b"
Accept-Ranges: bytes
常见的组合方式
shell 复制代码
LNMP (Linux + Nginx + MySQL/Mariadb + PHP)  #php-fpm进程,这个组合是公司用的最多的组合
LAMP (Linux + Apache + MySQL/Mariadb + PHP)
相关推荐
仍然探索未知中5 分钟前
【Linux内核源码分析】内核数据结构
linux·数据结构
chxii7 分钟前
linux 下用 acme.sh 搞定 Nginx 免费 SSL 证书自动续期(下) 对于acme.sh命令安装详解
linux·运维·服务器
雨落Liy20 分钟前
OpenClaw 腾讯云一键更新后全面排障与恢复
云计算·vim·腾讯云
Bert.Cai23 分钟前
Linux more命令详解
linux·运维
minji...25 分钟前
Linux 多线程(四)线程等待,线程分离,线程管理,C++多线程,pthread库
linux·运维·开发语言·网络·c++·算法
倔强的胖蚂蚁27 分钟前
云原生服务器存储规划与磁盘选型实施
运维·服务器·云原生
ZGUIZ28 分钟前
Ubuntu 25.10 无法外接显示器解决方案
linux·运维·ubuntu
yang)33 分钟前
JESD 204b
运维·服务器·网络
QJtDK1R5a36 分钟前
V4L2 vs GStreamer vs FFmpeg:Linux多媒体处理的三个层级
linux·运维·ffmpeg
倔强的石头10642 分钟前
【Linux指南】基础IO系列(四):文件描述符 fd——Linux 文件操作的 “万能钥匙”
linux·运维·服务器