Linux 云计算集群架构师(第2阶段)->第六章 使用 Apache 搭建 Web 网站服务器

第六章 使用 Apache 搭建 Web 网站服务器

(上课时间2021-08-07,笔记整理时间2021-08-09)php

本节所讲内容:html

6.1 Apache 服务器概述-安装mysql

6.2 配置 web 服务器-搭建 LAMP 环境linux

6.3 修改 web 网站根目录-配置别名-虚拟目录nginx

6.4 实现 Apache 打开软连接功能-禁止显示目录列表-用户认证web

6.5 Apache 虚拟主机基于-IP-域名-端口三种搭建方式sql

注意:本节课是用 yum 包来搭建 LAMP 环境,主要是让同窗们初步了解下相关概念和 LAMP 环境的功能,后期会带你们用源码编译的方式来精讲 LAMP 和 LNMP 环境,包括环境搭建,调优等等。chrome

6.1 Apache 服务器概述-安装

6.1.1 web 服务概述

WEB 服务器也称为 WWW (WORLD WIDE WEB),万维网)服务器,主要功能是提供网上信息浏览服务。shell

常见 web 服务器:数据库

httpd(Apache)、nginx+PHP

Tomcat: jsp + html

win:IIS

客户端:IE 、firefox、chrome、手机(浏览器)Browser

今天的主角:LAMP=Linux Apache Mysql PHP 中的 Apache(HTTPD)

类 Lunix 系统 Linux+Apache+Mysql/MariaDB+Perl/PHP/Python 一组经常使用来搭建动态网站或者服务器的开源软件,自己都是各自独立的程序,可是由于常被放在一块儿使用,拥有了愈来愈高的兼容度,共同组成了一个强大的 Web 应用程序平台。随着开源潮流的蓬勃发展,开放源代码 的 LAMP 已经与 J2EE 和.Net 商业软件造成三足鼎立之势,而且该软件开发的项目在软件方面的投资成本较低,所以受到整个 IT 界的关注。从网站的流量上来讲,50%以上的访问流量 是 LAMP 来提供的,LAMP 是最强大的网站解决方案。

6.1.2 什么是 Apache

Apache HTTPD Server 简称 Apache,是 Apache 软件基金会的一个开源的网页服务器, 能够在大多数计算机操做系统中运行,因为其多平台和安全性被普遍使用,是最流行的 Web 服务器端软件之一。

它快速、可靠而且可经过简单的 API 扩展,将 Perl/Python 等解释器编 译到服务器中!

Apache HTTP 服务器是一个模块化的服务器,各个功能使用模块化进行插拔! 目前支持 Windows,Linux,Unix 等平台!Apache 软件基金会(也就是 Apache Software Foundation,简称为 ASF),是专门为运 做一个开源软件项目的 Apache 的团体提供支持的非盈利性组织,这个开源软件项目就是 Apache 项目!那么咱们的 HTTPD 也只是 Apache 的开源项目之一!

主要的开源项目:HTTP Server,Ant,DB,iBATIS,Jakarta,Logging,Maven,Struts, Tomcat,Tapestry,Hadoop 等等。只是最有名的是 HTTP Server,因此如今所说的 Apache 已 经就是 HTTPD Server 的代号了! 咱们还见的比较多的是 Tomcat,Hadoop 等项目官方网站:http://www.apache.org/ http://httpd.apache.org

image-20210808185112974

6.1.3 什么是 Mysql

MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 旗下公司,他也是最流行的关系型数据库管理系统!特别是在 WEB 应用方面!

关系型数据库:它经过数据、关系和对数据的约束三者组成的数据模型来存放和管理数据,官方网站:http://www.mysql.com/

6.1.4 什么是 PHP

PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通 用开源脚本语言。语法吸取了 C 语言、Java 和 Perl 的特色,利于学习,使用普遍,主要适 用于 Web 开发领域。PHP 独特的语法混合了 C、Java、Perl 以及 PHP 自创的语法。

它能够 比 CGI 或者 Perl 更快速地执行动态网页。用 PHP 作出的动态页面与其余的编程语言相比, PHP 是将程序嵌入到 HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比 彻底生成 HTML 标记的 CGI 要高许多;PHP 还能够执行编译后代码,编译能够达到加密和 优化代码运行,使代码运行更快。简单的说,PHP 就是一个脚本解释器!

PHP 服务器安装好以后,升级切记不要盲目升级!由于它会废弃不少低效的语法! 官方网站:http://php.net/

6.1.5 LAMP 工做原理

image-20210808192418595

6.1.6 Web 服务器的工做模式和端口

工做模式是:B/S 模式

工做端口是:

正常端口:80/http

SSL 端口:443/https

安装服务器端:httpd

[root@centos80 ~]# yum -y  install httpd
[root@centos80 ~]# cd /media/Packages/
[root@centos80 Packages]# ls httpd-*
httpd-2.4.6-88.el7.centos.x86_64.rpm                #服务的主程序包,服务器端必须安装该软件包
httpd-devel-2.4.6-88.el7.centos.x86_64.rpm          #Apache 开发程序包
httpd-manual-2.4.6-88.el7.centos.noarch.rpm         #Apache 手册文档,包含 HTML 格式的Apache 计划的 Apache User's Guide 说明指南
httpd-tools-2.4.6-88.el7.centos.x86_64.rpm          #Apache 相关工具包

自动解决安装不少依赖的软件包,后期你们在学习源码编译安装的时候,会遇到这样的问题,须要手动把依赖的包安装好,才能编译经过。

[root@centos80 ~]# yum -y install elinks            #安装字符界面的下浏览器客户端:elinks

apache 主配置文件

[root@centos80 ~]# ll /etc/httpd/conf/httpd.conf 
-rw-r--r-- 1 root root 11753 11月 16 2020 /etc/httpd/conf/httpd.conf

启动服务-查看端口

[root@centos80 ~]# systemctl start httpd
[root@centos80 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@centos80 ~]# netstat -antup | grep 80
tcp      0    0 10.170.80.80:2049  10.170.80.81:826    ESTABLISHED -                   
tcp6     0    0 :::80              :::*                LISTEN      5704/httpd          
tcp6     0    0 10.170.80.80:22    10.170.80.102:52143     ESTABLISHED 1/systemd           
udp      0    0 127.0.0.1:809      0.0.0.0:*                        4024/rpc.statd      
udp      0    0 127.0.0.1:323      0.0.0.0:*                        805/chronyd         
udp6     0    0 ::1:323            :::*                             805/chronyd
[root@centos80 ~]# elinks 10.170.80.80              #按 q 退出

image-20210808194234920

6.1.7 主配置文件介绍

[root@centos80 ~]# cp /etc/httpd/conf/httpd.conf{,.bak}         #备份配置文件
[root@centos80 ~]# vim /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"         #httpd 服务的根目录
Listen 80                       #监听端口,默认本地 IP,若是指定 ip 写上 IP:80
Include conf.modules.d/*.conf   #当前目录下 conf.modules.d 目录下全部 conf 文件都生效
Options Indexes FollowSymLinks  #Options Indexes 目录浏览 FollowSymLinks 用连接浏览
AllowOverride None              #设置为 none,忽略.htaccess
Include conf.d/*.conf           #conf.d 里面的 conf 文件也属有效配置文件
User apache                     #运行以哪一个身份运行
Group apache                    #运行以哪一个组的身份运行
ServerAdmin root@localhost      #管理员邮箱
DocumentRoot "/var/www/html"    #默认的主目录,若是改动要改动两处,Directory
<Directory "/var/www/html">
Options Indexes FollowSymLinks AllowOverride None
</Directory>
LogLevel warn                   #日志等级
AddDefaultCharset UTF-8         #支持的语言,默认编码

配置文件的最后是虚拟主机的字段,其中你大部分字段作个了解便可

6.1.8 yum 的方式搭建 LAMP 环境

LAMP=Linux(存在)+Apache(HTTPD 已安装)+Mysql+PHP

#Centos6
[root@centos63 ~]# yum -y install httpd mysql mysql-server php php-mysql
#Centos7 & centos8
[root@centos80 ~]# yum -y install httpd mariadb mariadb-server php php-mysql

测试数据库:

[root@centos80 ~]# systemctl start mariadb.service 
[root@centos80 ~]# systemctl enable mariadb.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@centos80 ~]# mysql                #登录测试
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit;

测试 apache 是否支持 php

建立一个文件:

[root@centos80 html]# vim index.php
[root@centos80 html]# cat index.php 
<?php
    phpinfo();
?>
[root@centos80 html]# systemctl restart httpd.service

image-20210808201230780

PHP探针:一个 PHP 编写的文件,能够实时查看服务器硬盘资源、内存占用、网卡流量、系统负载、服务器时间等信息,定时刷新一次。以及包括服务器 IP 地址,Web 服务器环境监测,php 等信息。

6.2 配置 web 服务器-搭建 LAMP 环境

搭建一台测试 web 服务器

案例:

部门内部搭建一台 WEB 服务器,采用的 IP 地址和端口为 10.170.80.80:80,首页采用index.html 文件。管理员 E-mail 地址为yh@suboot.cn,网页的编码类型采用 UTF-8,全部网站资源都存放/var/www/html 目录下,并将 Apache 的配置文件根目录设置为/etc/httpd 目录。

修改配置文件:

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

设置 Apache 的文档目录为/etc/httpd

设置 httpd 监听端口 80

设置管理员 E-mail 地址为 cd@xuegod.cn

设置 WEB 服务器的主机名和监听端口为 10.170.80.80:80

设置 Apache 网站根目录为/var/www/html

设置主页文件为 index.html

设置服务器的默认编码为 UTF-8

改配置文件以前,先备份,以便恢复。

[root@centos80 ~]# vim /etc/httpd/conf/httpd.conf   #如下参数须要咱们掌握
31 ServerRoot "/etc/httpd"              #Apache 配置文件的根目录
42 Listen 80                            #监听的端口
改:87 ServerAdmin root@localhost
为:87 ServerAdmin MK@xuegod.cn           #设置管理员,e-mail 地址
改:96 #ServerName www.example.com:80
为:96 ServerName 192.168.1.63:80         #服务器主机名
120 DocumentRoot "/var/www/html"            #网站页面根目录
145 Options Indexes FollowSymLinks          #当一个目录没有默认首页时,容许
显示此目录列表
改:165 DirectoryIndex index.html
为:165 DirectoryIndex index.html index.php   #指定默认首页
317 AddDefaultCharset UTF-8                     #设置服务器的默认编码为: UTF8

取消 Apache 默认欢迎页:

[root@centos80 ~]# vim /etc/httpd/conf.d/welcome.conf
# This configuration file enables the default "Welcome"
# page if there is no default index page present for
# the root URL. To disable the Welcome page, comment
# out all the lines below.
#
改:
8 <LocationMatch "^/+$">
9 Options -Indexes
10 ErrorDocument 403 /.noindex.html
11 </LocationMatch>
把以上几行加上注释使其不生效:
8 #<LocationMatch "^/+$"> #把红色内容注释
9 # Options -Indexes
10 # ErrorDocument 403 /.noindex.html
11 #</LocationMatch>
[root@centos80 html]# systemctl restart httpd.service       #重启 httpd服务
[root@centos80 html]# echo 'welcome to www.suboot.cn!' > /var/www/html/index.html   #建立首页文件

image-20210808202627069

6.3 修改 web 网站根目录-配置别名-虚拟目录

LAMP 环境,几种不一样的安全访问机制的实现:

6.3.1 修改网站目录对应参数及权限:

修改网站默认根目录,而且设置访问权限,只有在我容许的范围以内能够访问建立目录,建立测试首页

[root@centos80 ~]# mkdir /var/www/html/bbs   
[root@centos80 ~]# cp /var/www/html/index.html /var/www/html/bbs/
[root@centos80 ~]# vim /etc/httpd/conf/httpd.conf       #修改网站默认根目录
设置文档目录为/var/www/html/bbs
修改 DocumentRoot 路径为 指定的目录
改:120 DocumentRoot "/var/www/html"
为:120 DocumentRoot "/var/www/html/bbs"
容许全部人访问/var/www/html/bbs 目录
目录与访问控制:
改:132 <Directory "/var/www/html ">
为:132 <Directory "/var/www/html/bbs">           #子目录会继承这个目录的属性
 145 Options Indexes FollowSymLinks              #Options:Indexes:目录浏览 
 152 AllowOverride None                          #Followsymlinks:能够用链接
改:157 Require all granted                       #表示支持全部访问
为:157 # Require all granted
#加入以下:
 158 <RequireALL>
 159 Require ip 10.170.80.80                    #容许本机 IP 访问
 160 Require not ip 10.170.80.102               #禁止物理机 IP 访问
 161 </RequireALL>
 161 </Directory>

image-20210808205450209

image-20210808205530110

在物理机上访问测试:http://10.170.80.80

image-20210808210018288

在 Linux 主机上访问:

[root@centos80 ~]# curl 10.170.80.80
welcome to www.suboot.cn!

6.3.2 使用别名,引用网站根目录之外的路径。

将/usr/local/phpdata 目录经过虚拟目录功能添加到网站根目录。当访问 http://10.170.80.80/phpdata/ 时,就能够访问目录/usr/local/phpdata 中的内容。

注:Apache 的别名也叫虚拟目录

语法:

AliasURL 路径 PATH 物理路径

建立测试数据:

[root@centos80 ~]# mkdir /usr/local/phpdata
[root@centos80 ~]# echo "This is Alias PHPDATA" > /usr/local/phpdata/index.html[root@centos80 ~]# 
[root@centos80 ~]# vim /etc/httpd/conf/httpd.conf       #修改配置文件
236       Alias /phpdata/ "/usr/local/phpdata/"
237         <Directory "/usr/local/phpdata/">
238         Options Indexes FollowSymLinks
239         AllowOverride None
240         Require all granted
241         </Directory>

注:Alias /phpdata/ "/usr/local/phpdata/"

#/phpdata/ 能够随意起。好比改/phpdata/ 为/php/ 则访问连接:http://10.170.80.80/php

测试:

[root@centos80 ~]# systemctl restart httpd.service
[root@centos80 ~]# curl 10.170.80.80/phpdata/
This is Alias PHPDATA

浏览器访问:http://10.170.80.80/phpdata/

image-20210808213751730

6.4 实现 Apache 打开软连接功能-禁止显示目录列表用户认证

6.4.1 当一个目录下没有默认首页时,访问http://10.170.80.80 禁止显示目录列表

显示目录的效果:

[root@centos80 html]# cp -r bbs1/ /var/www/html/bbs/
[root@centos80 html]# ls bbs/
bbs1  index1.html  index_2.html  index_3.html
[root@centos80 html]# vim /etc/httpd/conf/httpd.conf
    Require all granted                     #取消注释
#       <RequireALL>                        #注释如下 4 行
#         Require ip 10.170.80.80
#         Require not ip 10.170.80.102
#       </RequireALL>

image-20210808215115152

[root@centos80 html]# systemctl restart httpd.service

当网站没有默认的首页的时候,直接访问显示网站更目录下的文件夹资源

image-20210808215859304

以上的操做其实是恢复了 Apache 的原来配置,Apache 默认为网站目录下能够浏览文件夹资源。

取消网站目录下浏览文件夹资源
修改配置文件:

[root@centos80 html]# vim /etc/httpd/conf/httpd.conf    #注释掉此行
144     # Options Indexes FollowSymLinks
[root@centos80 html]# systemctl restart httpd.service   #重启服务测试

image-20210808220340652

6.4.2 经过用户认证的方式,对网站下/usr/local/phpdata/目录进行保护

设置/usr/local/phpdata/目录,只能经过用户名密码方式访问。

[root@centos80 html]# vim /etc/httpd/conf/httpd.conf
#在须要使用用户验证的目录的相关 Directory 段落添加之内容:
Alias /phpdata/ "/usr/local/phpdata /"
<Directory "/usr/local/phpdata /">
Options Indexes FollowSymLinks
AllowOverride None
#Require all granted #注释此行,并加入如下 4 行内容
authtype basic
 authname "my web site"
authuserfile /etc/httpd/conf/passwd.secret
require valid-user
</Directory>

image-20210808221012078

参数说明:
authtype basic
#authtype 命令:指定认证类型为:basic。
authname "my web site "
#AuthName 命令:指定认证区域名称。区域名称是在提示要求认证的对话框中显示给用户的。
authuserfile /etc/httpd/conf/passwd.secret 
#AuthUserFile 命令:指定一个包含用户名和密码的文本文件,每行一对。
require 命令
#指定哪些用户或组才能被受权访问。如:require user user1 user2 (只有用户 user1 和user2 能够访问)
require valid-user #(在 AuthUserFile 指定的文件中任何用户均可以访问)

利用 Apache 附带的程序 htpasswd,生成包含用户名和密码的文本文

命令格式:htpasswd –cm 存放用户名和密码的文件名 用户名

[root@centos80 html]# htpasswd -cm /etc/httpd/conf/passwd.secret tom
New password: 123456
Re-type new password: 123456
Adding password for user tom

#第一个生成用户名和密码,须要建立文件,所以须要加-c 参数

#第二次建立用户,不能用-c,不然会把前面的用户覆盖了

[root@centos80 ~]# htpasswd -h                          #查看参数
-c:建立新文件(Create a new file.)
-m:强制对密码进行 MD5 加密(Force MD5 encryption of the password.)
[root@centos80 html]# htpasswd -m /etc/httpd/conf/passwd.secret job
New password: 
Re-type new password: 
Adding password for user job
[root@centos80 html]# cat /etc/httpd/conf/passwd.secret 
tom:$apr1$a7.lK0Qe$EVctnZB/chtsG4P7oKTHr0
job:$apr1$4KWBLJAm$PT1q8EbuiwGI8m75jSxm91
[root@centos80 html]# systemctl restart httpd.service   #重启服务测试

测试登录:

image-20210808222210935

image-20210808222237314

6.5 Apache 虚拟主机基于-IP-域名-端口三种搭建方式

配置 Apache 虚拟主机,实如今一台服务器上运行多个网站

Apache 虚拟主机实现有三种方法:

一、经过不一样的 IP 地址

二、经过不一样的域名

三、经过不一样的端口号

三种都须要把虚拟主机功能打开:

(1)方法一:

经过不一样的 IP 地址,解析不一样域名

给服务器增长 IP(另外一个域名解析出来的那个 IP)。

[root@centos80 ~]# ifconfig ens33:1 10.170.80.180 netmask 255.255.255.0
[root@centos80 ~]# echo "bbs.suboot.cn" > /var/www/html/bbs/index.html
[root@centos80 ~]# cd /etc/httpd/conf.d/
[root@centos80 conf.d]# vim bbs.conf                #建立虚拟主机的配置文件
[root@centos80 conf.d]# cat bbs.conf 
<VirtualHost 10.170.80.80:80>
 ServerAdmin webmaster@dummy-host.example.com
 DocumentRoot /var/www/html/
 ServerName dummy-host.example.com
 ErrorLog logs/dummy-host.example.com-error_log
 CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost 10.170.80.180:80>
 ServerAdmin webmaster@dummy-host.example.com
 DocumentRoot /var/www/html/bbs/
 ServerName dummy-host.example.com
 ErrorLog logs/dummy-host.example.com-error_log
 CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
[root@centos80 conf.d]# curl 10.170.80.80/
Welcome to www.suboot.cn
[root@centos80 conf.d]# curl 10.170.80.180/
bbs.suboot.cn

(2)方法二:
经过不一样域名:(这种访问在企业比较常见,其余两种访问能够了解下便可)
在域名管理后台,修改 DNS 配置,www.suboot.cn 与 bbs.suboot.cn 解析成相同的 IP。

[root@centos80 conf.d]# vim bbs.conf 
<VirtualHost *:80>
 ServerAdmin webmaster@dummy-host.example.com
 DocumentRoot /var/www/html/
 ServerName www.suboot.cn
 ErrorLog logs/www.suboot.cn-error_log
 CustomLog logs/www.suboot.cn-access_log common
</VirtualHost>
<VirtualHost *:80>
 ServerAdmin webmaster@dummy-host.example.com
 DocumentRoot /var/www/html/bbs/
 ServerName bbs.suboot.cn
 ErrorLog logs/bbs.suboot.cn-error_log
 CustomLog logs/bbs.suboot.cn-access_log common
</VirtualHost>

配置文件修改最终如图下:

image-20210808224008856

[root@centos80 html]# systemctl restart httpd.service   #重启服务
[root@centos80 conf.d]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.170.80.80    www.suboot.cn   linux80
10.170.80.80    bbs.suboot.cn   linux80

测试:

[root@centos80 conf.d]# curl www.suboot.cn
Welcome to www.suboot.cn
[root@centos80 conf.d]# curl bbs.suboot.cn
bbs.suboot.cn

修改本地 Windows 的 hosts 文件,能够实如今浏览器上用域名访问:C:\Windows\System32\drivers\etc

image-20210808224458774

image-20210808224556773

image-20210808224636191

image-20210808224702578

(3)方法 3三:基于端口配置虚拟主机:

[root@centos80 conf.d]# vim /etc/httpd/conf/httpd.conf

42 Listen 80 #在此行下添加监听 81 号端口

43 Listen 81

[root@centos80 conf.d]# vim /etc/httpd/conf.d/bbs.conf 
改:8 <VirtualHost *:80>
为:8 <VirtualHost *:81>  #此处修改的端口,在 apache 的主配置文件须要有监听端口
# 更改成要添加的多个端口

image-20210808225013501

[root@centos80 conf.d]# systemctl restart httpd.service 
[root@centos80 conf.d]# curl www.suboot.cn
Welcome to www.suboot.cn
[root@centos80 conf.d]# curl www.suboot.cn:81
bbs.suboot.cn

总结:

6.1 Apache 服务器概述-安装

6.2 配置 web 服务器-搭建 LAMP 环境

6.3 修改 web 网站根目录-配置别名-虚拟目录

6.4 实现 Apache 打开软连接功能-禁止显示目录列表-用户认证

6.5 Apache 虚拟主机基于-IP-域名-端口三种搭建方式

相关文章
相关标签/搜索