Apache Basicstutorial

UnderstandApache HTTP Server basicconcepts, installationmethod and Basicsconfiguration

查看tutoriallist

Apache HTTP Server Introduction

Apache HTTP Server (简称Apache) is 世界 on 最流行 Webserver软件之一, 由Apache软件基金会maintenance. 它 is a open-source , 跨平台 Webserver, support many 种operationsystem, includingLinux, Windows, macOSetc..

Apache history

Apache 发展history可以追溯 to 1995年, 当时它 is 基于NCSA HTTPdserver codeDevelopment . 由于Developmentteam members分布 in 世界各地, 他们through电子email协作Development, 因此得名"Apache", 取自北美印第安人 阿帕奇族, 象征着团队合作精神.

Apache 特点

  • open-source免费: Apache is open-source软件, 可以自由using, modify and 分发.
  • 跨平台: support many 种operationsystem, includingLinux, Windows, macOSetc..
  • stable 性: 经过 many 年 发展 and test, Apache具 has 很 high stable 性 and reliability.
  • 可scale性: throughmodulesystem, 可以easilyscaleApache functions.
  • security性: Apache具 has 良 good security记录, 并且continuouslyupdate以应 for new security威胁.
  • performance优异: through合理 configuration, Apache可以processing big 量 concurrentrequest.
  • 广泛 communitysupport: serving as最流行 Webserver之一, Apache拥 has 庞 big user and Development者community.

Apache application场景

Apache被widely used in各种场景, from 个人网站 to big 型企业级application:

  • 托管静态网站 and 动态网站
  • serving asapplicationserver before 端proxy
  • implementation反向proxy and load balancing
  • providingsecurity HTTPSservice
  • deployment企业级Webapplication

Apache installation

Apache可以 in many 种operationsystem on installation, under 面介绍几种commonoperationsystem installationmethod:

in Ubuntu/Debian on installationApache

sudo apt update
sudo apt install apache2

in CentOS/RHEL on installationApache

sudo yum update
sudo yum install httpd

in Windows on installationApache

in Windows on installationApache has 两种主要method:

  1. using官方installationpackage: from Apache官方网站 under 载Windowsversion installationpackage, 然 after 按照installation向导forinstallation.
  2. using集成environment: such asXAMPP, WAMPetc., 这些集成environmentpackage含了Apache, MySQL, PHPetc.component, installation and configuration更加 simple .

verificationApacheinstallation

installationcompletion after , 可以through以 under methodverificationApache is 否正常run:

  • in Linux on : 启动Apacheservice并访问server IP地址 or 域名.
  • in Windows on : 启动Apacheservice并 in 浏览器in访问 http://localhost.

such as果Apache正常run, 你应该能看 to Apache 默认欢迎页面.

Apache basicconfiguration

Apache configurationfile位于不同 位置, 具体取决于operationsystem:

  • Ubuntu/Debian: /etc/apache2/
  • CentOS/RHEL: /etc/httpd/
  • Windows: ApacheinstallationTable of Contents/conf/

主要configurationfile

  • httpd.conf: Apache 主configurationfile, package含全局configuration指令.
  • apache2.conf: in Ubuntu/Debiansystem on , 这 is 主configurationfile.
  • sites-available/: in Ubuntu/Debiansystem on , 存放虚拟主机configurationfile Table of Contents.
  • mods-available/: in Ubuntu/Debiansystem on , 存放moduleconfigurationfile Table of Contents.

basicconfiguration指令

configuration指令格式

Apache configuration指令通常adopts以 under 格式:

指令名称 指令值

常用 basicconfiguration指令

  • ServerRoot: 指定Apache installationTable of Contents.
  • Listen: 指定Apache监听 端口.
  • ServerAdmin: 指定servermanagement员 电子email地址.
  • ServerName: 指定server 域名 or IP地址.
  • DocumentRoot: 指定网站file 存放Table of Contents.
  • Directory: 指定Table of Contents 访问控制configuration.
  • ErrorLog: 指定errorlogfile 位置.
  • CustomLog: 指定访问logfile 位置 and 格式.

Apache basicconcepts

虚拟主机

虚拟主机 is Apache 一个 important features, 它允许 in 同一台server on 托管 many 个网站. Apachesupport两种class型 虚拟主机:

  • 基于名称 虚拟主机: 根据request 域名来区分不同 网站.
  • 基于IP 虚拟主机: 根据request IP地址来区分不同 网站.

modulesystem

Apacheadoptsmodule化 design, corefunctions由basicmoduleproviding, 而scalefunctions则through可选moduleimplementation. 常用 moduleincluding:

  • mod_ssl: providingSSL/TLSsupport, 用于HTTPSservice.
  • mod_rewrite: providingURL重写functions.
  • mod_proxy: providingproxyserverfunctions.
  • mod_auth_basic: providingbasic userauthenticationfunctions.
  • mod_deflate: providing in 容压缩functions.

MPM ( many processingmodule)

MPM决定了Apachesuch as何processing客户端request, 常用 MPMincluding:

  • prefork: for 每个requestcreation一个 new process, stable 但memoryusing较 high .
  • worker: using many threadprocessingrequest, memoryusing较 low 但可能存 in threadsecurityissues.
  • event: worker improvementversion, usingevent驱动 方式processingrequest, performance更 good .

实践case: deployment第一个Apache网站

步骤1: 准备网站file

creation一个 simple HTMLfileserving as网站 首页:

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>我 第一个Apache网站</title> </head> <body> <h1>欢迎来 to 我 网站!</h1> <p>这 is a usingApache HTTP Serverdeployment 网站. </p> </body> </html>

步骤2: configurationApache

将creation HTMLfile保存 to Apache 默认documentation根Table of Contents:

  • Ubuntu/Debian: /var/www/html/index.html
  • CentOS/RHEL: /var/www/html/index.html
  • Windows: ApacheinstallationTable of Contents/htdocs/index.html

步骤3: 启动Apacheservice

in Linux on 启动Apacheservice:

# Ubuntu/Debian sudo systemctl start apache2 # CentOS/RHEL sudo systemctl start httpd

in Windows on , 可以throughservicemanagement器启动Apacheservice, or 者usingcommands行:

net start Apache2.4

步骤4: 访问网站

in 浏览器in访问server IP地址 or 域名, 你应该能看 to 你creation 网站首页.

互动练习

练习1: installationApache

in 你 计算机 on installationApache HTTP Server, 并verification它 is 否正常run.

练习2: creation simple 网站

creation一个package含以 under in 容 simple 网站:

  • 网站标题: "我 个人网站"
  • 网站 in 容: package含你 姓名, Introduction and 联系方式
  • usingApachedeployment这个网站

练习3: modifyApacheconfiguration

modifyApache configurationfile, 将网站 documentation根Table of Contents更改 for 你指定 Table of Contents, 然 after 重 new 启动Apacheservice并verification网站 is 否正常访问.