56341 发表于 2016-9-12 09:50:26

实战Apache安装配置

实验环境:RHEL7.0   server1.example.com172.25.254.1
实验内容:   1.Apache安装
                  2.Apache主配置文件

                  3.更改默认访问目录

                  4.更改默认端口

                  5.访问目录权限设置

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

                  7.更改默认访问页面

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

                  10.HTTPS自定义签名证书

                  11.网页重写                        

1.Apache安装
    1.1 安装apache软件包
# yum install httpd httpd-manual
    1.2启动apache服务
# 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查看监听端口
# netstat -antple|grep httpd      
tcp6       0      0 :::80                   :::*                  LISTEN      0          119746   1428/httpd
# 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)改安全上下文
# getenforce
Enforcing
# mkdir -p /www/html
# ls -ldZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
# ls -ldZ /www/html/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www/html/
# semanage fcontext -a -t httpd_sys_content_t '/www/html(/.*)?'
# 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
# ls -ldZ /www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /www/html/
# systemctl restart httpd
    2) 改配置
# vim /etc/httpd/conf/httpd.conf
       #DocumentRoot "/var/www/html"
      DocumentRoot "/www/html"
      <Directory />
         Require all granted
      </Directory>
    3)测试
# vim /www/html/index.html
                                       hello,willis.

# systemctl restart httpd.service
   
    4.更改默认端口
   1)查看可更改端口
# 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)配置
# vim /etc/httpd/conf/httpd.conf
       #Listen 12.34.56.78:80
      Listen 8080
3)访问
# systemctl restart httpd.service

    5.访问目录权限
# 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>
# systemctl restart httpd.service

测试:172.25.254.2主机访问172.25.254.1


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




    7.更改默认访问页面
# cd /var/www/html/
# ls
adminindex.html
# vimtest
This is a test page.
# vim /etc/httpd/conf/httpd.conf
<IfModule dir_module>
   # DirectoryIndex index.html
   DirectoryIndextest   index.html       #先访问test,
</IfModule>
# systemctl restart httpd.service

测试:


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


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





    9.虚拟主机

    虚拟主机允许您从一个httpd服务器同时为多个网站提供服务。在本节中,我们将了解基于名称的虚拟主机其中多个主机名都指向同一个IP地址,但是Web服务器根据用于到达站点的主机名提供具有不同内容的不同网站。
# 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
# cd /var/www/
# mkdir virtual/news/html -p
# mkdir virtual/sport/html -p
# echo new\'s page > virtual/news/html/index.html
# echo sport\'s page > virtual/sport/html/index.html

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

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






    10.HTTPS自定义自签名证书
    如果加密的通信非常重要,而经过验证的身份不重要,管理员可以通过生成self-signed certificate来避免与认证机构进行交互所带来的复杂性。
    使用genkey实用程序(通过crypto-utils软件包分发),生成自签名证书及其关联的私钥。为了简化起见,genkey将在“正确”的位置(/etc/pki/tls目录)创建证书及其关联的密钥。相应地,必须以授权用户(root)身份运行该实用程序。
# yum install crypto-utils mod_ssl -y   #生成自签名证书crypto-utils软件包
# genkey Apache.example.com    #调用genkey,同时为生成的文件指定唯一名称
      1)记录生成的证书(Apach.example.com .crt)和关联的私钥(Apach.example.com .key)的位置      2) 继续使用对话框,并选择合适的密钥大小。(默认的2048位密钥为推荐值)      3) 在生成随机数时比较慢,敲键盘和移动鼠标可以加速      4) 拒绝向认证机构(CA)发送证书请求(CSR)      5) 拒绝加密私钥      6) 为服务器提供合适的身份。Common Name必须与服务器的主机全名完全匹配。
# 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
# systemctl restart httpd.service
    如要进行确认,请使用https协议(https://serverX.example.com)通过Web客户端(如Firefox)访问Web服务器。
    Web客户端可能会发出它不认可证书发行者的警告。这种情况适用自签名证书。要求Web客户端绕过证书认证。(对于Firefox,请选择“I Understand the Risks” [我了解风险]、“Add Exception” [添加例外]和“Confirm Security Exception”[确认安全例外]。)
    测试:






    11.网页重写
# vim /etc/hosts    #浏览器访问地址的主机修改DNS配置文件
172.25.254.1   login.qq.com
# pwd
/var/www
# mkdir virtual/login/html -p
# echo login\'s page > virtual/login/html/index.html
# vim /etc/httpd/conf.d/login.conf
      <Virtualhost *:443>
      Servernamelogin.qq.com
      Documentroot "/var/www/virtual/login/html"
      Customlog "logs/login.log" combined
      SSLEngineon
      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">
      Requireall granted      #授权
      </Directory>
      <Virtualhost *:80>
      ServerName login.qq.com
      RewriteEngine on
      RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</Virtualhost>

测试:



页: [1]
查看完整版本: 实战Apache安装配置