├── README.md ├── docs ├── nginx-virtual-host-bash-script-help.png └── nginx-virtual-host-bash-script-result.png └── src └── vhost-nginx.sh /README.md: -------------------------------------------------------------------------------- 1 | # Nginx Virtual Host Creator (Magento 1, Magento 2, Laravel, WordPress) 2 | 3 | This Script creates Nginx virtual host for different applications. 4 | Some of the supported applications are: 5 | - Magento 1 6 | - Magento 2 7 | - WordPress 8 | - Laravel 9 | - Html 10 | - ~~Others~~ 11 | 12 | 13 | ## INSTALL 14 | To install, simply download the script file and give it the executable permission. 15 | ``` 16 | curl -0 https://raw.githubusercontent.com/MagePsycho/nginx-virtual-host-bash-script/master/src/vhost-nginx.sh -o vhost-nginx.sh 17 | chmod +x vhost-nginx.sh 18 | ``` 19 | 20 | To make it system wide command 21 | ``` 22 | sudo mv vhost-nginx.sh /usr/local/bin/vhost-nginx 23 | ``` 24 | OR 25 | (may not work with `sudo`) 26 | ``` 27 | mv vhost-nginx.sh ~/bin/vhost-nginx 28 | ``` 29 | Make sure your `$HOME/bin` folder is in executable path 30 | 31 | ## USAGE 32 | ### To display help 33 | ``` 34 | sudo ./vhost-nginx.sh --help 35 | ``` 36 | ### To Create Virtual Host for Magento 1 37 | ``` 38 | sudo ./vhost-nginx.sh --domain=magento1938.test --app=magento1 --root-dir=/var/www/magento1/magento1938 39 | ``` 40 | 41 | ### To Create Virtual Host for Magento 2 42 | ``` 43 | sudo ./vhost-nginx.sh --domain=magento223ce.test --app=magento2 --root-dir=/var/www/magento2/magento223ce 44 | ``` 45 | 46 | ### To Create Virtual Host for WordPress 47 | ``` 48 | sudo ./vhost-nginx.sh --domain=wordpress494.test --app=wordpress --root-dir=/var/www/wordpress/wordpress494 49 | ``` 50 | 51 | ### To Create Virtual Host for Laravel 52 | ``` 53 | sudo ./vhost-nginx.sh --domain=laravel560.test --app=laravel --root-dir=/var/www/laravel/laravel560 54 | ``` 55 | 56 | ### To Create Virtual Host for Default (Html) 57 | ``` 58 | sudo ./vhost-nginx.sh --domain=website.test --app=laravel --root-dir=/var/www/html/website 59 | ``` 60 | 61 | **Notes** 62 | - In case of system-wide command, you can omit the `--root-dir` parameter if you run the command from the root directory of application. 63 | 64 | ## Screenshots 65 | ![Nginx Virtual Host Creator Help](https://github.com/MagePsycho/nginx-virtual-host-bash-script/raw/master/docs/nginx-virtual-host-bash-script-help.png "Nginx Virtual Host Creator Help") 66 | Screentshot - Nginx Virtual Host Creator Help 67 | 68 | ![Nginx Virtual Host Creator Result](https://github.com/MagePsycho/nginx-virtual-host-bash-script/raw/master/docs/nginx-virtual-host-bash-script-result.png "Nginx Virtual Host Creator Result") 69 | Screentshot - Nginx Virtual Host Creator Result 70 | 71 | ## RoadMap 72 | - [ ] To Support multiple applications: 73 | - [x] Magento 1 74 | - [x] Magento 2 75 | - [x] WordPress 76 | - [x] Laravel 77 | - [x] Html 78 | - [ ] OroCrm 79 | - [ ] OroCommerce 80 | - etc. 81 | - [ ] Flexible settings for Nginx 82 | - fastcgi_pass: tcp port (127.0.0.1:9000) or unix socket (/var/run/php-fpm.sock) 83 | - [ ] Option to configure virtual host template from separate file. 84 | - [ ] Option to add SSL configuration 85 | -------------------------------------------------------------------------------- /docs/nginx-virtual-host-bash-script-help.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagePsycho/nginx-virtual-host-bash-script/a23b525daa4511a76bde6bf6ec05f3c7886dd601/docs/nginx-virtual-host-bash-script-help.png -------------------------------------------------------------------------------- /docs/nginx-virtual-host-bash-script-result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagePsycho/nginx-virtual-host-bash-script/a23b525daa4511a76bde6bf6ec05f3c7886dd601/docs/nginx-virtual-host-bash-script-result.png -------------------------------------------------------------------------------- /src/vhost-nginx.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # 4 | # Script to create virtual host for Nginx server 5 | # 6 | # @author Raj KB 7 | # @website http://www.magepsycho.com 8 | # @version 1.2.0 9 | 10 | # UnComment it if bash is lower than 4.x version 11 | shopt -s extglob 12 | 13 | ################################################################################ 14 | # CORE FUNCTIONS - Do not edit 15 | ################################################################################ 16 | 17 | ## Uncomment it for debugging purpose 18 | ###set -o errexit 19 | #set -o pipefail 20 | #set -o nounset 21 | #set -o xtrace 22 | 23 | # 24 | # VARIABLES 25 | # 26 | _bold=$(tput bold) 27 | _underline=$(tput sgr 0 1) 28 | _reset=$(tput sgr0) 29 | 30 | _purple=$(tput setaf 171) 31 | _red=$(tput setaf 1) 32 | _green=$(tput setaf 76) 33 | _tan=$(tput setaf 3) 34 | _blue=$(tput setaf 38) 35 | 36 | # 37 | # HEADERS & LOGGING 38 | # 39 | function _debug() 40 | { 41 | if [[ "$DEBUG" = 1 ]]; then 42 | "$@" 43 | fi 44 | } 45 | 46 | function _header() 47 | { 48 | printf '\n%s%s========== %s ==========%s\n' "$_bold" "$_purple" "$@" "$_reset" 49 | } 50 | 51 | function _arrow() 52 | { 53 | printf '➜ %s\n' "$@" 54 | } 55 | 56 | function _success() 57 | { 58 | printf '%s✔ %s%s\n' "$_green" "$@" "$_reset" 59 | } 60 | 61 | function _error() { 62 | printf '%s✖ %s%s\n' "$_red" "$@" "$_reset" 63 | } 64 | 65 | function _warning() 66 | { 67 | printf '%s➜ %s%s\n' "$_tan" "$@" "$_reset" 68 | } 69 | 70 | function _underline() 71 | { 72 | printf '%s%s%s%s\n' "$_underline" "$_bold" "$@" "$_reset" 73 | } 74 | 75 | function _bold() 76 | { 77 | printf '%s%s%s\n' "$_bold" "$@" "$_reset" 78 | } 79 | 80 | function _note() 81 | { 82 | printf '%s%s%sNote:%s %s%s%s\n' "$_underline" "$_bold" "$_blue" "$_reset" "$_blue" "$@" "$_reset" 83 | } 84 | 85 | function _die() 86 | { 87 | _error "$@" 88 | exit 1 89 | } 90 | 91 | function _safeExit() 92 | { 93 | exit 0 94 | } 95 | 96 | # 97 | # UTILITY HELPER 98 | # 99 | function _seekConfirmation() 100 | { 101 | printf '\n%s%s%s' "$_bold" "$@" "$_reset" 102 | read -p " (y/n) " -n 1 103 | printf '\n' 104 | } 105 | 106 | # Test whether the result of an 'ask' is a confirmation 107 | function _isConfirmed() 108 | { 109 | if [[ "$REPLY" =~ ^[Yy]$ ]]; then 110 | return 0 111 | fi 112 | return 1 113 | } 114 | 115 | 116 | function _typeExists() 117 | { 118 | if type "$1" >/dev/null; then 119 | return 0 120 | fi 121 | return 1 122 | } 123 | 124 | function _isOs() 125 | { 126 | if [[ "${OSTYPE}" == $1* ]]; then 127 | return 0 128 | fi 129 | return 1 130 | } 131 | 132 | function _isOsDebian() 133 | { 134 | if [[ -f /etc/debian_version ]]; then 135 | return 0 136 | else 137 | return 1 138 | fi 139 | } 140 | 141 | function _isOsRedHat() 142 | { 143 | if [[ -f /etc/redhat-release ]]; then 144 | return 0 145 | else 146 | return 1 147 | fi 148 | } 149 | 150 | function _isOsMac() 151 | { 152 | if [[ "$(uname -s)" = "Darwin" ]]; then 153 | return 0 154 | else 155 | return 1 156 | fi 157 | } 158 | 159 | function _checkRootUser() 160 | { 161 | #if [ "$(id -u)" != "0" ]; then 162 | if [ "$(whoami)" != 'root' ]; then 163 | _die "You cannot run $0 as non-root user. Please use sudo $0" 164 | fi 165 | } 166 | 167 | function _printPoweredBy() 168 | { 169 | local mp_ascii 170 | mp_ascii=' 171 | __ ___ ___ __ 172 | / |/ /__ ____ ____ / _ \___ __ ______/ / ___ 173 | / /|_/ / _ `/ _ `/ -_) ___(_-> Store: ${_reset}${_underline}${_blue}http://www.magepsycho.com${_reset}${_reset}${_green} 183 | >> Blog: ${_reset}${_underline}${_blue}http://www.blog.magepsycho.com${_reset}${_reset}${_green} 184 | 185 | ################################################################ 186 | ${_reset} 187 | EOF 188 | } 189 | 190 | ################################################################################ 191 | # SCRIPT FUNCTIONS 192 | ################################################################################ 193 | function _printUsage() 194 | { 195 | echo -n "$(basename "$0") [OPTION]... 196 | 197 | Nginx Virtual Host Creator 198 | Version $VERSION 199 | 200 | Options: 201 | --domain Server Name 202 | --root-dir Application Root Directory. Default: current (pwd) 203 | --app Application Name (magento1|magento2|wordpress|laravel|default). 204 | -d, --debug Run command in debug mode 205 | -h, --help Display this help and exit 206 | 207 | Examples: 208 | $(basename "$0") --domain=... --app=... [--root-dir=...] [--debug] 209 | 210 | " 211 | _printPoweredBy 212 | exit 1 213 | } 214 | 215 | function processArgs() 216 | { 217 | # Parse Arguments 218 | for arg in "$@" 219 | do 220 | case $arg in 221 | --domain=*) 222 | VHOST_DOMAIN="${arg#*=}" 223 | ;; 224 | --root-dir=*) 225 | VHOST_ROOT_DIR="${arg#*=}" 226 | ;; 227 | --app=*) 228 | APP_TYPE="${arg#*=}" 229 | ;; 230 | --debug) 231 | DEBUG=1 232 | set -o xtrace 233 | ;; 234 | -h|--help) 235 | _printUsage 236 | ;; 237 | *) 238 | _printUsage 239 | ;; 240 | esac 241 | done 242 | 243 | validateArgs 244 | sanitizeArgs 245 | } 246 | 247 | function initDefaultArgs() 248 | { 249 | VHOST_ROOT_DIR=$(pwd) 250 | NGINX_SITES_ENABLED_FILE= 251 | NGINX_SITES_AVAILABLE_FILE= 252 | 253 | if _isOsMac; then 254 | NGINX_SITES_ENABLED_DIR='/usr/local/etc/nginx/sites-enabled' 255 | NGINX_SITES_AVAILABLE_DIR='/usr/local/etc/nginx/sites-available' 256 | else 257 | NGINX_SITES_ENABLED_DIR='/etc/nginx/sites-enabled' 258 | NGINX_SITES_AVAILABLE_DIR='/etc/nginx/sites-available' 259 | fi 260 | } 261 | 262 | function validateArgs() 263 | { 264 | ERROR_COUNT=0 265 | if [[ -z "$VHOST_DOMAIN" ]]; then 266 | _error "--domain=... parameter is missing." 267 | ERROR_COUNT=$((ERROR_COUNT + 1)) 268 | fi 269 | if [[ -z "$APP_TYPE" ]]; then 270 | _error "--app=... parameter is missing." 271 | ERROR_COUNT=$((ERROR_COUNT + 1)) 272 | fi 273 | if [[ ! -z "$APP_TYPE" && "$APP_TYPE" != @(magento1|magento2|wordpress|laravel|default) ]]; then 274 | _error "Please enter valid application name for --app=... parameter(magento1|magento2|wordpress|laravel|default)." 275 | ERROR_COUNT=$((ERROR_COUNT + 1)) 276 | fi 277 | if [[ ! -d "$VHOST_ROOT_DIR" ]]; then 278 | _error "--root-dir =...parameter is not valid." 279 | ERROR_COUNT=$((ERROR_COUNT + 1)) 280 | fi 281 | if [[ ! -d "$NGINX_SITES_ENABLED_DIR" ]]; then 282 | _error "Nginx sites-enabled directory: ${NGINX_SITES_ENABLED_DIR} doesn't exist." 283 | ERROR_COUNT=$((ERROR_COUNT + 1)) 284 | fi 285 | if [[ ! -d "$NGINX_SITES_AVAILABLE_DIR" ]]; then 286 | _error "Nginx sites-available directory: ${NGINX_SITES_AVAILABLE_DIR} doesn't exist." 287 | ERROR_COUNT=$((ERROR_COUNT + 1)) 288 | fi 289 | if [[ -f "${NGINX_SITES_AVAILABLE_DIR}/${VHOST_DOMAIN}.conf" ]]; then 290 | _error "Vhost file already exists: ${NGINX_SITES_AVAILABLE_DIR}/${VHOST_DOMAIN}.conf." 291 | ERROR_COUNT=$((ERROR_COUNT + 1)) 292 | fi 293 | 294 | [[ "$ERROR_COUNT" -gt 0 ]] && exit 1 295 | } 296 | 297 | function sanitizeArgs() 298 | { 299 | # remove trailing / 300 | if [[ ! -z "$VHOST_ROOT_DIR" ]]; then 301 | VHOST_ROOT_DIR="${VHOST_ROOT_DIR%/}" 302 | fi 303 | if [[ ! -z "$VHOST_DOMAIN" ]] && [[ "$VHOST_DOMAIN" == http* ]]; then 304 | VHOST_DOMAIN=$(getPureDomain) 305 | fi 306 | } 307 | 308 | function getPureDomain() 309 | { 310 | echo "$VHOST_DOMAIN" | awk -F'[:\\/]' '{print $4}' 311 | } 312 | 313 | function checkCmdDependencies() 314 | { 315 | local _dependencies=( 316 | nginx 317 | wget 318 | cat 319 | basename 320 | mkdir 321 | cp 322 | mv 323 | rm 324 | chown 325 | chmod 326 | date 327 | find 328 | awk 329 | ) 330 | 331 | for cmd in "${_dependencies[@]}" 332 | do 333 | hash "${cmd}" &>/dev/null || _die "'${cmd}' command not found." 334 | done; 335 | } 336 | 337 | function createVirtualHost() 338 | { 339 | # @todo magento | default 340 | 341 | _arrow "Virtual host creation for Nginx started..." 342 | 343 | _arrow "Changing current working directory to ${VHOST_ROOT_DIR}..." 344 | cd "$VHOST_ROOT_DIR" || _die "Couldn't change current working directory to : ${VHOST_ROOT_DIR}." 345 | _success "Done" 346 | 347 | # Verify the current directory as per application 348 | _arrow "Verifying the current directory is root of ${APP_TYPE}..." 349 | verifyCurrentDirIsAppRoot 350 | _success "Done" 351 | 352 | # Prepare virtual host content as per application 353 | # @todo move it to template based 354 | # @todo add option for https 355 | _arrow "Creating Nginx Vhost File..." 356 | prepareVhostFilePaths 357 | prepareAppVhostContent 358 | createVhostSymlinks 359 | _success "Done" 360 | 361 | # @todo change-ownership 362 | 363 | _arrow "Creating an entry to /etc/hosts file..." 364 | createEtcHostEntry 365 | _success "Done" 366 | 367 | _arrow "Reloading the Nginx configuration..." 368 | reloadNginx 369 | _success "Done" 370 | } 371 | 372 | function createDefaultVhost() 373 | { 374 | #@todo implementation 375 | _die "Vhost for default application not supported yet. Please specify correct --app=... parameter." 376 | } 377 | 378 | function verifyCurrentDirIsAppRoot() 379 | { 380 | if [[ "$APP_TYPE" = 'magento2' ]]; then 381 | verifyCurrentDirIsMage2Root 382 | elif [[ "$APP_TYPE" = 'magento1' ]]; then 383 | verifyCurrentDirIsMage1Root 384 | elif [[ "$APP_TYPE" = 'wordpress' ]]; then 385 | verifyCurrentDirIsWpRoot 386 | elif [[ "$APP_TYPE" = 'laravel' ]]; then 387 | verifyCurrentDirIsLaravelRoot 388 | fi 389 | } 390 | 391 | function verifyCurrentDirIsMage2Root() 392 | { 393 | if [[ ! -f './bin/magento' ]] || [[ ! -f './app/etc/di.xml' ]]; then 394 | _die "Current directory is not Magento2 root. Please specify correct --root-dir=... parameter if you are running command from different directory." 395 | fi 396 | } 397 | 398 | function verifyCurrentDirIsMage1Root() 399 | { 400 | if [[ ! -f './mage' ]] || [[ ! -f './app/etc/local.xml' ]]; then 401 | _die "Current directory is not Magento1 root. Please specify correct --root-dir=... parameter if you are running command from different directory." 402 | fi 403 | } 404 | 405 | function verifyCurrentDirIsWpRoot() 406 | { 407 | if [[ ! -f './wp-config.php' ]]; then 408 | _die "Current directory is not WordPress root. Please specify correct --root-dir=... parameter if you are running command from different directory." 409 | fi 410 | } 411 | 412 | function verifyCurrentDirIsLaravelRoot() 413 | { 414 | if [[ ! -f './bootstrap/app.php' ]]; then 415 | _die "Current directory is not Laravel root. Please specify correct --root-dir=... parameter if you are running command from different directory." 416 | fi 417 | } 418 | 419 | function prepareVhostFilePaths() 420 | { 421 | NGINX_SITES_ENABLED_FILE="${NGINX_SITES_ENABLED_DIR}/${VHOST_DOMAIN}.conf" 422 | NGINX_SITES_AVAILABLE_FILE="${NGINX_SITES_AVAILABLE_DIR}/${VHOST_DOMAIN}.conf" 423 | } 424 | 425 | function prepareAppVhostContent() 426 | { 427 | if [[ "$APP_TYPE" = 'magento2' ]]; then 428 | prepareM2VhostContent 429 | elif [[ "$APP_TYPE" = 'magento1' ]]; then 430 | prepareM1VhostContent 431 | elif [[ "$APP_TYPE" = 'wordpress' ]]; then 432 | prepareWpVhostContent 433 | elif [[ "$APP_TYPE" = 'laravel' ]]; then 434 | prepareLaravelVhostContent 435 | elif [[ "$APP_TYPE" = 'default' ]]; then 436 | prepareDefaultVhostContent 437 | fi 438 | } 439 | 440 | function prepareM1VhostContent() 441 | { 442 | echo "server { 443 | listen 80; 444 | server_name ${VHOST_DOMAIN}; 445 | root ${VHOST_ROOT_DIR}; 446 | 447 | location / { 448 | index index.html index.php; ## Allow a static html file to be shown first 449 | try_files \$uri \$uri/ @handler; ## If missing pass the URI to Magento's front handler 450 | expires 30d; ## Assume all files are cachable 451 | } 452 | 453 | # add this for yii else css js will not be picked up 454 | location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ { 455 | try_files \$uri =404; 456 | } 457 | 458 | ## These locations would be hidden by .htaccess normally 459 | location /app/ { deny all; } 460 | location /includes/ { deny all; } 461 | location /lib/ { deny all; } 462 | location /media/downloadable/ { deny all; } 463 | location /pkginfo/ { deny all; } 464 | location /report/config.xml { deny all; } 465 | location /var/ { deny all; } 466 | 467 | ## Disable .htaccess and other hidden files 468 | location ~ /\. { 469 | deny all; 470 | access_log off; 471 | log_not_found off; 472 | } 473 | 474 | location @handler { ## Magento uses a common front handler 475 | rewrite / /index.php; 476 | } 477 | 478 | location ~ \.php/ { ## Forward paths like /js/index.php/x.js to relevant handler 479 | rewrite ^(.*\.php)/ \$1 last; 480 | } 481 | 482 | location ~ \.php$ { ## Execute PHP scripts 483 | if (!-e \$request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss 484 | 485 | # expires off; ## Do not cache dynamic content 486 | fastcgi_pass 127.0.0.1:9000; 487 | # fastcgi_param HTTPS \$fastcgi_https; 488 | fastcgi_index index.php; 489 | fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; 490 | 491 | fastcgi_param PHP_VALUE \"display_startup_errors=on\"; 492 | fastcgi_param PHP_VALUE \"display_errors=on\"; 493 | fastcgi_param PHP_VALUE \"html_errors=on\"; 494 | fastcgi_param PHP_VALUE \"log_errors=on\"; 495 | fastcgi_param PHP_VALUE \"error_log=${VHOST_ROOT_DIR}/var/log/system.log\"; 496 | fastcgi_param PHP_VALUE \"xdebug.show_exception_trace=0\"; 497 | 498 | #fastcgi_param MAGE_RUN_CODE default; 499 | #fastcgi_param MAGE_RUN_TYPE store; 500 | fastcgi_param MAGE_IS_DEVELOPER_MODE true; 501 | 502 | include fastcgi_params; 503 | fastcgi_read_timeout 300; 504 | } 505 | } 506 | " > "$NGINX_SITES_AVAILABLE_FILE" || _die "Couldn't write to file: ${NGINX_SITES_AVAILABLE_FILE}" 507 | _arrow "${NGINX_SITES_AVAILABLE_FILE} file has been created." 508 | } 509 | 510 | function prepareM2VhostContent() 511 | { 512 | local _nginxFile= 513 | if [[ -f "${VHOST_ROOT_DIR}/nginx.conf" ]]; then 514 | _nginxFile="${VHOST_ROOT_DIR}/nginx.conf" 515 | else 516 | _nginxFile="${VHOST_ROOT_DIR}/nginx.conf.sample" 517 | fi 518 | 519 | echo "#Magento Vars 520 | #set \$MAGE_ROOT ${VHOST_ROOT_DIR}; 521 | #set \$MAGE_MODE default; # or production or developer 522 | 523 | # Example configuration: 524 | #upstream fastcgi_backend { 525 | # # use tcp connection 526 | # server 127.0.0.1:9000; 527 | # # or socket 528 | # server unix:/var/run/php5-fpm.sock; 529 | #} 530 | server { 531 | listen 80; 532 | server_name ${VHOST_DOMAIN}; 533 | set \$MAGE_ROOT ${VHOST_ROOT_DIR}; 534 | set \$MAGE_MODE developer; 535 | include ${_nginxFile}; 536 | } 537 | " > "$NGINX_SITES_AVAILABLE_FILE" || _die "Couldn't write to file: ${NGINX_SITES_AVAILABLE_FILE}" 538 | _arrow "${NGINX_SITES_AVAILABLE_FILE} file has been created." 539 | } 540 | 541 | function prepareWpVhostContent() 542 | { 543 | echo "server { 544 | listen 80; 545 | server_name ${VHOST_DOMAIN}; 546 | root ${VHOST_ROOT_DIR}; 547 | 548 | location / { 549 | index index.html index.php; 550 | try_files \$uri \$uri/ /index.php?\$args; 551 | } 552 | 553 | location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { 554 | expires max; 555 | log_not_found off; 556 | } 557 | 558 | ## Disable .htaccess and other hidden files 559 | location ~ /\. { 560 | deny all; 561 | access_log off; 562 | log_not_found off; 563 | } 564 | 565 | location ~ \.php$ { 566 | if (!-e \$request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss 567 | 568 | # expires off; ## Do not cache dynamic content 569 | fastcgi_pass 127.0.0.1:9000; 570 | fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; 571 | include fastcgi_params; 572 | fastcgi_read_timeout 300; 573 | } 574 | }" > "$NGINX_SITES_AVAILABLE_FILE" || _die "Couldn't write to file: ${NGINX_SITES_AVAILABLE_FILE}" 575 | 576 | _arrow "${NGINX_SITES_AVAILABLE_FILE} file has been created." 577 | } 578 | 579 | function prepareLaravelVhostContent() 580 | { 581 | echo "server { 582 | listen 80; 583 | listen [::]:80 ipv6only=on; 584 | 585 | # Log files for Debugging 586 | #access_log /var/log/nginx/${VHOST_DOMAIN}.access.log; 587 | #error_log /var/log/nginx/${VHOST_DOMAIN}.error.log; 588 | 589 | # Web root Directory for Laravel project 590 | root ${VHOST_ROOT_DIR}; 591 | index index.php index.html index.htm; 592 | server_name ${VHOST_DOMAIN}; 593 | 594 | location / { 595 | try_files \$uri \$uri/ /index.php?\$query_string; 596 | } 597 | 598 | # PHP-FPM Configuration Nginx 599 | location ~ \.php$ { 600 | try_files \$uri =404; 601 | fastcgi_split_path_info ^(.+\.php)(/.+)$; 602 | fastcgi_pass 127.0.0.1:9000;; 603 | fastcgi_index index.php; 604 | fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; 605 | include fastcgi_params; 606 | } 607 | }" > "$NGINX_SITES_AVAILABLE_FILE" || _die "Couldn't write to file: ${NGINX_SITES_AVAILABLE_FILE}" 608 | 609 | _arrow "${NGINX_SITES_AVAILABLE_FILE} file has been created." 610 | } 611 | 612 | function prepareDefaultVhostContent() 613 | { 614 | echo "server { 615 | listen 80; 616 | listen [::]:80 ipv6only=on; 617 | 618 | # Log files for Debugging 619 | #access_log /var/log/nginx/${VHOST_DOMAIN}.access.log; 620 | #error_log /var/log/nginx/${VHOST_DOMAIN}.error.log; 621 | 622 | # Web root Directory for Laravel project 623 | root ${VHOST_ROOT_DIR}; 624 | index index.html index.htm; 625 | server_name ${VHOST_DOMAIN}; 626 | 627 | location / { 628 | try_files \$uri \$uri/ =404; 629 | } 630 | }" > "$NGINX_SITES_AVAILABLE_FILE" || _die "Couldn't write to file: ${NGINX_SITES_AVAILABLE_FILE}" 631 | 632 | _arrow "${NGINX_SITES_AVAILABLE_FILE} file has been created." 633 | } 634 | 635 | function createVhostSymlinks() 636 | { 637 | ln -s "$NGINX_SITES_AVAILABLE_FILE" "$NGINX_SITES_ENABLED_FILE" || _die "Couldn't create symlink to file: ${NGINX_SITES_AVAILABLE_FILE}" 638 | } 639 | 640 | function createEtcHostEntry() 641 | { 642 | local _etcHostLine="127.0.0.1 ${VHOST_DOMAIN}" 643 | if grep -Eq "127.0.0.1[[:space:]]+${VHOST_DOMAIN}" /etc/hosts; then 644 | _warning "Entry ${_etcHostLine} already exists in host file" 645 | else 646 | echo "127.0.0.1 ${VHOST_DOMAIN}" >> /etc/hosts || _die "Unable to write host to /etc/hosts" 647 | fi 648 | } 649 | 650 | function reloadNginx() 651 | { 652 | local _nginxTest=$(nginx -t) 653 | if [[ $? -eq 0 ]]; then 654 | nginx -s reload || _die "Nginx couldn't be reloaded." 655 | else 656 | echo "$_nginxTest" 657 | fi 658 | } 659 | 660 | function printSuccessMessage() 661 | { 662 | _success "Virtual host for Nginx has been successfully created!" 663 | 664 | echo "################################################################" 665 | echo "" 666 | echo " >> Domain : ${VHOST_DOMAIN}" 667 | echo " >> Application : ${APP_TYPE}" 668 | echo " >> Document Root : ${VHOST_ROOT_DIR}" 669 | echo " >> Nginx Config File : ${NGINX_SITES_ENABLED_FILE}" 670 | echo "" 671 | echo "################################################################" 672 | 673 | _printPoweredBy 674 | 675 | } 676 | 677 | ################################################################################ 678 | # Main 679 | ################################################################################ 680 | export LC_CTYPE=C 681 | export LANG=C 682 | 683 | DEBUG=0 684 | _debug set -x 685 | VERSION="1.2.0" 686 | 687 | function main() 688 | { 689 | _checkRootUser 690 | checkCmdDependencies 691 | 692 | [[ $# -lt 1 ]] && _printUsage 693 | 694 | initDefaultArgs 695 | processArgs "$@" 696 | 697 | createVirtualHost 698 | 699 | printSuccessMessage 700 | exit 0 701 | } 702 | 703 | main "$@" 704 | 705 | _debug set +x --------------------------------------------------------------------------------