我们平时访问的网站服务就是Web网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务。Web网络服务是一种被动访问的服务程序,即只有接收到互联网中其他主机发出的请求后才会响应,最终用于提供服务程序的Web服务器会通过HTTP或HTTPS把请求的内容传送给用户。
1.安装服务
web服务在rhel7中对应的服务是httpd,使用yum仓库安装httpd服务。
[root@shijie Desktop]# yum install -y httpd
打开火狐浏览器在地址栏中输入http://127.0.0.1就可以看到网站的默认页面了。
网站的数据默认保存在/var/www/html中,在该目录中加入首页文件index.html就可以在网站中看到文件内的内容了。
[root@shijie Desktop]# cd /var/www/html/
[root@shijie html]# ls
[root@shijie html]# echo hello world > index.html
[root@shijie html]# systemctl restart httpd
2.编辑配置文件
配置文件在/etc/httpd/conf/httpd.conf中,其中主要参数如下:
参数 作用
ServerRoot 服务目录
ServerAdmin 管理员邮箱
User 运行服务的用户
Group 运行服务的用户组
ServerName 网站服务器的域名
DocumentRoot 网站数据目录
Listen 监听的IP地址与端口号
DirectoryIndex 默认的索引页页面
ErrorLog 错误日志文件
CustomLog 访问日志文件
Timeout 网页超时时间,默认为300秒
3.开启个人主页
每个用户都有自己的主页,可以使用http://127.0.0.1/~用户名来访问,用户主页数据存放在用户家目录的public_html中。开启个人主页需要编辑/etc/httpd/conf.d/userdir.conf文件,将UserDir disabled注释,UserDir public_html取消注释。
[root@localhost html]# vim /etc/httpd/conf.d/userdir.conf
11 <IfModule mod_userdir.c>
12 #
13 # UserDir is disabled by default since it can confirm the presence
14 # of a username on the system (depending on home directory
15 # permissions).
16 #
17 # UserDir disabled
18
19 #
20 # To enable requests to /~user/ to serve the user's public_html
21 # directory, remove the "UserDir disabled" line above, and uncomment
22 # the following line instead:
23 #
24 UserDir public_html
25 </IfModule>
创建目录和编辑主页文件。
[root@localhost html]# vim /etc/httpd/conf.d/userdir.conf
[root@localhost html]# mkdir /home/shijie/public_html
[root@localhost html]# cd /home/shijie/public_html/
[root@localhost public_html]# echo hello shijie > index.html
重启服务,关闭selinux。
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# setenforce 0
让其他用户对该家目录拥有可读可执行的权限,如果没有权限是访问不了的。
[root@localhost shijie]# chmod -Rf 755 /home/shijie
[root@localhost shijie]# ls -ld /home/shijie/
drwxr-xr-x. 15 shijie shijie 4096 May 23 04:37 /home/shijie/
使用http://127.0.0.1/~shijie就可以访问个人主页了。
4.个人主页加密
使用htpasswd命令给个人主页设置密码,并将生成的密码文件放到家目录下。
[root@localhost shijie]# htpasswd -c /home/shijie/password shijie
New password:
Re-type new password:
Adding password for user shijie
编辑个人主页配置文件,在最下方Directory中加入密码验证参数。
<Directory "/home/*/public_html">
AllowOverride all
authuserfile /home/shijie/password
authname "hello"
authtype basic
require user shijie
</Directory>
重启服务后在访问个人主页时就需要输入用户和密码了。输入正确的密码才能访问个人主页。
[root@localhost shijie]# systemctl restart httpd
5.虚拟网站服务器
虚拟网站主机是将一台物理服务器逻辑上虚拟成多个网站,可以减少硬件资源的浪费。虚拟网站服务可以使用IP、域名和端口号来划分虚拟服务器。
5.1基于IP
一台服务器可以有多个IP,使用不同的IP绑定不同的网站,只要访问绑定的IP就可以访问相应的网站了。给服务器网卡增加IP,分别是192.168.10.10,192.168.10.11,192.168.10.12,重启网卡后ping以下三个地址验证是否配置成功。
[root@localhost shijie]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eno16777736
UUID=5305da6a-ee36-4f8e-bd14-985089fb8895
ONBOOT=yes
HWADDR=00:0C:29:04:A1:14
IPADDR0=192.168.10.10
PREFIX0=24
IPADDR1=192.168.10.11
PREFIX1=24
IPADDR2=192.168.10.12
PREFIX2=24
[root@localhost shijie]# systemctl restart network
在网站默认目录中增加三个目录分别对应三个IP。
[root@localhost shijie]# cd /var/www/html/
[root@localhost html]# mkdir 10 11 12
[root@localhost html]# ls
10 11 12
[root@localhost html]# echo 192.168.10.10 > 10/index.html
[root@localhost html]# echo 192.168.10.11 > 11/index.html
[root@localhost html]# echo 192.168.10.12 > 12/index.html
在网站配置文件中追加虚拟主机功能。
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
<VirtualHost 192.168.10.10>
DocumentRoot /var/www/html/10
<Directory /var/www/html/10>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.11>
DocumentRoot /var/www/html/11
<Directory /var/www/html/11>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.12>
DocumentRoot /var/www/html/12
<Directory /var/www/html/12>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost html]# systemctl restart httpd
输入IP就可以访问相对应的网站了。
5.2基于端口
基于端口号的虚拟主机功能可以让用户通过指定的端口号来访问服务器上的网站资源。删除上一节的目录,创建三个端口分别对应的目录。
[root@localhost html]# rm -rf 10 11 12
[root@localhost html]# mkdir 65510 65511 65512
[root@localhost html]# ls
65510 65511 65512
[root@localhost html]# echo 65510 > 65510/index.html
[root@localhost html]# echo 65511 > 65511/index.html
[root@localhost html]# echo 65512 > 65512/index.html
在网站配置文件中修改虚拟主机对应的IP、端口和对应的目录,增加监听端口,默认监听80。
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
Listen 65510
Listen 65511
Listen 65512
<VirtualHost 192.168.10.10:65510>
DocumentRoot /var/www/html/65510
<Directory /var/www/html/65510>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.10:65511>
DocumentRoot /var/www/html/65511
<Directory /var/www/html/65511>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.10:65512>
DocumentRoot /var/www/html/65512
<Directory /var/www/html/65512>
allowOverride None
Require all granted
</Directory>
[root@localhost html]# systemctl restart httpd
使用http://192.168.10.10:65510就可以访问对应端口的网站了。
5.3基于域名
创建三个网站数据的目录分别对应三个域名shijie10.com、shijie11.com、shijie12.com。
[root@localhost html]# rm -rf 65510 65511 65512
[root@localhost html]# mkdir shijie11 shijie12 shijie10
[root@localhost html]# ll
total 0
drwxr-xr-x 2 root root 6 May 23 08:00 shijie10
drwxr-xr-x 2 root root 6 May 23 08:00 shijie11
drwxr-xr-x 2 root root 6 May 23 08:00 shijie12
[root@localhost html]# echo www.shijie11.com > shijie11/index.html
[root@localhost html]# echo www.shijie12.com > shijie12/index.html
[root@localhost html]# echo www.shijie10.com > shijie10/index.html
编辑配置文件,加入域名。
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
<VirtualHost 192.168.10.10>
DocumentRoot /var/www/html/shijie10
ServerName www.shijie10.com
<Directory /var/www/html/shijie10>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.10>
DocumentRoot /var/www/html/shijie11
ServerName www.shijie11.com
<Directory /var/www/html/shijie11>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.10>
DocumentRoot /var/www/html/shijie12
ServerName www.shijie12.com
<Directory /var/www/html/shijie12>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost html]# systemctl restart httpd
如果有DNS服务器,可以将这三个域名都绑定在192.168.10.10上,若没有DNS服务器,就将域名和IP的对应关系写在HOST文件中,可以在本机中使用域名访问网站了。
[root@localhost html]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.10 www.shijie10.com www.shijie11.com www.shijie12.com
由于Linux服务器还未配置过DNS所以我配置了一台windowsDNS服务器。可以让局域网内全部可以使用域名访问了。
使用域名访问网站。