运行环境: Red Hat Enterprise Linux Server release 7.0 (Maipo)

  1. 网站服务程序配置

    1. 1 挂载系统镜像到 /media/cdrom 目录

# mkdir -p /media/cdrom
# mount /dev/cdrom /media/cdrom

    1.2  使用 vim 编辑器创建 Yum 仓库的配置文件

# vim /etc/yum.repos.d/rhel7.repo
[rhel7]
name=rhel7
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0

 

    1.3 动手安装 Apache 服务程序

# yum install httpd

 

      注:  系统提示 Is this ok [y/d/N]:  时 输入 y 即可

    1.4 启用 httpd 服务程序并将其加入到开机启动项中

# systemctl start httpd
# systemctl enable httpd

      现在打开浏览器输入127.0.0.1 便可访问到用于提供 web 服务的 httpd 服务程序的默认页了

 

  2. 配置服务文件参数  

      2.1 Linux 系统中的配置文件

        配置文件的名称           存放位置

          服务目录          /etc/httpd

          主配置文件         /etc/httpd/conf/httpd.conf

          网站数据目录        /var/www/html

          访问日志          /var/log/httpd/access_log

          错误日志          /var/log/httpd/error_log

 

      2.2 配置 httpd 服务程序时常用的参数及用途描述

         参数               用途

        ServerRoot            服务目录

        ServerAdmin            管理员邮箱

        User               运行服务的用户

        Group               运行服务的用户组

        ServerName             网站服务器的域名

        DocumentRoot            网站数据目录

        Directory                网站数据目录权限

        Listen               监听的 IP 地址与端口号

        DirectoryIndex            默认的索引页页面

        ErrorLog                错误日志文件

        CustomLog            访问日志文件

        Timeout             页面超时时间,默认为300秒

 

      2.3 建立网站数据的保存目录,并创建首页文件

# mkdir /home/wwwroot
# echo "The New Web Directory" > /home/wwwroot/index.html

  

      2.4 用 vim 打开 /etc/httpd/conf/httpd.conf 服务程序的主配置文件:

         将参数 DocumentRoot 的值修改为:  DocumentRoot “/home/wwwroot”

         将参数 <Directory “***”> 的值修改为: <Directory “/home/wwwroot”>

            修改完成后,保存并退出,重新启动 httpd 服务程序

# systemctl restart httpd

      

      2.5 配置 SELinux 安全子系统

          2.5.1 刷新浏览器页面,查看页面是否显示为 httpd 服务程序的默认页

          2.5.2 查看 SELinux 默认运行模式 (默认一般是 enforcing 模式)

# getenforce

             注: SELinux 服务有三种配置模式,分别为

                 enforcing: 强制启用安全策略模式,将拦截服务的不合法请求

                 permissive: 遇到服务越权访问时,只发出警告而不强制拦截

                 disabled: 对于越权的行为不警告不拦截

          2.5.3 为了确认是否是 SELinux 导致页面无法正常浏览,可已用 setenforce [0|1] 临时修改 SELinux 运行模式

setenforce 0

          2.5.4 再次刷新浏览器页面,查看页面显示正常

              2.5.4.1  将 SELinux 服务恢复到强制启用安全策略模式,并查看原始网站数据目录与当前数据目 SELinux 的安全上下文是否一致

setenforce 1
ls -Zd /var/www/html
ls -Zd /home/wwwoot

              2.5.4.1 查看两个目录的角色段 object_r: 值是否一致,如不一致则进行修改

/*
这是我这的显示状态

# ls -Zd /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
# ls -Zd /home/wwwroot
drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot

*/
// 执行一下语句,将当前网站目录 /home/wwwroot 的 SELinux 安全上下文修改为跟原始网站目录一样就可以了;(restorecon 立即生效, -Rv 对指定目录进行递归操作)

# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*
# restorecon -Rv /home/wwwroot

  

  3. 个人用户主页功能

     分别为每一位用户建立一个独立网站,该功能可以让系统内所有的用户在自己的家目录中管理个人的网站

     3.1 开启 httpd 的个人用户主页功能

        用 vim 打开 /etc/httpd/conf.d/userdir.conf

# vim /etc/httpd/conf.d/userdir.conf

          1.  打开文档后,找到参数 UserDir disabled   在该参数前面加上 # 号

          2.  找到参数 #UserDir public_html 将其前面的 # 号去除

        修改完成后,保存并退出

                                         

 

          3. 在用户家目录中建立用于保存网站数据的目录及首页面文件,并将家目录的权限修改为 755 ,保证其他人也有权限读取里面的内容

# su - user
# mkdir public_html
# echo "This is user website" > public_html/index.html
# chmod -Rf 755 /home/user

          

          4. 重新启动 httpd 

# su - root
# systemctl restart httpd

  

          5. 开启SELinux 的 httpd 服务

# setsebool -P httpd_enable_homedirs=on

        好了,现在打开浏览器输入 http://127.0.0.1/~user/ 就能访问到 user 用户下的 web 页面了

 

           6.  如果网站的拥有者并不希望直接将网页内容显示出来,可以设置通过身份验证来访问

            6.1 使用 htpasswd 命令生成密码数据库。(用户名不必是系统中已有的本地账户)

# htpasswd -c /etc/httpd/passwd username
// 输入密码
// 再次输入密码

            6.2 编辑个人用户主页功能的配置文件

#vim /etc/httpd/conf.d/userdir.conf
//找到原文件处 进行修改
// 原文件 <Directory "/home/*/public_html"> AllowOverride FileInfo AuthConfig Limit Indexes Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS </Directory>

// 修改后的文件
<Directory "/home/*/public_html">

    # AllowOverride FileInfo AuthConfig Limit Indexes
    # Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    # Require method GET POST OPTIONS

    AllowOverride all

    # 刚刚生成出来的秘密验证文件保存路径
    authuserfile “/etc/httpd/passwd”

    # 当用户尝试访问个人用户网站时的提示信息
    authname “My privately website”
    authtype basic

    # 用户进行账户密码登录时需要验证的用户名称
    require user user
  </Directory>

        修改完成后,保存并退出。重启 httpd 服务程序即可生效

 

   4. 在一台 Linux 系统服务器上,基于不同 IP 地址,访问不同的网站页面

       

      如果一台服务器有多个 IP 地址,而且每个 IP 地址与服务器上部署的每个网站 一 一 对应,这样当用户请求访问不同的 IP 地址时,会访问到不同的玩战地页面资源。

       1. 用 nmtui 命令配置 IP 地址,我这里分别配置了 192.168.31.207、192.168.31.208、192.168.31.209 三个 IP 地址,在配置完毕并重启网卡服务后,记得检查网络的连通性,确保三个 IP 地址均可正常访问。

# nmtui
# systemctl restart network

# ping -c 4 192.168.31.207
PING 192.168.31.207 (192.168.31.207) 56(84) bytes of data.
64 bytes from 192.168.31.207: icmp_seq=1 ttl=64 time=0.164 ms
64 bytes from 192.168.31.207: icmp_seq=2 ttl=64 time=0.132 ms
64 bytes from 192.168.31.207: icmp_seq=3 ttl=64 time=0.130 ms
64 bytes from 192.168.31.207: icmp_seq=4 ttl=64 time=0.132 ms
--- 192.168.31.207 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.130/0.139/0.164/0.018 ms

# ping -c 4 192.168.31.208
PING 192.168.31.208 (192.168.31.208) 56(84) bytes of data.
64 bytes from 192.168.31.208: icmp_seq=1 ttl=64 time=0.257 ms
64 bytes from 192.168.31.208: icmp_seq=2 ttl=64 time=0.128 ms
64 bytes from 192.168.31.208: icmp_seq=3 ttl=64 time=0.111 ms
64 bytes from 192.168.31.208: icmp_seq=4 ttl=64 time=0.123 ms
--- 192.168.31.208 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.111/0.154/0.257/0.061 ms

# ping -c 4 192.168.31.209
PING 192.168.31.209 (192.168.31.209) 56(84) bytes of data.
64 bytes from 192.168.31.209: icmp_seq=1 ttl=64 time=0.147 ms
64 bytes from 192.168.31.209: icmp_seq=2 ttl=64 time=0.133 ms
64 bytes from 192.168.31.209: icmp_seq=3 ttl=64 time=0.155 ms
64 bytes from 192.168.31.209: icmp_seq=4 ttl=64 time=0.222 ms
--- 192.168.31.209 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.133/0.164/0.222/0.035 ms

        

    2. 分别在 /home/wwwroot 中创建用于保存不同网站数据的 3 个目录,并向其中分别写入网站的首页文件

# mkdir -p /home/wwwroot/207
# mkdir -p /home/wwwroot/208
# mkdir -p /home/wwwroot/209
# echo "IP:192.168.31.207" > /home/wwwroot/207/index.html
# echo "IP:192.168.31.208" > /home/wwwroot/208/index.html
# echo "IP:192.168.31.209" > /home/wwwroot/209/index.html

  

    3. 在 httpd 服务的配置文件中分别追加写入三个基于 IP 地址的虚拟主机网站参数,然后保存并退出。重启 httpd 服务!!!

# vim /etc/httpd/conf/httpd.conf

# Note that from this point forward you must specifically allow # particular features to be enabled - so if something\'s not working as # you might expect, make sure that you have specifically enabled it # below. #可在以上文档后添加 <VirtualHost 192.168.31.207> DocumentRoot /home/wwwroot/207 ServerName www.test.com <Directory /home/wwwroot/207> AllowOverride None Require all granted </Directory> </VirtualHost>
<VirtualHost 192.168.31.208> DocumentRoot /home/wwwroot/208 ServerName bbs.test.com <Directory /home/wwwroot/208> AllowOverride None Require all granted </Directory> </VirtualHost>
<VirtualHost 192.168.31.209> DocumentRoot /home/wwwroot/209 ServerName tech.test.com <Directory /home/wwwroot/209> AllowOverride None Require all granted </Directory> </VirtualHost>

    

    4. 配置 SELinux 

# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/207
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/207/*
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/208
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/208/*
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/209
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/209/*
# restorecon -Rv /home/wwwroot

        运行一切正常后,打开浏览器 ,可分别输入以上三个 IP 地址,即可访问到相应页面

 

   5. 基于不同的主机域名 访问同一 IP 下的不同页面  

        当服务器无法为每个网站都分配一个独立 IP 地址的时候,可以尝试让 Apache 自动识别用户请求的域名,

       1. 手工定义 IP 地址与域名之间对应关系的配置文件。

# vim /etc/hosts

// 在打开的文档中添加一下域名
192.168.31.207 www.test.com bbs.test.com tech.test.com

        添加完成后,分别 ping 这些域名来验证域名是否已经成功解析为 IP 地址

       2. 分别在 /home/wwwroot 中创建用于保存不同网站数据的三个目录及文档

# mkdir -p /home/wwwroot/www
# mkdir -p /home/wwwroot/bbs
# mkdir -p /home/wwwroot/tech
# echo "www.test.com" > /home/wwwroot/www/index.html
# echo "bbs.test.com" > /home/wwwroot/bbs/index.html
# echo "tech.test.com" > /home/wwwroot/tech/index.html

  

       3. 在 httpd 服务的配置文件中分别追加写入三个基于 IP 地址的虚拟主机网站参数,然后保存并退出。重启 httpd 服务!!!

# vim /etc/httpd/conf/httpd.conf

# # Note that from this point forward you must specifically allow # particular features to be enabled - so if something\'s not working as # you might expect, make sure that you have specifically enabled it # below. # 可在以上文档后添加 <VirtualHost 192.168.31.207> DocumentRoot "/home/wwwroot/www" ServerName "www.test.com" <Directory "/home/wwwroot/www"> AllowOverride None Require all granted </Directory> </VirtualHost>
<VirtualHost 192.168.31.207> DocumentRoot "/home/wwwroot/bbs" ServerName "bbs.test.com" <Directory "/home/wwwroot/bbs"> AllowOverride None Require all granted </Directory> </VirtualHost>
<VirtualHost 192.168.31.207> DocumentRoot "/home/wwwroot/tech" ServerName "tech.test.com" <Directory "/home/wwwroot/tech"> AllowOverride None Require all granted </Directory> </VirtualHost>

  

      4. 配置 SELinux 

# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www/*
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs/*
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech
# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech/*
# restorecon -Rv /home/wwwroot

            运行一切正常后,打开浏览器 ,可分别输入以上三个域名地址,即可访问到相应页面

 

     

版权声明:本文为koreyoshi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/koreyoshi/articles/8902114.html