Centos7 Apache配置虚拟主机的三种方式
https://blog.csdn.net/tladagio/article/details/80760261
一、虚机主机的三种方式
1、基于IP
2、基于IP+端口
3、基于域名
官网文档:http://httpd.apache.org/docs/2.4/
二、安装Apache
1、系统环境
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@localhost ~]# ip ad
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:5c:ff:91 brd ff:ff:ff:ff:ff:ff
inet 192.168.253.128/24 brd 192.168.253.255 scope global dynamic eno16777736
valid_lft 1388sec preferred_lft 1388sec
inet6 fe80::20c:29ff:fe5c:ff91/64 scope link
valid_lft forever preferred_lft forever
2、yum安装
[root@localhost ~]# yum install -y httpd
*****
======================================================================================================================================================
Package 架构 版本 源 大小
======================================================================================================================================================
正在安装:
httpd x86_64 2.4.6-80.el7.centos base 2.7 M
为依赖而安装:
apr x86_64 1.4.8-3.el7_4.1 base 103 k
apr-util x86_64 1.5.2-6.el7 base 92 k
httpd-tools x86_64 2.4.6-80.el7.centos base 89 k
mailcap noarch 2.1.41-2.el7 base 31 k
***
已安装:
httpd.x86_64 0:2.4.6-80.el7.centos 作为依赖被安装:
apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-80.el7.centos mailcap.noarch 0:2.1.41-2.el7 完毕!
可以查看安装了内容
[root@localhost ~]# rpm -ql httpd | less
3、配置Selinux文件,SELINUX=disabled。
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted
或者临时关闭
[root@localhost ~]# setenforce 0
4、关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
5、启动httpd服务,访问测试
[root@localhost ~]# systemctl start httpd
三、修改主配置文件
1、查看apache主配置文件,确保存在以下配置,因为等下需要在conf.d/创建虚机主机配置。
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
IncludeOptional conf.d/*.conf
2、另外,把 Require all denied默认拒绝访问设置为允许访问: Require all granted,方便测试。
<Directory />
AllowOverride none
# Require all denied
Require all granted
</Directory>
四、新增虚拟主机配置文件
1、添加基于多个IP的虚拟主机
1)创建配置文件
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
<VirtualHost 192.168.253.128:80>
ServerName a.com
DocumentRoot "/www/a.com/" #网页路径
</VirtualHost> <VirtualHost 192.168.253.129:80>
ServerName b.com
DocumentRoot "/www/b.com/" #网页路径
</VirtualHost>
2)网卡绑定多个IP(我的网卡名是eno16777736,不是eth0)
[root@localhost conf.d]# ip addr add 192.168.253.129 dev eno16777736
[root@localhost conf.d]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:5c:ff:91 brd ff:ff:ff:ff:ff:ff
inet 192.168.253.128/24 brd 192.168.253.255 scope global dynamic eno16777736
valid_lft 1542sec preferred_lft 1542sec
inet 192.168.253.129/32 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe5c:ff91/64 scope link
valid_lft forever preferred_lft forever
3)创建虚机主机a.com和b.com的主页面
[root@localhost conf.d]# mkdir -pv /www/{a.com,b.com}
mkdir: 已创建目录 "/www"
mkdir: 已创建目录 "/www/a.com"
mkdir: 已创建目录 "/www/b.com"
[root@localhost conf.d]# vim /www/a.com/index.html
<h1>Hello,a.com</h1>
[root@localhost conf.d]# vim /www/b.com/index.html
<h1>Hello,b.com</h1>
4)检查配置文件是否正常
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
5)重启httpd服务
[root@localhost conf.d]# systemctl restart httpd
6)打开浏览器,查看结果
2、配置基于IP+端口的虚拟主机
1)创建配置文件
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
<VirtualHost 192.168.253.128:80>
ServerName a.com
DocumentRoot "/www/a.com/"
</VirtualHost> <VirtualHost 192.168.253.128:8080>
ServerName b.com
DocumentRoot "/www/b.com/"
</VirtualHost>
2)修改httpd主配置文件,在Listen 80下面添加一行监控8080端口
[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf
Listen 8080
3)创建虚机主机a.com和b.com的主页面(如果前面已经创建就不用重复)
[root@localhost conf.d]# mkdir -pv /www/{a.com,b.com}
mkdir: 已创建目录 "/www"
mkdir: 已创建目录 "/www/a.com"
mkdir: 已创建目录 "/www/b.com"
[root@localhost conf.d]# vim /www/a.com/index.html
<h1>Hello,a.com</h1>
[root@localhost conf.d]# vim /www/b.com/index.html
<h1>Hello,b.com</h1>
4)检查配置文件
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
5)重启httpd服务
[root@localhost conf.d]# systemctl restart httpd
6)打开浏览器,查看结果
3、基于域名的虚拟主机
1)创建配置文件
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
<VirtualHost *:80>
ServerName a.com
DocumentRoot "/www/a.com/"
</VirtualHost> <VirtualHost *:80>
ServerName b.com
DocumentRoot "/www/b.com/"
</VirtualHost>
2)修改物理主机hosts文件(C:\Windows\System32\drivers\etc),因为这里是因为物理机去访问Apache服务器
添加:
192.168.253.128 a.com
192.168.253.128 b.com
3)物理主机ping域名测试
4)创建虚机主机a.com和b.com的主页面(如果前面已经创建就不用重复)
[root@localhost conf.d]# mkdir -pv /www/{a.com,b.com}
mkdir: 已创建目录 "/www"
mkdir: 已创建目录 "/www/a.com"
mkdir: 已创建目录 "/www/b.com"
[root@localhost conf.d]# vim /www/a.com/index.html
<h1>Hello,a.com</h1>
[root@localhost conf.d]# vim /www/b.com/index.html
<h1>Hello,b.com</h1>
5)检查配置文件
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
6)重启httpd服务
[root@localhost conf.d]# systemctl restart httpd
7)打开浏览器,查看结果
五、扩展虚机主机配置文件
1、先修改回apache主配置文件,设置 Require all denied默认拒绝访问
<Directory />
AllowOverride none
Require all denied </Directory>
这时候再去访问以上的三种配置虚机主机,会全部访问不了。因此需要针对虚机目录设置访问权限。
2、修改虚拟主机配置文件
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
<VirtualHost *:80>
#绑定的主域
ServerName a.com
#绑定的子域名
ServerAlias www.test.com
#网站主目录
DocumentRoot "/www/a.com/"
#错误日志目录
ErrorLog "/var/log/httpd/a.com/error.log"
#访问日志目录
CustomLog "/va/log/httpd/a.com.access.log"
<Directory "www/a.com/">
Options FollowSymLinks
AllowOverride All
#允许任意访问
Require all granted
</Directory>
</VirtualHost>
3、创建日志目录
- [root@localhost b.com]# cd /var/log/httpd/
- [root@localhost httpd]# mkdir a.com
- [root@localhost httpd]# ll
- 总用量 60
- -rw-r--r--. 1 root root 37976 1月 23 22:26 access_log
- drwxr-xr-x. 2 root root 6 1月 23 22:41 a.com
- -rw-r--r--. 1 root root 17795 1月 23 22:38 error_log
- [root@localhost httpd]# cd a.com/
- [root@localhost a.com]# touch error.log
- [root@localhost a.com]# touch access.log
日志目录记得更改属主和属组为Apache,否则httpd启动失败
[root@localhost httpd]# chown -R apache:apache a.com/
4、配置指定IP可以访问虚拟主机(可以单个IP,也可以是一个网段)
[root@localhost conf.d]# vim /etc/httpd/conf.d/virtual.conf
- <VirtualHost *:80>
- #绑定的主域
- ServerName a.com
- #绑定的子域名
- ServerAlias www.test.com
- #网站主目录
- DocumentRoot "/www/a.com/"
- #错误日志目录
- ErrorLog "/var/log/httpd/a.com/error.log"
- #访问日志目录
- CustomLog "/va/log/httpd/a.com.access.log"
- <Directory "www/a.com/">
- Options FollowSymLinks
- AllowOverride All
- #允许任意访问
- Require ip 192.168.253.0/24
- </Directory>
5、配置指定用户可以访问虚拟主机
[root@localhost conf.d]# vim /etc/httpd/conf.d/virtual.conf
<VirtualHost *:80>
#绑定的主域
ServerName a.com
#绑定的子域名
ServerAlias www.test.com
#网站主目录
DocumentRoot "/www/a.com/"
#错误日志目录
ErrorLog "/var/log/httpd/a.com/error.log"
#访问日志目录
CustomLog "/va/log/httpd/a.com.access.log"
<Directory "www/a.com/">
Options FollowSymLinks
AllowOverride authconfig
AuthType basic
AuthName "Restrict area"
AuthUserFile "etc/httpd/.htpasswd"
Require valid-user
</Directory>
创建用户文件,第一次创建的时候要加-c,以后创建都不用加-c,否则会覆盖原数据
[root@localhost conf.d]# pwd
/etc/httpd/conf.d
[root@localhost conf.d]# htpasswd -h
htpasswd: illegal option -- h
Usage:
htpasswd [-cimBdpsDv] [-C cost] passwordfile username
htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password htpasswd -n[imBdps] [-C cost] username
htpasswd -nb[mBdps] [-C cost] username password
-c Create a new file.
-n Don't update file; display results on stdout.
-b Use the password from the command line rather than prompting for it.
-i Read password from stdin without verification (for script usage).
-m Force MD5 encryption of the password (default).
-B Force bcrypt encryption of the password (very secure).
-C Set the computing time used for the bcrypt algorithm
(higher is more secure but slower, default: 5, valid: 4 to 31).
-d Force CRYPT encryption of the password (8 chars max, insecure).
-s Force SHA encryption of the password (insecure).
-p Do not encrypt the password (plaintext, insecure).
-D Delete the specified user.
-v Verify password for the specified user.
On other systems than Windows and NetWare the '-p' flag will probably not work.
The SHA algorithm does not use a salt and is less secure than the MD5 algorithm.
[root@localhost conf.d]# htpasswd -c -m /etc/httpd/.htpasswd tom
New password:
Re-type new password:
Adding password for user tom
重启httpd服务
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@localhost conf.d]# systemctl restart httpd
在浏览器测试登录
6、Require参考
Require all granted
无条件允许访问。
Require all denied
访问被无条件拒绝。
Require env env-var [env-var] ...
只有在给定的环境变量之一被设置的情况下才允许访问。
Require method http-method [http-method] ...
只有给定的HTTP方法才允许访问。
Require expr expression
如果表达式计算结果为true,则允许访问。
Require user userid [userid] ...
只有指定的用户才能访问资源。
Require group group-name [group-name] ...
只有指定组中的用户才能访问资源。
Require valid-user
所有有效的用户都可以访问资源。
Require ip 10 172.20 192.168.2
指定IP地址范围内的客户端可以访问资源。
7、Options
Centos7 Apache配置虚拟主机的三种方式的更多相关文章
- apache配置虚拟主机的三种方式
Apache 配置虚拟主机三种方式 一.基于IP 1. 假设服务器有个IP地址为192.168.1.10,使用ifconfig在同一个网络接口eth0上绑定3个IP: [root@localhos ...
- Apache配置虚拟主机的三种方法(基于IP、端口、域名)
1 Apache虚拟主机的实现方式有3种. 基于IP的虚拟主机 基于端口的虚拟主机 基于域名的虚拟主机 2.1 启用虚拟主机的准备工作 2.1.1安装httpd [root@mail httpd]# ...
- Nginx下配置虚拟主机的三种方法
Nginx下,一个server标签就是一个虚拟主机. 1.基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站 2.基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的 ...
- nginx 配置虚拟主机的三种方法
nginx,一个server标签就是一个虚拟主机. 1.基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站 2.基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的管 ...
- 部署web应用到虚拟主机的三种方式
方式一: 在 [tomcat]/conf/server.xml 文件中的<Engine>标签下的<Host>标签内部, 添加一个 <Context ...
- Tomcat配置虚拟主机的两种方式
1.基于主机名的虚拟主机配置 在随意盘符下建立一个目录作为虚拟地址的目录.例如:F:\virtualhost1,在其下建立 test1.html,写入内容例如:test 在tomcat/conf/se ...
- nginx配置虚拟主机的两种方式
一. 通过端口区分不同的虚拟主机 二. 通过域名区分不同的虚拟主机 备注: 1)hosts文件路径:
- 【转】Apache 配置虚拟主机三种方式
Apache 配置虚拟主机三种方式 原文博客http://www.cnblogs.com/hi-bazinga/archive/2012/04/23/2466605.html 一.基于IP 1. 假 ...
- apache配置虚拟主机后,启动速度慢
apache配置虚拟主机后,启动速度慢且提示“the requested operation has failed” 可以通过在cmd下启动,来查找问题(命令中的“apache2.2”,是服务名,根据 ...
随机推荐
- Color the ball HDU - 1556 (线段树)
思路:线段树,区间更新 #include<iostream> #include<vector> #include<string> #include<cmath ...
- description The request sent by the client was syntactically incorrect.
shi用url传递参数,其他页面都ojbk的 唯独有一个请求 不应该啊 这明显的已经配置好了啊 因为别的请求我也是这样配置的啊,运行是没问题的啊 运行好好的啊 一时间,感觉无从下手了 经过十几分钟各种 ...
- Django-2.2.1学习感悟
或许是看的教程比较老,跟不上2.2.1版本的Django,所以在编写代码时可以说是bug不断,这算是编程必经阶段 每当遇到django的bug logging,先看头几行再看最后几行,能自己解决最好, ...
- vue 使用 element ui动态添加表单
html部分 <div class="hello"> <el-form :model="dynamicValidateForm" ref=&q ...
- 2019-10-18-WPF-解决-StylusPlugIn-点击穿透问题
title author date CreateTime categories WPF 解决 StylusPlugIn 点击穿透问题 lindexi 2019-10-18 20:55:35 +0800 ...
- 计蒜客 Prefix Free Code(字典树+树状数组)
Consider n initial strings of lower case letters, where no initial string is a prefix of any other i ...
- node的源码安装
Node.js 安装配置 本章节我们将向大家介绍在 Windows 和 Linux 上安装 Node.js 的方法. 本安装教程以 Node.js v4.4.3 LTS(长期支持版本)版本为例. No ...
- QT_string转char*
char* convertQString2char(const QString &str) { QByteArray ba = str.toUtf8(); char * pathChar = ...
- 全球城市群Megalopolis
Megalopolis From Wikipedia, the free encyclopedia (Redirected from Megalopolis (city type)) &quo ...
- 百度DMLC分布式深度机器学习开源项目(简称“深盟”)上线了如xgboost(速度快效果好的Boosting模型)、CXXNET(极致的C++深度学习库)、Minerva(高效灵活的并行深度学习引擎)以及Parameter Server(一小时训练600T数据)等产品,在语音识别、OCR识别、人脸识别以及计算效率提升上发布了多个成熟产品。
百度为何开源深度机器学习平台? 有一系列领先优势的百度却选择开源其深度机器学习平台,为何交底自己的核心技术?深思之下,却是在面对业界无奈时的远见之举. 5月20日,百度在github上开源了其 ...