小工具知识网

当前位置: 首页 > 行业知识 > Apache的运用与实战

行业知识

Apache的运用与实战

2026-11-16 01:47:14 来源:互联网转载

WEB服务器

1、WEB服务简介

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

2、WEB 服务协议

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

Apache的主程序名叫httpd。

一、apache安装

[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.可以改
index.html:默认访问网站的主页名称默认发布网站的目录:/var/www/html

1.apache目录介绍

apache的工作目录:conf   存储配置文件conf.d 存储配置子文件logs   存储日志 modules 存储模块run    存储Pid文件,存放的pid号码。是主进程号
认识主配置文件:# 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指定目录的属性   # 网站容器开始标识Options Indexes FollowSymLinks   # 找不到主页时,链接到网站目录以外,如测试页面AllowOverride None               # 对网站设置特殊属性:none不设置特殊属性,all允许Require all granted                 # granted表示允许所有人访问,denied表示拒绝所有人访问    # 容器结束DirectoryIndex index.html      # 定义主页文件,会自动访问该文件。

二、访问控制

1.准备测试页面

[root@qfedu.com ~]# echo test1 > /var/www/html/index.html #编写测试文件

2.访问控制测试

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

1.默认允许所有主机访问[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf
[root@qfedu.com ~]# systemctl restart httpd
2.只拒绝一部分客户端访问:[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf访问网站服务器反回的状态码:403 没有权限访问200:表示访问网站成功
[root@qfedu.com ~]# systemctl restart httpd
[root@test ~]# curl -I http://192.168.153.144  #用另外一台机器测试访问成功HTTP/1.1 200 OKDate: Thu, 06 Aug 2020 20:40:37 GMTServer: Apache/2.4.6 (CentOS)Last-Modified: Thu, 06 Aug 2020 20:12:02 GMTETag: "6-5ac3b1a02ac4f"Accept-Ranges: bytesContent-Length: 6Content-Type: text/html; charset=UTF-8
在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,它支持文件的上传和下载,是综合传输工具,习惯称url为下载工具。-o:指定下载路径-I:查看服务器的响应信息
3.拒绝所有人[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf
[root@qfedu.com ~]# systemctl restart httpd
[root@test ~]# curl -I http://192.168.153.144HTTP/1.1 403 ForbiddenDate: Thu, 06 Aug 2020 20:38:00 GMTServer: Apache/2.4.6 (CentOS)Content-Type: text/html; charset=iso-8859-1
修改默认网站发布目录
[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf119  DocumentRoot "/www"            # 修改网站根目录为/www131                 # 把这个也对应的修改为/www[root@qfedu.com ~]# mkdir /www    #创建定义的网站发布目录[root@qfedu.com ~]# echo "这是新修改的网站根目录/www" > /www/index.html #创建测试页面[root@qfedu.com ~]# systemctl restart httpd      #重启服务

三、虚拟主机

虚拟主机:将多个网站放在一台服务器上。web服务器都可以实现。三种:基于域名 基于端口 基于Ip
1.基于域名[root@qfedu.com ~]# cd /etc/httpd/conf.d/[root@qfedu.com conf.d]# vim test.conf   #创建配置文件   #指定虚拟主机端口,*代表监听本机所有ip,也可以指定ipDocumentRoot /soso     #指定发布网站目录,自己定义ServerName www.soso666.com  #指定域名,可以自己定义  AllowOverride None    #设置目录的特性,不设置目录的特性  Require all granted   #允许所有人访问DocumentRoot /sohoServerName test.soso666.com  AllowOverride None  Require all granted[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

配置域名解析:

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

测试访问

基于端口

[root@qfedu.com ~]# vim /etc/httpd/conf/httpd.conf  ---添加
2.基于端口[root@qfedu.com ~]# vim /etc/httpd/conf.d/test.conf  DocumentRoot /soso  ServerName www.soso666.com  AllowOverride None  Require all granted   #修改端口  DocumentRoot /soho  ServerName test.soso666.com  AllowOverride None  Require all granted[root@qfedu.com ~]# systemctl restart httpd注意:域名解析并没有变

访问:www.soso666.com

访问: test.soso666.com:81

3.基于IP[root@qfedu.com ~]# ifconfig ens33:0 192.168.153.123  #添加一个临时ip[root@qfedu.com ~]# vim /etc/httpd/conf.d/test.conf   #指定ip  DocumentRoot /soso  #ServerName www.soso666.com  AllowOverride None  Require all granted   #指定ip  DocumentRoot /soho  #ServerName test.soso666.com  AllowOverride None  Require all granted[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基本使用

获取NginxNginx的官方主页: http://nginx.org
关闭防火墙关闭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=nginxbaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0enabled=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
[root@qfedu.com ~]# systemctl start nginx  #启动[root@qfedu.com ~]# systemctl restart nginx #重启[root@qfedu.com ~]# systemctl enable nginx  #开机启动[root@qfedu.com ~]# systemctl stop nginx  #关闭
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.confnginx      3928  0.0  0.1  46792  1932 ?        S    18:46   0:00 nginx: worker processroot       3932  0.0  0.0 112660   968 pts/1    R+   18:47   0:00 grep --color=auto nginx2.查看nginx端口[root@qfedu.com ~]# netstat -lntp | grep 80tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3927/nginx: master#注意:nginx默认端口为803.测试主页是否可以访问:[root@qfedu.com ~]# curl -I http://127.0.0.1HTTP/1.1 200 OKServer: nginx/1.16.1Date: Sat, 16 Nov 2019 10:49:48 GMTContent-Type: text/htmlContent-Length: 635Last-Modified: Fri, 11 Oct 2019 06:45:33 GMTConnection: keep-aliveETag: "5da0250d-27b"Accept-Ranges: bytes
常见的组合方式
LNMP (Linux + Nginx + MySQL/Mariadb + PHP)  #php-fpm进程,这个组合是公司用的最多的组合LAMP (Linux + Apache + MySQL/Mariadb + PHP)

上一篇:微信开发者工具下载指南

下一篇:怎么通过网页链接服务器?