文章目录

  • 前言
  • 配置
  • 实验
  • 基于不同IP访问
  • 基于不同端口访问
  • 基于域名访问


前言

搭建一个网站,首先需要的是Web服务器,我们这里使用 Apache,它是由 httpd 服务调度,我们先使用这个服务搭建一个简单的静态页面

配置

下载服务:yum install httpd -y

linux 大型网站架构 linux网站搭建步骤_linux 大型网站架构


我们可以看一下它的配置信息:rpm -ql httpd | grep /etc

linux 大型网站架构 linux网站搭建步骤_apache_02


tree /etc/httpd

linux 大型网站架构 linux网站搭建步骤_linux_03


可以看到有三个配置文件目录,四个软连接目录

conf 是主配置文件目录,conf.d 目录也是配置文件目录,它会被加载到主配置文件中与其一同生效,故我们编辑的配置文件也应该放在这里我们在下载一个客户端指令:yum install curl -y

linux 大型网站架构 linux网站搭建步骤_linux_04


现在我们开启服务:systemctl start httpd

linux 大型网站架构 linux网站搭建步骤_配置文件_05


我们可以使用Windows访问一下(关闭防火墙和selinux)

linux 大型网站架构 linux网站搭建步骤_linux_06


linux 大型网站架构 linux网站搭建步骤_服务器_07


可以看到出现了 Apache 的欢迎页面,这是因为 /var/www/html/ 目录下没有 index.html 文件,这是HTML文件的默认路径,下来我们写一个文件进去:echo Hello, World! > /var/www/html/index.html

linux 大型网站架构 linux网站搭建步骤_linux 大型网站架构_08


我们使用Linux命令行来访问一下:curl 192.168.32.128

linux 大型网站架构 linux网站搭建步骤_配置文件_09


也可以通过 Windows 来访问

linux 大型网站架构 linux网站搭建步骤_linux_10

实验

我们成功下载 Apache Web 服务器,并且成功访问了他的静态页面,我们也可以写一些配置文件对细节做一些设置

基于不同IP访问

  1. 我们先添加两个IP,添加两个网卡也好,直接给本网卡配置IP也可
    我们使用后者:nmcli connection modify ens33 +ipv4.addresses 192.168.32.100/24 +ipv4.addresses 192.168.32.200/24 连接网卡:nmcli connection up ens33
  2. linux 大型网站架构 linux网站搭建步骤_linux 大型网站架构_11


  3. 接下来就是创建两个网页的根目录,并为其添加 index.html 文件
    创建根目录:mkdir -p /var/www/ip/{100,200}
  4. linux 大型网站架构 linux网站搭建步骤_linux 大型网站架构_12

  5. 写入网页内容
  6. linux 大型网站架构 linux网站搭建步骤_linux_13


  7. 接下来最重要的一步,编辑配置文件
    我们可以先看一下示例文件:cat /usr/share/doc/httpd/httpd-vhosts.conf
  8. linux 大型网站架构 linux网站搭建步骤_服务器_14

  9. 我们接下来编写自己的配置文件:vim /etc/httpd/conf.d/ip.conf
  10. linux 大型网站架构 linux网站搭建步骤_服务器_15


  11. 改完配置文件后一定要重启服务:systemctl restart httpd
  12. linux 大型网站架构 linux网站搭建步骤_服务器_16

  13. 我们先用命令行访问一下:curl 192.168.32.100 / curl 192.168.32.200
  14. linux 大型网站架构 linux网站搭建步骤_配置文件_17

  15. 再用 Windows 访问一下
  16. linux 大型网站架构 linux网站搭建步骤_配置文件_18


基于不同端口访问

  1. 同样的,我们先创建两个根目录:mkdir -p /var/www/port/{10000,20000}
  2. linux 大型网站架构 linux网站搭建步骤_linux_19

  3. 写入页面
  4. linux 大型网站架构 linux网站搭建步骤_apache_20


  5. 写配置文件:vim /etc/httpd/conf.d/port.conf
  6. linux 大型网站架构 linux网站搭建步骤_apache_21


  7. 重启服务:systemctl restart httpd
  8. linux 大型网站架构 linux网站搭建步骤_apache_22


  9. 访问该端口

基于域名访问

  1. 创建一个目录与网卡:mkdir /var/www/port / nmcli connection modify ens33 +ipv4.addresses 192.168.32.10/24
  2. linux 大型网站架构 linux网站搭建步骤_配置文件_23


  3. linux 大型网站架构 linux网站搭建步骤_apache_24

  4. 写入网页:echo this is a test > /var/www/host/index.html
  5. linux 大型网站架构 linux网站搭建步骤_apache_25

  6. 编辑配置文件:vim /etc/httpd/conf.d/host.conf
  7. linux 大型网站架构 linux网站搭建步骤_apache_26

  8. 重启服务:systemctl restart httpd
  9. linux 大型网站架构 linux网站搭建步骤_linux_27

  10. 编辑 host 文件,它相当于系统自带的 DNS 数据库:vim /etc/hosts
  11. linux 大型网站架构 linux网站搭建步骤_apache_28

  12. Windows 的这个文件在:C:\Windows\System32\drivers\etc\hosts
  13. linux 大型网站架构 linux网站搭建步骤_linux_29

  14. 访问网页:curl www.test.com
  15. linux 大型网站架构 linux网站搭建步骤_apache_30


  16. linux 大型网站架构 linux网站搭建步骤_linux 大型网站架构_31