Linux搭建http服务
一、什么是Apache?
Apache通常指的是Apache HTTP服务器,这是一个广泛使用的开源Web服务器软件,它由Apache软件基金会管理。这个服务器可以运行在多种操作系统上,包括Unix、Linux以及Windows等,并且支持多种网络协议,不仅限于HTTP和HTTPS。
- http:超文本传输协议,通过线路以明文形式发送,默认使用80端口/TCP
- https:经TLS/SSL安全加密的超文本传输协议,默认使用443端口/TCP
二、Apache的配置文件
主配置文件的重要参数
主配置文件:/etc/httpd/conf/httpd.conf
三、如何搭建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.baidu.com" 为域名来创建一个虚拟网站
1)网页数据存放在/www/hubstc/下;
2)网站主页内容为:"welcome to Hubei Open University,our domain name is www.baidu.com";
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.baidu.com #定义域名
DocumentRoot /www/baidu #网站主页文件的目录
</VirtualHost>
4、做域名解析文件(注意:client端也要配置)
[root@server ~]# vim /etc/hosts
加入:192.168.100.10 www.baidu.com
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.baidu.com
# 访问:
# 有图形化
[root@client ~]# firefox http://www.baidu.com
# 没有图形化
[root@client ~]# curl http://www.baidu.com
4、搭建基于端口访问的虚拟主机
配置两个新的访问端口,分别为8088和8089
1)网站域名为:www.baidu.com;
2)网页数据分别存放在/www/8088/和/www/8089/下;
3)每个端口主页内容分别为:"this is new port 8088或8089 for website www.baidu.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.badiu.com">>index.html
[root@server ~]# cd /www/8089
[root@server 8089]# echo "this is new port 8089 for website www.baidu.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.baidu.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.baidu.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.baidu.com;
2)网页文件:/var/www/html/index.html
3)网页内容:this is www.baidu.com
在以上基础上完成:
# 服务端创建网页文件
[root@server ~]# cd /var/www/html
[root@server html]# echo "this is www.baidu.com" >> index.html
# 做域名解析文件
[root@server ~]# vim /etc/hosts
加入:192.168.100.10 www.baidu.com
[root@server ~]# scp /etc/hosts 192.168.100.20:/etc
# 客户端使用浏览器去访问
https://www.baidu.com
更多推荐



所有评论(0)