| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- #!/bin/bash
- # Nginx配置自动安装脚本
- # 支持自动检测系统类型并选择合适的配置方法
- CONFIG_FILE="hzdyjsweb.camasort.com.conf"
- SITE_NAME="hzdyjsweb.camasort.com"
- # 颜色定义
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- BLUE='\033[0;34m'
- NC='\033[0m' # No Color
- # 日志函数
- log_info() {
- echo -e "${BLUE}[INFO]${NC} $1"
- }
- log_success() {
- echo -e "${GREEN}[SUCCESS]${NC} $1"
- }
- log_warning() {
- echo -e "${YELLOW}[WARNING]${NC} $1"
- }
- log_error() {
- echo -e "${RED}[ERROR]${NC} $1"
- }
- # 检查是否为root用户
- check_root() {
- if [[ $EUID -eq 0 ]]; then
- log_warning "检测到root用户,直接执行操作"
- SUDO=""
- else
- log_info "检测到普通用户,将使用sudo执行操作"
- SUDO="sudo"
- fi
- }
- # 检查nginx是否已安装
- check_nginx() {
- if ! command -v nginx &> /dev/null; then
- log_error "nginx未安装,请先安装nginx"
- echo "Ubuntu/Debian: sudo apt update && sudo apt install nginx"
- echo "CentOS/RHEL: sudo yum install nginx 或 sudo dnf install nginx"
- exit 1
- fi
-
- log_success "nginx已安装"
- nginx -v
- }
- # 检查配置文件是否存在
- check_config_file() {
- if [ ! -f "$CONFIG_FILE" ]; then
- log_error "配置文件 $CONFIG_FILE 不存在"
- log_info "请确保配置文件在当前目录中"
- exit 1
- fi
-
- log_success "找到配置文件: $CONFIG_FILE"
- }
- # 检测nginx配置目录结构
- detect_nginx_structure() {
- log_info "检测nginx配置目录结构..."
-
- # 获取nginx配置目录
- NGINX_CONF_DIR=$(nginx -t 2>&1 | grep "nginx.conf" | head -1 | sed 's/.*test is successful//' | sed 's/nginx: configuration file //' | sed 's/ test is successful//' | xargs dirname)
-
- if [ -z "$NGINX_CONF_DIR" ]; then
- NGINX_CONF_DIR="/etc/nginx"
- fi
-
- log_info "nginx配置目录: $NGINX_CONF_DIR"
-
- # 检查目录结构
- if [ -d "$NGINX_CONF_DIR/sites-available" ] && [ -d "$NGINX_CONF_DIR/sites-enabled" ]; then
- CONFIG_METHOD="sites"
- log_info "检测到 sites-available/sites-enabled 结构(Ubuntu/Debian风格)"
- elif [ -d "$NGINX_CONF_DIR/conf.d" ]; then
- CONFIG_METHOD="conf.d"
- log_info "检测到 conf.d 结构(CentOS/RHEL风格)"
- else
- CONFIG_METHOD="vhosts"
- log_warning "未检测到标准目录结构,将创建vhosts目录"
- fi
- }
- # 备份现有配置
- backup_existing_config() {
- local target_file=""
-
- case $CONFIG_METHOD in
- "sites")
- target_file="$NGINX_CONF_DIR/sites-available/$CONFIG_FILE"
- ;;
- "conf.d")
- target_file="$NGINX_CONF_DIR/conf.d/$CONFIG_FILE"
- ;;
- "vhosts")
- target_file="$NGINX_CONF_DIR/vhosts/$CONFIG_FILE"
- ;;
- esac
-
- if [ -f "$target_file" ]; then
- log_warning "发现现有配置文件,创建备份"
- $SUDO cp "$target_file" "$target_file.backup.$(date +%Y%m%d_%H%M%S)"
- log_success "备份完成: $target_file.backup.$(date +%Y%m%d_%H%M%S)"
- fi
- }
- # 安装配置文件
- install_config() {
- log_info "开始安装nginx配置..."
-
- case $CONFIG_METHOD in
- "sites")
- install_sites_method
- ;;
- "conf.d")
- install_conf_d_method
- ;;
- "vhosts")
- install_vhosts_method
- ;;
- esac
- }
- # sites-available/sites-enabled 方法
- install_sites_method() {
- log_info "使用 sites-available/sites-enabled 方法安装配置"
-
- # 复制配置文件到sites-available
- $SUDO cp "$CONFIG_FILE" "$NGINX_CONF_DIR/sites-available/"
- log_success "配置文件已复制到 $NGINX_CONF_DIR/sites-available/"
-
- # 创建软链接到sites-enabled
- $SUDO ln -sf "$NGINX_CONF_DIR/sites-available/$CONFIG_FILE" "$NGINX_CONF_DIR/sites-enabled/"
- log_success "已创建软链接到 $NGINX_CONF_DIR/sites-enabled/"
-
- # 检查主配置文件是否包含sites-enabled
- if ! grep -q "sites-enabled" "$NGINX_CONF_DIR/nginx.conf"; then
- log_warning "主配置文件可能未包含sites-enabled目录"
- log_info "请确保 $NGINX_CONF_DIR/nginx.conf 中包含:"
- echo " include $NGINX_CONF_DIR/sites-enabled/*;"
- fi
- }
- # conf.d 方法
- install_conf_d_method() {
- log_info "使用 conf.d 方法安装配置"
-
- # 复制配置文件到conf.d
- $SUDO cp "$CONFIG_FILE" "$NGINX_CONF_DIR/conf.d/"
- log_success "配置文件已复制到 $NGINX_CONF_DIR/conf.d/"
-
- # 检查主配置文件是否包含conf.d
- if ! grep -q "conf.d" "$NGINX_CONF_DIR/nginx.conf"; then
- log_warning "主配置文件可能未包含conf.d目录"
- log_info "请确保 $NGINX_CONF_DIR/nginx.conf 中包含:"
- echo " include $NGINX_CONF_DIR/conf.d/*.conf;"
- fi
- }
- # vhosts 方法
- install_vhosts_method() {
- log_info "使用 vhosts 方法安装配置"
-
- # 创建vhosts目录
- $SUDO mkdir -p "$NGINX_CONF_DIR/vhosts"
-
- # 复制配置文件到vhosts
- $SUDO cp "$CONFIG_FILE" "$NGINX_CONF_DIR/vhosts/"
- log_success "配置文件已复制到 $NGINX_CONF_DIR/vhosts/"
-
- # 检查主配置文件是否包含vhosts
- if ! grep -q "vhosts/$CONFIG_FILE" "$NGINX_CONF_DIR/nginx.conf"; then
- log_warning "需要手动在主配置文件中添加include指令"
- log_info "请在 $NGINX_CONF_DIR/nginx.conf 的http块中添加:"
- echo " include $NGINX_CONF_DIR/vhosts/$CONFIG_FILE;"
-
- # 提供自动添加选项
- read -p "是否自动添加到nginx.conf中?(y/n): " -n 1 -r
- echo
- if [[ $REPLY =~ ^[Yy]$ ]]; then
- add_include_to_main_config
- fi
- fi
- }
- # 自动添加include到主配置文件
- add_include_to_main_config() {
- log_info "自动添加include指令到主配置文件"
-
- # 创建备份
- $SUDO cp "$NGINX_CONF_DIR/nginx.conf" "$NGINX_CONF_DIR/nginx.conf.backup.$(date +%Y%m%d_%H%M%S)"
-
- # 在http块的最后添加include
- $SUDO sed -i '/http {/,/}/ { /}/i\ # Include vhost configurations\n include /etc/nginx/vhosts/*.conf;
- }' "$NGINX_CONF_DIR/nginx.conf"
-
- log_success "include指令已添加到主配置文件"
- }
- # 创建网站目录
- create_web_directory() {
- log_info "创建网站目录..."
-
- WEB_ROOT="/root/hzdyjsweb9091/dist"
-
- if [ ! -d "$WEB_ROOT" ]; then
- $SUDO mkdir -p "$WEB_ROOT"
- log_success "已创建网站目录: $WEB_ROOT"
- else
- log_info "网站目录已存在: $WEB_ROOT"
- fi
-
- # 设置适当的权限
- $SUDO chown -R www-data:www-data "$WEB_ROOT" 2>/dev/null || $SUDO chown -R nginx:nginx "$WEB_ROOT" 2>/dev/null || true
- $SUDO chmod -R 755 "$WEB_ROOT"
- log_success "已设置目录权限"
- }
- # 测试nginx配置
- test_nginx_config() {
- log_info "测试nginx配置..."
-
- if $SUDO nginx -t; then
- log_success "nginx配置测试通过"
- return 0
- else
- log_error "nginx配置测试失败"
- return 1
- fi
- }
- # 重载nginx
- reload_nginx() {
- log_info "重载nginx配置..."
-
- if $SUDO nginx -s reload; then
- log_success "nginx配置重载成功"
- return 0
- else
- log_error "nginx配置重载失败"
- return 1
- fi
- }
- # 验证安装
- verify_installation() {
- log_info "验证安装..."
-
- # 检查端口监听
- if netstat -tlnp 2>/dev/null | grep -q ":80 "; then
- log_success "nginx正在监听80端口"
- else
- log_warning "nginx可能未监听80端口"
- fi
-
- # 检查配置是否被加载
- if $SUDO nginx -T 2>/dev/null | grep -q "server_name $SITE_NAME"; then
- log_success "站点配置已加载"
- else
- log_warning "站点配置可能未被加载"
- fi
-
- log_info "请配置DNS解析或hosts文件指向您的服务器IP"
- log_info "然后访问: http://$SITE_NAME"
- }
- # 显示管理命令
- show_management_commands() {
- echo
- log_info "管理命令:"
-
- case $CONFIG_METHOD in
- "sites")
- echo "启用站点: sudo ln -s $NGINX_CONF_DIR/sites-available/$CONFIG_FILE $NGINX_CONF_DIR/sites-enabled/"
- echo "禁用站点: sudo rm $NGINX_CONF_DIR/sites-enabled/$CONFIG_FILE"
- echo "列出可用站点: ls $NGINX_CONF_DIR/sites-available/"
- echo "列出已启用站点: ls $NGINX_CONF_DIR/sites-enabled/"
- ;;
- "conf.d")
- echo "禁用站点: sudo mv $NGINX_CONF_DIR/conf.d/$CONFIG_FILE $NGINX_CONF_DIR/conf.d/$CONFIG_FILE.disabled"
- echo "启用站点: sudo mv $NGINX_CONF_DIR/conf.d/$CONFIG_FILE.disabled $NGINX_CONF_DIR/conf.d/$CONFIG_FILE"
- ;;
- "vhosts")
- echo "编辑配置: sudo nano $NGINX_CONF_DIR/vhosts/$CONFIG_FILE"
- echo "禁用站点: 需要从主配置文件中注释include行"
- ;;
- esac
-
- echo
- echo "通用命令:"
- echo "测试配置: sudo nginx -t"
- echo "重载配置: sudo nginx -s reload"
- echo "查看错误日志: sudo tail -f /var/log/nginx/error.log"
- echo "查看站点日志: sudo tail -f /var/log/nginx/$SITE_NAME.access.log"
- }
- # 主函数
- main() {
- echo -e "${BLUE}================================${NC}"
- echo -e "${BLUE} Nginx站点配置自动安装脚本${NC}"
- echo -e "${BLUE}================================${NC}"
- echo
-
- # 执行检查和安装步骤
- check_root
- check_nginx
- check_config_file
- detect_nginx_structure
- backup_existing_config
- install_config
- create_web_directory
-
- # 测试和重载
- if test_nginx_config; then
- if reload_nginx; then
- verify_installation
- show_management_commands
- log_success "安装完成!"
- else
- log_error "nginx重载失败,请检查配置"
- exit 1
- fi
- else
- log_error "nginx配置测试失败,安装中止"
- exit 1
- fi
- }
- # 运行主函数
- main "$@"
|