Linux学习之路Linux我用 Linux

RedHat7的Apache配置介绍

2018-10-25  本文已影响11人  小短腿电工

这里我们介绍yum安装httpd yum install -y httpd


[root@100 ~]# systemctl restart httpd
[root@100 ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
[root@100 ~]# vim /etc/httpd/

安装的版本介绍


image.png

备份一下配置文件
[root@100 ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak


需要了解的知识块
1、了解Apache的基本配置
2、配置动态页面:CGI、wsgi、ssl
3、配置一个虚拟主机
4、配置https

1、了解Apache的基本配置
编辑Apache的配置文件httpd.conf 路径在/etc/httpd/conf/httpd.conf
默认的配置文件路径ServerRoot "/etc/httpd"
默认监听的端口Listen 80
模块存放路径[root@100 ~]# ls /etc/httpd/conf.modules.d/
所属者和所属组
User apache
Group apache
默认安装的时候他会创建一个不可登录系统的用户
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
默认的管理员邮箱是ServerAdmin root@localhost
站点配置#ServerName www.example.com:80 默认是禁用的
站点的目录设置
<Directory "/var/www"> ------------<Directory "/var/www/html">
AllowOverride None ------------------Options Indexes FollowSymLinks
# Allow open access: -----------------AllowOverride None
Require all granted --------------------Require all granted
</Directory>----------------------------</Directory>
站点设置项:
(Options 里的indexes是开启开启索引,FollowSymLinks是允许链接文件--需要修改上下文chcon -R --reference=/var/www/html /new)

(AllowOverride None 是否允许单前目录下的一个隐藏文件.htaccess里的配置覆盖当前httpd.conf
的里设置--用于http的认证
在网页的根目录下创建一个
[root@100 html]# cat /var/www/html/.htaccess
AuthType Basic
AuthName haha
AuthUserfile /etc/httpd/conf/.htpasswd
Require user tom

/etc/httpd/conf/.htpasswd这个文件需要用
[root@100 html]# htpasswd -cm /etc/httpd/conf/.htpasswd tom ##-cm 创建用MD5
加密
New password:
Re-type new password:
Adding password for user tom
[root@100 html]#
修改后的AllowOverride AuthConfig 接下来访问就要提示用户密码

(Require all granted 允许所有
Require all denied 拒绝所有
Require ip 192.168.1.1 允许这地址访问
Require ip 192.168.1.0/24 这个端地址可以访问
Require local
Require ip 192.168.1.1 192.168.2.2 这俩个地址可以访问


站点的文档目录DocumentRoot "/var/www/html"
设置文件的权限
<Files ".ht*">
Require all denied
</Files>

错误日志ErrorLog "logs/error_log"

Alias 设置别名 # Alias /webpath /full/filesystem/path默认是禁用的--每设置一个目录就需要加一个
<Directory "/mnt/html">
AllowOverride None
# Allow open access:
Require all granted
</Directory>

2、配置动态页面:CGI、wsgi、ssl
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" 这是CGI的脚本路径
[root@100 cgi-bin]# pwd
/var/www/cgi-bin
[root@100 cgi-bin]# cat *

#!/usr/bin/perl
print "Content-Type: text/html; charset=UTF-8\n\n";
$time=localtime();
print "$time\n"
#!/bin/bash
echo "Content-Type: text/html; charset=UTF-8"
echo 
date +'%F %T'
hostname 

[root@100 cgi-bin]#
浏览器访问http://xxxxxxxxx/cgi-bin/aa.sh 实现的效果就是获取本地时间

wsgi是python实现的一种动态页面功能默认没有按照
[root@100 cgi-bin]# yum install -y mod_wsgi
在/etc/httpd/conf/httpd.conf配置文件里修改
253 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
254
255 </IfModule>
256
257 WSGIScriptAlias /wsgi-bin/ "/var/www/cgi-bin/"
浏览器访问测试http://xxxxxx/wsgi-bin/aa.wsgi 实现的效果就是显示本地时间
[root@100 wsgi-bin]# cat aa.wsgi

#!/usr/bin/env python
import time
def application (environ, start_response):
    a = time.ctime(time.time())
    response_body = 'This Dynamic WSGI Page Was Generated at: \n%s\n'%a
    status = '200 OK'
    response_headers = [('Content-Type', 'text/plain'),
               ('Content-Length', '1'),
               ('Content-Length',str(len(response_body)))]
    start_response(status, response_headers)
    return [response_body]

SSI 设置动态页面是需要在配置文件里设置
在站点目录这里添加includes

 Options Indexes FollowSymLinks Includes   ##include包含

ssi设置的页面默认是*.shtml
Vim /var/www/html/index.shtml
<h2>
<span style="background-color:#E56600;">helloworld</span>
</h2>
<p>

       当前的用户是: <!--#exec cmd="whoami" -->
       当前的时间是:<!--#echo var="DATE_LOCAL" -->
       访问者的ip是:<!--#echo var="REMOTE_ADDR"-->

</p>
~
测试连接http://xxxxx/index.shtml
##调用刚才的cgi脚本

注意点:
站点根目录下的默认是访问index.html怎么把它设置成index.shtml呢?
就需要用到地址重写功能
在http的配置文件里追加
362 RewriteEngine on
363 RewriteRule ^/?$ /index.shtml [R]
他会使你的浏览器默认重定向站点的shtml里

3、虚拟主机的配置
默认http的配置文件在这里

# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

这里我们写一个基于一个IP解析俩个不同站点的案例(基于主机名的虚拟主机)


image.png

这里复制一个模板文件

[root@100 ~]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d/vhosts.conf
[root@100 conf.d]# echo zzz > /var/www/html/index.html;echo cc /cc/index.html
[root@100 conf.d]# systemctl restart httpd
[root@100 conf.d]# cat vhosts.conf 
<VirtualHost *:80>
    DocumentRoot /cc
    ServerName www.cc.com
    ErrorLog "/var/log/httpd/www.cc.com-error_log"
    CustomLog "/var/log/httpd/dummy-www.cc.com.com-access_log" common
</VirtualHost>
<Directory "/cc">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName www.zzz.com
    ErrorLog "/var/log/httpd/www.zzz.com-error_log"
    CustomLog "/var/log/httpd/dummy-www.zzz.com.com-access_log" common
</VirtualHost>
<Directory "/var/www/html">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
cho
[root@100 conf.d]# chcon -R --reference=/var/www/html /cc  #更改上下文

(如果是基于ip的虚拟主机只需修改你的dns解析即可)

基于端口的虚拟主机

[root@100 conf.d]# cat vhosts.conf 
<VirtualHost *:801>
    DocumentRoot /cc
    ServerName www.cc.com
    ErrorLog "/var/log/httpd/www.cc.com-error_log"
    CustomLog "/var/log/httpd/dummy-www.cc.com.com-access_log" common
</VirtualHost>
<Directory "/cc">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

<VirtualHost *:802>
    DocumentRoot /var/www/html
    ServerName www.zzz.com
    ErrorLog "/var/log/httpd/www.zzz.com-error_log"
    CustomLog "/var/log/httpd/dummy-www.zzz.com.com-access_log" common
</VirtualHost>
<Directory "/var/www/html">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
[root@100 conf.d]# cat /etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80
Listen 80
Listen 801
Listen 802

有探讨的在下方留言
--END--

上一篇下一篇

猜你喜欢

热点阅读