一、安装、启动与停止Apache服务
1、安装Apache服务软件
# yum install -y httpd
2、启动Apache服务
# systemctl start httpd
systemctl的其它选项
start:启动
stop:停止
restart:重启
enable:设置开机自动启动
disable:禁用服务,从开机启动项把服务移除
status:查看服务状态
3、关闭selinux
#setenforce 0
4、关闭防火墙
# systemctl stop firewalld
5、测试
(1)安装火狐浏览器
#yum install -y firefox
(2)打开网页
#firefox http://127.0.0.1
出现以下页面(Apache默认首页)即表示 httpd 服务启动


二、认识Apache服务器的配置文件
配置文件的名称 |
存放位置 |
主配置文件 |
/etc/httpd/conf/ |
网站数据目录 |
/var/www/html |
|
默认网站首页文件/var/www/html/ |
虚拟主机目录 |
/etc/httpd// 这个文件默认是没有,需要新建 |
访问日志 |
/var/log/httpd/access_log |
错误日志 |
/var/log/httpd/error_log |
测试:修改默认首页的内容,然后重新打开
# echo "我来测试默认首页" > /var/www/html/

重新打开网页
发现内容已经改了

主配置文件/etc/httpd/conf/
1、先备份原配置文件

2、为了方便查看,从备份文件中反选不包含“#”的行,覆盖输出到原文件

3、打开配置文件
常用配置说明:
ServerRoot "/etc/httpd" #服务目录
Listen 80 #监听端口
User apache #运行服务的用户
Group apache #运行服务的用户组
ServerAdmin root@localhost #管理员邮箱
DocumentRoot "/var/www/html" #网站数据目录(网页文件)
#/var/www的权限设置
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
#/var/www/html的权限设置
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#<IfModule>会判断 dir_module 是否载入,如果载入则会执行 DirectoryIndex 指令
<IfModule dir_module>
DirectoryIndex #默认的索引页页面
</IfModule>
#错误日志文件路径
ErrorLog "logs/error_log"
三、配置虚拟主机
需要在一台web物理服务器上,配置多个网站
三种方法:基于不同的IP地址;基于不同的端口号;基于不同的域名(主机号)。
方法一:基于不同的IP地址
要求
IP地址 |
网站数据目录 |
默认网页内容 |
192.168.128.77 |
/var/www/ip77 |
l am from 192.168.128.77 |
192.168.128.78 |
/var/www/ip78 |
l am from 192.168.128.78 |
1、添加第二个IP地址:192.168.128.78

保存退出,重启网络
#systemctl restart network
2、创建网站数据目录

3、创建默认网页文件,并写入内容

4、创建虚拟主机配置文件,并编辑内容
#vi /etc/httpd/./

5、重启Apache服务
#systemctl restart httpd
6、测试


方法二:基于不同的端口号
IP地址 |
端口号 |
网站数据目录 |
默认网页内容 |
192.168.128.77 |
8080 |
/var/www/p8080 |
l am from port 8080 |
192.168.128.77 |
8088 |
/var/www/p8088 |
l am from port 8088 |
1、创建网站数据目录
#mkdir /var/www/p8080 /var/www/p8088
2、创建默认网页文件,并写入内容

3、修改文件,把原来的内容删掉
#vi /etc/httpd//

4、修改虚拟主机配置文件
#vi /etc/httpd/conf/

5、重启Apache服务
#systemctl restart httpd
6、测试

方法三:基于不同的域名(主机号)
IP地址 |
域名 |
网站数据目录 |
默认网页内容 |
192.168.128.77 |
/var/www/www111 |
l am from www111 |
|
192.168.128.77 |
/var/www/www222 |
l am from www222 |
1、创建网站数据目录
#mkdir /var/www/www111 /var/www/www222
2、创建默认网页文件,并写入内容

3、修改文件,把原来的内容删掉
#vi /etc/httpd//

4、修改虚拟主机配置文件
#vi /etc/httpd/conf/

5、重启Apache服务
#systemctl restart httpd
6、测试
(1)修改/etc/hosts文件,添加以下域名解析信息
#vi /etc/hosts

(2)测试
