Nginx虚拟主机
在真实的服务器环境,为了充分利用服务器资源,一台Nginx web服务器会同时配置N个虚拟主机,这样可以充分利用服务器的资源,方便管理员的统一管理
准备工作
# list
# CentOS Linux release 7.3.1611 (Core)
# Nginx-1.14.2.tar.gz
# Nginx-1.16.0.tar.gz
# install_Nginx.sh
初始化环境
init_security() {
systemctl stop firewalld
systemctl disable firewalld &>/dev/null
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
sed -i '/^GSSAPIAu/ s/yes/no/' /etc/ssh/sshd_config
sed -i '/^#UseDNS/ {s/^#//;s/yes/no/}' /etc/ssh/sshd_config
systemctl enable sshd crond &> /dev/null
echo -e "\033[32m [安全配置] ==> OK \033[0m"
}
init_yumsource() {
if [ ! -d /etc/yum.repos.d/backup ];then
mkdir /etc/yum.repos.d/backup
fi
mv /etc/yum.repos.d/* /etc/yum.repos.d/backup 2>/dev/null
if ! ping -c 2 baidu.com &>/dev/null
then
echo "您无法上外网,不能配置yum源"
exit
fi
curl -o /etc/yum.repos.d/163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo &>/dev/null
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null
echo -e "\033[32m [YUM Source] ==> OK \033[0m"
}
init_data() {
# 准备数据目录,方便待会区分虚拟主机
mkdir /a_test
mkdir /b_test
echo a_test >> /a_test/index.html
echo b_test >> /a_test/index.html
mkdir /usr/local/Nginx/logs/{a,b}_test
mkdir /usr/local/Nginx/conf.d
}
main() {
init_security
init_yumsource
init_data
}
使用脚本安装Nginx
#!/usr/bin/env bash
# Author: ZhouJian
# Mail: 18621048481@163.com
# Time: 2019-9-3
# Describe: CentOS 7 Install Nginx Source Code Script
version="Nginx-1.14.2.tar.gz"
user="Nginx"
Nginx=${version%.tar*}
path=/usr/local/src/$Nginx
echo $path
if ! ping -c2 www.baidu.com &>/dev/null
then
echo "网络不通,无法安装"
exit
fi
yum install -y gcc gcc-c++ openssl-devel pcre-devel make zlib-devel wget psmisc
#if [ ! -e $version ];then
# wget http://Nginx.org/download/$version
#fi
if ! id $user &>/dev/null
then
useradd $user -M -s /sbin/nologin
fi
if [ ! -d /var/tmp/Nginx ];then
mkdir -p /var/tmp/Nginx/{client,proxy,fastcgi,uwsgi,scgi}
fi
tar xf $version -C /usr/local/src
cd $path
./configure \
--prefix=/usr/local/Nginx \
--user=Nginx \
--group=Nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_realip_module \
--http-client-body-temp-path=/var/tmp/Nginx/client \
--http-proxy-temp-path=/var/tmp/Nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/Nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/Nginx/scgi \
--with-pcre \
--with-file-aio \
--with-http_secure_link_module && make && make install
if [ $? -ne 0 ];then
echo "Nginx未安装成功"
exit
fi
killall Nginx
/usr/local/Nginx/sbin/Nginx
#echo "/usr/local/Nginx/sbin/Nginx" >> /etc/rc.local
#chmod +x /etc/rc.local
#systemctl start rc-local
#systemctl enable rc-local
ss -antp |grep Nginx
# 至此服务就起来了,接下来我们分别做两种虚拟主机
基于域名的虚拟主机
基于域名的虚拟主机原理:相同IP地址,相同端口、不同的域名。也就是说多个虚拟主机之间共用一个ip地址以及一个端口(80),区分各个主机之间使用不同的域名,当然访问的时候也就只能使用域名进行访问了,基于域名的虚拟主机是最常用的方式
# 注意修改此处,否则启动Nginx只会生效默认位置Nginx配置文件
# include /usr/local/Nginx/conf.d/*.conf;
[root@test1 Nginx]# cat conf.d/a.conf
server{
listen 80;
server_name www.a.com a.com;
access_log /usr/local/Nginx/logs/a_test/access.log;
location / {
root /a_test;
index index.html;
}
}
[root@test1 Nginx]# cat conf.d/b.conf
server{
listen 80;
server_name www.b.com b.com;
access_log /usr/local/Nginx/logs/b_test/access.log;
location / {
root /b_test;
index index.html;
}
}
/usr/local/Nginx/sbin/Nginx -s reload # 平滑加载配置
[root@test1 Nginx]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:81 *:*
LISTEN 0 128 *:82 *:*
基于端口的虚拟主机
[root@test1 Nginx]# cat conf.d/a.conf
server{
listen 81;
server_name www.a.com a.com;
access_log /usr/local/Nginx/logs/a_test/access.log;
location / {
root /a_test;
index index.html;
}
}
server{
listen 82;
server_name www.a.com a.com;
access_log /usr/local/Nginx/logs/a_test/access.log;
location / {
root /a_test;
index index.html;
}
}
Nginx平滑升级
# 在上面源码编译安装Nginx1.14的基础上升级到1.16
wget http://Nginx.org/download/Nginx-1.16.0.tar.gz
tar xf Nginx-1.16.0.tar.gz -C /usr/local/src/
cd /usr/local/src/Nginx-1.16.0/
./configure --prefix=/usr/local/Nginx116 --with-http_stub_status_module --with-http_ssl_module --user=Nginx --group=Nginx
make && make install
# 备份原来老的Nginx文件,主要是为了回退
cp ./Nginx/sbin/Nginx ./Nginx/sbin/Nginx.bak
# 将新版的Nginx二进制文件替换已安装的Nginx的二进制文件
cp Nginx116/sbin/Nginx Nginx/sbin/Nginx -rf
./Nginx/sbin/Nginx -v
# Nginx version: Nginx/1.16.0
# 给Nginx旧的主进程发送一个USR2信号,让新主进程和旧进程同时工作.
# 再发一个Winch给旧的主进程号让子进程退出,如果主进程还在,方便回滚
kill -USR2 17522
版本回滚
cp Nginx.bak ./Nginx/sbin/Nginx -rf
# 发送HUP信号唤醒旧版本
kill -HUP `cat /usr/local/Nginx/logs/Nginx.pid.oldbin `
# 关闭新版本的主进程和Worker进程
kill -USR2 24148
kill -WINCH 24148
[root@test1 local]# ./Nginx/sbin/Nginx -v
# Nginx version: Nginx/1.14.2