1. Install, start and stop the Apache service
1. Install Apache service software
# yum install -y httpd
2. Start the Apache service
# systemctl start httpd
Other options for systemctl
start: start
stop: stop
restart: restart
enable: Set to start automatically at boot
disable: disable the service and remove the service from the startup item
status: View service status
3. Close selinux
#setenforce 0
4. Turn off the firewall
# systemctl stop firewalld
5. Test
(1) Install Firefox browser
#yum install -y firefox
(2) Open the web page
#firefox http://127.0.0.1
When the following page appears (Apache's default homepage), the httpd service is started.
2. Understand the configuration file of the Apache server
The name of the configuration file |
Storage location |
main configuration file |
/etc/httpd/conf/httpd.conf |
Website data directory |
/var/www/html |
|
Default website homepage file/var/www/html/index.html |
Virtual host directory |
/etc/httpd/conf.d/vhost.conf The vhost.conf file does not exist by default and needs to be created. |
access log |
/var/log/httpd/access_log |
error log |
/var/log/httpd/error_log |
Test: Modify the content of the default homepage and then reopen it
# echo "Let me test the default homepage" > /var/www/html/index.html
Reopen the web page
Found that the content has been changed
Main configuration file/etc/httpd/conf/httpd.conf
1. Back up the original configuration file first
2. In order to facilitate viewing, invert the lines that do not contain "#" from the backup file and overwrite the output to the original file.
3. Open the configuration file
Common configuration instructions:
ServerRoot "/etc/httpd" #Service directory
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 index.html 指令
<IfModule dir_module>
DirectoryIndex index.html #默认的索引页页面
</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/conf.d./vhost.conf
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/conf.d/vhost.conf
4、修改虚拟主机配置文件
#vi /etc/httpd/conf/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/conf.d/vhost.conf
4、修改虚拟主机配置文件
#vi /etc/httpd/conf/httpd.conf
5、重启Apache服务
#systemctl restart httpd
6、测试
(1)修改/etc/hosts文件,添加以下域名解析信息
#vi /etc/hosts
(2)测试