Detailed Nginx build multi-site implementation of virtual host application

Source: Internet
Author: User
Tags nameserver nginx server nslookup

Brief introduction

Virtual host refers to a certain amount of disk space on the network server, the administrator can take advantage of this part of the space, put the site and application components, provide the necessary data storage and transmission functions. A virtual host is one that divides a physical server running on the Internet into multiple "virtual" servers.

Advantages

Low price. Compared to the purchase of a standalone server, the cost of website construction is greatly reduced, providing a great convenience for the popularization of small and medium-sized websites.
Improve efficiency. Because multiple virtual hosts share the resources of a real host, it greatly increases the utilization of the server and bandwidth, which makes it possible to configure multiple network IP addresses on a single server without conflict.

Disadvantages

Functional limitations. For example, may consume the system resources of the Forum program, traffic statistics function.
Access speed and traffic limits. The different virtual hosts on a single server are separate and managed by the administrator. However, a server host can only support a certain number of virtual hosts, and when this amount is exceeded, the server performance drops sharply.
The independence of the poor. Virtual host can not be arbitrarily installed by the customer software and Remote Desktop and other operations.

Use

Suitable as small and medium-sized web portal, save money resources.
Small and medium-sized enterprises, professional portals can use virtual host space to provide data sharing, data download services.
can provide the data storage data function for the SME. Because the cost is lower than the standalone server, high security is preferred as a small database.
The application templates that are unique to the virtual host space allow users to quickly deploy in batches and are the preferred platform for small and midsize businesses to run ASP or PHP applications.

Experimental environment
    • System Environment: CentOS7.4
    • Server IP Address: 192.168.100.71
    • Client IP Address: 192.168.100.72
    • Yum Mount directory:/mnt/sr0
    • Related source package: Baidu Cloud download?? Password: UHZM
Build step one, prepare for work 1, close the firewall and SELinux

[Email protected] ~]# systemctl stop Firewalld.service #关闭防火墙
[Email protected] ~]# systemctl disable Firewalld.service #随开机自动关闭

[Email protected] ~]# Vim/etc/sysconfig/selinux

[email protected] ~]# reboot #重启Linux生效

Second, build the DNS server 1, install the BIND package

[Email protected] ~]# cd/mnt/sr0/packages/
[Email protected] packages]# RPM-IVH bind-9.9.4-50.el7.x86_64.rpm

2. Modify the master configuration file named.conf

[Email protected] ~]# vim/etc/named.conf

3. Create and modify the DNS forward parsing file

[Email protected] ~]# cd/var/named/
[Email protected] named]# cp-p named.localhost bt.com.zone #将模板文件改为正向解析文件进行修改
[Email protected] ~]# Vim/var/named/bt.com.zone #修改正向解析文件

[Email protected] named]# cp-p bt.com.zone cloud.com.zone
[Email protected] ~]# Vim/var/named/bt.com.zone

4. Start the DNS server

[Email protected] ~]# systemctl start Named.service

5. Modify the DNS client configuration file

[Email protected] ~]# echo "nameserver 192.168.100.71" >>/etc/resolv.conf

6. Test DNS resolution

[[email protected] ~]# nslookup www.bt.com #nslookup used to query DNS records to see if the domain name resolution is normal
[email protected] ~]# nslookup www.cloud.com

Second, build Nginx service 1, install the corresponding toolkit and C language compiler

[Email protected] ~]# yum-y install pcre-devel zlib-devel gcc gcc-c++

2. Create Nginx Process User

[Email protected] ~]# useradd-m-s/sbin/nologin nginx

3, configure, compile and compile the installation of Nginx source package

[Email protected] ~]# tar xvf nginx-1.12.0.tar.gz-c/usr/src/
[Email protected] ~]# cd/usr/src/nginx-1.12.0/
[Email protected] nginx-1.12.0]#/configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

parameter resolution:

Prefix #指定安装位置
User=nginx #指定nginx系统用户
Group=nginx #指定组

[[email protected] nginx-1.12.0]# make && make install

4, optimize the path

[Email protected] ~]# ln-s/usr/local/nginx/sbin/*/usr/local/sbin/

Three, set Nginx operation control 1, detection grammar

[Email protected] ~]# nginx-t #测试配置是否有语法错误

2. Start Nginx Service

[Email protected] ~]# vim/usr/local/nginx/conf/nginx.conf #修改主配置文件

[Email protected] ~]# Nginx #启动Nginx
[Email protected] ~]# NETSTAT-ANPT | grep ":" #检测服务是否启动
[Email protected] ~]# cat/usr/local/nginx/logs/nginx.pid #查看pid

3. Stop Nginx Service

[Email protected] ~]# kill-1 3809 #平滑重启Nginx服务. Equivalent to Killall-s HUP nginx;nginx-s Reload
[Email protected] ~]# kill-3 1514 #关闭Nginx服务. Equivalent to Killall-s quit Nginx;nginx-s quit
[Email protected] ~]# Nginx #重启服务
[Email protected] ~]# cat/usr/local/nginx/logs/nginx.pid #查看pid号

4. Edit Nginx Service Script

[Email protected] ~]# Vim/lib/systemd/system/nginx.service

[Unit]

Description=nginx Server Control Script #说明
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 $PIDFile #重载服务
Execstop=/usr/bin/kill-s QUIT $PIDFile #停止服务

[Install]

Wanteby=multi-user.target

[Email protected] ~]# Systemctl daemon-reload #重新加载服务单元
[Email protected] ~]# Systemctl enable Nginx.service #设置开机自启动

[[email protected] ~]# systemctl stop nginx.service# Service
[[email protected] ~]# systemctl start nginx.service# start service
[[email protected] ~]# Systemctl reload nginx.service# Smooth Restart service
[[email protected] ~]# systemctl restart nginx.service# Restart Service

Four, domain-based virtual host building (same IP, same port, different hostname) 1, create virtual Web host Site Directory and Web page

[Email protected] ~]# mkdir-p/var/www/html/{btcom,cloudcom} #创建网站目录

[Email protected] ~]# echo "This is bt.com aaaaaaaaaa" >/var/www/html/btcom/index.html #创建网页
[Email protected] ~]# echo "This is cloud.com bbbbbbbbbb" >/var/www/html/cloudcom/index.html #创建网页

2, modify the configuration file, add the virtual host configuration

[Email protected] ~]# cd/usr/local/nginx/conf/
[email protected] conf]# CP nginx.conf Nginx.conf.bak #将源配置文件进行备份
[Email protected] conf]# grep-v "#" Nginx.conf.bak > Nginx.conf #将注释过滤掉
[Email protected] conf]# vim nginx.conf

3. Restart Nginx Service

[Email protected] ~]# systemctl restart Nginx.service

4. Client Testing

[Email protected] ~]# echo "nameserver 192.168.100.71" >>/etc/resolv.conf #修改DNS客户机配置文件

Five, IP address-based virtual host (the same port, the same hostname, different IP) 1, the server side to add a sub-network card

[Email protected] ~]# ifconfig ens33:0 192.168.100.77

2. Modify the master configuration file

[Email protected] ~]# vim/usr/local/nginx/conf/nginx.conf

3. Restart Nginx Service

[Email protected] ~]# systemctl restart Nginx.service

4. Client Testing

Six, Port-based virtual host (same port, same hostname, different IP) 1, modify the master configuration file

[Email protected] ~]# vim/usr/local/nginx/conf/nginx.conf

2. Restart Nginx Service

[Email protected] ~]# systemctl restart Nginx.service

3. Client Testing

Detailed Nginx build multi-site implementation of virtual host application

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.