实战Apache安装配置

来源:互联网 发布:linux查看电源功率 编辑:程序博客网 时间:2024/05/17 11:37

原文地址:http://willis.blog.51cto.com/11907152/1851477


实验环境: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软件包

[root@server1 ~]# yum install httpd httpd-manual 

    1.2启动apache服务

[root@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查看监听端口

[root@server1 ~]# netstat -antple|grep httpd       

tcp6       0      0 :::80                   :::*                    LISTEN      0          119746     1428/httpd  

[root@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)改安全上下文

[root@server1 ~]# getenforce 

Enforcing

[root@server1 ~]# mkdir -p /www/html

[root@server1 ~]# ls -ldZ /var/www/html/

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

[root@server1 ~]# ls -ldZ /www/html/

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www/html/

[root@server1 ~]# semanage fcontext -a -t httpd_sys_content_t '/www/html(/.*)?'

[root@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

[root@server1 ~]# ls -ldZ /www/html/

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /www/html/

[root@server1 ~]# systemctl restart httpd

    2) 改配置

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

       #DocumentRoot "/var/www/html"

        DocumentRoot "/www/html"

        <Directory />

           Require all granted

        </Directory>

    3)测试

[root@server1 ~]# vim /www/html/index.html

                                         hello,willis.

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

wKiom1fT92bjpuhRAAAe2pxz7Dw965.png   



    4.更改默认端口

   1)查看可更改端口

[root@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)配置

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

       #Listen 12.34.56.78:80

        Listen 8080

  3)访问

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

wKiom1fT-GHzddIMAAAhFREJkgI423.png



    5.访问目录权限

[root@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>

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


测试:172.25.254.2主机访问172.25.254.1

wKioL1fT-fzR6INeAABn5-Stp1g579.png



    6.配置基于用户的身份验证(帐号密码访问网页)

    Apache无格式文件用户身份验证

    在此配置中,用户账户和密码存储在本地.htpasswd文件中。处于安全原因,该文件不能

保存在网站的DocumentRoot中,而应保存在Web服务器不提供服务的一些目录中。特殊

的htpasswd命令用于在.htpasswd文件中管理用户。

[root@server1 ~]# vim /etc/httpd/conf/httpd.conf    #还原端口与目录配置

[root@server1 ~]# cd /var/www/html/

[root@server1 html]# mkdir admin

[root@server1 html]# cd admin/

[root@server1 admin]# vim index.html

                                        hello,boy.

[root@server1 admin]# cd /etc/httpd/conf

[root@server1 conf]# ls

httpd.conf  magic

[root@server1 conf]# htpasswd -cm htpasswd admin     #用两个账户创建Apache密码文件

New password: 

Re-type new password: 

Adding password for user admin

[root@server1 conf]# htpasswd -m htpasswd willis

New password: 

Re-type new password: 

htpasswd: password verification error

[root@server1 conf]# htpasswd -cm htpasswd willis

New password: 

Re-type new password: 

Adding password for user willis

[root@server1 conf]# ls

htpasswd  httpd.conf  magic

[root@server1 conf]# cat htpasswd 

admin:$apr1$rEBIJilB$qGzEG6c4NYvOxg2qZLSfk/

willis:$apr1$qFoQCa7F$5IZqbqG5d5hVclspCl6R/0

[root@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>

[root@server1 conf]# systemctl restart httpd.service   

测试:

wKioL1fUDsbjj6xgAACayFuSjpc369.png


wKiom1fUDsah3kXcAAAkDZrLPwU470.png



    7.更改默认访问页面

[root@server1 ~]# cd /var/www/html/

[root@server1 html]# ls

admin  index.html

[root@server1 html]# vim  test

This is a test page.

[root@server1 html]# vim /etc/httpd/conf/httpd.conf 

<IfModule dir_module>

   # DirectoryIndex index.html

     DirectoryIndex  test   index.html       #先访问test,

</IfModule>

[root@server1 html]# systemctl restart httpd.service

测试:

wKioL1fUECOh_h1RAAAZ0pLWSLw291.png



    8.可访问php,cgi等网页

1) 访问 .php  

[root@server1 html]# yum install php -y    ##安装php软件包,其中包含mod_php模块:

[root@server1 html]# pwd

/var/www/html

[root@server1 html]# vim index.php       #写php测试页

<?php

phpinfo();

?>

[root@server1 html]# systemctl restart httpd.service 

测试:

wKioL1fUEpORClbSAADt35ypsCQ584.png


2)可访问 .cgi

    通用网关接口(CGI)是网站上放置动态内容的最简单的方法。CGI脚本可用于许多目的,但是谨慎控制使用哪个CGI脚本以及允许谁添加和运行这些脚本十分重要。编写质量差的CGI脚本可能为外部攻击者提供了破坏网站及其内容安全性的途径。因此,在Web服务器级别和SELinux策略级别,都存在用于限制CGI脚本使用的设置。

[root@server1 html]# pwd

/var/www/html

[root@server1 html]# mkdir scripts

[root@server1 html]# cd scripts/

[root@server1 scripts]# vim index.cgi    #测试内容,可从文档中拷贝

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print `date`;

[root@server1 scripts]# chmod +x index.cgi     #权限

[root@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

[root@server1 scripts]# vim /etc/httpd/conf/httpd.conf     #配置内容,可从文档中拷贝

  DocumentRoot "/var/www/html"

  <Directory "/var/www/html/scripts">

    Options +ExecCGI

    AddHandler cgi-script .cgi

  </Directory>

[root@server1 scripts]# systemctl restart httpd.service

测试:http://172.25.254.1/scripts/index.cgi  


wKioL1fUFlejom9pAAAsW4rXzpI629.png


wKiom1fUFlfQ7JKQAAAsaZtr8sU566.png


  

    9.虚拟主机

    虚拟主机允许您从一个httpd服务器同时为多个网站提供服务。在本节中,我们将了解基于名称的虚拟主机其中多个主机名都指向同一个IP地址,但是Web服务器根据用于到达站点的主机名提供具有不同内容的不同网站。

[root@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

[root@server1 scripts]# cd /var/www/

[root@server1 www]# mkdir virtual/news/html -p

[root@server1 www]# mkdir virtual/sport/html -p

[root@server1 www]# echo new\'s page > virtual/news/html/index.html

[root@server1 www]# echo sport\'s page > virtual/sport/html/index.html


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

      <Virtualhost _default_:80>      #定义默认虚拟主机的块

        Documentroot "/var/www/html"  #在<VirtualHost>块内部,指定从中提供内容的目录。

        Customlog "logs/default.log" combined

      </Virtualhost>

[root@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>

[root@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.png


wKiom1fUGoTx7xbUAAAbRRbLl4Y626.png


wKioL1fUGoSiA2pUAAAZfNu9SF0178.png



    10.HTTPS自定义自签名证书

    如果加密的通信非常重要,而经过验证的身份不重要,管理员可以通过生成self-signed certificate来避免与认证机构进行交互所带来的复杂性。

    使用genkey实用程序(通过crypto-utils软件包分发),生成自签名证书及其关联的私钥。为了简化起见,genkey将在“正确”的位置(/etc/pki/tls目录)创建证书及其关联的密钥。相应地,必须以授权用户(root)身份运行该实用程序。

[root@server1 www]# yum install crypto-utils mod_ssl -y   #生成自签名证书crypto-utils软件包

[root@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.pngwKioL1fUHNPA7YrTAABxLbc5DmE475.png

wKiom1fUHNLBgPzsAAAU-wsX_yM830.png

wKioL1fUHNLg1c_QAAA7i7RRpQ0050.png

wKiom1fUHNGyGJxPAAAlV5DjfEQ981.png

wKioL1fUHNCAnlh4AAAQWh_qutQ743.png

wKiom1fUHT2j8hpNAACMXsRm56s795.png


[root@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

[root@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.png


wKiom1fUIEaRteTUAADGOyXAgOs793.png

wKiom1fUIEXBv0bJAAARTvqz37Q605.png



    11.网页重写

[root@server1 www]# vim /etc/hosts    #浏览器访问地址的主机修改DNS配置文件

  172.25.254.1   login.qq.com

[root@server1 www]# pwd

/var/www

[root@server1 www]# mkdir virtual/login/html -p

[root@server1 www]# echo login\'s page > virtual/login/html/index.html

[root@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.png


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 做完眼线后眼睛肿了怎么办 洗眼线眼睛肿了怎么办 眼线留眼泪总是爱花妆怎么办 戴眼镜眼球外凸怎么办 修眉后的眉渣怎么办 内眼线容易晕妆怎么办 手抖不会画眼线怎么办 画眼线眼睛总眨怎么办 画眼线总是晕妆怎么办 眼线笔没用完干了怎么办 新的眼线笔干了怎么办 不涂口红没气色怎么办 眼线笔容易晕妆怎么办 眼线笔老是晕妆怎么办 眼线液老是晕妆怎么办 旋转眼线笔断了怎么办 眼睛去皮以后眉眼距窄怎么办 速写型总是画不准怎么办 速写人物不会打形怎么办 鼻头又圆又大怎么办 耳鸣嘴溃疡眼流泪上火怎么办 孩子看电视总挤眼睛怎么办 小孩老是咳嗽有痰怎么办 长时间看手机眼睛模糊怎么办 长时间看电脑眼睛模糊怎么办 手机玩多了眼睛模糊怎么办 手机看多了眼睛模糊怎么办 孩子玩手机眼睛红怎么办 手机玩多了眼睛红怎么办 手机看久了眼花怎么办 玩手机眼睛近视了怎么办 近视了怎么办30个字 吃了长牙的土豆怎么办 鸡蛋和土豆吃了怎么办 狗狗眼睛流血水怎么办 石粉粘土干了怎么办 樱花针管笔干了怎么办 想学linux不会c语言怎么办 被摩托车排气管烫伤了怎么办 泡泡糖粘在衣服上怎么办 皮卡书屋办卡怎么办