保姆级教学!!! Linux下搭建Apche(httpd)服务 超详细

搭建Apache HTTP服务器与认证

  • 一、什么是Apache?
  • 二、Apache的配置文件
  • 三、如何搭建Apache服务器
    • 1、更改主机名
    • 2、配置网络
    • 3、配置yum源
    • 2、搭建基于用户的个人网站
      • 2.1.新建用户(用于基于该用户)
      • 2.2.创建个人的网页文件
      • 2.3.修改用户网页文件的访问权限
      • 2.4.修改基于用户的配置文件.
      • 2.5.关闭防火墙修改selinux权限
      • 2.6.重启服务
      • 2.7.访问网页
    • 3、搭建基于域名访问的虚拟主机
      • 3.1、创建虚拟主机的网页文件
      • 3.2、修改文件的访问权限(使其它用户具有可执行权力)
      • 3.3、配置虚拟主机的网页文件
      • 3.4、做域名解析文件
      • 3.5、配置防火墙和selinux
      • 3.6、重启服务
    • 4、搭建基于端口访问的虚拟主机
      • 4.1、新建虚拟主机的网页文件
      • 4.2、修改文件的访问权限
      • 4.3、配置虚拟主机的文件
      • 4.4、添加监听端口
      • 4.5、添加新的端口到防火墙(前面只是添加了服务,并没有添加新的端口)
      • 4.6、重启服务
    • 5、搭建网站并完成认证
      • 5.1、新建虚拟机的网页文件
      • 5.2、修改文件的访问权限
      • 5.3、修改主文件
      • 5.4 创建用户和认证文件
      • 5.4 关闭防护墙
      • 5.5、重启服务
      • 5.6、测试:

一、什么是Apache?

Apache(或httpd)服务,是Internet上使用最多的Web服务器技术之一,通俗来讲就是一个用于搭建网站的服务。
有两个版本:

  • http:超文本传输协议,通过线路以明文形式发送,默认使用80端口/TCP
  • https:经TLS/SSL安全加密的超文本传输协议,默认使用443端口/TCP

二、Apache的配置文件

1、配置文件的位置

	配置文件   存放位置
服务目录	/etc/httpd
主配置文件	/etc/httpd/conf/httpd.conf
虚拟主机的配置文件目录
配置文件								存放位置
服务目录							   /etc/httpd
主配置文件					       /etc/httpd/conf/httpd.conf
虚拟主机的配置文件目录				   /etc/httpd/conf.d
基于用户的配置文件					   /etc/httpd/conf.d/userdir.conf
日志文件目录					       /etc/httpd/logs
默认的网站数据目录					   /var/www/html

2、主配置文件的重要参数

主配置文件:/etc/httpd/conf/httpd.conf
参数	作用	参数	作用
serverRoot			服务目录			Servername				网站服务器的域名
Listen			监听的IP地址端口号		DocumentRoot			默认网站数据目录
User			运行服务的用户		Directory				文件目录权限
Group			运行服务的用户组		DirectoryIndex			默认的索引页面
Serveradmin		管理员邮箱			ErrorLog				错误日志文件

三、如何搭建Apache服务器

基本环境:主机名、网卡网络、yum源

1、更改主机名

[root@localhost ~]# hostnamectl set-hostname Ayaka
[root@localhost ~]# bash

2、配置网络

(1)虚拟机NAT网段配置为192.168.123.0网段(可随意)、网卡适配器选择仅主机模式
(2)配置网卡:
需要修改的参数:
BOOTPROTO=static
IPADDR=192.168.123.101
NETMASK=255.255.255.0
(3)重新启动网络服务
[root@ayaka ~]# systemctl restart network

3、配置yum源

1、搭建简单的httpd服务
1.1、安装Apache服务
  [root@ayaka ~]# yum install -y http
  1.2、关闭防火墙
[root@ayaka ~]# systemctl stop firewalld
1.3启动Apache服务
[root@ayaka ~]# systemctl restart httpd
  访问Apche网站
[root@ayaka ~]# curl 192.168.123.101

2、搭建基于用户的个人网站

·首先确定已经安装了httpd服务、

2.1.新建用户(用于基于该用户)

[root@localhost ~]# useradd ayaka

2.2.创建个人的网页文件

[root@localhost ~]# mkdir /home/ayaka/public_html
[root@localhost ~]# cd /home/ayaka/public_html/
[root@localhost ~]# echo “welcome to ayaka’s website” >>index.html

2.3.修改用户网页文件的访问权限

[root@localhost ~]# chmod -R 705 /home/ayaka

2.4.修改基于用户的配置文件.

[root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf
修改第17行和24行
UserDir enable 改为开启,表示开启个人用户主页功能
UserDir public_html 去注释,表示网站数据在用户家目录中的名称

2.5.关闭防火墙修改selinux权限

[root@localhost public_html]# systemctl stop firewalld
[root@localhost public_html]# setenforce 0

2.6.重启服务

[root@localhost public_html]# systemctl restart httpd

2.7.访问网页

没有图形化:Curl httpd://192.168.123.101/~ayaka/
图形化:firefox //192.168.123.101/~ayaka/
或在主机浏览器搜索192.168.123.101/~ayaka/

3、搭建基于域名访问的虚拟主机

以“www.toto.com”为域名来创建一个虚拟网站
1.网站数据存放在/www/toto/下
2.网站主页内容为:“welcome to toto’s website”
3.网站对所有客户端开放

#老样子 首先还是确认安装了httpd服务

[root@localhost public_html]# rpm -q httpd
httpd-2.4.6-95.el7.centos.x86_64

3.1、创建虚拟主机的网页文件

[root@localhost public_html]# mkdir /www/toto -p

3.2、修改文件的访问权限(使其它用户具有可执行权力)

[root@localhost toto]# chmod o+x /www
[root@localhost toto]# chmod o+x index.html

3.3、配置虚拟主机的网页文件

[root@localhost toto]# cd /etc/httpd/conf.d
[root@localhost conf.d]# vim toto.conf
<Virtualhost 192.168.123.101>
        ServerName www.toto.com   //定义域名
        DocumentRoot /www/toto    //网站主页文件的目录
<Directory /www/toto>
        require all granted    //所有客户端都可以访问
</Directory>
</Virtualhost>

~

3.4、做域名解析文件

[root@localhost conf.d]# vim /etc/hosts
第三行添加 192.168.123.101 www.toto.com

3.5、配置防火墙和selinux

[root@localhost conf.d]# firewall-cmd --reload
[root@localhost conf.d]# firewall-cmd --permanent --add-service=http

3.6、重启服务

[root@localhost conf.d]# systemctl restart httpd
访问:
无图形化界面
Curl www.toto.com
又图形画界面
Firefox www.toto.com

4、搭建基于端口访问的虚拟主机

配置两个新的访问端口,分别为8088和8089
1.网站域名为 www.toto.com
2.网页数据分别存在/www/8088和/www/8089下面
3.每个端口主页内容分别为:“this is new port (8088或8089)for www.toto.com”

配置:
#首确安服

4.1、新建虚拟主机的网页文件

[root@localhost conf.d]# mkdir /www/8088 -p
[root@localhost conf.d]# mkdir /www/8089 -p
[root@localhost conf.d]# cd /www/8088
[root@localhost 8088]# echo "this is a new port 8088 for www.toto.com" >>index.html
[root@localhost 8088]# cd /www/8089
[root@localhost 8089]# echo "this is a new port 8089 for www.toto.com" >>index.html

4.2、修改文件的访问权限

[root@localhost 8089]# chmod o+x /www
[root@localhost 8089]# chmod o+x /www/8088/index.html
[root@localhost 8089]# chmod o+x /www/8089/index.html

4.3、配置虚拟主机的文件

[root@localhost conf.d]# vim 8088.conf
<Directory /www/8088/>
        Require all granted
</Directory>
<VirtualHost 192.168.123.101:8088>
        DocumentRoot /www/8088
        Servername www.toto.com
</VirtualHost>
<VirtualHost 192.168.123.101:8089>
        DocumentRoot /www/8089
        ServerName www.toto.com
</VirtualHost>

4.4、添加监听端口

[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf
42 Listen 80
43 Listen 8088
44 Listen 8089

4.5、添加新的端口到防火墙(前面只是添加了服务,并没有添加新的端口)

[root@localhost conf.d]# firewall-cmd --add-port=8088/tcp
success
[root@localhost conf.d]# firewall-cmd --add-port=8089/tcp
success
[root@localhost conf.d]# firewall-cmd --reload
success

4.6、重启服务

systemctl restart httpd

5、搭建网站并完成认证

搭建网站并完成认证
以www.basic.com为域名创建一个虚拟网站
1.网页数据放在/www/basic 主页为basic.html
2.网页主内容为 “Hello world”
3.创建用户webuser1,webuser2 密码为1,实现网站的认证访问,只有这两个用户才能访问

5.1、新建虚拟机的网页文件

[root@localhost ~]# mkdir /www/basic -p
[root@localhost ~]# cd /www/basic/
[root@localhost toto]# echo “Hello world” >>index.html

5.2、修改文件的访问权限

[root@localhost toto]# chmod +x /www
[root@localhost toto]# chmod +x /www/basic/index.html

5.3、修改主文件

需要修改的参数

119 DocumentRoot "/www"
124 <Directory "/www">
131 <Directory "/www">
在服务目录的最后添加认证信息
355 <VirtualHost 192.168.123.101:80>
356         ServerName www.basic.com
357         DocumentRoot /www/basic
358 <Directory /www/basic>
359         AuthType basic
360         Authname passwd
361         AuthUserfile /etc/httpd/webpasswd
362         require user webuser1
363 </Directory>
364 
365 </VirtualHost

5.4 创建用户和认证文件

[root@www ~]# htpasswd -c /etc/httpd/webpasswd webuser1  //创建认证用户webuser1
New password:   //输入密码
Re-type new password:   //输入密码
Adding password for user webuser1
[root@www ~]# htpasswd  /etc/httpd/webpasswd webuser2
New password:   //输入密码 
Re-type new password:   //输入密码
Adding password for user webuser2

5.4 关闭防护墙

[root@www ~]# systemctl stop firewalld
[root@www ~]# setenforce 0

5.5、重启服务

[root@www ~]# systemctl restart httpd

5.6、测试:

字符界面

[root@www ~]# curl www.basic.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>

图形化访问
保姆级教学!!! Linux下搭建Apche(httpd)服务 超详细_第1张图片

  • 输入用户密码
    保姆级教学!!! Linux下搭建Apche(httpd)服务 超详细_第2张图片

你可能感兴趣的:(linux,centos,计算机网络)