Nginx虚拟主机

准备工作
# 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: [email protected]
# 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 # 至此服务就起来了,接下来我们分别做两种虚拟主机
基于域名的虚拟主机
# 注意修改此处,否则启动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
05-11 22:09