设为首页 收藏本站
查看: 833|回复: 0

[经验分享] 实战Apache安装配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-12 09:50:26 | 显示全部楼层 |阅读模式
实验环境:RHEL7.0   server1.example.com  172.25.254.1
实验内容:   1.Apache安装
                    2.Apache主配置文件

                    3.更改默认访问目录

                    4.更改默认端口

                    5.访问目录权限设置

                    6.基于用户的身份认证配置(加密网页)

                    7.更改默认访问页面

                    8.添加访问php,cgi等网页
                    9.虚拟主机

                    10.HTTPS自定义签名证书

                    11.网页重写                        

1.Apache安装
    1.1 安装apache软件包
[iyunv@server1 ~]# yum install httpd httpd-manual
    1.2启动apache服务
[iyunv@server1 ~]# systemctl start httpd;systemctl enable httpd   
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
    1.3查看监听端口
[iyunv@server1 ~]# netstat -antple|grep httpd      
tcp6       0      0 :::80                   :::*                    LISTEN      0          119746     1428/httpd  
[iyunv@server1 ~]# ss -antple |grep httpd
LISTEN     0      128                      :::80                      :::*      users:(("httpd",1433,4),("httpd",1432,4),("httpd",1431,4),("httpd",1430,4),("httpd",1429,4),("httpd",1428,4)) ino:119746 sk:ffff88003bfd6800 <->
2.Apache主配置文件:/etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"  #用于指定Apache的运行目录
Listen 80                # 监听端口
User apache              #运行apache程序的用户和组
Group apache
ServerAdmin root@localhost    #管理员邮箱
DocumentRoot "/var/www/html"  #网页文件的存放目录
<Directory "/var/www/html">   #<Directory>语句块自定义目录权限
  Require all granted
</Directory>
ErrorLog "logs/error_log"      #错误日志存放位置
AddDefaultCharset UTF-8        #默认支持的语言
IncludeOptional conf.d/*.conf  #加载其它配置文件
DirectoryIndex index.html      #默认主页名称
3.更改默认访问目录
   1)改安全上下文
[iyunv@server1 ~]# getenforce
Enforcing
[iyunv@server1 ~]# mkdir -p /www/html
[iyunv@server1 ~]# ls -ldZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[iyunv@server1 ~]# ls -ldZ /www/html/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www/html/
[iyunv@server1 ~]# semanage fcontext -a -t httpd_sys_content_t '/www/html(/.*)?'
[iyunv@server1 ~]# restorecon -FvvR /www/html/
restorecon reset /www/html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
[iyunv@server1 ~]# ls -ldZ /www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /www/html/
[iyunv@server1 ~]# systemctl restart httpd
    2) 改配置
[iyunv@server1 ~]# vim /etc/httpd/conf/httpd.conf
       #DocumentRoot "/var/www/html"
        DocumentRoot "/www/html"
        <Directory />
           Require all granted
        </Directory>
    3)测试
[iyunv@server1 ~]# vim /www/html/index.html
                                         hello,willis.

[iyunv@server1 ~]# systemctl restart httpd.service
wKiom1fT92bjpuhRAAAe2pxz7Dw965.jpg    


    4.更改默认端口
   1)查看可更改端口
[iyunv@server1 ~]# semanage port -l |grep http      # selinux标签
http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t              udp      3130
http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989
   2)配置
[iyunv@server1 ~]# vim /etc/httpd/conf/httpd.conf
       #Listen 12.34.56.78:80
        Listen 8080
  3)访问
[iyunv@server1 ~]# systemctl restart httpd.service
wKiom1fT-GHzddIMAAAhFREJkgI423.jpg


    5.访问目录权限
[iyunv@server1 ~]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/www/html"
<Directory />
    Require all granted
    order allow,deny          #读取顺序,后读取的覆盖前读取的
    Allow from all            #允许所有用户访问
    Deny from 172.25.254.2  #拒绝172.25.254.2访问
</Directory>
[iyunv@server1 ~]# systemctl restart httpd.service

测试:172.25.254.2主机访问172.25.254.1
wKioL1fT-fzR6INeAABn5-Stp1g579.jpg


    6.配置基于用户的身份验证(帐号密码访问网页)
    Apache无格式文件用户身份验证
    在此配置中,用户账户和密码存储在本地.htpasswd文件中。处于安全原因,该文件不能
保存在网站的DocumentRoot中,而应保存在Web服务器不提供服务的一些目录中。特殊
的htpasswd命令用于在.htpasswd文件中管理用户。
[iyunv@server1 ~]# vim /etc/httpd/conf/httpd.conf    #还原端口与目录配置
[iyunv@server1 ~]# cd /var/www/html/
[iyunv@server1 html]# mkdir admin
[iyunv@server1 html]# cd admin/
[iyunv@server1 admin]# vim index.html
                                        hello,boy.
[iyunv@server1 admin]# cd /etc/httpd/conf
[iyunv@server1 conf]# ls
httpd.conf  magic
[iyunv@server1 conf]# htpasswd -cm htpasswd admin     #用两个账户创建Apache密码文件
New password:
Re-type new password:
Adding password for user admin
[iyunv@server1 conf]# htpasswd -m htpasswd willis
New password:
Re-type new password:
htpasswd: password verification error
[iyunv@server1 conf]# htpasswd -cm htpasswd willis
New password:
Re-type new password:
Adding password for user willis
[iyunv@server1 conf]# ls
htpasswd  httpd.conf  magic
[iyunv@server1 conf]# cat htpasswd
admin:$apr1$rEBIJilB$qGzEG6c4NYvOxg2qZLSfk/
willis:$apr1$qFoQCa7F$5IZqbqG5d5hVclspCl6R/0
[iyunv@server1 conf]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html"
<Directory "/var/www/html/admin">
     AuthUserfile  /etc/httpd/conf/htpasswd
     AuthName   "Please input your user name and password "
     AuthType basic
     Require valid-user
    # Require user admin
</Directory>
[iyunv@server1 conf]# systemctl restart httpd.service   
测试:
wKioL1fUDsbjj6xgAACayFuSjpc369.jpg

wKiom1fUDsah3kXcAAAkDZrLPwU470.jpg



    7.更改默认访问页面
[iyunv@server1 ~]# cd /var/www/html/
[iyunv@server1 html]# ls
admin  index.html
[iyunv@server1 html]# vim  test
This is a test page.
[iyunv@server1 html]# vim /etc/httpd/conf/httpd.conf
<IfModule dir_module>
   # DirectoryIndex index.html
     DirectoryIndex  test   index.html       #先访问test,
</IfModule>
[iyunv@server1 html]# systemctl restart httpd.service

测试:
wKioL1fUECOh_h1RAAAZ0pLWSLw291.jpg


    8.可访问php,cgi等网页
1) 访问 .php  
[iyunv@server1 html]# yum install php -y    ##安装php软件包,其中包含mod_php模块:
[iyunv@server1 html]# pwd
/var/www/html
[iyunv@server1 html]# vim index.php       #写php测试页
<?php
phpinfo();
?>
[iyunv@server1 html]# systemctl restart httpd.service
测试:

wKioL1fUEpORClbSAADt35ypsCQ584.jpg

2)可访问 .cgi
    通用网关接口(CGI)是网站上放置动态内容的最简单的方法。CGI脚本可用于许多目的,但是谨慎控制使用哪个CGI脚本以及允许谁添加和运行这些脚本十分重要。编写质量差的CGI脚本可能为外部攻击者提供了破坏网站及其内容安全性的途径。因此,在Web服务器级别和SELinux策略级别,都存在用于限制CGI脚本使用的设置。
[iyunv@server1 html]# pwd
/var/www/html
[iyunv@server1 html]# mkdir scripts
[iyunv@server1 html]# cd scripts/
[iyunv@server1 scripts]# vim index.cgi    #测试内容,可从文档中拷贝
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[iyunv@server1 scripts]# chmod +x index.cgi     #权限
[iyunv@server1 scripts]# setenforce 0     #安全上下文   
或者
     semanage fcontext -l | grep httpd
     /var/www/perl(/.*)?         all files   system_u:object_r:httpd_sys_script_exec_t:s0
     semanage fcontext -a -t  httpd_sys_script_exec_t '/var/www/html/scripts(/.*)?'
     restorecon  -FvvR   /var/www/html/scripts
[iyunv@server1 scripts]# vim /etc/httpd/conf/httpd.conf     #配置内容,可从文档中拷贝
  DocumentRoot "/var/www/html"
  <Directory "/var/www/html/scripts">
    Options +ExecCGI
    AddHandler cgi-script .cgi
  </Directory>
[iyunv@server1 scripts]# systemctl restart httpd.service
测试:http://172.25.254.1/scripts/index.cgi  

wKioL1fUFlejom9pAAAsW4rXzpI629.jpg


wKiom1fUFlfQ7JKQAAAsaZtr8sU566.jpg



    9.虚拟主机

    虚拟主机允许您从一个httpd服务器同时为多个网站提供服务。在本节中,我们将了解基于名称的虚拟主机其中多个主机名都指向同一个IP地址,但是Web服务器根据用于到达站点的主机名提供具有不同内容的不同网站。
[iyunv@server1 scripts]# vim /etc/hosts   #浏览器访问地址的主机修改DNS配置文件
  172.25.254.1   www.qq.com
  172.25.254.1   news.qq.com
  172.25.254.1   sport.qq.com
[iyunv@server1 scripts]# cd /var/www/
[iyunv@server1 www]# mkdir virtual/news/html -p
[iyunv@server1 www]# mkdir virtual/sport/html -p
[iyunv@server1 www]# echo new\'s page > virtual/news/html/index.html
[iyunv@server1 www]# echo sport\'s page > virtual/sport/html/index.html

[iyunv@server1 www]# vim /etc/httpd/conf.d/default.conf                                 

      <Virtualhost _default_:80>      #定义默认虚拟主机的块
        Documentroot "/var/www/html"  #在<VirtualHost>块内部,指定从中提供内容的目录。
        Customlog "logs/default.log" combined
      </Virtualhost>
[iyunv@server1 www]# vim  /etc/httpd/conf.d/news.conf
      <Virtualhost *:80>           #定义虚拟主机的块
        Servername  news.qq.com    #指定服务器名称。在使用基于名称的虚拟主机的情况下,此处的名称必须与客户端请求完全的匹配。。
        Documentroot "/var/www/virtual/news/html"   #指定从中提供内容的目录。
        Customlog "logs/news.log" combined
      </Virtualhost>
      <Directory "/var/www/virtual/news/html">     
        Require  all granted         #授权
      </Directory>
[iyunv@server1 www]# vim  /etc/httpd/conf.d/sport.conf
      <Virtualhost *:80>
        Servername  sport.qq.com
        Documentroot "/var/www/virtual/sport/html"
        Customlog "logs/sport.log" combined
      </Virtualhost>
      <Directory "/var/www/virtual/sport/html">
        Require  all granted        #授权
      </Directory>

测试访问:
wKiom1fUGoPzx3NEAAAdsEJgoYk287.jpg

wKiom1fUGoTx7xbUAAAbRRbLl4Y626.jpg


wKioL1fUGoSiA2pUAAAZfNu9SF0178.jpg



    10.HTTPS自定义自签名证书
    如果加密的通信非常重要,而经过验证的身份不重要,管理员可以通过生成self-signed certificate来避免与认证机构进行交互所带来的复杂性。
    使用genkey实用程序(通过crypto-utils软件包分发),生成自签名证书及其关联的私钥。为了简化起见,genkey将在“正确”的位置(/etc/pki/tls目录)创建证书及其关联的密钥。相应地,必须以授权用户(root)身份运行该实用程序。
[iyunv@server1 www]# yum install crypto-utils mod_ssl -y   #生成自签名证书crypto-utils软件包
[iyunv@server1 www]# genkey Apache.example.com    #调用genkey,同时为生成的文件指定唯一名称
      1)记录生成的证书(Apach.example.com .crt)和关联的私钥(Apach.example.com .key)的位置
      2) 继续使用对话框,并选择合适的密钥大小。(默认的2048位密钥为推荐值)
      3) 在生成随机数时比较慢,敲键盘和移动鼠标可以加速
      4) 拒绝向认证机构(CA)发送证书请求(CSR)
      5) 拒绝加密私钥
      6) 为服务器提供合适的身份。Common Name必须与服务器的主机全名完全匹配。
wKiom1fUHNSSQYr9AABdQhYZ-tE860.jpg wKioL1fUHNPA7YrTAABxLbc5DmE475.jpg
wKiom1fUHNLBgPzsAAAU-wsX_yM830.jpg
wKioL1fUHNLg1c_QAAA7i7RRpQ0050.jpg
wKiom1fUHNGyGJxPAAAlV5DjfEQ981.jpg
wKioL1fUHNCAnlh4AAAQWh_qutQ743.jpg
wKiom1fUHT2j8hpNAACMXsRm56s795.jpg

[iyunv@server1 www]# vim /etc/httpd/conf.d/ssl.conf  
SSLCertificateFile /etc/pki/tls/certs/Apache.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/Apache.example.com.key
[iyunv@server1 www]# systemctl restart httpd.service
    如要进行确认,请使用https协议(https://serverX.example.com)通过Web客户端(如Firefox)访问Web服务器。
    Web客户端可能会发出它不认可证书发行者的警告。这种情况适用自签名证书。要求Web客户端绕过证书认证。(对于Firefox,请选择“I Understand the Risks” [我了解风险]、“Add Exception” [添加例外]和“Confirm Security Exception”[确认安全例外]。)
    测试:


wKioL1fUIEfDWhxJAACEUy7XAes613.jpg

wKiom1fUIEaRteTUAADGOyXAgOs793.jpg
wKiom1fUIEXBv0bJAAARTvqz37Q605.jpg


    11.网页重写
[iyunv@server1 www]# vim /etc/hosts    #浏览器访问地址的主机修改DNS配置文件
  172.25.254.1   login.qq.com
[iyunv@server1 www]# pwd
/var/www
[iyunv@server1 www]# mkdir virtual/login/html -p
[iyunv@server1 www]# echo login\'s page > virtual/login/html/index.html
[iyunv@server1 www]# vim /etc/httpd/conf.d/login.conf
      <Virtualhost *:443>
        Servername  login.qq.com
        Documentroot "/var/www/virtual/login/html"
        Customlog "logs/login.log" combined
        SSLEngine  on
        SSLCertificateFile /etc/pki/tls/certs/Apach.example.com.crt
        SSLCertificateKeyFile /etc/pki/tls/private/Apach.example.com.key
      </Virtualhost>
      <Directory "/var/www/virtual/login/html">
        Require  all granted        #授权
      </Directory>
      <Virtualhost *:80>
        ServerName login.qq.com
        RewriteEngine on
        RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>

测试:
wKioL1fUJhCRw6pCAAAU8p4w4o0114.jpg



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-271089-1-1.html 上篇帖子: Debian 8 apache2 下篇帖子: Apache环境中重定向301的配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表