一、常用的安装和管理命令
yum install httpd #安装Apache,也就是httpd服务
systemctl start httpd #启动httpd服务
systemctl enable httpd #设置开机自动启动httpd服务
systemctl restart httpd #重启httpd服务
二、配置文件说明
/etc/httpd/conf/httpd.conf #主配置文件
/etc/httpd/conf.d #子配置文件目录
/var/log/httpd/ #日志文件目录(access.log error.log)
/etc/httpd/modules #模块文件的目录
/var/www/html #默认网站根目录
主配置文件(/etc/httpd/conf/httpd.conf)信息:
由<></>包含的的配置信息都是区域配置信息,其他则是全局配置信息。
ServerRoot "/etc/httpd" #服务主目录
Listen 80 #监听IP地址及端口号
Include conf.modules.d/*.conf
User apache #运行httpd服务的用户
Group apache #运行httpd服务的用户组
ServerAdmin root@localhost #管理员邮箱
ServerName www.example.com:80 #服务器域名,默认为注释
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html" #网站数据根目录
<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>
<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
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 #访问日志文件
</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
IncludeOptional conf.d/*.conf
关于网站目录配置信息(以根目录为例):
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AllowOverride: 允许存在于.htaccess文件中的指令类型(.htaccess文件名是可以改变的,其文件名由AccessFileName指令决定)。其值为None时,表示不搜索该目录下的.htaccess文件;当其值为All时,表示在.htaccess文件中可以使用所有的指令。
Options: 配置在特定目录使用哪些特性,多个特性之间用空格隔开。ExecCGI表示在该目录下允许执行CGI脚本;FollowSymLinks表示在该目录下允许文件系统使用符号连接;Indexes表示当用户访问该目录时,如果用户找不到DirectoryIndex指定的主页文件(例如index.html),则返回该目录下的文件列表给用户;SymLinksIfOwnerMatch表示当使用符号连接时,只有当符号连接的文件拥有者与实际文件的拥有者相同时才可以访问。
Order: 控制在访问时Allow和Deny两个访问规则哪个优先:Allow表示允许访问的主机列表(可用域名或子网,例如:Allow from 192.168.0.0/16);Deny表示拒绝访问的主机列表。
DirectoryIndex index.html index.htm index.php #设置默认的主页文件名
三、虚拟主机配置
1.基于域名的虚拟主机
1)配置域名信息
[root@hollowman ~]# echo '192.168.100.100 bbs.hollowman.cn' >> /etc/hosts
2)设置对应域名的站点目录及测试网页
[root@hollowman ~]# mkdir /var/www/bbs
[root@hollowman ~]# echo bbs.hollowman.cn > /var/www/bbs/index.html
3)配置虚拟主机信息
可在主配置文件(/etc/httpd/conf/httpd.conf)任意处添加虚拟主机配置信息,一般放在网站数据根目录后面后者前面便于更加清晰读懂配置文件
[root@hollowman ~]# vim /etc/httpd/conf/httpd.conf
......
DocumentRoot "/var/www/html" #网站数据根目录
<VirtualHost 192.168.100.100>
DocumentRoot "/var/www/bbs"
ServerName "bbs.hollowman.cn"
<Directory "/var/www/bbs">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
......
4)重启httpd服务
[root@hollowman ~]# systemctl restart httpd
[root@hollowman ~]# systemctl enable httpd
5)浏览器输入域名bbs.hollowman.cn访问
2.基于端口的虚拟主机
1)设置对应端口的站点目录及测试网页
[root@hollowman ~]# mkdir /var/www/8080
[root@hollowman ~]# echo ‘192.168.100.100:8080’ > /var/www/8080/index.html
2)配置基于端口的虚拟主机信息
注意需要配置监听端口信息,可和前一次配置的基于域名的虚拟主机信息放在一起
[root@hollowman ~]# vim /etc/httpd/conf/httpd.conf
......
DocumentRoot "/var/www/html" #网站数据根目录
...基于域名的虚拟主机配置信息...
Listen 8080
<VirtualHost 192.168.100.100:8080>
DocumentRoot "/var/www/8080"
<Directory "/var/www/8080">
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
......
3)重启httpd服务
[root@hollowman ~]# systemctl restart httpd
4)输入带端口的ip地址:192.168.100.100:8080
四、修改网站根目录
因为修改网站根目录涉及到SELinux安全策略,需要修改安全上下文,可查看“SElinux学习笔记”内容。