Linux——搭建Apache(httpd)服务器

一、什么是Apache?

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

有两个版本:

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

二、Apache的配置文件

1、配置文件的位置

配置文件 存放位置
服务目录 /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 server
[root@localhost ~]# bash
[root@server ~]# 

# 2、配置网络
# (1)虚拟交换机配置为192.168.100.0网段,网络适配器选择仅主机模式;

# (2)编辑网络配置文件:
[root@server ~]# cd /etc/sysconfig/network-scripts/
[root@server network-scripts]# vim ifcfg-ens33
#需要修改的参数为:
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.100.10
PREFIX=24

# (3)重启网络服务:
[root@server network-scripts]# systemctl restart network

# 3、配置yum源
# (1)先进入虚拟机设置,把系统镜像连接到虚拟机的光驱上;

# (2)挂载光驱里的镜像:
[root@server ~]# mount /dev/cdrom /media/
mount: /dev/sr0 is write-protected, mounting read-only

# (3)修改yum源配置文件:
[root@server ~]# cd /etc/yum.repos.d/
[root@server yum.repos.d]# ls
[root@server yum.repos.d]# vim local.repo
[rhel]
name=rhel
baseurl=file:///media
enabled=1
gpgcheck=0

# (4)清空yum源缓存并检索yum源
[root@server yum.repos.d]# yum clean all 
[root@server yum.repos.d]# yum repolist

1、搭建简单的httpd服务

Server端配置:

# 1、安装httpd服务
[root@server ~]# yum -y install httpd

# 2、配置防火墙
[root@server ~]# firewall-cmd --permanent --add-service=http
[root@server ~]# firewall-cmd --reload

# 3、开启服务
[root@server ~]# systemctl restart httpd
[root@server ~]# systemctl enable httpd

Client端访问:

# 有图形化
[root@client ~]# firefox http://192.168.100.10
# 没有图形化
[root@client ~]# curl http://192.168.100.10

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

Server端配置:

# 首先已经安装了httpd服务

# 1、新建用户(网站基于该用户)
[root@server ~]# useradd wzg

# 2、创建个人的网页文件
[root@server ~]# mkdir /home/wzg/public_html
[root@server ~]# cd /home/wzg/public_html
[root@server public_html]# echo "hello,欢迎访问王智刚的个人网站">>index.html

# 3、修改用户网页文件的访问权限
[root@server ~]# chmod -R 705 /home/wzg		#使其他用户具有读取和执行的权限

# 4、修改基于用户的配置文件
[root@server ~]# vim /etc/httpd/conf.d/userdir.conf
UserDir enabled		#第17行,改为开启,表示开启个人用户主页功能
UserDir public_html	#第24行,去注释,表示网站数据在用户家目录中的名称

# 5、修改selinux权限
[root@server ~]# getsebool -a | grep home
[root@server ~]# setsebool httpd_enable_homedirs on

# 6、配置防火墙(同上)
[root@server ~]# firewall-cmd --permanent --add-service=http
[root@server ~]# firewall-cmd --reload

# 7、重启服务
[root@server ~]# systemctl restart httpd

Client端访问:

# 有图形化
[root@client ~]# firefox http://192.168.100.10/~wzg/
# 没有图形化
[root@client ~]# curl http://192.168.100.10/~wzg/

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

以"www.hubtvu.edu.cn" 为域名来创建一个虚拟网站

1)网页数据存放在/www/hubstc/下;
2)网站主页内容为:"welcome to Hubei Open University,our domain name is www.hubtvu.edu.cn";
3)网站对所有客户端开放。

Server端配置:

# 首先已经安装了httpd服务

# 1、创建虚拟主机的网页文件
[root@server ~]# mkdir -p /www/hubstc
[root@server ~]# cd /www/hubstc/
[root@server hubstc]# echo "welcome to Hubei Open University,our domain name is www.hubtvu.edu.cn" >> index.html

# 2、修改文件的访问权限(使其他用户具有可执行权限)
[root@server hubstc]# chmod o+x /www
[root@server hubstc]# chmod o+x index.html

# 3、配置虚拟主机的网页文件
[root@server ~]# cd /etc/httpd/conf.d
[root@server conf.d]# vim hubstc.conf
<Directory /www/hubstc>
        Require all granted		#所有客户端都可访问
</Directory>
<VirtualHost 192.168.100.10>
        ServerName www.hubtvu.edu.cn	#定义域名
        DocumentRoot /www/hubstc	#网站主页文件的目录
</VirtualHost>

# 4、做域名解析文件(注意:client端也要配置)
[root@server ~]# vim /etc/hosts
加入:192.168.100.10	www.hubtvu.edu.cn

# 5、修改虚拟主机网页文件的selinux上下文类型
[root@server ~]# ll -dZ /www		#查看/www的上下文类型
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www
[root@server ~]# ll -dZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?'	#修改文件的上下文类型,包括/www下面的子文件
[root@server ~]# restorecon -RFv /www		#以可视化模式强制递归刷新

# 6、配置防火墙(同上)
[root@server ~]# firewall-cmd --permanent --add-service=http
[root@server ~]# firewall-cmd --reload

# 7、重启服务
[root@server ~]# systemctl restart httpd

Client端配置及访问:

# 做域名解析
[root@client ~]# vim /etc/hosts
加入:192.168.100.10	www.hubtvu.edu.cn

# 访问:
# 有图形化
[root@client ~]# firefox http://www.hubtvu.edu.cn
# 没有图形化
[root@client ~]# curl http://www.hubtvu.edu.cn

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

配置两个新的访问端口,分别为8088和8089

1)网站域名为:www.hubtuv.com;
2)网页数据分别存放在/www/8088/和/www/8089/下;
3)每个端口主页内容分别为:"this is new port 8088或8089 for website www.hubtuv.com"。

Server端配置:

# 首先已经安装了httpd服务

# 1、新建虚拟主机的网页文件
[root@server ~]# mkdir -p /www/8088 /www/8089
[root@server ~]# cd /www/8088
[root@server 8088]# echo "this is new port 8088 for website www.hubtuv.com">>index.html
[root@server ~]# cd /www/8089
[root@server 8089]# echo "this is new port 8089 for website www.hubtuv.com">>index.html

# 2、修改文件的访问权限
[root@server ~]# chmod o+x /www
[root@server ~]# chmod o+x /www/8088/index.html 
[root@server ~]# chmod o+x /www/8089/index.html 

# 3、配置虚拟主机的文件
[root@server ~]# cd /etc/httpd/conf.d
[root@server ~]# vim 8088.conf
<Directory /www/8088/>
        Require all granted
</Directory>
<virtualHost 192.168.100.10:8088>	#注意:IP地址后面要加新端口号
        DocumentRoot /www/8088/
        ServerName www.hubtuv.com	#也可以不添加域名
</virtualHost>


[root@server ~]# vim 8089.conf
<Directory /www/8089/>
        Require all granted
</Directory>
<virtualHost 192.168.100.10:8089>
        DocumentRoot /www/8089/
        ServerName www.hubtuv.com
</virtualHost>

# 4、添加监听端口
[root@server ~]# cd /etc/httpd/conf/
[root@server conf]# vim httpd.conf
#在42行 Listen 80的下一行添加:
Listen 8088
Listen 8089

# 5、修改端口的上下文类型
[root@server ~]# semanage port -l | grep http	#查看httpd服务端口的类型
[root@server ~]# semanage port -a 8088 -t http_port_t -p tcp	#将8088端口加入httpd服务端口类型
[root@server ~]# semanage port -a 8089 -t http_port_t -p tcp	#将8089端口加入httpd服务端口类型
[root@server ~]# netstat -pant			#查看端口号

# 6、修改网页文件的上下文类型(前面已经做过)
[root@server ~]# ll -dZ /www		#查看/www的上下文类型
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www
[root@server ~]# ll -dZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?'	#修改文件的上下文类型,包括/www下面的子文件
[root@server ~]# restorecon -RFv /www		#以可视化模式强制递归刷新

# 7、添加新端口到防火墙(注意:前面只添加了服务,并没有添加新的端口)
[root@server ~]# firewall-cmd --list-all
[root@server ~]# firewall-cmd --permanent --add-port=8088/tcp
[root@server ~]# firewall-cmd --permanent --add-port=8089/tcp
[root@server ~]# firewall-cmd --reload

# 8、重启服务
[root@server ~]# systemctl restart httpd

Client端访问:

# 有图形化
[root@client ~]# firefox www.hubtuv.com:8088
[root@client ~]# firefox www.hubtuv.com:8089
# 没有图形化
[root@client ~]# curl www.hubtuv.com:8088
[root@client ~]# curl www.hubtuv.com:8089

(五)搭建基于TLS加密的虚拟主机

注意:经TLS/SSL安全加密的超文本传输协议,默认情况下使用端口443/TCP

Server端配置:

注意:前面做的的配置要全部删除,否则配置文件会起冲突

# 1、安装httpd服务
[root@server ~]# yum -y install httpd

# 2、安装TLS加密软件,网站内容不用明文传输
[root@server ~]# yum -y install mod_ssl

# 3、生成密钥文件
[root@server ~]# openssl genrsa > tlsweb.key

# 4、生成证书的请求文件
[root@server ~]# openssl req -new -key tlsweb.key > tlsweb.csr

# 5、生成证书文件
[root@server ~]# openssl req -x509 -days 365 -key tlsweb.key -in tlsweb.csr > tlsweb.crt

# 6、修改ssl.conf配置文件(第100行、107行)
[root@server ~]# vim /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/pki/tls/certs/tlsweb.crt
SSLCertificateKeyFile /etc/pki/tls/private/tlsweb.key

# 7、把证书文件和秘钥文件分别拷贝到ssl.conf配置文件里的对应路径下面
[root@server ~]# cp tlsweb.crt /etc/pki/tls/certs/
[root@server ~]# cp tlsweb.key /etc/pki/tls/private/

# 8、配置防火墙(注意:添加的是https)
[root@server ~]# firewall-cmd --permanent --add-service=https
[root@server ~]# firewall-cmd --reload

# 9、重启服务
[root@server ~]# systemctl restart httpd

Client端访问:

# 使用浏览器去访问
https://192.168.100.10

创建一个基于tls加密的虚拟主机,要求如下:

1)网站域名:www.hubtvu.edu.cn;
2)网页文件:/var/www/html/index.html
3)网页内容:this is www.hubtvu.edu.cn

在以上基础上完成:
# 服务端创建网页文件
[root@server ~]# cd /var/www/html
[root@server html]# echo "this is www.hubtvu.edu.cn" >> index.html

# 做域名解析文件
[root@server ~]# vim /etc/hosts
加入:192.168.100.10	www.hubtvu.edu.cn
[root@server ~]# scp /etc/hosts 192.168.100.20:/etc

# 客户端使用浏览器去访问
https://www.hubtvu.edu.cn

查看报错日志文件:journalctl -xe

声明:未经许可,不得转载

posted @ 2021-11-22 09:47  王智刚  阅读(12068)  评论(0编辑  收藏  举报