YUM
安装即可# yum -y install httpd
tree
命令查看Httpd
主体配置结构# tree /etc/httpd/
/etc/httpd/ #httpd配置目录
├── conf #主配置目录
│ ├── httpd.conf #主配置文件
│ └── magic
├── conf.d #辅助配置目录
│ ├── autoindex.conf
│ ├── README
│ ├── userdir.conf
│ └── welcome.conf
├── conf.modules.d #模块文件路径
│ ├── 00-base.conf
│ ├── 00-dav.conf
│ ├── 00-lua.conf
│ ├── 00-mpm.conf
│ ├── 00-proxy.conf
│ ├── 00-systemd.conf
│ └── 01-cgi.conf
├── logs -> ../../var/log/httpd #日志存储目录(注: 这里软链到/var/log/httpd)
├── modules -> ../../usr/lib64/httpd/modules #模块存储目录
└── run -> /run/httpd #主进程文件目录(PID文件)
6 directories, 13 files
# cp conf/httpd.conf{,.bak}
[root@Center httpd]# less conf/httpd.conf
# sed -i '/^[[:space:]]*#/d' conf/httpd.conf
[root@Center httpd]# less conf/httpd.conf
ServerRoot "/etc/httpd" #服务器根目录(守护进程运行目录)
Listen 80 #服务监听端口
Include conf.modules.d/*.conf #引用模块配置文件
User apache #以指定用户运行httpd进程
Group apache #以指定组运行httpd进程
ServerAdmin root@localhost #指定服务管理员
<Directory /> #目录权限控制,下同
AllowOverride none
Require all denied #拒绝所有
</Directory>
DocumentRoot "/var/www/html" #Main站点根目录
<Directory "/var/www">
AllowOverride None
Require all granted #允许所有
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted #允许所有
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
#拒绝访问所有 .ht 后缀的文件(常用于拒绝非法访问 .htaccess 文件)
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log" #错误日志存储路径
LogLevel warn #日志记录级别
#日志模块配置段
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined #定义日志(access_log)格式,下同
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined #引用LogFormat,格式形如:CustomLog "LOGS_PATH" LogFormat_NAME
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8 #默认字符集
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on #开启Sendfile
IncludeOptional conf.d/*.conf #引用辅助配置文件
Httpd
后,将默认启用Main主机
,且该站点为默认响应站点,即Default_Web_Site
,默认站点根目录为/var/www/html
[root@Center httpd]# echo "Hi, XiaoMu" > /var/www/html/index.html #写入自定义网页内容
[root@Center httpd]# curl localhost #访问本地Main主机
Hi, XiaoMu
虚拟主机就是在一台物理主机上,实现多个站点的构建和部署
;Httpd支持3
种模式的虚拟主机构建实现,分别为:IP(网际协议地址)
的虚拟主机PORT(端口)
的虚拟主机FQDN(域名)
的虚拟主机尽量不要将虚拟主机同Main主机混合使用(即httpd.conf配置下的主机),建议使用虚拟主机时,禁用Main主机
,即注释下面一行:# DocumentRoot "/var/www/html"
# ifconfig | grep inet\
inet 192.168.119.137 netmask 255.255.255.0 broadcast 192.168.119.255
inet 192.168.1.128 netmask 255.255.255.0 broadcast 192.168.31.255
inet 127.0.0.1 netmask 255.0.0.0
# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d/
/var/www/html
,则必须显式授权,否则将跳转至默认页(即:"/usr/share/httpd/noindex/index.html")# vim /etc/httpd/conf.d/httpd-vhosts.conf
# 同一端口,但不同地址
<VirtualHost 192.168.119.137:80>
# ServerAdmin webmaster@dummy-host.example.com #管理员邮箱
DocumentRoot "/www/web1" #当前虚拟主机站点根目录
# ServerName dummy-host.example.com #域名
# ServerAlias www.dummy-host.example.com #域名别名
# ErrorLog "/var/log/httpd/dummy-host.example.com-error_log" #错误日志存储路径
# CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common #访问日志存储路径,其格式默认继承自 httpd.conf ;允许在当前配置段自定义格式,但仅在当前配置段有效
<Directory "/www/web1"> #显式授权
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.1.128:80>
# ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/www/web2"
# ServerName dummy-host2.example.com
# ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
# CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
<Directory "/www/web2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@Center httpd]# mkdir -pv /www/web{1..2}
mkdir: created directory ‘/www’
mkdir: created directory ‘/www/web1’
mkdir: created directory ‘/www/web2’
[root@Center httpd]# echo "This is web1" > /www/web
web1/ web2/
[root@Center httpd]# echo "This is web1" > /www/web1/index.html
[root@Center httpd]# echo "This is web2" > /www/web2/index.html
[root@Center httpd]# httpd -t #语法检查
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::727e:c9e:9edf:ff8d. Set the 'ServerName' directive globally to suppress this message
Syntax OK
ServerName
错误,解决上述错误只需去除下列行首注释即可:[root@Center httpd]# vim conf/httpd.conf #注意,应该在主配置文件修改
ServerName www.example.com:80
[root@Center httpd]# httpd -t
Syntax OK
[root@Center httpd]# vim conf.d/httpd-vhosts.conf
# 同一地址,但不同端口(截取部分配置信息)
<VirtualHost 192.168.1.128:80> #注意,此处监听的是80/tcp
# ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/www/web2"
# ServerName dummy-host2.example.com
# ErrorLog "/var/log/httpd/du mmy-host2.example.com-error_log"
# CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
<Directory "/www/web2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
Listen 8080 #必须显式声明监听8080/tcp
<VirtualHost 192.168.1.128:8080> #注意,此处监听的是8080/tcp
# ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/www/web3"
# ServerName dummy-host2.example.com
# ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
# CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
<Directory "/www/web3">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@Center httpd]# mkdir -pv /www/web3
mkdir: created directory ‘/www/web3’
[root@Center httpd]# echo "This is web3" > /www/web3/index.html
- 基于DNS域名解析系统
- 基于hosts文件此处为方便演示,采用hosts的方式,如想通过DNS解析,请参照
- Like Unix OS
- Windows NT X
- `如果不是以Windows本地账户登录,你可能需要通过具有管理员权限的账号手动授权,默认只读`
- 在 C:WindowsSystem32driversetchosts 中添加下述条目:
192.168.1.128 t1.zhimajihua.cn t2.zhimajihua.cn
[root@Center httpd]# vim conf.d/httpd-vhosts.conf
<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/www/web1"
ServerName t1.zhimajihua.cn
# ServerAlias www.dummy-host.example.com
# ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
# CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
<Directory "/www/web1">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/www/web2"
ServerName t2.zhimajihua.cn
# ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
# CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
<Directory "/www/web2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@Center httpd]# echo "This is t1.zhimajihua.cn" > /www/web1/index.html
[root@Center httpd]# echo "This is t2.zhimajihua.cn" > /www/web2/index.html
[root@Center httpd]# httpd -t
Syntax OK
[root@Center httpd]# systemctl restart httpd