Linux-基于RHEL的Apache搭建与配置

Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。

实验环境

[root@desktop ~]# hostnamectl
   Static hostname: desktop
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 946cb0e817ea4adb916183df8c4fc817
           Boot ID: e757ea01a42a45918fb9c395ab7dadf9
    Virtualization: kvm
  Operating System: Red Hat Enterprise Linux Server 7.0 (Maipo)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:7.0:GA:server
            Kernel: Linux 3.10.0-123.el7.x86_64
      Architecture: x86_64

安装Apache

[root@desktop ~]# yum install httpd -y	        ##安装Apache
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-17.el7 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-17.el7 for package: httpd-2.4.6-17.el7.x86_64
………………………………………………………………………………………………………………………………………………………………………………………………………………
Installed:
  httpd.x86_64 0:2.4.6-17.el7                                                   

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7                 apr-util.x86_64 0:1.5.2-6.el7       
  httpd-tools.x86_64 0:2.4.6-17.el7        mailcap.noarch 0:2.1.41-2.el7       

Complete!

[root@desktop ~]# systemctl start httpd		##打开服务
[root@desktop ~]# systemctl enable httpd	##设置开机启动
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

[root@desktop ~]# systemctl stop firewalld	##关闭防火墙
[root@desktop ~]# systemctl disable firewalld	##禁止防火墙开机启动
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
####因为是实验环境,所以直接关闭防火墙,生产环境可以对防火墙进行相关配置

安装配置完成后,客户端使用浏览器进行访问,测试服务是否安装成功,可以打开Apache默认测试页面为安装成功。


Apache基本信息

默认发布文件:
index.html
默认发布目录:
/var/www/html
默认端口:
80
配置文件存放位置:
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
Apache手册(需要单独安装):
httpd-manual    ##安装完成后通过浏览器打开

Apache的基本配置

[root@desktop ~]# vim /etc/httpd/conf/httpd.conf     ##编辑配置文件

##运行目录,服务启动后所有的相对路径都是相对这个路径下
31 ServerRoot "/etc/httpd"

##监听端口,Apache服务使用的端口
42 Listen 80

##服务启动后,转换的身份
66 User apache
67 Group apache

##默认发布目录位置
119 DocumentRoot "/var/www/html"

##默认发布文件指定
163 <IfModule dir_module>
164     DirectoryIndex index.html
165 </IfModule>

##针对目录的权限设置
131 <Directory "/var/www/html">
144     Options Indexes FollowSymLinks    ##表示如果目录中没有默认发布文件,允许访问目录中的其他文件()
151     AllowOverride None                ##AllowOverride为空,不允许覆盖此配置
156     Require all granted               ##允许所有请求访问
157 </Directory>
##目录权限其他参数
        ##访问控制
        Order Allow,Deny                  ##黑白名单读取顺序,后者覆盖前者
        Allow from All                    ##白名单,所有人
        Deny from 172.25.254.250          ##黑名单,禁止此IP访问
        ##用户的访问控制
[root@desktop html]# htpasswd -cm /etc/httpd/accessuser admin    ##创建用户列表文件,-c:新建加密文件,文件以存在添加此参数将覆盖源文件
                                                                 ##-m:默认采用MD5算法进行加密
New password: 
Re-type new password: 
Adding password for user admin
[root@desktop html]# htpasswd -m /etc/httpd/accessuser jinx
New password: 
Re-type new password: 
Adding password for user jinx
[root@desktop html]# cat /etc/httpd/accessuser            ##查看加密文件
admin:$apr1$QjWPB3/y$YX3odrG9lGJ2X7Ei/yfQ.1
jinx:$apr1$pYH03YWM$kuC0/8qN.rZ99sLBGz4u8.
        AuthUserFile /etc/httpd/accessuser                ##指定用户认证文件
        AuthName "Please input your name and password !!"    ##用户认证提示信息
        AuthType basic                        ##认证类型
        Require valid-user                    ##认证用户,认证文件中所有用户都可以通过
        #Require user admin                   ##只允许认证文件中admin用户访问

Apache语言支持

HTML:默认支持
Apache默认语言

PHP:需要安装php
[root@desktop html]# yum install php -y

CGI语言:默认支持未开启
    Options +ExecCGI                    ##配置,添加支持CGI语言
    AddHandler cgi-script .cgi          ##添加支持的CGI语言的文件

Apache虚拟主机

可以让Apache主机在被访问不同域名时,显示不同的主页

[root@desktop html]# mkdir php            ##创建php文件夹
[root@desktop html]# mkdir cgi            ##创建cgi文件夹

[root@desktop html]# echo Hello,World! > index.html    ##创建默认发布目录页面

[root@desktop html]# vim php/index.php    ##创建php测试文件
##PHP测试页
<?php
        phpinfo();
?>

[root@desktop html]# vim cgi/index.cgi    ##创建cgi测试文件
##CGI测试页
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;

[root@desktop html]# vim /etc/httpd/conf.d/vhost.conf    ##配置虚拟主机
<Virtualhost    _default_:80>                            ##默认80端口配置
        DocumentRoot "/var/www/html"                     ##发布目录
        CustomLog "logs/default.log" combined            ##日志文件及内容
</Virtualhost>
###php.jinx.com###
<Virtualhost    *:80>
        ServerName "php.jinx.com"                        ##访问域名为php.jinx.com时
        DocumentRoot "/var/www/html/php"                 ##发布目录设置
        CustomLog "logs/php.log" combined                ##日志文件及内容
</Virtualhost>
###cgi.jinx.com###
<Virtualhost    *:80>
        ServerName "cgi.jinx.com"                        ##访问域名为cgi.jinx.com时
        DocumentRoot "/var/www/html/cgi"                 ##发表目录设置
        CustomLog "logs/cgi.log" combined                ##日志文件及内容
</Virtualhost>

<Directory "/var/www/html/cgi">                          ##配置CGI语言页面目录
        Options +ExecCGI                                 ##设置支持CGI
        AddHandler cgi-script .cgi                       ##添加支持文件类型
        DirectoryIndex index.cgi                         ##设置默认发布文件
</Directory>

###访问端配置域名解析
[kiosk@foundation80 ~]$  vim /etc/hosts        
172.25.254.143 www.jinx.com php.jinx.com cgi.jinx.com login.jinx.com

通过浏览器,访问不同域名,打开同一服务器的相应页面


页面加密https

HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。

启用https需要安装:mod_ssl(ssl模块)、crypto-utils(加密工具)
[root@desktop ~]# yum install mod_ssl -y            ##安装ssl模块
[root@desktop ~]# yum install crypto-utils -y       ##安装加密工具

####创建login.jinx.com页面文件####
[root@desktop ~]# mkdir /var/www/html/login
[root@desktop ~]# echo "Welcome to Login's page" >> /var/www/html/login/index.html

####使用加密工具加密网页,生成key和证书####
[root@desktop ~]# genkey www.jinx.com               ##加密网页
/usr/bin/keyutil -c makecert -g 1024 -s "CN=www.jinx.com, OU=Linux, O=Jinx, L=Xi'an, ST=ShaanXi, C=CN" -v 1 -a -z /etc/pki/tls/.rand.1802 -o /etc/pki/tls/certs/www.jinx.com.crt -k /etc/pki/tls/private/www.jinx.com.key
cmdstr: makecert

cmd_CreateNewCert
command:  makecert
keysize = 1024 bits
subject = CN=www.jinx.com, OU=Linux, O=Jinx, L=Xi'an, ST=ShaanXi, C=CN
valid for 1 months
random seed from /etc/pki/tls/.rand.1802
output will be written to /etc/pki/tls/certs/www.jinx.com.crt    ##证书路径
output key written to /etc/pki/tls/private/www.jinx.com.key      ##key路径


Generating key. This may take a few moments...

Made a key
Opened tmprequest for writing
/usr/bin/keyutil Copying the cert pointer
Created a certificate
Wrote 882 bytes of encoded data to /etc/pki/tls/private/www.jinx.com.key 
Wrote the key to:
/etc/pki/tls/private/www.jinx.com.key

####编辑配置文件,添加https相关配置####
[root@desktop ~]# vim /etc/httpd/conf.d/vhost.conf         ##编辑配置文件

###login.jinx.com###
<VirtualHost *:443>                                  ##虚拟主机配置,443端口
        ServerName "login.jinx.com"                  ##访问域名为login.jinx.com时
        DocumentRoot "/var/www/html/login"           ##默认发布目录
        CustomLog "logs/login.log" combined          ##日志文件及内容
        SSLEngine on                                 ##打开ssl
        SSLCertificateFile /etc/pki/tls/certs/www.jinx.com.crt        ##指定证书
        SSLCertificateKeyFile /etc/pki/tls/private/www.jinx.com.key   ##指定key
</VirtualHost>
###页面重写,用于访问80端口时,自动重写为443端口(https)###
<VirtualHost *:80>                                   ##通过80端口访问
        ServerName login.jinx.com                    ##访问域名为login.jinx.com时
        RewriteEngine on                             ##打开页面重写
        rewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=302]
</VirtualHost>
###    ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=302]    ###
###    ^(/.*)$        ##表示客户端在地址栏中输入的所有字符
###    https://       ##新的访问协议
###    %{HTTP_HOST}   ##
###    $1             ##$1的值就表示^(/.*)$的值
###    [redirect=302] ##重定向,301为临时重定向
重启服务,通过浏览器访问login.jinx.com,页面自动重写为https,实现网页数据加密传输,并且需要添加证书

猜你喜欢

转载自blog.csdn.net/xin1889/article/details/80468634