Nginx虚拟主机 (基于域名 基于端口 基于ip)
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,Nginx虚拟主机基于域名的虚拟主机基于IP地址的虚拟主机基于端口的虚拟主机一,安装DNS域名解析服务器1,安装bind服务器[root@localhost ~]# yum install bind
千家信息网最后更新 2026年11月02日Nginx虚拟主机 (基于域名 基于端口 基于ip)
Nginx虚拟主机
- 基于域名的虚拟主机
- 基于IP地址的虚拟主机
- 基于端口的虚拟主机
一,安装DNS域名解析服务器
1,安装bind服务器
[root@localhost ~]# yum install bind -y2,修改主配置文件(named.conf)
[root@localhost ~]# vim /etc/named.conf options { listen-on port 53 { any; }; ##监听所有 listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; recursing-file "/var/named/data/named.recursing"; secroots-file "/var/named/data/named.secroots"; allow-query { any; }; ##允许所有3,修改区域配置文件(named.rfc1912.zones)
[root@localhost ~]# vim /etc/named.rfc1912.zones ##配置区域配置文件zone "kgc.com" IN { type master; file "kgc.com.zone"; ##kgc区域数据配置文件 allow-update { none; }; };zone "accp.com" IN { type master; file "accp.com.zone"; ##accp区域数据配置文件 allow-update { none; };};4,修改区域数据配置文件(kgc.com.zone accp.com.zone)
[root@localhost ~]# cd /var/named/ [root@localhost named]# cp -p named.localhost kgc.com.zone ##复制模板[root@localhost named]# vim kgc.com.zone ##修改区域配置文件$TTL 1D@ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1www IN A 192.168.13.128 ##本机地址[root@localhost named]# cp -p kgc.com.zone accp.com.zone ##复制文件为accp区域数据配置文件[root@localhost named]# systemctl start named ##开启dns服务[root@localhost named]# systemctl stop firewalld.service ##关闭防火墙[root@localhost named]# setenforce 05,创建两个网站的站点目录并添加首页内容
[root@localhost ~]# mkdir -p /var/www/html/accp ##创建accp站点[root@localhost ~]# mkdir -p /var/www/html/kgc ##创建kgc站点[root@localhost ~]# cd /var/www/html/[root@localhost html]# lsaccp kgc[root@localhost html]# echo "this a accp web" > accp/index.html ##创建首页内容[root@localhost html]# echo "this a kgc web" > kgc/index.html ##创建首页内容二,在Windows上将LAMP所需压缩软件包共享出来(此处如有问题请看之前的博客相关文章)

三,在Linux上使用远程共享获取文件并挂载到mnt目录下
[root@localhost ~]# smbclient -L //192.168.100.3/ ##远程共享访问Enter SAMBA\root's password: Sharename Type Comment --------- ---- ------- LNMP-C7 Disk [root@localhost ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt ##挂载到/mnt目录下四,编译安装Nginx
1,解压源码包到/opt下,并查看
[root@localhost ~]# cd /mnt ##切换到挂载点目录[root@localhost mnt]# lsDiscuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gzmysql-boost-5.7.20.tar.gz php-7.1.20.tar.gz[root@localhost mnt]# tar zxvf nginx-1.12.2.tar.gz -C /opt ##解压Nginx源码包到/opt下[root@localhost mnt]# cd /opt/ ##切换到解压的目录下[root@localhost opt]# lsnginx-1.12.2 rh2,安装编译需要的环境组件包
[root@localhost opt]# yum -y install \gcc \ //c语言gcc-c++ \ //c++语言pcre-devel \ //pcre语言工具zlib-devel //数据压缩用的函式库3,创建程序用户nginx并编译Nginx
[root@localhost opt]# useradd -M -s /sbin/nologin nginx ##创建程序用户,安全不可登陆状态[root@localhost opt]# id nginxuid=1001(nginx) gid=1001(nginx) 组=1001(nginx)[root@localhost opt]# cd nginx-1.12.0/ ##切换到nginx目录下[root@localhost nginx-1.12.0]# ./configure \ ##配置nginx> --prefix=/usr/local/nginx \ ##安装路径> --user=nginx \ ##用户名> --group=nginx \ ##用户组> --with-http_stub_status_module ##状态统计模块4,编译和安装
[root@localhost nginx-1.12.0]# make ##编译...[root@localhost nginx-1.12.0]# make install ##安装...5,优化nginx启动脚本,以便于系统识别
[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##创建软连接让系统识别nginx启动脚本[root@localhost nginx]# nginx -t ##检查配置文件的语法问题nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost nginx]# nginx ##开启ngnix[root@localhost nginx]# netstat -ntap | grep 80 ##查看端口,nginx已经开启tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 39620/nginx: master [root@localhost nginx]# systemctl stop firewalld.service ##关闭防火墙[root@localhost nginx]# setenforce 0 [root@localhost nginx]# nginx ##开启6,制作管理脚本,便于使用service管理使用
[root@localhost nginx]# cd /etc/init.d/ ##切换到启动配置文件目录[root@localhost init.d]# lsfunctions netconsole network README[root@localhost init.d]# vim nginx ##编辑启动脚本文件#!/bin/bash# chkconfig: - 99 20 ##注释信息# description: Nginx Service Control ScriptPROG="/usr/local/nginx/sbin/nginx" ##设置变量为nginx命令文件PIDF="/usr/local/nginx/logs/nginx.pid" ##设置变量PID文件 进程号为5346case "$1" in start) $PROG ##开启服务 ;; stop) kill -s QUIT $(cat $PIDF) ##关闭服务 ;; restart) ##重启服务 $0 stop $0 start ;; reload) ##重载服务 kill -s HUP $(cat $PIDF) ;; *) ##错误输入提示 echo "Usage: $0 {start|stop|restart|reload}" exit 1esacexit 0[root@localhost init.d]# chmod +x /etc/init.d/nginx ##给启动脚本执行权限[root@localhost init.d]# chkconfig --add nginx ##添加到service管理器中[root@localhost init.d]# service nginx stop ##就可以使用service控制nginx[root@localhost init.d]# service nginx start7,或者方便systemctl管理,配置文件(为方便写一种即可)
[root@localhost ~]# vim /lib/systemd/system/nginx.service ##创建配置文件[Unit]Description=nginx ##描述After=network.target ##描述服务类型[Service]Type=forking ##后台运行形式PIDFile=/usr/local/nginx/logs/nginx.pid ##PID文件位置ExecStart=/usr/local/nginx/sbin/nginx ##启动服务ExecReload=/usr/bin/kill -s HUP $MAINPID ##根据PID重载配置ExecStop=/usr/bin/kill -s QUIT $MAINPID ##根据PID终止进程PrivateTmp=true[Install]WantedBy=multi-user.target[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service ##设置执行权限[root@localhost ~]# systemctl stop nginx.service ##关闭[root@localhost ~]# systemctl start nginx.service ##开启五,基于不同域名的虚拟主机
1,修改nginx配置文件信息
[root@localhost ~]# cd /usr/local/nginx/conf[root@localhost conf]# vim nginx.conf ##修改Nginx配置文件server { listen 80; server_name www.kgc.com; ##kgc网站 charset utf-8; access_log logs/www.kgc.com.access.log; ##日志文件 location / { root /var/www/html/kgc; ##站点目录 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.accp.com; ##accp网站 charset utf-8; access_log logs/www.accp.com.access.log; ##日志文件 location / { root /var/www/html/accp; ##站点目录 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } [root@localhost conf]# service nginx restart ##重启nginx服务2,用测试机访问不同域名的网站

六,基于不同端口的虚拟主机
1,修改nginx配置文件信息
[root@localhost ~]# cd /usr/local/nginx/conf[root@localhost conf]# vim nginx.conf ##修改Nginx配置文件server { listen 80; server_name www.accp.com; charset utf-8; access_log logs/www.accp.com.access.log; location / { root /var/www/html/accp; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}server { listen 192.168.13.138:8080; ##修改监听端口为8080 server_name www.accp.com; charset utf-8; access_log logs/www.accp8080.com.access.log; ##日志文件修改为8080 location / { root /var/www/html/accp8080; ##8080端口的站点目录 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}2,创建accp8080站点目录,并创建首页内容
[root@localhost conf]# cd /var/www/html/ ##切换到站点目录中[root@localhost html]# mkdir accp8080 ##创建站点目录[root@localhost html]# cd accp8080/[root@localhost accp8080]# echo "this is accp8080 web" > index.html ##创建首页内容[root@localhost accp8080]# service nginx restart ##重启Nginx服务3,用测试机访问不同端口的网站
七,基于不同IP的虚拟主机
1,在虚拟机上添加一块网卡
192.168.13.138192.168.13.1332,修改dns区域数据配置文件
[root@localhost ~]# cd var/named/[root@localhost named]# vim kgc.com.zone ##修改kgc的区域数据配置文件$TTL 1D@ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1www IN A 192.168.13.133 ##地址为133[root@localhost named]# vim accp.com.zone ##修改accp的区域数据配置文件$TTL 1D@ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1www IN A 192.168.13.138 ##地址为138[root@localhost named]# systemctl restart named ##重启dns服务3,修改nginx配置文件信息
[root@localhost ~]# cd /usr/local/nginx/conf[root@localhost conf]# vim nginx.conf ##修改Nginx配置文件 server { listen 192.168.13.133:80; ##指定IP地址 server_name www.kgc.com; charset utf-8; access_log logs/www.kgc.com.access.log; location / { root /var/www/html/kgc; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.13.138:80; ##指定IP地址 server_name www.accp.com; charset utf-8; access_log logs/www.accp.com.access.log; location / { root /var/www/html/accp; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } [root@localhost conf]# service nginx restart ##重启Nginx服务4,用测试机访问不同IP的网站
谢谢阅读!
文件
配置
目录
服务
区域
数据
站点
主机
虚拟主机
端口
不同
地址
网站
内容
脚本
首页
切换
编译
域名
信息
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技公司经营范围
互联网科技工作委员会主任
科技制造互联网公司
科技互联网公司logo
时间互联网络科技
撒么互联网科技公司
汉立互联网科技有限公司联系电话
互联网科技寓意
现代互联网科技股
互联网科技公司实习生
移动互联网科技公司
广州市恰铎互联网科技有限公司
想开互联网科技公司怎么样
云南省自由互联网科技有限公司
昆明中奇联合互联网科技
陕西九四连强互联网科技有限公司
中国科技和美国互联网
西安左小互联网科技有限公司
杭州融税互联网科技有限公司
科技互联网企业在行动
广州紫鲸互联网科技有限公司
互联网科技小贷
狮子座互联网科技有限公司
广西互联网科技公司
居家互联网科技有限公司
互联网科技类展会
恒生科技互联网指数分析
乌鲁木齐新华智原互联网科技学校
广州富喜互联网络科技
数字互联网科技有限公司