httpd 简单配置及虚拟目录

安装

yum install httpd

httpd主配置目录

cd/etc/httpd/conf/

配置主要文件

在vim /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf复制
在/etc/httpd/conf/httpd.conf粘贴,并在124行复制五行并粘贴
<VirtualHost *:80>					#IP:端口
    DocumentRoot /www/www			#你的默认页所在路径
    ServerName www.jnds.com	        #域名或IP
<Directory "/www/www">				#定义目录访问权限
    AllowOverride None              #固定参数
    Require all granted             #对这个目录给予授权
</Directory>
</VirtualHost>

创建对应目录及网页

mkdir -p /www/www

vi /www/www/index.html` 或 `echo "内容" > /www/www/index.html

curl (网页路径)可以查看网页内容

多端口

先关闭防火墙,不然apache重启不成功

[root@localhost /]# systemctl stop firewalld

[root@localhost /]# setenforce 0

42行后添加
Listen 8080
Listen 8888

<VirtualHost *:8080>
     DocumentRoot /www/www1
     ServerName www.jnds.com
<Directory "/www/www">
     AllowOverride None
     Require all granted
</Directory>
</VirtualHost>

<VirtualHost *:8888>
    DocumentRoot /www/www2
    ServerName www.jnds.com
<Directory "/www/www">
    AllowOverride None
    Require all granted
</Directory>
</VirtualHost>

多IP

[root@localhost /]# vi /etc/sysconfig/network-scripts/ifcfg-ens33

IPADDR1=172.16.100.100
IPADDR2=172.16.100.110

[root@localhost /]# vi/etc/httpd/conf/httpd.conf

<VirtualHost 172.16.100.100:80>
    DocumentRoot /www/www1
    ServerName www.jnds.com
<Directory "/www/www">
    AllowOverride None
    Require all granted
</Directory>
</VirtualHost>

<VirtualHost 172.16.100.110:80>
    DocumentRoot /www/www2
    ServerName www.jnds.com
<Directory "/www/www">
    AllowOverride None
    Require all granted
</Directory>
</VirtualHost>

多域名

$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
        AAAA    ::1
        PTR     localhost.
110     PTR     www.jnds.com
120     PTR     www.jnds.net

[root@localhost /] vi /etc/httpd/conf/httpd.com

<VirtualHost *:80>
    DocumentRoot /www/www1
    ServerName www.jnds.com
<Directory "/www/www">
    AllowOverride None
    Require all granted
</Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /www/www2
    ServerName www.jnds.net
<Directory "/www/www">
    AllowOverride None
    Require all granted
</Directory>
</VirtualHost>

虚拟目录

[root@localhost /] vi /etc/httpd/conf.d/antoindex.conf

166 Alias /www/ "/www/html"				#/名称 /路径
<Directory "/www/html/">				#路径
    AllowOverride None
    Require all granted
</Directory>
更改index.html默认页名称
163 <IfModule dir_module>
164     DirectoryIndex test.html   #把默认页的名称自定义为test.hmtl
165 </IfModule>
主目录和虚拟目录同时存在
Alias /test "/test"
<VirtualHost *:80>
    DocumentRoot /jnds
    ServerName 192.168.1.60
<Directory "/jnds">
    AllowOverride None
    Require all granted
</Directory>
<Directory "/test">
    AllowOverride None
    Require all granted
</Directory>
</VirtualHost>
访问日志设置

httpd的日志格式有两种,分别是: 通用日志(Common Log Format) 组合(复合型)日志(Combined Log Format)

vim /etc/httpd/conf/httpd.conf
211     #CustomLog "logs/access_log" common
212
213     #
214     # If you prefer a logfile with access, agent, and referer information
215     # (Combined Logfile Format) you can use the following directive.
216     #
217     CustomLog "logs/access_log" combined
例如配置访问日志路径和名称为/var/log/httpd/www2.chinaskills.net-access_log;日志记录格式为复合型
CustomLog "/var/log/httpd/www2.chinaskills.net-access_log" combined

也可以使用 /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf 的模板

<VirtualHost *:80>
    DocumentRoot /var/www/html/
    ServerName 172.16.10.4
    CustomLog "/var/log/httpd/www2.chinaskills.net-access_log" combined
</VirtualHost>