765435 发表于 2016-9-14 08:58:47

apache服务的配置

apache(web服务器)
Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。

一、实验环境:RHEL7.0172.25.254.10 server1.example.comfirewalld disabled
二、实验内容:
1.apache的安装:
yum install -t httpd httpd-manual
systemctl start httpd启动apache服务
systemctl enable httpd自启动
查看端口:


2.apache的主配置文件:
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.更改apache默认访问目录:
更改安全上下文,或者把selinux设为disabled都可以。

# 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
修改主配置文件:将默认访问目录改成自己想要的访问目录
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/www/html"
<Directory "/">
    AllowOverride None
    Require all granted
</Directory>
echo hello > /www/html/index.html      给默认发布目录写个首页
systemctl restart httpd      重启服务
测试:



4.更改默认端口:
查看可更改的端口:      ##selinux标签


修改主配置文件更改端口:
vim /etc/httpd/conf/httpd.conf
Listen 80 改成 8080
systemctl restart httpd.service
测试:


5.访问目录权限:
vim /etc/httpd/conf/httpd.conf
    DocumentRoot "/www/html"
    <Directory "/">
      AllowOverride None
      Require all granted
      order allow,deny      ##读取按顺序,先读取前面的
      Allow from all             ##允许所有用户访问
      Deny from 172.25.254.20   ##禁止172.25.254.20访问
    </Directory>
systemctl restart httpd.service
测试:
用172.25.254.20主机进行测试:访问被拒绝


用其他主机测试,可以测试成功。
6.配置基于用户的身份验证:
Apache无格式文件用户身份验证
在此配置中,用户账户和密码存储在本地.htpasswd文件中。处于安全原因,该文件不能保存在网站的DocumentRoot中,而应保存在Web服务器不提供服务的一些目录中。特殊的htpasswd命令用在.htpasswd文件中管理用户。

vim /etc/httpd/conf/httpd.conf      还原默认访问目录和端口
# cd /var/www/html/
# mkdir admin
# cd admin/
# vim index.html
      my name is xiaoze.
# cd /etc/httpd/conf
# ls
httpd.confmagic
# htpasswd -cm htpasswd admin      用两个账户创建Apache密码文件,(第一次创建密码文件是需要有“c“参数,m表示使用md5加密)
New password:
Re-type new password:
Updating password for user admin
# htpasswd -m htpasswd xiaoze
New password:
Re-type new password:
Adding password for user xiaoze
# cat htpasswd
admin:$apr1$NmEXjM8K$H9QaQ4IBXsbjjrgqsvoI6/
xiaoze:$apr1$oiBEyWhW$CEe9Js1Gh5pu1TQ3/J.l5.
# ls
htpasswdhttpd.confmagic
# vim /etc/httpd/conf/httpd.conf


# systemctl restart httpd.service
测试:



7.更改默认访问页面:


vim /etc/httpd/conf/httpd.conf
    <IfModule dir_module>
         # DirectoryIndex index.html
            DirectoryIndex test index.html
    </IfModule>
测试:


8.php语言支持,可访问cgi等网页:
php语言支持:
yum install -y php (安装php软件包,其中包含mod_php模块)
# cat index.php   写php的测试网页
<?php
phpinfo();
?>
systemctl restart httpd.service
测试:

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



9.虚拟主机:
虚拟主机允许您从一个httpd服务器同时为多个网站提供服务。在本节中,我们将了解基于名称的虚
拟主机其中多个主机名都指向同一个IP地址,但是Web服务器根据用于到达站点的主机名提供具有不
同内容的不同网站。
vim /etc/hosts                将虚拟主机地址做解析
172.25.254.10 news.qq.com
172.25.254.10 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>            #定义虚拟主机的块      

      Servername news.qq.com      #指定服务器名称。在使用基于名称的虚拟主机的情况下,此处的名称必须与客户端请求完全的匹配。      

      DocumentRoot "/var/www/virtual/news/html"      #指定从中提供内容的目录。      

      Customlog "logs/neww.log" combined
</Virtualhost>
<Directory "/var/www/virtual/news/html">      
      Require all 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>
systemctl restart httpd.service
测试:



10.HTTPS自定义签名证书:
如果加密的通信非常重要,而经过验证的身份不重要,管理员可以通过生成self-signed certificate来避免与认证机构进行交互所带来的复杂性。使用genkey实用程序(通过crypto-utils软件包分发),生成自签名证书及其关联的私钥。为了简化起见,genkey将在“正确”的位置(/etc/pki/tls目录)创建证书及其关联的密钥。相应地,必须以授权用户(root)身份运行该实用程序。
生成自签名证书:
确保已安装crypto-utils软件包。
# yum install crypto-utils mod_ssl
调用genkey,同时为生成的文件指定唯一名称(例如,服务器的主机全名)。
--days可以指定证书有效期
# genkey server1.example.com
      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/server1.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/server1.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      添加解析
172.25.254.10 login.qq.com

cd /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/server1.example.com.crt
      SSLCertificateKeyFile /etc/pki/tls/private/server1.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>
systemctl restart httpd.service
测试:

页: [1]
查看完整版本: apache服务的配置