nginx自动部署脚本-2021-06-16教程
nginx自动部署脚本
#!/bin/bash
echo "=============================================版本确定=============================================="
read -p "pcre版本:" P_V
read -p "openssl版本:" O_V
read -p "zlib版本:" Z_V
read -p "nginx版本:" N_V
pcre_version=$P_V
openssl_version=$O_V
zlib_version=$Z_V
nginx_version=$N_V
#pcre_version="8.36"
#openssl_version="1.1.1"
#zlib_version="1.2.11"
#nginx_version="1.14.0"
echo "=====================================安装wget gcc gcc-c++========================================="
yum install -y gcc gcc-c++ wget net-tools
echo ""
echo "进入目录:/usr/local/"
cd /usr/local/
echo ""
echo "===========================================安装pcre=============================================="
echo "下载:pcre-"$pcre_version""
pcre_url="http://jaist.dl.sourceforge.net/project/pcre/pcre/"$pcre_version"/pcre-"$pcre_version".tar.gz"
wget $pcre_url
echo "解压:pcre-"$pcre_version".tar.gz"
tar -zxvf pcre-"$pcre_version".tar.gz
echo "进入目录:/usr/local/pcre-"$pcre_version""
cd pcre-"$pcre_version"
echo "编译安装:pcre-"$pcre_version""
./configure
make && make install
echo "返回到目录:/usr/local/"
cd /usr/local/
echo ""
echo "==========================================安装openssl============================================="
echo "下载:openssl-"$openssl_version""
openssl_url="http://www.openssl.org/source/openssl-"$openssl_version".tar.gz"
wget $openssl_url
echo "解压:openssl-"$openssl_version".tar.gz"
tar -zxvf openssl-"$openssl_version".tar.gz
echo "进入目录:openssl-"$openssl_version""
cd openssl-"$openssl_version"
echo "编译安装:openssl-"$openssl_version""
./config
make && make install
echo "返回到目录:/usr/local/"
cd /usr/local/
echo ""
echo "==============================================安装zlib============================================"
echo "下载:zlib-"$zlib_version""
zlib_url="http://zlib.net/zlib-"$zlib_version".tar.gz"
wget $zlib_url
echo "解压:zlib-"$zlib_version".tar.gz"
tar -zxvf zlib-"$zlib_version".tar.gz
echo "进入目录:zlib-"$zlib_version""
cd zlib-"$zlib_version"
echo "编译安装:zlib-"$zlib_version""
./configure
make && make install
echo "返回到目录:/usr/local/"
cd /usr/local/
echo ""
echo "===============================================安装nginx=========================================="
echo "下载:nginx-"$nginx_version""
nginx_url="http://nginx.org/download/nginx-"$nginx_version".tar.gz"
wget $nginx_url
echo "解压:nginx-"$nginx_version".tar.gz"
tar -zxvf nginx-"$nginx_version".tar.gz
echo "重命名nginx-"$nginx_version"为nginx"
mv nginx-"$nginx_version" nginx
echo "进入目录:nginx"
cd nginx
echo "编译安装:nginx-"$nginx_version""
./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-pcre=/usr/local/pcre-"$pcre_version" --with-zlib=/usr/local/zlib-"$zlib_version" --with-openssl=/usr/local/openssl-"$openssl_version"
make && make install
echo "创建目录:/usr/local/nginx/logs"
mkdir logs
echo "返回到目录:/usr/local/"
cd /usr/local/
echo ""
echo "======================================将nginx加入环境变量==========================================="
cat >> /etc/profile << EOF
NGINX_HOME=/usr/local/nginx
PATH=$NGINX_HOME/sbin:$PATH
export PATH
EOF
source /etc/profile
eccho ""
echo"=========================================配置开机启动==============================================="
nginx_start_path=/lib/systemd/system/nginx.service
touch $nginx_start_path
cat >> $nginx_start_path << EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl enable nginx
systemctl start nginx
systemctl status nignx
echo ""
echo "版本信息:"
echo "pcre:"$pcre_version
echo "openssl:"$openssl_version
echo "zlib:"$zlib_version
echo "nginx:"$nginx_version
echo "安装路径: /usr/local/"
echo ""
echo "===========================================软件包删除============================================="
echo ""
read -p "是否需要删除下载的安装(输入y/Y删除,其他不删除):" inputMsg
if [ "$inputMsg" == 'y' ] || [ "$inputMsg" == 'Y' ]
then
rm -rf nginx-"$nginx_version".tar.gz pcre-"$pcre_version".tar.gz openssl-"$openssl_version".tar.gz zlib-"$zlib_version".tar.gz
echo "删除完成"
else
echo "不删除"
fi
echo ""
echo "end"
补充部分
一般没有进行启动设置,需要进入部署的nginx进行启动
/usr/local/nginx/sbin/nginx
下面是nginx的一个启动脚本,但是还是进行上面的配置比较好
#!/bin/bash
read -p "是否需要启动nginx(输入y/Y启动,其他不启动):" startNginxMsg
if [ "$startNginxMsg" == 'y' ] || [ "$startNginxMsg" == 'Y' ]
then
/usr/local/nginx/sbin/nginx
if [ $? -eq 0 ]
then
localIp=`hostname -I`
echo "启动成功,请访问: http://$localIp"
else
echo "启动失败,请查看异常信息确定失败原因"
fi
else
echo "不启动"
fi
echo ""
nginx配置文件需要修改部分
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
---------------------------------------------------------------
location中配置的路径要对应自己部署的nginx的html路径
我的虚拟机是刚创建出来的防火墙没有关,所有访问不通,可以关闭防火墙或者开放80端口
##关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
##开放端口
[root@centos7_9-mod conf]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@centos7_9-mod conf]# vim nginx.conf
[root@centos7_9-mod conf]# firewall-cmd --add-service=http --permanent
success
[root@centos7_9-mod conf]# firewall-cmd --add-port=80/tcp --permanent
Warning: ALREADY_ENABLED: 80:tcp
success
[root@centos7_9-mod conf]# firewall-cmd --reload
success
[root@centos7_9-mod conf]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens192
sources:
services: dhcpv6-client http ssh
ports: 80/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules: