apache虚拟主机配置与nginx虚拟主机配置

article/2025/9/1 23:45:06

1.什么是虚拟主机?

虚拟主机(英语:virtual hosting)或称 共享主机(shared web hosting),又称虚拟服务器,是一种在单一主机或主机群上,实现多网域服务的方法,可以运行多个网站或服务的技术。虚拟主机之间完全独立,并可由用户自行管理,虚拟并非指不存在,而是指空间是由实体的服务器延伸而来,其硬件系统可以是基于服务器群,或者单个服务器。

主流的操作有三种:

  • 基于IP的虚拟主机
  • 基于域名的虚拟主机
  • 基于端口的虚拟主机

下面进行两大主流中间件apache和nginx的配置演示

2.apache配置虚拟主机

演示环境:centos7

启动apache时可以用以下命令排查80端口占用情况:
[root@blackstone ~]# netstat -antlp | grep 80
使用此命令可以强制停止占用进程
[root@blackstone ~]# kill -9 33396

2.1 apache适配php环境

httpd主配置文件是/etc/httpd/conf/httpd.conf
直接编辑配置文件:

vim /etc/httpd/conf/httpd.conf

修改以下4个地方:
1.去掉ServerName那一行最前的注释

2.“Require all denied” 中denied改为granted,如果不改有时会拒绝访问(返回403)

3.在AddType开头的那几行下面添加一行:“AddType application/x-httpd-php .php”

4.在“DirectoryIndex index.html”后面添加“ index.php”

重启服务

[root@blackstone html]# systemctl restart httpd

尝试向根目录下写入php文件测试访问情况:

[root@blackstone html]# cat 01.php
<?php phpinfo();

在这里插入图片描述

2.2 基于IP的虚拟主机

1.创建虚拟网卡的链接,并为其添加两个IP地址

#注意这里网卡硬件名称的一致性,使用nmcli connection device查看
[root@blackstone html]# nmcli connection add ifname ens33 con-name ens33-tes type ethernet
Connection 'ens33-tes' (6788f949-8035-40b8-8412-316453bf1eaa) successfully added.
[root@blackstone html]# nmcli connection
NAME       UUID                                  TYPE            DEVICE
docker0    cf8efaa5-0062-482b-b481-0f4019db624f  bridge          docker0
ens33      df079c21-1852-4edb-bff1-81b2896feede  802-3-ethernet  ens33
ens33-tes  6788f949-8035-40b8-8412-316453bf1eaa  802-3-ethernet  --
[root@blackstone html]#  nmcli connection modify ens33-tes ipv4.addresses 192.168.2.100/24 +ipv4.addresses 192.168.2.169/24 +ipv4.addresses 192.168.2.200/24 ipv4.gateway 192.168.2.2 ipv4.dns 192.168.2.2 ipv4.method manual
[root@blackstone html]# nmcli connection up ens33-tes

2.创建虚拟网页内容

[root@blackstone www]# mkdir -p /var/www/ip1
[root@blackstone www]# mkdir -p /var/www/ip2
[root@blackstone www]# echo 192.168.2.100 > /var/www/ip1/index.html
[root@blackstone www]# echo 192.168.2.200 > /var/www/ip2/index.html
[root@blackstone www]# tree /var/www/
/var/www/
├── cgi-bin
├── html
│   └── 01.php
├── ip1
│   └── index.html
└── ip2└── index.html

3.编辑配置文件,这里直接将vhost示例配置文件移动到此处

#1.查找.conf结尾的配置文件
[root@blackstone conf.d]# rpm -ql httpd | grep conf | grep -v etc#2.讲配置文件移动到对应位置
[root@blackstone conf.d]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf .
#3.修改配置文件内容

文件内容

# ip 100
<VirtualHost 192.168.2.100:80>DocumentRoot /var/www/ip1ServerName 192.168.2.100ErrorLog "/var/log/httpd/ip1-error_log"CustomLog "/var/log/httpd/ip1-access_log" common
</VirtualHost>
<directory /var/www/ip1>
allowoverride none
require all granted
</directory>
# ip 200
<VirtualHost 192.168.2.200:80>DocumentRoot /var/www/ip2ServerName 192.168.2.200ErrorLog "/var/log/httpd/ip2-error_log"CustomLog "/var/log/httpd/ip2-access_log" common
</VirtualHost>
<directory /var/www/ip2>
allowoverride none
require all granted
</directory>

4.重启服务进行测试

[root@blackstone conf.d]# systemctl restart httpd

在这里插入图片描述
在这里插入图片描述

2.3 基于端口的虚拟主机

1.创建测试网页

[root@blackstone www]# mkdir /var/www/port81
[root@blackstone www]# mkdir /var/www/port82
[root@blackstone www]# echo 'this is port 81' > /var/www/port81/index.html
[root@blackstone www]# echo 'this is port 82' > /var/www/port82/index.html
[root@blackstone www]# tree /var/www/
/var/www/
├── cgi-bin
├── html
│   └── 01.php
├── ip1
│   ├── 01.php
│   └── index.html
├── ip2
│   └── index.html
├── port81
│   └── index.html
└── port82└── index.html

2.修改配置文件

#port 81
listen 81
<VirtualHost 192.168.2.169:81>DocumentRoot /var/www/port81ServerName 192.168.2.169:81ErrorLog "/var/log/httpd/port81-error_log"CustomLog "/var/log/httpd/port81-access_log" common
</VirtualHost>
<directory /var/www/port81>
allowoverride none
require all granted
</directory>#port 82
listen 82
<VirtualHost 192.168.2.169:82>DocumentRoot /var/www/port82ServerName 192.168.2.169:82ErrorLog "/var/log/httpd/port82-error_log"CustomLog "/var/log/httpd/port82-access_log" common
</VirtualHost>
<directory /var/www/port82>
allowoverride none
require all granted
</directory>

注意:端口绑定错误可以关闭这里的selinux安全机制

[root@blackstone www]# vim /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加[root@blackstone www]# setenforce 0
[root@blackstone www]# getenforce
Permissive

3.测试

[root@blackstone ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
[root@blackstone ~]# systemctl restart httpd
[root@blackstone ~]# curl 192.168.2.169:82
this is port 82
[root@blackstone ~]# curl 192.168.2.169:81
this is port 81

2.4基于域名的虚拟主机

1.创建网页文件夹

[root@blackstone www]# mkdir /var/www/aaa
[root@blackstone www]# mkdir /var/www/bbb
[root@blackstone www]# echo 'this is aaa.com' > /var/www/aaa/index.html
[root@blackstone www]# echo 'this is bbb.com' > /var/www/bbb/index.html

2.配置文件

#domain aaa
<VirtualHost localhost:80>DocumentRoot /var/www/aaaServerName www.aaa.comErrorLog "/var/log/httpd/aaa-error_log"CustomLog "/var/log/httpd/aaa-access_log" common
</VirtualHost>
<directory /var/www/aaa>
allowoverride none
require all granted
</directory>
#domain bbb<VirtualHost  localhost:80>DocumentRoot /var/www/bbbServerName www.bbb.comErrorLog "/var/log/httpd/bbb-error_log"CustomLog "/var/log/httpd/bbb-access_log" common
</VirtualHost>
<directory /var/www/bbb>
allowoverride none
require all granted
</directory>

3.测试

在本地修改host文件将两个域名指向192.168.2.169
host文件目录:C:\Windows\System32\drivers\etc\

192.168.2.169 www.bbb.com 
192.168.2.169 www.aaa.com 

或者在服务器上修改hosts文件均可,此处采用服务器上测试。

[root@blackstone ~]# curl www.bbb.com
this is bbb.com
[root@blackstone ~]# curl www.aaa.com
this is aaa.com

在这里插入图片描述
在这里插入图片描述
注意访问时使用http://www.aaa.com

3.ndinx配置虚拟主机

1.沿用上述的所有网卡配置以及目录配置

涉及2.2的地址添加、2.2-2.4的目录创建。

2.编辑nginx配置文件

关于nginx的配置其只需要在配置文件中的http模块内插入相应的server模块即可。

[root@blackstone nginx]# vim /usr/local/nginx/conf/nginx.conf

文件内容:

#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}
#v-domain --- 基于域名的虚拟主机配置server {listen       80;server_name  www.aaa.com;location / {root   /var/www/aaa;index  index.html index.htm;}}server {listen       80;server_name  www.bbb.com;location / {root   /var/www/bbb;index  index.html index.htm;}}
#v-ip---基于IP的虚拟主机配置server {listen       80;server_name  192.168.2.100;location / {root   /var/www/ip1;index  index.html index.htm;}}server {listen       80;server_name  192.168.2.200;location / {root   /var/www/ip2;index  index.html index.htm;}}#v-port---基于端口的虚拟主机配置server {listen       81;server_name  192.168.2.169;location / {root   /var/www/port81;index  index.html index.htm;}}server {listen       82;server_name  192.168.2.169;location / {root   /var/www/port82;index  index.html index.htm;}}#server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}
}

3.全路径nginx重启+测试

#nginx语法检测 --- 注意这里一定要使用全路径
[root@blackstone conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#nginx平滑重启
[root@blackstone conf]# /usr/local/nginx/sbin/nginx -s reload#测试基于域名的虚拟主机
[root@blackstone conf]# curl www.aaa.com
this is aaa.com
[root@blackstone conf]# curl www.bbb.com
this is bbb.com#测试基于IP的虚拟主机
[root@blackstone conf]# curl http://192.168.2.100
192.168.2.100
[root@blackstone conf]# curl http://192.168.2.200
192.168.2.200#测试基于端口的虚拟主机
[root@blackstone conf]# curl http://192.168.2.169:81
this is port 81
[root@blackstone conf]# curl http://192.168.2.169:82
this is port 82

http://chatgpt.dhexx.cn/article/wCnkFa6K.shtml

相关文章

apache 虚拟主机如何配置

apache 虚拟主机如何配置&#xff1f;相信很多没有经验的人对此束手无策&#xff0c;为此本文总结了问题出现的原因和解决方法&#xff0c;通过这篇文章希望你能解决这个问题。 apache虚拟主机就是在apache服务器上配置多个虚拟主机&#xff0c;从而让一个服务器提供多站点的服…

Apache虚拟主机的实现方式

Apache虚拟主机的实现方式有3种。 基于IP的虚拟主机 基于端口的虚拟主机 基于域名的虚拟主机 配置环境 系统&#xff1a;CentOS7.5 工具&#xff1a;XShell6 ** 配置准备 ** 1.安装httpd yum install httpd -y2.查看安装了内容 rpm -ql httpd | less3、配置Selinux文件&am…

如何配置apache虚拟主机

如何配置apache虚拟主机 实验目标&#xff1a;在apache实现基于域名的虚拟主机 实验用的XAMPP版本为1.7.7&#xff0c;内含apache版本为2.2.21 实验前准备&#xff1a; 1. 为了测试不同的域名&#xff0c;在Windows/System32/drivers/etc/下觅得hosts文件&#xff0c;在其中…

Apache 虚拟主机

文章目录 一、访问主机服务的本质1.1Apache访问控制1.2Apache保持连接1.3配置httpd-default.conf 二、httpd支持的虚拟主机类型2.1、基于域名2.2、基于IP地址2.3、基于端口 三、基于域名访问虚拟主机3.1安装Apache3.2虚拟主机提供域名解析3.3基于IP地址访问虚拟主机3.4基于端口…

Apache配置虚拟主机

1.安装apache服武器 yum install httpd -y ##apache软件 yum install httpd-manual ##apache的手册 systemctl start httpd systemctl enable httpd firewall-cmd --list-all ##列出火墙信息 firewall-cmd --permanent --add-servicehttp ##永久允许…

Apache虚拟主机配置详细教程

文章目录 Apache 虚拟主机企业应用部署一个端口不同域名调试worker工作模式进行压力测试权限设置Apache 配置文件详解Apache Rewrite 规则讲解Apache配置文件权限操作 总结 Apache 虚拟主机企业应用 企业真实环境中&#xff0c;一台WEB服务器发布单个网站会非常浪费资源&#x…

Apache虚拟主机的三种实现方式

Apache虚拟主机的三种实现方式 一、Apache简介二、Apache的特性三、Apache虚拟主机介绍四、安装Apache步骤1.安装httpd软件包2.放行防火墙服务3.查看httpd进程4.创建测试网站文件 五、三种虚拟主机的配置方法1.基于域名的虚拟主机①创建网页文件②创建服务配置文件③查看web1.c…

Apache配置虚拟主机三种方式(保姆级教学)

由于本人很笨&#xff0c;第一次学步骤比较多。想学的可以耐下心来看、来学。 实验环境&#xff1a;ContOS 7&#xff0c;Apache服务器 在ContOS中可以运行一下代码直接下载 [rootlocalhost ~]# yum install -y httpd 下载完可以查看下载的内容 [rootlocalhost ~]# rpm -q…

Apache的虚拟主机

一&#xff1a;web的虚拟主机 虚拟web主机指的是在同一台服务器中运行多个web站点&#xff0c;其中的每个网站并不单独占用整个服务器&#xff0c;因此被称为“虚拟”web主机&#xff0c;通过虚拟web主机服务可以充分利用服务器的硬件资源&#xff0c;从而降低网站构建与运行成…

excel一直显示“操作系统当前的配置不能运行此应用程序”

问题描述 我的系统win10&#xff0c;操作x64,安装office2019&#xff0c;由于学习中不经常使用到excel时隔半年我今天打开excel一直显示“操作系统当前的配置不能运行此应用程序 ”&#xff0c;好家伙给我整不会了&#xff0c;不慌让我们打开百度&#xff01;百度半天都没有解…

由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序可能会纠正这个问题

我们用 VS 2005 编写非托管的程序, 在一台未安装 .net 开发环境的机器上运行会出现 "由于应用程序的配置不正确&#xff0c;应用程序未能启动&#xff0c;重新安装应用程序可能会纠正这个问题"。 开始时还以为必须要安装 .net Framework 2.0 &#xff0c; 然后安装…

由于应用程序配置不正确,程序未能启动”--原因及解决方法

由于应用程序配置不正确,程序未能启动”--原因及解决方法 http://moogge.spaces.live.com/blog/cns!ab9b00d806d52aed!245.entry 问题描述:当运行由VC 2005 编译的程序时,出现错误消息“由于应用程序配置不正确,程序未能启动.重新安装应用程序可能会纠正这个错误”解决方法…

一种解决运行程序报“应用程序配置不正确”的问题

在我们开发工程中&#xff0c;可能有些情况下&#xff0c;不能在本机进行调试。这个时候我们一般会使用VM&#xff08;vmware)建立一个虚拟机环境&#xff0c;然后把编译过的程序放在该虚拟机环境下执行调试。可是在某些情况下&#xff0c;不管我们编译的是debug还是release版本…

C# .NET 解决提示“由于应用程序配置不正确,应用程序未能启动..”的错误

C# 应用程序开发时&#xff0c;修改程序代码后&#xff0c;调试运行程序 莫名其妙弹出这个报错。 百度找解药呀&#xff0c;&#xff0c;找了好多方法&#xff0c;&#xff0c;都是 " 项目--->配置属性---->链接器---->清单文件--->生成清单-->设置为&q…

Win11应用程序启动提示配置不正确解决方法

Win11应用程序启动提示配置不正确解决方法分享。近期有不少的用户遇到了在启动应用程序的时候&#xff0c;出现错误提示的问题。出现的错误提示是应用配置不正确&#xff0c;那么这个错误提示问题要怎么去进行解决&#xff0c;一起看看具体的解决方法吧。 解决方法&#xff1a;…

“应用程序配置不正确, 程序无法启动”解决方法

最近在XP 32位系统上用VS2008开发的MFC Application程序在没有安装VS2008的Win7系统上双击运行以后出现了“应用程序配置不正确, 程序无法启动”的bug。经过半天时间的研究整理出三种方案可以用来解决该问题。 方法一:对于不需要运行时函数支持的工程可以采用该方法。 (1)…

nmap参数

nmap参数&#xff08;转&#xff09; 进行ping扫描&#xff0c;打印出对扫描做出响应的主机,不做进一步测试(如端口扫描或者操作系统探测): nmap -sP 192.168.1.0/24 仅列出指定网络上的每台主机&#xff0c;不发送任何报文到目标主机: nmap -sL 192.168.1.0/24 探测目标主…

主动扫描技术nmap详解

主动扫描与被扫描目标有一个互动的过程&#xff0c;目标是可能知道自己在被扫描的&#xff1b;主动扫描一般都是针对目标发送特制的数据包&#xff0c;然后根据目标主机的反应来获得一些信息&#xff0c;帮助我们过滤无意义的主机&#xff0c;建立目标的网络拓扑&#xff0c;主…

nmap 扫描数据分析

本案22端口为开放端口&#xff0c;110为未开放端口 Wireshark上使用下面的表达式 ip.addr192.168.104.127 and ip.addr192.168.104.61 and tcp.port22 ip.addr192.168.104.127 and ip.addr192.168.104.61 and tcp.port110 命令一、 nmap -sS SYN-->SYN ACK-->RST …

Nmap专题

Nmap专题笔记 介绍网络扫描相关概念和Nmap的作用。介绍Nmap相关概念、演示简单的安装过程。列举Nmap的主要参数&#xff0c;介绍相关参数。实例演示Nmap的常用扫描技巧。介绍使用Nmap的脚本扫描。 建议学习方法 1.熟记Nmap的主要参数命令。 2.学习OSI七层模型的相关知识&…