zoukankan      html  css  js  c++  java
  • CentOS7 配置httpd

    1:安装 yum install httpd

    2:systemctl start httpd

      systemctl enable httpd

    3:更改网站数据目录

      [root@localhost ~]# mkdir /home/wwwroot

      [root@localhost ~]# echo "this is /home/wwwroot/index.html" > /home/wwwroot/index.html

      [root@localhost ~]# vim /etc/httpd/conf/httpd.conf

        #网站数据保存目录
        DocumentRoot "/home/wwwroot" 
        #网站数据目录权限
        <Directory "/home/wwwroot">
            AllowOverride None
            Require all granted
         </Directory>

      [root@localhost ~]# systemctl restart httpd.service

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

      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*

      [root@localhost ~]# restorecon -Rv /home/wwwroot/
      restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
      restorecon reset /home/wwwroot/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

      成功!

    4:配置个人用户主页

      [root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf

        修改<IfModule mod_userdir.c>模块内容

        在 UserDir disabled 行前面加#

        去掉 UserDir public_html 行前面的#号

      [languang@localhost ~]$ mkdir public_html
      [languang@localhost ~]$ echo "this is languang's website" > public_html/index.html
      [languang@localhost ~]$ chmod -Rf 755 /home/languang/


      [root@localhost ~]# getsebool -a | grep httpd

        查看selinux管理域内关于httpd的所有项

      [root@localhost ~]# setsebool -P httpd_enable_homedirs=on

      [root@localhost ~]#systemctl restart httpd.service

    完成!

    添加访问个人用户主页验证

      [root@localhost ~]# htpasswd -c /etc/httpd/passwd zhangsan
      New password:
      Re-type new password:
      Adding password for user zhangsan
      [root@localhost ~]# htpasswd /etc/httpd/passwd lisi
      New password:
      Re-type new password:
      Adding password for user lisi

      [root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf

        <Directory "/home/*/public_html">
          AllowOverride all
          authuserfile "/etc/httpd/passwd"
          authname "this is alex'website,please enter username and password"
          authtype basic
          Require user zhangsan lisi
        </Directory>

      [root@localhost ~]# systemctl restart httpd

      完成!

    5:配置基于IP地址的虚拟主机功能(使用虚拟机)

      配置网络IP地址(192.168.0.104,192.168.0.105,192.168.0.106)

      [root@localhost ~]# nmtui

      [root@localhost ~]# systemctl restart network

      [root@localhost ~]# mkdir -p /home/wwwroot/104
      [root@localhost ~]# mkdir -p /home/wwwroot/105
      [root@localhost ~]# mkdir -p /home/wwwroot/106

      [root@localhost ~]# echo "192.168.0.104" > /home/wwwroot/104/index.html
      [root@localhost ~]# echo "192.168.0.105" > /home/wwwroot/105/index.html
      [root@localhost ~]# echo "192.168.0.106" > /home/wwwroot/106/index.html

      [root@localhost ~]# vim /etc/httpd/conf/httpd.conf   (大约在113行处添加)     

        <VirtualHost 192.168.0.104>
          DocumentRoot "/home/wwwroot/104"
          ServerName www.linuxprobe.com
          <Directory "/home/wwwroot/104">
            AllowOverride None
              Require all granted
          </Directory>
        </VirtualHost>
        <VirtualHost 192.168.0.106>
          DocumentRoot "/home/wwwroot/106"
          ServerName tech.linuxprobe.com
          <Directory "/home/wwwroot/106">
            AllowOverride None
            Require all granted
          </Directory>
        </VirtualHost>
        <VirtualHost 192.168.0.105>
          DocumentRoot "/home/wwwroot/105"
          ServerName bbs.linuxprobe.com
          <Directory "/home/wwwroot/105">
            AllowOverride None
            Require all granted
          </Directory>
        </VirtualHost>

      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/104
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/104/*
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/105
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/105/*
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/106
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/106/*
      [root@localhost ~]# restorecon -Rv /home/wwwroot/

      [root@localhost ~]# firewall-config    (配置防火墙允许http服务)
      [root@localhost ~]# systemctl restart httpd
      完成!

    6:配置基于域名的虚拟主机功能 (在虚拟机上配置)

      [root@localhost ~]# vim /etc/hosts (添加)

        192.168.0.188 www.languang.com bbs.languang.com tech.languang.com

      [root@localhost ~]# ping tech.languang.com
      PING www.languang.com (192.168.0.188) 56(84) bytes of data.
      64 bytes from www.languang.com (192.168.0.188): icmp_seq=1 ttl=64 time=0.200 ms 

      [root@localhost ~]# mkdir -p /home/wwwroot/www
      [root@localhost ~]# mkdir -p /home/wwwroot/bbs
      [root@localhost ~]# mkdir -p /home/wwwroot/tech
      [root@localhost ~]# echo "www.languang.com" > /home/wwwroot/www/index.html
      [root@localhost ~]# echo "bbs.languang.com" > /home/wwwroot/bbs/index.html
      [root@localhost ~]# echo "tech.languang.com" > /home/wwwroot/tech/index.html
      [root@localhost ~]# ls -Zd /var/www/html/
      drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/www/*
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/bbs/*
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech
      [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/tech/*
      [root@localhost ~]# restorecon -Rv /home/wwwroot/
      restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
      restorecon reset /home/wwwroot/www context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
      restorecon reset /home/wwwroot/www/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
      restorecon reset /home/wwwroot/bbs context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
      restorecon reset /home/wwwroot/bbs/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
      restorecon reset /home/wwwroot/tech context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
      restorecon reset /home/wwwroot/tech/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

      [root@localhost ~]# vim /etc/httpd/conf/httpd.conf  

        <VirtualHost 192.168.0.188>
          DocumentRoot "/home/wwwroot/www"
          ServerName "www.languang.com"
          <Directory "/home/wwwroot/www">
            AllowOverride None
            Require all granted
          </Directory>
        </VirtualHost>
        <VirtualHost 192.168.0.188>
          DocumentRoot "/home/wwwroot/bbs"
          ServerName "bbs.languang.com"
          <Directory "/home/wwwroot/bbs">
            AllowOverride None
            Require all granted
          </Directory>
        </VirtualHost>
        <VirtualHost 192.168.0.188>
          DocumentRoot "/home/wwwroot/tech"
          ServerName "tech.languang.com"
          <Directory "/home/wwwroot/tech">
            AllowOverride None
            Require all granted
          </Directory>

        </VirtualHost>

      [root@localhost ~]# systemctl restart httpd

      完成!

    7:配置基于端口号的虚拟主机功能 (在虚拟机上配置)

    [root@localhost ~]# mkdir -p /home/virtualhost/6111
    [root@localhost ~]# echo "this is 6111" > /home/virtualhost/6111/index.html

    [root@localhost ~]# mkdir -p /home/virtualhost/6222
    [root@localhost ~]# echo "this is 6222" > /home/virtualhost/6222/index.html

    [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/virtualhost
    [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/virtualhost/6111
    [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/virtualhost/6111/*

    [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/virtualhost/6222
    [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/virtualhost/6222/*

    [root@localhost ~]# restorecon -Rv /home/virtualhost/

    [root@localhost ~]# vim /etc/httpd/conf/httpd.conf(在大概113行处添加)

    <VirtualHost 192.168.0.188:6111>
    DocumentRoot "/home/virtualhost/6111"
    ServerName "www.languang.com"
    <Directory /home/virtualhost/6111>
    AllowOverride None
    Require all granted
    </Directory>
    </VirtualHost>
    <VirtualHost 192.168.0.188:6222>
    DocumentRoot "/home/virtualhost/6222"
    ServerName "www.languang.com"
    <Directory /home/virtualhost/6222>
    AllowOverride None
    Require all granted
    </Directory>
    </VirtualHost>

    (在Listen 80 处添加)

    Listen 6111

    Listen 6222

    [root@localhost ~]# semanage port -a -t http_port_t -p tcp 6111
    [root@localhost ~]# semanage port -a -t http_port_t -p tcp 6222

    [root@localhost ~]# firewall-config(添加开放6111 6222 tcp端口,并放开http协议)

    [root@localhost ~]# systemctl start httpd
    [root@localhost ~]# systemctl enable httpd

    完成!

  • 相关阅读:
    cookies
    php文件上传
    pho文件和目录操作
    php 日期和时间
    json解析网站
    only_full_group_by的注意事项
    $.extend()、$.fn和$.fn.extend()
    select样式美化(简单实用)
    toArray(),toJson(),hidden([ ]),visible([ ])
    tp5 model 中的查询范围(scope)
  • 原文地址:https://www.cnblogs.com/languang9801/p/11064579.html
Copyright © 2011-2022 走看看