在CentOS 8上安装与配置Apache虚拟主机

导读Apache服务器是现代操作系统包括UNIX和Windows等开发和维护开源的HTTP服务器,apache提供一个安全、高效、可扩展的服务器,提供与当前HTTP标准同步的HTTP服务。

实验环境

操作系统:Centos 8

web应用:apache

内网IP:192.168.3.21

shell执行:root

在CentOS 8上安装与配置Apache虚拟主机在CentOS 8上安装与配置Apache虚拟主机

以root或具有sudo权限的用户身份登录执行如下操作。

安装httpd服务

[root@linuxcool ~]# yum install httpd-devel.x86_64 httpd.x86_64 httpd-tools.x86_64

验证httpd是否安装成功

[root@linuxcool ~]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Apr  2 2020 13:13:23

启动httpd服务

[root@linuxcool ~]# systemctl start httpd.service 
       
[root@linuxcool ~]# systemctl status httpd.service 
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-05-22 12:23:03 CST; 25s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 952 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    Tasks: 6
   Memory: 3.6M
   CGroup: /system.slice/httpd.service
           ├─952 /usr/sbin/httpd -DFOREGROUND
           ├─953 /usr/sbin/httpd -DFOREGROUND
           ├─954 /usr/sbin/httpd -DFOREGROUND
           ├─955 /usr/sbin/httpd -DFOREGROUND
           ├─956 /usr/sbin/httpd -DFOREGROUND
           └─957 /usr/sbin/httpd -DFOREGROUND

May 22 12:23:03 linuxcool systemd[1]: Starting The Apache HTTP Server...
May 22 12:23:03 linuxcool httpd[952]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, u...message
May 22 12:23:03 linuxcool systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

apache安装成功,默认web目录/var/www/

测试

http://192.168.3.21/

在CentOS 8上安装与配置Apache虚拟主机在CentOS 8上安装与配置Apache虚拟主机

安装成功!

配置apache虚拟主机

创建网站目录

[root@linuxcool ~]# mkdir -p /var/www/web1
[root@linuxcool ~]# mkdir -p /var/www/web2

web1添加index.html

[root@linuxcool ~]# vim /var/www/web1/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to Web1</title>
  </head>
  <body>
    <h1>Success! Web1 home page!</h1>
  </body>
</html>

web2添加index.html

[root@linuxcool ~]# vim /var/www/web2/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to Web1</title>
  </head>
  <body>
    <h1>Success! Web1 home page!</h1>
  </body>
</html>

授权web目录权限

[root@linuxcool ~]# chown -R apache: /var/www/web1/
[root@linuxcool ~]# chown -R apache: /var/www/web2/

创建虚拟主机文件

#web1

[root@linuxcool ~]# vim /etc/httpd/conf.d/web1.conf
<VirtualHost *:80>
ServerName web1.com
ServerAlias www.web1.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www/web1

<Directory /var/www/web1/>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>

ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>

#web2

[root@linuxcool ~]# vim /etc/httpd/conf.d/web2.conf
<VirtualHost *:80>
ServerName web2.com
ServerAlias www.web2.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www/web2

<Directory /var/www/web2/>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>

ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>

重启httpd服务

[root@linuxcool ~]# systemctl restart httpd.service

验证httpd虚拟主机结果

http://www.web1.com

å¨CentOS 8ä¸å®è£ä¸éç½®Apacheèæ主æºå¨CentOS 8ä¸å®è£ä¸éç½®Apacheèæ主æº

 

http://www.web2.com

在CentOS 8上安装与配置Apache虚拟主机在CentOS 8上安装与配置Apache虚拟主机

Linux就该这么学

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值