nginx-setup.sh 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #!/bin/bash
  2. # Nginx配置自动安装脚本
  3. # 支持自动检测系统类型并选择合适的配置方法
  4. CONFIG_FILE="hzdyjsweb.camasort.com.conf"
  5. SITE_NAME="hzdyjsweb.camasort.com"
  6. # 颜色定义
  7. RED='\033[0;31m'
  8. GREEN='\033[0;32m'
  9. YELLOW='\033[1;33m'
  10. BLUE='\033[0;34m'
  11. NC='\033[0m' # No Color
  12. # 日志函数
  13. log_info() {
  14. echo -e "${BLUE}[INFO]${NC} $1"
  15. }
  16. log_success() {
  17. echo -e "${GREEN}[SUCCESS]${NC} $1"
  18. }
  19. log_warning() {
  20. echo -e "${YELLOW}[WARNING]${NC} $1"
  21. }
  22. log_error() {
  23. echo -e "${RED}[ERROR]${NC} $1"
  24. }
  25. # 检查是否为root用户
  26. check_root() {
  27. if [[ $EUID -eq 0 ]]; then
  28. log_warning "检测到root用户,直接执行操作"
  29. SUDO=""
  30. else
  31. log_info "检测到普通用户,将使用sudo执行操作"
  32. SUDO="sudo"
  33. fi
  34. }
  35. # 检查nginx是否已安装
  36. check_nginx() {
  37. if ! command -v nginx &> /dev/null; then
  38. log_error "nginx未安装,请先安装nginx"
  39. echo "Ubuntu/Debian: sudo apt update && sudo apt install nginx"
  40. echo "CentOS/RHEL: sudo yum install nginx 或 sudo dnf install nginx"
  41. exit 1
  42. fi
  43. log_success "nginx已安装"
  44. nginx -v
  45. }
  46. # 检查配置文件是否存在
  47. check_config_file() {
  48. if [ ! -f "$CONFIG_FILE" ]; then
  49. log_error "配置文件 $CONFIG_FILE 不存在"
  50. log_info "请确保配置文件在当前目录中"
  51. exit 1
  52. fi
  53. log_success "找到配置文件: $CONFIG_FILE"
  54. }
  55. # 检测nginx配置目录结构
  56. detect_nginx_structure() {
  57. log_info "检测nginx配置目录结构..."
  58. # 获取nginx配置目录
  59. 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)
  60. if [ -z "$NGINX_CONF_DIR" ]; then
  61. NGINX_CONF_DIR="/etc/nginx"
  62. fi
  63. log_info "nginx配置目录: $NGINX_CONF_DIR"
  64. # 检查目录结构
  65. if [ -d "$NGINX_CONF_DIR/sites-available" ] && [ -d "$NGINX_CONF_DIR/sites-enabled" ]; then
  66. CONFIG_METHOD="sites"
  67. log_info "检测到 sites-available/sites-enabled 结构(Ubuntu/Debian风格)"
  68. elif [ -d "$NGINX_CONF_DIR/conf.d" ]; then
  69. CONFIG_METHOD="conf.d"
  70. log_info "检测到 conf.d 结构(CentOS/RHEL风格)"
  71. else
  72. CONFIG_METHOD="vhosts"
  73. log_warning "未检测到标准目录结构,将创建vhosts目录"
  74. fi
  75. }
  76. # 备份现有配置
  77. backup_existing_config() {
  78. local target_file=""
  79. case $CONFIG_METHOD in
  80. "sites")
  81. target_file="$NGINX_CONF_DIR/sites-available/$CONFIG_FILE"
  82. ;;
  83. "conf.d")
  84. target_file="$NGINX_CONF_DIR/conf.d/$CONFIG_FILE"
  85. ;;
  86. "vhosts")
  87. target_file="$NGINX_CONF_DIR/vhosts/$CONFIG_FILE"
  88. ;;
  89. esac
  90. if [ -f "$target_file" ]; then
  91. log_warning "发现现有配置文件,创建备份"
  92. $SUDO cp "$target_file" "$target_file.backup.$(date +%Y%m%d_%H%M%S)"
  93. log_success "备份完成: $target_file.backup.$(date +%Y%m%d_%H%M%S)"
  94. fi
  95. }
  96. # 安装配置文件
  97. install_config() {
  98. log_info "开始安装nginx配置..."
  99. case $CONFIG_METHOD in
  100. "sites")
  101. install_sites_method
  102. ;;
  103. "conf.d")
  104. install_conf_d_method
  105. ;;
  106. "vhosts")
  107. install_vhosts_method
  108. ;;
  109. esac
  110. }
  111. # sites-available/sites-enabled 方法
  112. install_sites_method() {
  113. log_info "使用 sites-available/sites-enabled 方法安装配置"
  114. # 复制配置文件到sites-available
  115. $SUDO cp "$CONFIG_FILE" "$NGINX_CONF_DIR/sites-available/"
  116. log_success "配置文件已复制到 $NGINX_CONF_DIR/sites-available/"
  117. # 创建软链接到sites-enabled
  118. $SUDO ln -sf "$NGINX_CONF_DIR/sites-available/$CONFIG_FILE" "$NGINX_CONF_DIR/sites-enabled/"
  119. log_success "已创建软链接到 $NGINX_CONF_DIR/sites-enabled/"
  120. # 检查主配置文件是否包含sites-enabled
  121. if ! grep -q "sites-enabled" "$NGINX_CONF_DIR/nginx.conf"; then
  122. log_warning "主配置文件可能未包含sites-enabled目录"
  123. log_info "请确保 $NGINX_CONF_DIR/nginx.conf 中包含:"
  124. echo " include $NGINX_CONF_DIR/sites-enabled/*;"
  125. fi
  126. }
  127. # conf.d 方法
  128. install_conf_d_method() {
  129. log_info "使用 conf.d 方法安装配置"
  130. # 复制配置文件到conf.d
  131. $SUDO cp "$CONFIG_FILE" "$NGINX_CONF_DIR/conf.d/"
  132. log_success "配置文件已复制到 $NGINX_CONF_DIR/conf.d/"
  133. # 检查主配置文件是否包含conf.d
  134. if ! grep -q "conf.d" "$NGINX_CONF_DIR/nginx.conf"; then
  135. log_warning "主配置文件可能未包含conf.d目录"
  136. log_info "请确保 $NGINX_CONF_DIR/nginx.conf 中包含:"
  137. echo " include $NGINX_CONF_DIR/conf.d/*.conf;"
  138. fi
  139. }
  140. # vhosts 方法
  141. install_vhosts_method() {
  142. log_info "使用 vhosts 方法安装配置"
  143. # 创建vhosts目录
  144. $SUDO mkdir -p "$NGINX_CONF_DIR/vhosts"
  145. # 复制配置文件到vhosts
  146. $SUDO cp "$CONFIG_FILE" "$NGINX_CONF_DIR/vhosts/"
  147. log_success "配置文件已复制到 $NGINX_CONF_DIR/vhosts/"
  148. # 检查主配置文件是否包含vhosts
  149. if ! grep -q "vhosts/$CONFIG_FILE" "$NGINX_CONF_DIR/nginx.conf"; then
  150. log_warning "需要手动在主配置文件中添加include指令"
  151. log_info "请在 $NGINX_CONF_DIR/nginx.conf 的http块中添加:"
  152. echo " include $NGINX_CONF_DIR/vhosts/$CONFIG_FILE;"
  153. # 提供自动添加选项
  154. read -p "是否自动添加到nginx.conf中?(y/n): " -n 1 -r
  155. echo
  156. if [[ $REPLY =~ ^[Yy]$ ]]; then
  157. add_include_to_main_config
  158. fi
  159. fi
  160. }
  161. # 自动添加include到主配置文件
  162. add_include_to_main_config() {
  163. log_info "自动添加include指令到主配置文件"
  164. # 创建备份
  165. $SUDO cp "$NGINX_CONF_DIR/nginx.conf" "$NGINX_CONF_DIR/nginx.conf.backup.$(date +%Y%m%d_%H%M%S)"
  166. # 在http块的最后添加include
  167. $SUDO sed -i '/http {/,/}/ { /}/i\ # Include vhost configurations\n include /etc/nginx/vhosts/*.conf;
  168. }' "$NGINX_CONF_DIR/nginx.conf"
  169. log_success "include指令已添加到主配置文件"
  170. }
  171. # 创建网站目录
  172. create_web_directory() {
  173. log_info "创建网站目录..."
  174. WEB_ROOT="/root/hzdyjsweb9091/dist"
  175. if [ ! -d "$WEB_ROOT" ]; then
  176. $SUDO mkdir -p "$WEB_ROOT"
  177. log_success "已创建网站目录: $WEB_ROOT"
  178. else
  179. log_info "网站目录已存在: $WEB_ROOT"
  180. fi
  181. # 设置适当的权限
  182. $SUDO chown -R www-data:www-data "$WEB_ROOT" 2>/dev/null || $SUDO chown -R nginx:nginx "$WEB_ROOT" 2>/dev/null || true
  183. $SUDO chmod -R 755 "$WEB_ROOT"
  184. log_success "已设置目录权限"
  185. }
  186. # 测试nginx配置
  187. test_nginx_config() {
  188. log_info "测试nginx配置..."
  189. if $SUDO nginx -t; then
  190. log_success "nginx配置测试通过"
  191. return 0
  192. else
  193. log_error "nginx配置测试失败"
  194. return 1
  195. fi
  196. }
  197. # 重载nginx
  198. reload_nginx() {
  199. log_info "重载nginx配置..."
  200. if $SUDO nginx -s reload; then
  201. log_success "nginx配置重载成功"
  202. return 0
  203. else
  204. log_error "nginx配置重载失败"
  205. return 1
  206. fi
  207. }
  208. # 验证安装
  209. verify_installation() {
  210. log_info "验证安装..."
  211. # 检查端口监听
  212. if netstat -tlnp 2>/dev/null | grep -q ":80 "; then
  213. log_success "nginx正在监听80端口"
  214. else
  215. log_warning "nginx可能未监听80端口"
  216. fi
  217. # 检查配置是否被加载
  218. if $SUDO nginx -T 2>/dev/null | grep -q "server_name $SITE_NAME"; then
  219. log_success "站点配置已加载"
  220. else
  221. log_warning "站点配置可能未被加载"
  222. fi
  223. log_info "请配置DNS解析或hosts文件指向您的服务器IP"
  224. log_info "然后访问: http://$SITE_NAME"
  225. }
  226. # 显示管理命令
  227. show_management_commands() {
  228. echo
  229. log_info "管理命令:"
  230. case $CONFIG_METHOD in
  231. "sites")
  232. echo "启用站点: sudo ln -s $NGINX_CONF_DIR/sites-available/$CONFIG_FILE $NGINX_CONF_DIR/sites-enabled/"
  233. echo "禁用站点: sudo rm $NGINX_CONF_DIR/sites-enabled/$CONFIG_FILE"
  234. echo "列出可用站点: ls $NGINX_CONF_DIR/sites-available/"
  235. echo "列出已启用站点: ls $NGINX_CONF_DIR/sites-enabled/"
  236. ;;
  237. "conf.d")
  238. echo "禁用站点: sudo mv $NGINX_CONF_DIR/conf.d/$CONFIG_FILE $NGINX_CONF_DIR/conf.d/$CONFIG_FILE.disabled"
  239. echo "启用站点: sudo mv $NGINX_CONF_DIR/conf.d/$CONFIG_FILE.disabled $NGINX_CONF_DIR/conf.d/$CONFIG_FILE"
  240. ;;
  241. "vhosts")
  242. echo "编辑配置: sudo nano $NGINX_CONF_DIR/vhosts/$CONFIG_FILE"
  243. echo "禁用站点: 需要从主配置文件中注释include行"
  244. ;;
  245. esac
  246. echo
  247. echo "通用命令:"
  248. echo "测试配置: sudo nginx -t"
  249. echo "重载配置: sudo nginx -s reload"
  250. echo "查看错误日志: sudo tail -f /var/log/nginx/error.log"
  251. echo "查看站点日志: sudo tail -f /var/log/nginx/$SITE_NAME.access.log"
  252. }
  253. # 主函数
  254. main() {
  255. echo -e "${BLUE}================================${NC}"
  256. echo -e "${BLUE} Nginx站点配置自动安装脚本${NC}"
  257. echo -e "${BLUE}================================${NC}"
  258. echo
  259. # 执行检查和安装步骤
  260. check_root
  261. check_nginx
  262. check_config_file
  263. detect_nginx_structure
  264. backup_existing_config
  265. install_config
  266. create_web_directory
  267. # 测试和重载
  268. if test_nginx_config; then
  269. if reload_nginx; then
  270. verify_installation
  271. show_management_commands
  272. log_success "安装完成!"
  273. else
  274. log_error "nginx重载失败,请检查配置"
  275. exit 1
  276. fi
  277. else
  278. log_error "nginx配置测试失败,安装中止"
  279. exit 1
  280. fi
  281. }
  282. # 运行主函数
  283. main "$@"